Can I assign one of these to a local variable? with inferred type? with an explicit type annotation?
func foo(a: some T) {
let b = a # Does this work? I assume so…
let c: some T = a # How about this? I think not?
let d: any T = a # How about this? I assume it does, but…but what?
}
If there is a need to leap into explicit generics:
func foo<SomeT: T>(a: SomeT) {
let c: SomeT = a
}
…then how do we help programmers discover this? That’s a hell of a leap there for somebody who doesn’t have the whole back story.
Yes, their motivation section is well-thought-out, and many of the arguments map precisely to Swift because Rust and Swift have very similar generics systems in this regard. It's probably worth paraphrasing their arguments and linking to that RFC, with a big disclaimed that Rust uses the term "existential" very differently, which causes endless confusion.
which is a bit more general in that it allows using the same type for more than one parameter without resorting to the full form.
Edit: comparing a few alternative forms (unconstraint type case):
func foo<Apple>(v: Apple) {} // full form
func foo(v: some) {} // Doug's version. is that allowed?
func foo(v: generic Apple) // alternative form
func foo(v: some Apple) // alternative spelling in alternative form
I'd even like to see us drop the some, but perhaps the lesson from our current existential syntax is that neither generics nor existentials should be spelled using only the type name. some vs any gives us a nice language model that is also explicit.
c has some opaque type, which we only know is "conforming to T", but whose underlying type is the same as a.
d can hold any type that conforms to T. At the point of its initialization, it dynamically holds something that is the type of a.
This isn't the same as your first one, because every utterance of some T is a unique opaque type.
A refactoring action that took a single utterance of some P and turned it into an explicit generic type parameter would affect only the signature of your function (not the body), and everything else would work the same:
func foo<SomeT: T>(a: SomeT) {
let b = a
let c: some T = a
let d: any T = a
}
}
Yes, absolutely! I'll clarify this in the proposal document, thanks.
[EDIT: this was already in the document, with this example:
func encodeAnyDictionaryOfPairs(_ dict: [some Hashable & Codable: Pair<some Codable, some Codable>]) -> Data
]
I really think that, if you're going to be introducing a new for the generic type parameter so that you can use it in multiple places, the generic parameter list is the right place for it.
No, this would be written some Any, just like we have the ability to write any Any with SE-0335.
This proposal is a necessary step to doing that, but there are a lot of other considerations as well. Can we keep that discussion separate?
Ah, I pushed the proposal before posting the thread here. I'll go update the links.
Great pitch. One other future direction worth exploring would be the ability to further constrain the opaque parameter types.
Bikeshedding syntax:
func foo(v1: some View) where v1.Type: MyProtocol
I'm also grateful that this pitch did not included the some Sequence<...> feature and kept it in future direction as it definitely will be a very hot topic. I personally strongly remain against dropping the explicit associated type and mixing it with pseudo generic protocols.
None, but that example is probably oversimplified. There are definitely other use cases where a where clause would be the right extension for this type of feature.
Okay I have to express a bit of negative impression. So the proposal puts the Protocol<...> feature into the "Future direction" bucket. Yet it already implements this under the hood?! Please DO NOT make this feature available in the public without a proper evolution process. I'd be highly disappointed to see that move.
Sure the feature is hiding behind the private @_primaryAssociatedType attribute, but it's not hard to find it and actually start using it. As soon as someone used it people start arguing that we have to keep it because there is code that relies on it. That's not a great way to sneak this feature in.
The "primary associated type" feature is implemented behind an experimental frontend flag, just like the current implementation of opaque parameters has been merged behind a different experimental frontend flag. Both features have been pitched separately, but yes, there are test cases that use these experimental features together. Each feature will, of course, go through the Swift Evolution process.
No features are being "sneaked in". You cannot use @_primaryAssociatedType without also enabling the experimental compiler flag.