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'

2017-10-09 00:17:36

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.

2017-10-09 02:16:34
I.e. there's no return type overloading, test cannot automatically get, by what type it is parameterised.
2017-10-09 05:32:13
Thank you very much. 2017-10-10 02:51:35