danny.lesnik danny.lesnik - 3 months ago 11
Scala Question

How to pass implicit reference from constructor

I have the following code:

class myClass(someObject:Option[SomeThing]){

def doSomeThing(a:Int,b:Int)(implicit someOtherOption:Option[SomeThing]) = ???
}


I would like to pass
someObject
from constructor to
doSomething
method.

I can do it by making
implicit
reference inside a class

implicit val someImplictObject = someObject


but this way looks ugly. I guess, I can use
implicitly
method but I can't find correct syntax.

Answer

You can set default value for someOtherOption this way:

def doSomeThing(a:Int, b:Int)
   (implicit someOtherOption:Option[SomeThing] = someObject) = ???
Comments