Suppose I use a class to handle buffers like this:
public class BufRef<T> where T: P {
typealias DataType = T
let buf : UnsafeMutablePointer<DataType>
let m : Int
let ldm : Int
let n : Int
init(_ m: Int, _ n: Int) { /*allocate buffer*/ }
deinit { buf.deallocate() }
}
protocol P {}
extension Float : P {}
extension Float16 : P {}
There’s no way to refer to DataType in a MemoryLayout<T>.stride-style struct…
func printstride(buffer: BufRef<some P>) {
// No no. Compiler chokes.
let stride = MemoryLayout<type(of:bufref).DataType>.stride
print(stride)
}
func printstride2(buffer: BufRef<some P>) {
// Compiler passes alright, but seriously??? Outputs 8 for T=Float16
let stride = MemoryLayout.stride(ofValue: type(of:bufref).DataType)
print(stride)
}
Have to write func printstride<T>(buffer: BufRef<T>) where T: P, which is rather sad.