初始化两个元组的数组失败

时间:2021-01-08 21:32:42

I have a simple function that returns an array of tuples

我有一个简单的函数返回一个元组数组

func findNodeNeighbors(node: Node) -> [(node: Node, distance: Double)] {
    var neighbors = [(node: Node, distance: Double)]()
    var nodeLinks = linksWith(node)
    for link in nodeLinks {
        neighbors.append((node: link.otherNodeLinkedWith(node), distance: link.length))
    }
    return neighbors
}

But this turns out is an error Invalid use of () to call a clue of non-function type on the first line of the function body.

但事实证明这是一个错误无效的使用()来调用函数体的第一行上的非函数类型的线索。

If instead I declare the type for neighbors explicitly, everything is fine.

如果我明确地声明邻居的类型,一切都很好。

var neighbors: [(node: Node, distance: Double)] = []

How come?

I've read that it is preferred to declare arrays by initialising them and allowing for implicit type inference.

我已经读过,最好通过初始化它们并允许隐式类型推断来声明数组。

3 个解决方案

#1


4  

Pretty certain this is a bug in Swift's parser, specifically to do with the [Type] sugar in combination with named tuples.

很确定这是Swift解析器中的一个错误,特别是与[Type]糖结合命名元组。

var neighbors = Array<(node: Node, distance: Double)>() (which should be identical to [(node: Node, distance: Double)]()) works fine.

var neighbors = Array <(node:Node,distance:Double)>()(应该与[(node:Node,distance:Double)]()相同,工作正常。

edit: looks like the dictionary equivalent has the same problem

编辑:看起来像字典等效有相同的问题

Works fine:

var d = Dictionary<Int,(x: Int, y: Int)>()

Busted:

var d = [Int:(x: Int, y: Int)]()

#2


2  

what if I want an empty array as the initial value?

如果我想要一个空数组作为初始值怎么办?

I am not 100% certain - but I think that another way you can quickly get around this issue currently is by declaring the tuple using a typealias.

我不是100%肯定 - 但我认为你现在能够快速解决这个问题的另一种方法是使用一个类型来声明元组。

Eg:

typealias Test = (Node, Double)

func findNodeNeighbors(node: Node) -> [Test] {
    var neighbors = [Test]()
    //etc
}

#3


0  

You should initialise the array using default values:

您应该使用默认值初始化数组:

var neighbors = [(node: node, distance: 0.0)]

#1


4  

Pretty certain this is a bug in Swift's parser, specifically to do with the [Type] sugar in combination with named tuples.

很确定这是Swift解析器中的一个错误,特别是与[Type]糖结合命名元组。

var neighbors = Array<(node: Node, distance: Double)>() (which should be identical to [(node: Node, distance: Double)]()) works fine.

var neighbors = Array <(node:Node,distance:Double)>()(应该与[(node:Node,distance:Double)]()相同,工作正常。

edit: looks like the dictionary equivalent has the same problem

编辑:看起来像字典等效有相同的问题

Works fine:

var d = Dictionary<Int,(x: Int, y: Int)>()

Busted:

var d = [Int:(x: Int, y: Int)]()

#2


2  

what if I want an empty array as the initial value?

如果我想要一个空数组作为初始值怎么办?

I am not 100% certain - but I think that another way you can quickly get around this issue currently is by declaring the tuple using a typealias.

我不是100%肯定 - 但我认为你现在能够快速解决这个问题的另一种方法是使用一个类型来声明元组。

Eg:

typealias Test = (Node, Double)

func findNodeNeighbors(node: Node) -> [Test] {
    var neighbors = [Test]()
    //etc
}

#3


0  

You should initialise the array using default values:

您应该使用默认值初始化数组:

var neighbors = [(node: node, distance: 0.0)]