Skip to content

Commit 3ea1f7c

Browse files
committed
Preliminary commit for adding a doc page regarding singleton-types
1 parent fa9f8b2 commit 3ea1f7c

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
layout: doc-page
3+
title: "Literal Singleton Types"
4+
---
5+
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
11+
12+
```scala
13+
val t: "Jedi".type = "Jedi"
14+
```
15+
or
16+
```scala
17+
val t: 42.type = 42
18+
```
19+
or
20+
```scala
21+
val t: 42 = 42
22+
```
23+
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+
```
29+
30+
Dotty provides support for using Literal Singleton types of the last format namely:
31+
```scala
32+
val t: 42 =42
33+
val x: "Jedi" = "Jedi"
34+
```
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.
38+
39+
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)

docs/sidebar.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ sidebar:
2727
url: docs/reference/implicit-function-types.html
2828
- title: Phantom Types
2929
url: docs/reference/phantom-types.html
30+
- title: Literal Singleton Types
31+
url: docs/reference/singleton-types.html
3032
- title: Enums
3133
subsection:
3234
- title: Enumerations

0 commit comments

Comments
 (0)