กำลังพยายามทำความเข้าใจและชื่นชมวิธีการทำงานของ ArrayLiteralConvertible...
struct Struct<T>: ArrayLiteralConvertible {
init(arrayLiteral elements: T...) {
for element in elements {
print(element)
}
}
}
let str: Struct<Int> = [1,2,3]
เอาท์พุท:
1 2 3
ตอนนี้ฉันกำลังพยายามทำสิ่งเดียวกัน แต่คราวนี้กับ ArrayLiteralConvertible เวอร์ชันของฉันเอง:
protocol MyALC {
typealias Element
init(arrLit elements: Self.Element...)
}
struct NewStruct<T>: MyALC {
init(arrLit elements: T...) {
for element in elements {
print(element)
}
}
}
let newStr: NewStruct<Int> = [1,2,3]
อย่างไรก็ตามมันไม่ได้ผล!
error: cannot convert value of type '[Int]' to specified type 'NewStruct' let newStr: NewStruct = [1,2,3]
ฉันกำลังทำอะไรผิดหรือมีการจัดการพิเศษสำหรับ ArrayLiteralConvertible หรือไม่