1

I am trying to initialise a empty dictionary with custom type in Swift but I am getting '>' is not a postfix unary operator error

struct Prims {
    var msSet = [Vertex<Int> : Double]() // This lines gives error
}

I tried another way; still getting same error

struct Prims {
  var msSet: [Vertex<Int> : Double]

   init() {
    self.msSet = [Vertex<Int> : Double]()
   }
}

I have defined Vertex in separate file

import Foundation

public struct Vertex<T: Hashable> {
    var data: T
}

extension Vertex: Hashable {
    public var hashValue: Int {
        return "\(data)".hashValue
    }

    static public func ==(lhs: Vertex, rhs: Vertex) -> Bool {
        return lhs.data == rhs.data
    }
}

extension Vertex: CustomStringConvertible {
    public var description: String {
        return "\(data)"
    }
}

I am looking for Why it is happening. I know using var msSet = Dictionary<Vertex<Int>, Double>() will work.

Rahul
  • 2,056
  • 1
  • 21
  • 37
  • 2
    There have been a few bugs surrounding the syntactic sugar form of the array initialiser – compare [Weird escaping function behavior after updating to Swift 3](http://stackoverflow.com/q/40599881/2976878) & [Why can't I use the short Array constructor syntax when creating an Array of a nested Struct?](http://stackoverflow.com/q/39041712/2976878). I would say this is just another variant of it. – Hamish Apr 07 '17 at 10:58

3 Answers3

2

While I can't tell you why the swift compiler emits this error, you can make it compile like this:

struct Prims {
    var msSet = Dictionary<Vertex<Int>, Double>()
}

or like this:

struct Prims {
    typealias V = Vertex<Int>
    var msSet = [V : Double]()
}
thm
  • 1,217
  • 10
  • 12
  • I am looking for `Why`? – Rahul Apr 07 '17 at 10:49
  • 1
    Sure sounds like a bug to me. Have you tried reporting it? – thm Apr 07 '17 at 11:09
  • Thanks for the accept and the upvote. Have you ever gotten around to filing a bug or found a duplicate? I just tried to compile your code using swift-DEVELOPMENT-SNAPSHOT-2017-05-12-a.xctoolchain and the error is still there. – thm May 15 '17 at 10:07
  • you are welcome. It is just a syntactic sugar and I read that it will be added later on and so didn't file a bug for this. – Rahul May 15 '17 at 11:15
2

There are multiple ways of going about this:

msSet = Dictionary< Vertex<Int>, Double >()

or

mSet = [ (Vertex<Int>), Double ]()

or even more verbose

typealias VertexInt=Vertex<Int>
mSet = [ VertexInt, Double ]

I looked over the swift grammar and couldn't find an answer as to why that particular syntax is not valid. It may very well be a bug.

Radu Diță
  • 13,476
  • 2
  • 30
  • 34
1
struct Prims {
    var msSet = [Vertex<Int> : Double]() // This lines gives error
}

change to

struct Prims {
    var msSet = [(Vertex<Int>) : Double]() // This lines gives error
}

complete code

struct Prims {
    var msSet = [(Vertex<Int>) : Double]()
}

public struct Vertex<T: Hashable> {
    var data: T
}

extension Vertex: Hashable {
    public var hashValue: Int {
        return data.hashValue
    }

    static public func ==(lhs: Vertex, rhs: Vertex) -> Bool {
        return lhs.data == rhs.data
    }
}

extension Vertex: CustomStringConvertible {
    public var description: String {
        return "\(data)"
    }
}

test Code

var test = Prims()
test.msSet.updateValue(43, forKey: Vertex(data: 12))
曾祥林
  • 96
  • 4