Closed tindzk closed 7 years ago
I think you're misunderstanding how type inference is working in these cases. In your first example note that the inferred type of val x
is the singleton type of the stable identifier x
, not the literal type "test"
that you were most likely expecting. The use of x2
is rejected because it isn't a stable identifier. In the val x
case you will get the behaviour you are expecting if you mark the val as final. In the def x2
case you will need to provide an explicit result type.
Presently, singleton types cannot be used with function return values:
I stumbled upon this limitation when I wanted to define a wrapper for strings which may also be singleton types. Here is an example:
In cases, where we do not care about the singleton type, we could return
Wrap[_]
from functions:As the type of
value
isString with T
, we can access it without casting: