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?

tribbloid
  • 4,026
  • 14
  • 64
  • 103
  • Is there a reason that you are defining it using [currying](http://stackoverflow.com/questions/8650549/using-partial-functions-in-scala-how-does-it-work/8650639#8650639) (and e.g. [here](http://stackoverflow.com/questions/14309501/scala-currying-vs-partially-applied-functions))? 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. – wwkudu Jun 07 '14 at 06:51

2 Answers2

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)
Vinicius Miana
  • 2,047
  • 17
  • 27
  • 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. – tribbloid Jun 09 '14 at 04:57
  • 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 Jun 09 '14 at 05:00
  • @tribbloid, see my edit. I hope it clarifies your doubt. – Vinicius Miana Jun 09 '14 at 17:59
  • 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 – tribbloid Jun 15 '14 at 17:35
  • One more question: I can overcome this by using implicit delimiter on the second parameter, but would this have any side effect? – tribbloid Jun 19 '14 at 20:56
  • This is actually an interesting idea. The implicit parameter with default value might work (to drop the extra parentheses). The only side effect I can think of is that if you have implicit values of the same time in some calling context, the default will be overridden (some might consider this a feature not a bug, allowing to override a default value in a context). – Andrzej Wąsowski Aug 18 '16 at 07:59
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
dvk317960
  • 672
  • 5
  • 10