Skip to content

Instantly share code, notes, and snippets.

@mflamer
Created December 4, 2013 03:31
Show Gist options
  • Save mflamer/7781907 to your computer and use it in GitHub Desktop.
Save mflamer/7781907 to your computer and use it in GitHub Desktop.
type
TMaybeKind = enum
kNone,
kJust
TMaybe[T] = object
case kind: TMaybeKind
of kJust:
v: ref T
else: nil
proc `$`[T](x: TMaybe[T]): string =
case x.kind:
of kNone: result = "None"
of kJust: result = "Just " & $x.v[]
proc Just[T](x: T): TMaybe[T] =
result.kind = kJust
new(result.v)
result.v[] = x
proc None(): TMaybe[_] =
result.kind = kNone
@mflamer
Copy link
Author

mflamer commented Dec 4, 2013

Any thoughts on what to use to instantiate TMaybe[_] as the result of None()? Ideally it would be a "Top" type that would be the super-type of all types. This would allow a None to be universal instead of different for each instantiated type (TMaybe[int], TMaybe[string]), a None should always be None.

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