3 ms·
Yep. The funny thing is that if you try to use this new automatic type-erasure stuff for anything but SwiftUI, you will soon encounter compiler errors like “`so
by layoutIfNeeded 7y ago
Yep. The funny thing is that if you try to use this new automatic type-erasure stuff for anything but SwiftUI, you will soon encounter compiler errors like “`some` types are only implemented for method return types or computed property accessor return types”. Lol.
So e.g. you can’t use `some`-typed variables or stored properties.
- NobodyNada 7y agoThe `some` keyword declares an opaque result type ([0]), which means "this function has a concrete return type, but the identity of that type is a hidden implementation detail." An opaque result type can be used like a generic type -- the compiler doesn't know the exact type, but it does know that calls to the function will always return the same type (which is important when working with protocols such as `Equatable` that have associatedtype or Self requirements). A `some`-typed variable or stored property wouldn't make any sense because it would be a variable that has a concrete but unknown type. You can't assign something to such a variable, because there's no way to know whether the value you're trying to assign matches the hidden concrete type of the variable. Perhaps the "automatic type-erasure" feature you're wanting is actually generalized existentials ([1])? Generalized existentials would allow you to declare a variable whose type can be "any Equatable", rather than "some (specific but unknown) Equatable", and the compiler would allow you to "open" the existential and use it like a generic type (without throwing the "protocol can only be used as a generic constraint because it has Self or associatedtype requirements" error). [0]: https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.... [1]: https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#generalized-existentials https://github.com/apple/swift/blob/master/docs/GenericsMani...
- favorited 7y agoYou definitely can have a 'some' type property[0]. Computed properties work just like functions do. Stored properties just need a value assigned, like so: let strings: some Collection = ["hello", "world"] [0]https://github.com/apple/swift-evolution/blob/master/proposals/0244-opaque-result-types.md#properties-and-subscripts https://github.com/apple/swift-evolution/blob/master/proposa...