5 ms·
> For example, if you want to write some code that accepts any Sequence of Int values, you can't write this: > func f(seq: Sequence<Int>) { ... > […] Swift
by runeks 9y ago
> For example, if you want to write some code that accepts any Sequence of Int values, you can't write this:
> func f(seq: Sequence<Int>) { ...
> […] Swift provides the AnySequence type to solve this problem. AnySequence wraps an arbitrary Sequence and erases its type, providing access to it through the AnySequence type instead. Using this, we can rewrite f and g:
> func f(seq: AnySequence<Int>) { ...
> […] The Swift standard library has a bunch of these Any types, such as AnyCollection, AnyHashable, and AnyIndex.
Please excuse my ignorance, but what’s the point of this?
Have all these <SomeType> and their Any<SomeType> counterpart seems like unnecessary code duplication to me.
- mikeash 9y agoCode duplication yes, but it's not unnecessary. You need them so you can write code that works on Sequences and other such things without knowing the actual implementing type. Ideally this would not be necessary, and in some future version of Swift it probably won't be, but for now it is.
- deleted 9y ago[deleted]
- zacwest 9y agoThe biggest reason is that you cannot write `var someVariable: SomeGenericType<Type>` nor use it as a parameter or a return in a protocol, for example. For another type to reference a generic type, it would either have to be generic itself, use a type erased `AnySomeGenericType<Type>` reference, or specify the exact version that's being used, e.g. ConcreteGenericType<Type>, which makes your code wayyyyy less abstract and entangled with another type. This is very much something the language should handle, it just hasn't yet, so you have to write these large complicated boxing and unboxings.
- culturedsystems 9y ago"The biggest reason is that you cannot write `var someVariable: SomeGenericType<Type>` nor use it as a parameter or a return in a protocol, for example." What's the reason for that? In Java, for example, interfaces can be generic in just the same way concrete types can, which also means you can declare variables and parameters of some specific instantiation of the interface. It looks like Swift has two different generics systems, one for concrete types and one for protocols, leading to this extra complexity. Presumably there's some reason for doing things this way.
- mikeash 9y agoAs with many deficiencies in Swift, the reason is just that it takes time and effort to make it happen and it hasn’t been done yet.
- superlopuh 9y agoIn the function input scenario, it's more like an alternative to a generic type Elements: Sequence with the Element constrained to Int. Either would work. There are cases where this is not possible, like when you are returning a sequence from a function whose API you don't want to bind to a specific type. Then the only thing you can do is to pass an AnySequence.
- Tyr42 9y agoPerhaps you'll be interested in someone's reverse engineering of how AnySequence was implemented? https://gist.github.com/rnapier/3095ab4986fce8d4fe8d https://gist.github.com/rnapier/3095ab4986fce8d4fe8d
- deleted 9y ago[deleted]
- jordansmithnz 9y agoNot sure why it was reverse engineered when the source code is publicly available: https://github.com/apple/swift/blob/4ddac3fbbd5b9ba8d980fa8810472cad3d79b7e6/stdlib/public/core/ExistentialCollection.swift.gyb https://github.com/apple/swift/blob/4ddac3fbbd5b9ba8d980fa88...
- armadsen 9y agoSwift was open-sourced in December of 2015, which was a few months after that gist was written.
- deleted 9y ago[deleted]