Last active
December 24, 2015 15:29
-
-
Save mflamer/6821275 to your computer and use it in GitHub Desktop.
poor mans ADT?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#---------------------------------------------------- | |
# 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