This does not work, but illustrates what I'm looking for:
type FileBits = case BitDepth
of 8: uint8
of 16: uint16
of 24: uint32
of 32: uint32
of 64: uint64
else : discard
How do I create a type depending on a variable?
TIA
Here you are:
type
BitDepth = enum
BD8, BD16, BD24, BD32, BD64
FileBits = object
case kind: BitDepth:
of BD8: smpl8: uint8
of BD16: smpl16: uint16
of BD24: smpl24: uint32
of BD32: smpl32: uint32
of BD64: smpl64: uint64
The catch is, it's not any better and you can't really use it anyway without generics or lots of case branching each time you process FileBits.Ok, If you're sure you need just one FileBits definition,
const BitDepth = 32
when BitDepth == 8:
type FileBits = uint8
when BitDepth == 16:
type FileBits = uint16
when BitDepth == 24:
type FileBits = uint32
when BitDepth == 32:
type FileBits = uint32
when BitDepth == 64:
type FileBits = uint64
var
bits : FileBits
echo sizeof(bits)
this can be slightly less boilerplatey and error-checked if you fancy:
from macros import error
type FileBits = (
when BitDepth == 8: uint8
elif BitDepth == 16: uint16
elif BitDepth in {24,32}: uint32
elif BitDepth == 64: uint64
else: static: error("bitdepth of " & $BitDepth & "unsupported"); nil
)
Haha, no I'm not that easily scared. More to study.
Thanks