scala - Set#apply with argument `Unit` -


scala puzzlers presents "puzzler":

scala> list("1", "2").toset() + "3" warning: there 1 deprecation warning; re-run -deprecation details res13: string = false3 

the explanation notes above code de-sugars to:

(list("1", "2").toset[any] apply ()) + "3"

but why doesn't following return false?

scala> set("1")() <console>:11: error: not enough arguments method apply: (elem: string)boolean in trait gensetlike. unspecified value parameter elem.        set("1")()                ^ 

the compiler smart enough know makes no sense set[a].

set[a]#apply has signature:

apply(elem: a): boolean 

i.e., must supply argument of a, , a invariant set. if try supply unit set[int], type mismatch, not false.

scala> set("1")(()) <console>:19: error: type mismatch;  found   : unit  required: string               set("1")(())                        ^ 

okay, question has 1 less set of parentheses code above. compiler assuming you're not trying pass unit, because can't. if had set[unit], could.

scala> set(())() warning: there 1 deprecation warning(s); re-run -deprecation details res41: boolean = true 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -