Here is my case:
I want 2 or more UIControls to conform to a common protocol: For example UISlider
, UIStepper
, MyCustomControl
. This is what they have:
class UIStepper {
var value: Float
}
class UISlider {
var value: Double
}
class MyCustomControl {
var value: Int
}
Now, I'd love something similar to a protocol like that:
protocol Valuable {
associatedtype T
var value: T
}
and then be able to use a [Valuable]
. But of course I get into the famous PATs problem
protocol Valuable can only be used as a generic constraint because it has Self or associated type requirements
I've seen methods of type erasure and things similar to that online. But I feel it's kind of messy for what I'm trying to do. I want to be able to just have an array of Controls that have a value property, and this property can only be primitive types. Sure I can go ahead and create multiple arrays like [IntValuable]
, [DoubleValuable]
, [FloatValuable]
to get around it. Or maybe use NSNumber somehow somewhere. or enum with associated values. Or maybe there's something I am not seeing, and hence why I am posting here :) A little guidance is really appreciated! Thanks.