Skip to content

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

Merged
merged 3 commits into from
Jun 10, 2017

Conversation

Varunram
Copy link
Contributor

@Varunram Varunram commented Jun 7, 2017

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

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
Copy link

@lforite lforite Jun 7, 2017

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

Copy link
Contributor Author

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:
Copy link

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 ?

Copy link
Contributor

@OlivierBlanvillain OlivierBlanvillain left a 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.
Copy link
Contributor

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

Copy link
Contributor Author

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.
Copy link
Contributor

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

Copy link
Contributor Author

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:
Copy link
Contributor

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.
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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:
```
Copy link

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

Copy link
Contributor Author

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 :)

@Varunram
Copy link
Contributor Author

Varunram commented Jun 7, 2017

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
Copy link
Contributor

@OlivierBlanvillain OlivierBlanvillain Jun 7, 2017

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

Copy link
Contributor Author

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.

@Varunram Varunram closed this Jun 7, 2017
@Varunram
Copy link
Contributor Author

Varunram commented Jun 7, 2017

Okay, that wasn't supposed to happen. Lemme figure out why

@Varunram Varunram reopened this Jun 7, 2017
@Varunram
Copy link
Contributor Author

Varunram commented Jun 7, 2017

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"
Copy link
Contributor

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? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it

@Varunram
Copy link
Contributor Author

ping @felixmulder @OlivierBlanvillain

@OlivierBlanvillain
Copy link
Contributor

LGTM, thanks!

@OlivierBlanvillain OlivierBlanvillain merged commit b38fa72 into scala:master Jun 10, 2017
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.

4 participants