Domanda

I have a trait as follows:

trait SomeBaseTrait{
  def someFun[B](args:SomeArgs)(f: => B):B
}

In my test class, I am trying to mock this class as follows:

class MyMockOfBase extends SomeBaseTrait{
     def someFun[Boolean](args:SomeArgs)(f: => Boolean):Boolean = true
}

The problem here is that compiling this class throws the following:

[error]  found   : scala.Boolean(true)
[error]  required: Boolean
[error]   def someFun[Boolean](args:SomeArgs)(f: => Boolean):Boolean = true
[error]                                                                ^
[error] one error found

Edit.

Also, Things are also a little weird when my trait has a method as follows:

 trait SomeBaseTrait2{
    def someFun2[B](args:SomeArgs):B
  }

I am extending it as follows:

val mockBase = new SomeBaseTrait2{def someFun2(args:SomeArgs):Boolean = true}

Now here I get the following:

new SomeBaseTrait2{def someFun2(args:String):Boolean = true}
<console>:9: error: object creation impossible, since method someFun2 in trait SomeBaseTrait2 of type [B](args: String)B is not defined
              new SomeBaseTrait2{def someFun2(args:String):Boolean = true}

Can somebody tell me what I might be doing wrong here?

È stato utile?

Soluzione

In your class MyMockOfBase Boolean is the name of type parameter, just like T:

class MyMockOfBase extends SomeBaseTrait{
     def someFun[T](args:SomeArgs)(f: => T):T= true
}

You are trying to use true as T.

I guess you want to do something like this:

trait SomeBaseTrait[B]{
  def someFun(args:SomeArgs)(f: => B):B
}

class MyMockOfBase extends SomeBaseTrait[Boolean]{
     def someFun(args:SomeArgs)(f: => Boolean):Boolean = true
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top