2

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?

3
  • 1
    That's a bug, compare e.g. stackoverflow.com/questions/32833078/…. Commented May 31, 2017 at 16:38
  • 1
    Workaround: typealias Gravl_Node = Gravl.Node, var attributes = [(attribute: String?, value: Gravl_Node)]() Commented May 31, 2017 at 16:41
  • @vacawama Aha, I didn't think to try a typealias, thanks! Commented May 31, 2017 at 16:53

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.