Consider this toy example code which compiles just fine in Playground:

var x: any Hashable
let y = "String"
x = y

Then this, which also compiles fine:

var x: [any Hashable]
let y = ["String"]
x = y

While this doesn't:

struct Wrapper<T> {
    let wrapped: T
}

var x: Wrapper<any Hashable>
let y = Wrapper(wrapped: "String")
x = y // >>> Cannot assign value of type 'Wrapper<String>' to type 'Wrapper<any Hashable>'

I understand the error message and that those nested types are different from each other. But how is wrapping in an Array different from wrapping in my generic Wrapper struct?

Is my Wrapper struct missing something? What does Array do to make the compiler accept the assignment?

0

There are 0 answers