1-877-547-7272:
protocol InstantiableAndConvertableToInt {
init()
func makeInt() -> Int
}
func instantiateAndMakeInt<I: InstantiableAndConvertableToInt>(from type: I.Type) -> Int {
I().makeInt()
}
let integer = instantiateAndMakeInt(from: Never.self)
How would this work? Would it crash (which may be unexpected)?
Even if Never
becomes the bottom type, this still would not compile.
let integer = instantiateAndMakeInt(from: Never.self)
This line uses the metatype Never.Type
(Never.self
) which is not a bottom type. Secondly, making Never
the bottom type does not mean that it implicitly conforms to all protocols. These points are spelled out in the original pitch :
Also, while with this new subtyping rule Never
becomes a subtype of all metatypes, Never.Type
does not become a subtype of all metatypes because unlike Never
, it is inhabited.
Additionally, this proposal intentionally does not automatically conform Never
to additional protocols. The core team's acceptance notes from SE-0215 do a good job of expressing why this isn't yet being addressed:
However, being a bottom type does not imply that Never should implicitly conform to all protocols. Instead, convenient protocol conformances for Never should be added as deemed useful or necessary.
...
With respect to protocol conformances, the Core Team felt the language has clearly moved in a direction where explicit protocol conformance is fundamental to the language model. The compiler has grown affordances to make the implementation of protocol conformances easy in many cases (e.g., synthesized implementations of Hashable) but that the explicit protocol conformance is quite important. Adding rules for implicit protocol conformances — something that has been considered in Swift’s history — ends up adding real complexity to the language and its implementation that can be hard to reason about by a user as well as by the language implementation.