Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-19 Thread Howard Lovatt via swift-users
You are hardly alone struggling with this, it seems to come up every other week! You can write your own custom AnyProtocol type that includes Self, a pain but doable, e.g.: protocol A { func a() -> String } protocol B { func b() -> String } struct AB1: A, B, Hashable { func a() ->

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-19 Thread David Hart via swift-users
> On 19 Jul 2017, at 09:22, Glen Huang via swift-users > wrote: > > Thanks for the heads up. > > I wonder what’s the root cause of the difficulty to make "such an existential > conform to Hashable in a general way”. Is it because objects of different > types have

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-19 Thread David Hart via swift-users
> On 19 Jul 2017, at 09:22, Glen Huang via swift-users > wrote: > > Thanks for the heads up. > > I wonder what’s the root cause of the difficulty to make "such an existential > conform to Hashable in a general way”. Is it because objects of different > types have

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-19 Thread Slava Pestov via swift-users
Hopefully we will one day have generalized existentials which would at least allow a heterogeneous array of protocols with an associated type; however it is not clear how such an existential could conform to Hashable in a general way, given that Hashable implies Equatable which has a Self

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-18 Thread Glen Huang via swift-users
Do you think if there will be any evolution proposal to address this limitation? Or it’s an inherent tradeoff that is unlikely to be changed? > On 19 Jul 2017, at 8:49 AM, Jordan Rose wrote: > > > >> On Jul 18, 2017, at 10:33, Vladimir.S via swift-users

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-18 Thread Vladimir.S via swift-users
On 17.07.2017 4:51, Glen Huang via swift-users wrote: Thanks for the code sample and link, but if I’m not wrong, this pattern doesn’t allow heterogeneous items. Support the question. Trying to understand if we can have something like [AnyHashable] for our custom protocol(with associated type)

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-16 Thread Glen Huang via swift-users
Thanks for the code sample and link, but if I’m not wrong, this pattern doesn’t allow heterogeneous items. If I have these definitions: struct Chicken {} struct Pig {} class ChickenFarm: Farm { func grow() -> Chicken { return Chicken() } } class PigFarm: Farm { func grow()

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-16 Thread Nevin Brackett-Rozinsky via swift-users
The standard pattern for type-erasure in Swift looks like this: protocol Farm { associatedtype Produce func grow() -> Produce } private class _AnyFarmBase : Farm { func grow() -> T { fatalError() } } private final class _AnyFarmBox: _AnyFarmBase { var farm: U init(_ x: U) {

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-15 Thread Glen Huang via swift-users
This sounds like the right approach! However, as I experimented with AnyHashable more, I found out that after converting a concrete type to it, I could still convert back using “as”: AnyHashable(Foo()) as! Foo I guess that’s not the case with AnyNamed? I tried to imitate AnyHashable: struct

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-15 Thread Ole Begemann via swift-users
One way to do this in Swift is a method called type erasure. Type erasure means you create a new type that wraps any value whose concrete type you want to erase. This new type also conforms to the protocol. By convention the type is named Any... (compare AnyIterator and AnySequence in the

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-11 Thread Glen Huang via swift-users
Thanks for the detailed example. It makes sense. But the code can grow pretty quickly with just a few methods and a few concrete types. Also most are repetitive. I’ll try to incorporate this pattern when the use case is simple. But for more complex ones I guess I have to stick with classes for

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-11 Thread Howard Lovatt via swift-users
I would be tempted to use classes for this if you can use single inheritance. If you need multiple inheritance then use an enum and hand code the dispatch, a lot more work :(. E.G.: protocol A { func a() -> String } protocol B { func b() -> String } struct AB1: A, B, Hashable { func

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-11 Thread Adrian Zubarev via swift-users
No it does not have to be a generic enum at all, as long you do not want extending the types from a diffrent module. Simply add a new enum case for each type you need, like `case string(String)`. You may also want to ask the wrapped type for it's hashValue so that it will hash correctly. --

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-11 Thread Glen Huang via swift-users
NM, I think you meant this? enum Either { case Left(T1) case Right(T2) } > On 11 Jul 2017, at 9:06 PM, Glen Huang via swift-users > wrote: > > This sounds pretty interesting. > > But I can’t totally wrap my head around it. How do I "wrap types into

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-11 Thread Glen Huang via swift-users
This sounds pretty interesting. But I can’t totally wrap my head around it. How do I "wrap types into enum cases”? Could you provide a sample code? Thanks. > On 11 Jul 2017, at 8:50 PM, Adrian Zubarev > wrote: > > If the solution you seek is not designed so

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-11 Thread Adrian Zubarev via swift-users
If the solution you seek is not designed so that the module user can extend the set of types then you could wrap your types into enum cases and use the enum for your set. ;) When Swift will support anonymous enum cases, this will be an elegant solution to these type of things. -- Adrian

Re: [swift-users] ⁨Is it possible to store a set of heterogeneous items with protocol?

2017-07-11 Thread Glen Huang via swift-users
Thanks for bringing AnyHashable to my attention. It works, but the types are now erased. I want to have a union of the two sets because I want to loop over it to treat each contained item as Named, so I can process them as though they are of the same type. Is this type of use case really