Skip to content

WIP Fix SIP-23 (Literal Types) #1599

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

Merged
merged 10 commits into from
Jan 25, 2020
Merged

WIP Fix SIP-23 (Literal Types) #1599

merged 10 commits into from
Jan 25, 2020

Conversation

valencik
Copy link
Contributor

@valencik valencik commented Dec 10, 2019

This PR cleans up some incorrect code examples in the SIP-23 document.

a PR bringing the SIP in line with the implementation would be welcome; so would a PR merging the whole SIP into the spec - @SethTisue (via gitter)

  • update comments showing return types to match repl session
  • confirm all examples in proposal details section
  • figure out if we need to update the history section to reflect these changes

Additional commentary

This PR better addresses the issues discussed in #1570
I do not think this addresses all the confusion one could run into when playing with literal types.
I think we need to improve the spec and write some documentation elsewhere, perhaps an overview or section in the tour.
However, hopefully this PR serves as an incremental improvement over the current state.

@SethTisue SethTisue requested a review from milessabin December 10, 2019 05:51
@milessabin
Copy link
Contributor

I think the example use of .type with subtypes of AnyVal should be tweaked, but should stay. We can prevent the widening with a singleton expected type, or by assigning to a final val, eg.,

scala> def foo[T](t: T): t.type = t
foo: [T](t: T)t.type

scala> final val bar = foo(23)
bar: 23 = 23

Otherwise LGTM.

@valencik
Copy link
Contributor Author

Hi @milessabin, thanks for the comment.
Unfortunately I am not seeing that behavior in scala 2.13.1:

Welcome to Scala 2.13.1 (OpenJDK 64-Bit Server VM, Java 1.8.0_222).
Type in expressions for evaluation. Or try :help.

scala> def foo[T](t: T): t.type = t
foo: [T](t: T)t.type

scala> foo(23)
res0: Int = 23

scala> final var bar = foo(23)
bar: Int = 23

In each case I am seeing the widened type.

@valencik
Copy link
Contributor Author

Oh how embarrassing, look how that dreaded var snuck in and ruined the fun.
Using a val not a var we see the expected behavior return:

Welcome to Scala 2.13.0 (OpenJDK 64-Bit Server VM, Java 1.8.0_222).
Type in expressions for evaluation. Or try :help.

scala> def foo[T](t: T): t.type = t
foo: [T](t: T)t.type

scala> foo(23)
res0: Int = 23

scala> final val bar = foo(23)
bar: 23 = 23

Although it is perhaps interesting to note that the type gets widened for a var...

I'll restore the example with .type

@valencik valencik marked this pull request as ready for review December 12, 2019 04:24
@valencik
Copy link
Contributor Author

A remaining question is whether we should update the history section of the SIP or not.

@SethTisue
Copy link
Member

SethTisue commented Jan 25, 2020

I don't think the history section needs updating; that section's purpose is to alert the SIP committee to design changes in the proposal. we have the git history for those interested in full details

thanks @valencik for tackling this!

@SethTisue SethTisue merged commit b4e7f22 into scala:master Jan 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants