Is there any advantage to doing this:
func test1<Integer: FixedWidthInteger>(_ b: [UInt8], as: Integer.Type) -> Integer {
let r = b.withUnsafeBytes { $0.baseAddress!.bindMemory(to: Integer.self, capacity: 1) }
return r.pointee
}
do {
let i8 = test1([1,0,0,0,0,0,0,0], as: UInt8.self)
print(type(of: i8), i8)
let i32 = test1([1,0,0,0,0,0,0,0], as: UInt32.self)
print(type(of: i32), i32)
}
vs this?
func test1<Integer: FixedWidthInteger>(_ b: [UInt8]) -> Integer {
let r = b.withUnsafeBytes { $0.baseAddress!.bindMemory(to: Integer.self, capacity: 1) }
return r.pointee
}
do {
let i8 = test1([1,0,0,0,0,0,0,0]) as UInt8
print(type(of: i8), i8)
let i32 = test1([1,0,0,0,0,0,0,0]) as UInt32
print(type(of: i32), i32)
}
I'm simply referring to the use of the Type parameter in the first example.
I think I was thrown by the definition of bindMemory, which does this sort of. But I now see that it returns a pointer to the type, and not the type itself, so I imagine that's why its needed in that case. Probably in my case above (yes, its a strawman example) the second way is fine. Yes? No? Who knows?