Skip to content

Instantly share code, notes, and snippets.

@mflamer
Last active December 24, 2015 15:29
Show Gist options
  • Save mflamer/6821275 to your computer and use it in GitHub Desktop.
Save mflamer/6821275 to your computer and use it in GitHub Desktop.
poor mans ADT?
#----------------------------------------------------
# ADT
#----------------------------------------------------
type
SumT[T1,T2,T3,T4] = int
proc popFlags(x: int): int {.inline.} = x and 3
proc popPtr(x: int): int {.inline.} =
result = x and 0xFFFFFFFC'i32
proc getyp(s: SumT[T1,T2,T3,T4]): auto =
var
f = s.popFlags
p = s.popPtr
if f == 0: return cast[ptr T1](p)[]
if f == 1: return cast[ptr T2](p)[]
if f == 2: return cast[ptr T3](p)[]
if f == 3: return cast[ptr T4](p)[]
var val = 999.99
var typ: SumT[int,float,int,float] = cast[int](val.addr) and 1
echo(typ.getyp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment