I can’t find any documentation on generic methods in Swift (if that’s what it is). I thought it might be equivalent to “Extensions with a Generic Where Clause,” but rewriting it like this:
extension Data where T: ConvertibleFromByte {
func asTensor() -> Tensor<T> {
return Tensor(map(T.init))
}
}
gives the compiler error:
trailing 'where' clause for extension of non-generic type 'Data'
if ‘Data’ is non-generic, where does T’s Type come from?
it looks as if Data’s Type can be inferred by the compiler (and put into T), but what in the code makes this possible?
is this a special case for “Data”, or generally available in the language?
Can anyone suggest any resources that would get someone to understanding this grammar?!
And the T.init closure, I think it is taking an Array of UInt8, initing an Array of something ConvertibleFromByte (either Float or Int32 here), and passing that to the Tensor constructor, is that accurate?
The ConvertibleFromByte protocol defines an init(_: UInt8) initializer requirement, which exactly has the function type (UInt8) -> Self and can be passed to Data.map(_:).
The generic parameter T on Data.asTensor() is constrained to ConvertibleFromByte, so we can access the ConvertibleFromByte protocol requirement as T.init(_:).