-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix definition of narrow in SIP-23 #1570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Indeed, the empty refinement seems to be needed by Scala 2.13: https://scastie.scala-lang.org/45N4U3xfTkmgEuReMoq2cA But I believe this is a problem in the implementation. This behavior is not specified in the description:
So, the presence of the I think we should open an issue in scala/bug. By the way, Dotty does not implement what is in the proposal, and the empty type refinement does not work: https://scastie.scala-lang.org/qkmxdI6GQyG9IFk8cLTzpw (so, you can not have an inferred narrow type, you have to write it explicitly). Any comment @smarter or @milessabin ? |
The following should work in Scala 2 (and Dotty) without relying on the semantics of empty refinements, type Id[T] = T
def narrow[T <: Singleton](t: T): Id[T] = t Demo ...
|
According to the specification, the following should work as well: def narrow[T <: Singleton](t: T): T = t
val x = narrow(42)
x: 42 Or am I missing something? BTW, I find it confusing that the introduction of the |
Yes. In the example you give the compiler will widen the result type of the call of scala> def narrow[T <: Singleton](t: T): T = t
narrow: [T <: Singleton](t: T)T
scala> val x = narrow(42)
x: Int = 42
It's consistent with the behaviour of other type constructors, eg., scala> def narrow[T <: Singleton](t: T): Option[T] = Some(t)
narrow: [T <: Singleton](t: T)Option[T]
scala> val x = narrow(42)
x: Option[42] = Some(42) ie. widening doesn't reach under the type constructor. |
In that case, I suggest that we fix the specification, then. Do you agree? |
Yes, we should. I suggest using the |
would someone like to PR the suggested spec change? |
I'd love to open a PR changing the spec. :) |
great! it's over in the scala/scala repo, https://github.com/scala/scala/tree/2.13.x/spec |
In SIP-23 there is a function defined:
Using it to form a tuple appears to still return a wide type:
But if one changes the return type from
T
toT {}
in, say, anarrower
function:A tuple of literal types appears:
Additionally the
T {}
return type is used in sip23-narrow.scala.