3

I'm creating a case class with default-valued constructor:

abstract class Interaction extends Action
case class Visit(val url: String)(val timer: Boolean = false) extends Interaction

But I cannot create any of its instance without using all of its parameters, for example. If I write:

Visit("https://www.linkedin.com/")

The compiler will complain:

missing arguments for method apply in object Visit;
follow this method with `_' if you want to treat it as a partially applied function
[ERROR]     Visit("http://www.google.com")

What do I need to do to fix it?

1
  • Is there a reason that you are defining it using currying (and e.g. here)? If not, as suggested below, you could define all your parameters in the first set, and your default will work as requested. If you need the currying/second set of arguments, then the extra set of brackets will cause the default for that set of arguments to be applied. Commented Jun 7, 2014 at 6:51

2 Answers 2

15

You need to tell the compiler that this is not a partially applied function, but that you want the default values for the second set of parameters. Just open and close paranthesis...

scala> Visit("https://www.linkedin.com/")()
res1: Visit = Visit(https://www.linkedin.com/)

scala> res1.timer
res2: Boolean = false

EDIT to explain @tribbloid comment.

If you use _, instead of creating a visit you are creating a partially applied function which then can be use to create a Visit object:

val a = Visit("asdsa")_ // a is a function that receives a boolean and creates and Visit
a: Boolean => Visit = <function1> 

scala> val b = a(true) // this is equivalent to val b = Visit("asdsa")(true)
b: Visit = Visit(asdsa)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot! I speculated that a partially applied function should always have a _ in case of ambiguity. Looks like I'm wrong, and this is the only option.
An interesting feature of scala is that in case of an ambiguity between an old feature and a new feature, the old feature always win and the new one is sacrificed. The infix method as operators on tuples declared by parenthesis never works, because the compiler think those parenthesis are priority brackets :)
@tribbloid, see my edit. I hope it clarifies your doubt.
I've seen this rule somewhere else before, that's why I'm confused because if this is the only way to define a PAF then Visit("Something") will have no ambiguity. Scala has some wierd rules to judge if you need that _ or not
One more question: I can overcome this by using implicit delimiter on the second parameter, but would this have any side effect?
|
5

Please correct the syntax of specifying the optional field in your case class as follows

case class Visit(val url: String,val timer: Boolean = false) extends Interaction

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.