Fast metatypical inconsistency

Given the following code ...

protocol MyProtocol {}
enum MyEnum: MyProtocol {}
struct MyStruct: MyProtocol {}
class MyClass: MyProtocol {}
func MyFunction(parameter: MyProtocol.Type) -> String {
    return "Hi"
}

Why should I use .selfwhen assigning a variable ...

var variable: MyProtocol.Type = MyStruct.self

but not when I pass the metatype as a parameter to a function?

var result = MyFunction(MyStruct)
+4
source share
1 answer

It looks like a compiler error. Personally, it seems to me that it .selfshould be included in the function argument.

However, as soon as the Swift source code is released later this year, a better explanation may come up. At the same time, you can radar.

: , .self, Class.Type . Swift . , ?

+1

Source: https://habr.com/ru/post/1614292/


All Articles