The following program generates the errors:
t.nim(15, 17) template/generic instantiation from here
t.nim(5, 28) Error: expression has no address
Why does the expression buffer[0] have no address?
import endians
import strutils
proc parse[T](buffer: openArray[uint8]): T =
let pointer = addr(buffer[0])
when sizeof(T) == 2:
littleEndian16(addr(result), pointer)
elif sizeof(T) == 4:
littleEndian32(addr(result), pointer)
else:
result = (T)42
var buffer = [0x01'u8, 0x23, 0x45, 0x67]
echo toHex(parse[uint16](buffer))
echo toHex(parse[uint32](buffer))
Is the rule that variables defined with var have addresses and defined with let
do not?
It seems like defining the proc without the var is the right thing to do since
the procedure is not modifying the array or sequence passed in.
It would be convenient to be able to pass let variables to the procedure too:
let buffer = [0x01'u8, 0x23, 0x45, 0x67]
Do I need to rewrite the endian procedures so they don't require addresses to
be able to get what I want? Can someone explain the issues to me?