Swift cannot create array of tuples containing a nested type

124 views Asked by At

I'm trying to do something that should be possible, but I'm getting a strange error. I have a nested type Gravl.Node and am trying to instantiate an array of tuples like so:

var attributes = [(attribute: String?, value: Gravl.Node)]()

…but Swift is complaining:

Cannot call value of non-function type '[(attribute: String?.Type, value: Gravl.Node.Type)]'

Any idea what gives? If I replace the nested type with something else it works fine.

Is this a bug in the parser/compiler?

1

There are 1 answers

0
vacawama On BEST ANSWER

Yes, this is a bug as noted by this question.

Following the example of that Q & A, you can change the syntax of the way you create the array:

var attributes: [(attribute: String?, value: Gravl.Node)] = []

or

var attributes = Array<(attribute: String?, value: Gravl.Node)>()

In this case, you can also work around the issue by creating a typealias:

Example:

class Gravl {
    enum Node {
        case first, last
    }
}

typealias Gravl_Node = Gravl.Node  // work around Swift bug
var attributes = [(attribute: String?, value: Gravl_Node)]()

attributes.append((attribute: "hello", value: Gravl.Node.first))