I have a class like this:
class A(arg: Int)(implicit i: Boolean) {
def apply(v: Double): this.type = {
// do stuff
this
}
}
and I want to create an instance of it by both initializing it and calling apply in the same line:
implicit val i = false
val a = A(arg=1)(v=2.0) // doesn't work
val a2 = (A(arg=1))(v=2.0) // doesn't work
Unfortunately the compiler assumes that v=2.0 is meant for the implicit parameter instead of being for the apply(). I tried a number of different syntaxes with inserting {}’s and ()’s, but none of them worked. I realize that v could be moved into the constructor, but in my case that isn’t an option because A is subclassed and I don’t want to add v to every subclass constructor. Is there a way to achieve this? Thanks.
How about “ugly but it seems to work”…
Honestly, no idea 🙂 However, consider this which might be some insight:
error: type mismatch; found : Double(2.0) required: Boolean val a = (new A(arg=1))(2.0)Whoa!
Happy coding.