Scala 创建类的任意

Scala 创建类的任意,scala,implicit,scalacheck,Scala,Implicit,Scalacheck,我有一个类似这样的类: class EpidemySimulator extends Simulator { ... class Person{ // Implementation } } import org.scalacheck._ import org.scalacheck.Prop.forAll // define a class with an inner class class A { class B } // define an implicit tha

我有一个类似这样的类:

class EpidemySimulator extends Simulator {
   ...
   class Person{
   // Implementation
   }
}
import org.scalacheck._
import org.scalacheck.Prop.forAll

// define a class with an inner class
class A { class B }
// define an implicit that provides a A#B given an A
implicit def arbAB(implicit a:A) : Arbitrary[a.B] = Arbitrary[a.B] { new a.B }
// the non-working code
def test(a:A) = forAll { x:a.B => true }
<console>:25: error: could not find implicit value for parameter a1: org.scalacheck.Arbitrary[a.B]
   def test(a:A) = forAll { x:a.B => true }
                          ^ 
// making a an implicit works
def test(implicit a:A) = forAll { x:a.B => true }
我想添加一个任意的内部类Persons,这样我就可以在EpidemySimulator类上定义一些属性。我认为伴随对象可以定义必要的隐式生成器,以便定义将生成Person类型对象的属性

object EpidemySimulator
{
    implicit def arbPerson(implicit sim: EpidemySimulator ) = 
    Arbitrary(for(n <- Gen.choose(1, roomRows * roomColumns))yield{ new sim.Person(n) })

    def propertyOne(sim: EpidemySimulator ) = forAll { person: sim.Person =>
         person.row > 0 && person.row < roomRows && person.col > 0 && person.col < roomColumns  
    }
}
知道我做错了什么吗。我希望最终能够:

class EpidemySuite extends FunSuite with Checkers {
    test("EpidemySimulator fulfills property one"){
        val es = new EpidemySimulator
        check( EpidemySimulator.propertyOne(sim) )
    }
}

我认为问题在于:您提供了一个隐式的方法来生成任意的EpidemySimulator,即给定一个EpidemySimulator的人

implicit def arbPerson(implicit sim: EpidemySimulator ) = 
Arbitrary(for(n <- Gen.choose(1, roomRows * roomColumns))yield{ new sim.Person(n) })
implicit-def-arbPerson(implicit-sim:EpidemySimulator)=
任意的(对于(n
person.row>0&&person.row0&&person.col
考虑到您可能需要测试EpidemySimulator的各种属性,在对象中的某个位置定义隐式EpidemySimulator val可能是一个好主意

请注意,我只在scala控制台会话中检查了这些内容,如下所示:

class EpidemySimulator extends Simulator {
   ...
   class Person{
   // Implementation
   }
}
import org.scalacheck._
import org.scalacheck.Prop.forAll

// define a class with an inner class
class A { class B }
// define an implicit that provides a A#B given an A
implicit def arbAB(implicit a:A) : Arbitrary[a.B] = Arbitrary[a.B] { new a.B }
// the non-working code
def test(a:A) = forAll { x:a.B => true }
<console>:25: error: could not find implicit value for parameter a1: org.scalacheck.Arbitrary[a.B]
   def test(a:A) = forAll { x:a.B => true }
                          ^ 
// making a an implicit works
def test(implicit a:A) = forAll { x:a.B => true }
import org.scalacheck_
导入org.scalacheck.Prop.forAll
//用内部类定义一个类
A类{B类}
//定义一个在给定a的情况下提供a#B的隐式
隐式定义arbAB(隐式a:a):任意[a.B]=任意[a.B]{new a.B}
//非工作代码
def test(a:a)=对于所有{x:a.B=>true}
:25:错误:找不到参数a1的隐式值:org.scalacheck.arbitral[a.B]
def test(a:a)=对于所有{x:a.B=>true}
^ 
//制作一个含蓄的作品
def测试(隐式a:a)=对于所有{x:a.B=>true}