Consider the following code:
extension UInt32 {
// Convert byte array to UInt32
static func fromByteArray(_ bytes: [UInt8]) -> UInt32 {
var result: UInt32 = 0
let count = bytes.count <= 4 ? bytes.count : 4
for byte in bytes[0 ..< count] {
result = result << 8 | UInt32(byte)
}
return result
}
}
extension UInt16 {
// Convert byte array to UInt16
static func fromByteArray(_ bytes: [UInt8]) -> UInt16 {
var result: UInt16 = 0
let count = bytes.count <= 2 ? bytes.count : 2
for byte in bytes[0 ..< count] {
result = result << 8 | UInt16(byte)
}
return result
}
}
let array = [Int]((1 ... 4).GetSequence()) // Due to compiler error
let byteArray = [UInt8](array) // Due to compiler error
let short = UInt16.fromByteArray([UInt8](byteArray)) // Ambiguous call(?!)
let long = UInt32.fromByteArray([UInt8](byteArray)) // Ambiguous call(?!)
print(short)
print(long)
Both fromByteArray
calls give me (Cocoa on macOS):
E: Ambiguous call to overloaded method "fromByteArray" N: Potential overload: static func UInt32.fromByteArray(_ bytes: Swift.Array<Byte>) -> UInt32? N: Potential overload: static func UInt16.fromByteArray(_ bytes: Swift.Array<Byte>) -> UInt16?
I’m telling the compiler which one to use, so it’s hardly ambiguous.