Skip to content

Commit a66ec9d

Browse files
committed
update from swift evolution discussion
1 parent 64b4209 commit a66ec9d

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

NNNN-type-self-omission.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ app.get("users", Int, "posts", String) { request, userId, postName in
2727
// prints "You requested the post named foo from user #5"
2828
```
2929

30+
Here we are trying to specify the type of item we would like to receive in our handler closure by simply passing the name of the type.
31+
3032
### Current State
3133

34+
However, the compiler currently complains that `.self` is required after type names. The compiling code looks like the following:
35+
3236
```swift
3337
app.get("users", Int.self, "posts", String.self) { request, userId, postName in
3438
...
3539
```
3640

37-
or
41+
or, by tricking the compiler a bit:
3842

3943
```swift
4044
let i = Int.self
@@ -46,6 +50,8 @@ app.get("users", i, "posts", s) { request, userId, postName in
4650

4751
With `.self` required, more code is necessary that ultimately provides less clarity and concision. The current state of requiring `.self` in some places, and not requiring it in others is confusing to developers.
4852

53+
### Inconsistency
54+
4955
Here is a demonstration of the current inconsistency.
5056
```swift
5157
func test<T: Any>(type: T.Type, two: String) {
@@ -63,6 +69,20 @@ test(Int.self, two: "")
6369
test(Int, two: "") //Expected member name or constructor call after type name
6470
```
6571

72+
### From Joe Groff
73+
74+
"As the designer in question, I've come around to this as well; our type system is sufficiently stronger than that other language that I think the consequences of accidentally typing `let x = Int` when you meant `let x = Int()` are far easier to understand."
75+
76+
### Community Response
77+
78+
"It seemed like there was general agreement to do something about the “.self” requirement when referencing types. I would personally like to see it go away."
79+
80+
"+1 for junking the .self requirement"
81+
82+
"Nice to see that this might be fixed. Evolution is making me happier and happier lately :)"
83+
84+
"Swift's type-checking engine is strong enough to merit not needing the redundant `.self` safety check. It feels like I'm doing something wrong when I need to use `.self`."
85+
6686
## Proposed solution
6787

6888
Make the use of `.self` optional everywhere.
@@ -91,4 +111,3 @@ This fixes the confusion of being able to use both methods, but `.self` is unnec
91111
#### Making `Type` (without `.self`) required
92112

93113
This would be less confusing to developers, but would break old code.
94-

0 commit comments

Comments
 (0)