Created
December 4, 2013 03:31
-
-
Save mflamer/7781907 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.