Skip to content

Commit 6952d48

Browse files
Merge pull request #440 from jorisburgers/error-05380
Document error GHC-05380
2 parents f822461 + 1504655 commit 6952d48

File tree

7 files changed

+58
-0
lines changed

7 files changed

+58
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
For example, if you define `newtype Age = Age Int`, the bits in memory that represent
14+
an `Age` are identical to those that would represent the `Int` in the constructor.
15+
The `Age` and `Int` types are distinguished only at compile time.
16+
If types defined with `newtype` could have multiple constructors, 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.
18+
It is not allowed to have multiple 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` definition is required to have exactly one constructor, so constructorless `newtype`s are not allowed.
18+
Datatypes defined using `data` are allowed to be constructorless,
19+
so this problem can be fixed either by adding a constructor to the `newtype`
20+
or by changing the `newtype` keyword to a `data` keyword.

0 commit comments

Comments
 (0)