Skip to content

Commit 5b8250a

Browse files
committed
Document error GHC-05380
Explain why a newtype only can have 1 constructor.
1 parent 70c332c commit 5b8250a

File tree

7 files changed

+57
-0
lines changed

7 files changed

+57
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Multiple newtype constructors
3+
summary: More than 1 constructor for a newtype
4+
severity: error
5+
introduced: 9.6.1
6+
---
7+
8+
A newtype constructor can only have a single constructor. If you want multiple
9+
constructors, you should use `data` instead of `newtype`.
10+
11+
The reason that you need exactly one constructor is that `newtype` constructors
12+
are removed at runtime.
13+
So during runtime, if you define `newtype Age = Age Int`, `Age` is the same as `Int`.
14+
Only during the compilation phase, will the `Age` and `Int` types be different.
15+
If we allow multiple constructors for a newtype, there would be no way to distinguish them during execution.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data Direction = Up | Down
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
newtype Direction = Up | Down
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: More than 1 constructor for a newtype
3+
---
4+
5+
## Error Message
6+
```
7+
Main.hs:1:1: error: [GHC-05380]
8+
A newtype must have exactly one constructor,
9+
but ‘Direction’ has two
10+
In the newtype declaration for ‘Direction’
11+
|
12+
1 | newtype Direction = Up | Down
13+
|
14+
```
15+
## Explanation
16+
17+
A newtype constructor can only have a single constructor. It is not allowed to have multiple
18+
constructors when defining a newtype.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data Void
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
newtype Void
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Zero constructors for a newtype
3+
---
4+
5+
## Error Message
6+
```
7+
Main.hs:1:1: error: [GHC-05380]
8+
A newtype must have exactly one constructor, but ‘Void’ has none
9+
In the newtype declaration for ‘Void’
10+
|
11+
1 | newtype Void
12+
|
13+
```
14+
15+
## Explanation
16+
17+
A `newtype` constructor does need to have a single constructor. It is not allowed to
18+
have a newtype without a constructor. It is allowed to have a `data` type without a
19+
constructor, so in order to fix this, you need to come up with a constructor for the newtype
20+
or change the `newtype` keyword to a `data` keyword.

0 commit comments

Comments
 (0)