You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// prints "You requested the post named foo from user #5"
28
28
```
29
29
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
+
30
32
### Current State
31
33
34
+
However, the compiler currently complains that `.self` is required after type names. The compiling code looks like the following:
35
+
32
36
```swift
33
37
app.get("users", Int.self, "posts", String.self) { request, userId, postName in
34
38
...
35
39
```
36
40
37
-
or
41
+
or, by tricking the compiler a bit:
38
42
39
43
```swift
40
44
let i =Int.self
@@ -46,6 +50,8 @@ app.get("users", i, "posts", s) { request, userId, postName in
46
50
47
51
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.
48
52
53
+
### Inconsistency
54
+
49
55
Here is a demonstration of the current inconsistency.
50
56
```swift
51
57
functest<T: Any>(type: T.Type, two: String) {
@@ -63,6 +69,20 @@ test(Int.self, two: "")
63
69
test(Int, two: "") //Expected member name or constructor call after type name
64
70
```
65
71
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
+
66
86
## Proposed solution
67
87
68
88
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
91
111
#### Making `Type` (without `.self`) required
92
112
93
113
This would be less confusing to developers, but would break old code.
0 commit comments