I don’t understand why this is a problem
protocol Element {
}
enum ElementNode<T: Element> {
case element(T)
case empty
}
var childElements = [ElementNode<Element>]()
I need to represent an array of my nodes that could be multiple kinds of elements
Is there a workaround?
Brandon
Nevin
2
It will work if you change the enum declaration to:
enum ElementNode<T>
In other words, let the enum hold arbitrary unconstrained associated types,
and then make your APIs utilize instances of the enum with the associated
type constrained to a protocol.
The specific example you provide is essentially equivalent to:
var childElements = [Element?]()
Nevin
···
On Wed, Dec 28, 2016 at 6:41 PM, Brandon Knope via swift-users < swift-users@swift.org> wrote:
I don’t understand why this is a problem
protocol Element {
}
enum ElementNode<T: Element> {
case element(T)
case empty
}
var childElements = [ElementNode<Element>]()
I need to represent an array of my nodes that could be multiple kinds of
elements
Is there a workaround?
Brandon
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
1 Like
Rien
(Rien)
3
I think you made your type (enum) unnecessary complex, the following works:
protocol Element {
}
enum ElementNode {
case element(Element)
case empty
}
var childElements = [ElementNode]()
Regards,
Rien
Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Swiftrien (Rien) · GitHub
Project: http://swiftfire.nl
···
On 29 Dec 2016, at 00:41, Brandon Knope via swift-users <swift-users@swift.org> wrote:
I don’t understand why this is a problem
protocol Element {
}
enum ElementNode<T: Element> {
case element(T)
case empty
}
var childElements = [ElementNode<Element>]()
I need to represent an array of my nodes that could be multiple kinds of elements
Is there a workaround?
Brandon
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
Rien
(Rien)
4
As to the why question: (just guessing here)
By the time the compiler want to know what type will be in the array, it cannot do so. The enum is a generic and thus without full type information (it only has partial type information).
Regards,
Rien
Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: Swiftrien (Rien) · GitHub
Project: http://swiftfire.nl
···
On 29 Dec 2016, at 00:41, Brandon Knope via swift-users <swift-users@swift.org> wrote:
I don’t understand why this is a problem
protocol Element {
}
enum ElementNode<T: Element> {
case element(T)
case empty
}
var childElements = [ElementNode<Element>]()
I need to represent an array of my nodes that could be multiple kinds of elements
Is there a workaround?
Brandon
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users
Aren’t I losing the ability to enforce what is going into this enum’s associated value then?
Brandon
···
On Dec 28, 2016, at 7:05 PM, Nevin Brackett-Rozinsky <nevin.brackettrozinsky@gmail.com> wrote:
It will work if you change the enum declaration to:
enum ElementNode<T>
In other words, let the enum hold arbitrary unconstrained associated types, and then make your APIs utilize instances of the enum with the associated type constrained to a protocol.
The specific example you provide is essentially equivalent to:
var childElements = [Element?]()
Nevin
On Wed, Dec 28, 2016 at 6:41 PM, Brandon Knope via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
I don’t understand why this is a problem
protocol Element {
}
enum ElementNode<T: Element> {
case element(T)
case empty
}
var childElements = [ElementNode<Element>]()
I need to represent an array of my nodes that could be multiple kinds of elements
Is there a workaround?
Brandon
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users
Ray_Fix
(Ray Fix)
6
Using Optional, your enum type goes away. (I think it is a great solution unless you need something more than .element and .none in real life.) Great to get all that optional machinery for missing values for free! Then you can constrain elements simply from the Element protocol as in as in:
protocol Element {
func compute() -> Int
}
struct ElementType1: Element {
func compute() -> Int {
return 1
}
}
struct ElementType2: Element {
func compute() -> Int {
return 2
}
}
var childElements: [Element?] =
childElements.append(ElementType1())
childElements.append(ElementType2())
childElements.append(nil)
childElements.flatMap { $0 }.forEach { print($0.compute()) }
1
2
···
On Dec 28, 2016, at 4:10 PM, Brandon Knope via swift-users <swift-users@swift.org> wrote:
Aren’t I losing the ability to enforce what is going into this enum’s associated value then?
Brandon
On Dec 28, 2016, at 7:05 PM, Nevin Brackett-Rozinsky <nevin.brackettrozinsky@gmail.com <mailto:nevin.brackettrozinsky@gmail.com>> wrote:
It will work if you change the enum declaration to:
enum ElementNode<T>
In other words, let the enum hold arbitrary unconstrained associated types, and then make your APIs utilize instances of the enum with the associated type constrained to a protocol.
The specific example you provide is essentially equivalent to:
var childElements = [Element?]()
Nevin
On Wed, Dec 28, 2016 at 6:41 PM, Brandon Knope via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
I don’t understand why this is a problem
protocol Element {
}
enum ElementNode<T: Element> {
case element(T)
case empty
}
var childElements = [ElementNode<Element>]()
I need to represent an array of my nodes that could be multiple kinds of elements
Is there a workaround?
Brandon
_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users