-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add doc page for Literal Singleton Types #2699
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
Right now, a popular way to replicate literal singleton types is to create macros and/or use libraries such as | ||
`shapeless` due to the lack of a syntax to express them. | ||
|
||
With Literal Singelton types, it is possible to do something like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems there is a typo on Singelton
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, no. Really sorry, will edit that. Thanks!
val t: 42 = 42 | ||
``` | ||
|
||
to use literal Singleton types. Trying to do the same in Scala produces the following error: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to use literal Singleton types
is redundant with With Literal Singelton types
. What do you think ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for starting this up! I wouldn't mind having a very short doc here, just explaining what they are and how to use them. It might be nice to add examples with functions taking / returning singleton types such as the one I pointed in comments, or also this one:
def f(t: Double): t.type = t
val a: 1.2 = f(1.2)
title: "Literal Singleton Types" | ||
--- | ||
|
||
Singleton types is one of the most popular features in Scala. Literal Singleton types takes it a step further. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would cut the intro down to the most important bit, something like this:
Literal Singleton Types allows primitive literals to be used as types. For example:
val fourtyTwo: 42 = 42
val hero: "Jedi".type = "Jedi"
val trivial: true = true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do :)
|
||
to use literal Singleton types. Trying to do the same in Scala produces the following error: | ||
``` | ||
<console>:1: error: identifier expected but string literal found. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the point of showing this error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will remove
val t: "Jedi".type = "Jedi" | ||
``` | ||
|
||
Dotty provides support for using Literal Singleton types of the last format namely: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is Dotty's documentation, just show the Dotty format / example :)
``` | ||
and so on. | ||
|
||
There is a possibility to use singleton types in some contexts, but only on identifiers which points to a constant that conforms to `AnyRef`. This restriction is due to `Any` not having an eq method, which is what’s used for singleton type-equality check and pattern matching. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming you are talking about type contexts here? I'm not aware of this restriction with AnyRef
, do you have an example? This seems to work fine:
scala> def f[T <: Any](t: T) = t
def f[T <: Any](t: T): [T](t: T)T
scala> f[1.1](1.1)
val res3: Double = 1.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this one is on me again, I referred to some old documentation and was led to believe that. Thanks for pointing it out!
--- | ||
|
||
Singleton types is one of the most popular features in Scala. Literal Singleton types takes it a step further. | ||
Right now, a popular way to replicate literal singleton types is to create macros and/or use libraries such as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now
Doc should be timeless when possible.
If you want to cite shapeless here please do it more precisely by pointing to the corresponding documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, will edit the md by adding a link to shapeless. I thought of referencing this primarily because it was cited so in the SIP
``` | ||
|
||
to use literal Singleton types. Trying to do the same in Scala produces the following error: | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this block missing scala syntax coloring ? I am not sure if it should be applied here as the first line is not Scala code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I'll remove that part since its not needed :)
Done! Thanks a ton @OlivierBlanvillain and @lforite for being so patient and pointing out changes! |
|
||
```scala | ||
val t: "Jedi".type = "Jedi" | ||
val t: 42.type = 42 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These ones with a .type
are not valid in Dotty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Um, that's why I had a separate section for Dotty earlier. One minute, will revert changes and push again.
Okay, that wasn't supposed to happen. Lemme figure out why |
Really sorry for the delay! Some weird thing happened while resetting and that caused the PR to close. Thank you very much for putting up with me :) |
|
||
```scala | ||
val t: 42 = 42 | ||
val t: "Jedi" = "Jedi" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a different name for the second variable? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On it
LGTM, thanks! |
Intended as a fix for #2615.
My first time writing a doc page, so please do comment your views. I do feel that some prose can be added, which I'm searching for. CC @OlivierBlanvillain