Skip to content

Commit 81522a4

Browse files
authored
Fix #2615: Document literal singleton types
1 parent 3ea1f7c commit 81522a4

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

docs/docs/reference/singleton-types.md

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,19 @@ layout: doc-page
33
title: "Literal Singleton Types"
44
---
55

6-
Singleton types is one of the most popular features in Scala. Literal Singleton types takes it a step further.
7-
Right now, a popular way to replicate literal singleton types is to create macros and/or use libraries such as
8-
`shapeless` due to the lack of a syntax to express them.
9-
10-
With Literal Singelton types, it is possible to do something like
6+
Literal Singleton Types allows primitive literals to be used as types. For example:
117

128
```scala
139
val t: "Jedi".type = "Jedi"
14-
```
15-
or
16-
```scala
1710
val t: 42.type = 42
18-
```
19-
or
20-
```scala
2111
val t: 42 = 42
2212
```
2313

24-
to use literal Singleton types. Trying to do the same in Scala produces the following error:
25-
```
26-
<console>:1: error: identifier expected but string literal found.
27-
val t: "Jedi".type = "Jedi"
28-
```
14+
It is also possible to define functions returning or taking singleton types such as
2915

30-
Dotty provides support for using Literal Singleton types of the last format namely:
3116
```scala
32-
val t: 42 =42
33-
val x: "Jedi" = "Jedi"
17+
def f(t: Double): t.type = t
18+
val a: 1.2 = f(1.2)
3419
```
35-
and so on.
36-
37-
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.
3820

3921
For more details and the motivation behind the need for literal singleton types, check out [SIP-23](http://docs.scala-lang.org/sips/pending/42.type.html)

0 commit comments

Comments
 (0)