Dictionary of Int and an Embedded type = compile error when using the shorthand notation

extension Array {
	struct Results {
		var count: Int = 1
	}
}

struct SomeStruct<T> {
	var items = [Int: [T].Results]() // ERROR: Generic struct 'Dictionary' requires that 'Int.Type' conform to 'Hashable'
}

Is there a reason this isn't allowed? Using the full Array<T> notation does work fine:

extension Array {
	struct Results {
		var count: Int = 1
	}
}

struct SomeStruct<T> {
	var items = [Int: Array<T>.Results]() // compiles
}
1 Like

It looks like the compiler is interpreting this as [Int.self: [T].Results.self], i.e. a dictionary literal with one entry, probably because it doesn't know that you can access type members after using the sugared syntax for arrays and dictionaries. Can you file a bug at https://bugs.swift.org?

3 Likes

Yeah, it looks like a bug. On master compiler, I get:

/Users/spare/Desktop/test.swift:8:14: error: cannot call value of non-function type '[Int.Type : Array<T>.Results.Type]'
        var items = [Int: [T].Results]()
                    ^                 ~~

/Users/spare/Desktop/test.swift:8:14: error: type 'Int.Type' cannot conform to 'Hashable'; only concrete types such as structs, enums and classes can conform to protocols
        var items = [Int: [T].Results]()
                    ^
/Users/spare/Desktop/test.swift:8:14: note: required by generic struct 'Dictionary' where 'Key' = 'Int.Type'
        var items = [Int: [T].Results]()
                    ^

Thanks for the responses! I was unsure if this was something not allowed by swift or another compiler bug. Bug filed: https://bugs.swift.org/browse/SR-13352

1 Like
Terms of Service

Privacy Policy

Cookie Policy