I'm trying to write a generic function, here is a simplified version. It generates the errors shown below. What am I doing wrong?
import strutils proc test16(): uint16 = var buffer: array[sizeof(uint16), uint8] for ix in 0..sizeof(uint16)-1: buffer[ix] = cast [uint8](ix) result = cast[uint16](buffer) proc test[T](): T = var buffer: array[sizeof(T), uint8] for ix in 0..sizeof(T)-1: buffer[ix] = cast [uint8](ix) result = cast[T](buffer) var value16: uint16 = test16() echo "value16 = ", toHex(value16) var valueT: uint16 = test() echo "valueT = ", toHex(valueT)
Here are the errors:
# t2.nim(18, 26) template/generic instantiation from here # t2.nim(9, 11) Error: cannot instantiate: 'T'
Try either (on line 18)
var valueT = test[uint16]()
var valueT: uint16 = test[uint16]()
The issue was that test() needed to be "told" what the generic type T was.