Skip to content

Instantly share code, notes, and snippets.

@mflamer
Last active December 29, 2015 01:18
Show Gist options
  • Save mflamer/7591727 to your computer and use it in GitHub Desktop.
Save mflamer/7591727 to your computer and use it in GitHub Desktop.
Enum as tagged pointer in nimrod
type
TPnt = tuple[x,y: float]
TMaybe {.enumSumTyp.} = enum
None,
Just = TPnt
var
x = (x: 45.96, y: 25.25)
test = Just(x)
proc `$`(m: TMaybe): string =
if m == None:
result = "None"
elif m == Just:
result = "Just " & $m[]
else:
result = "something went wrong!"
echo(test) #Using `$` above
echo(test[]) #Deref
echo(test.y) #Field Access
@mflamer
Copy link
Author

mflamer commented Nov 28, 2013

The next hurdle is to skip the step of creating x first. If the user were to create a Just((x: 45.96, y: 25.25)) directly, I need to create a heap copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment