summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-04-03 07:54:58 +0200
committerAraq <rumpf_a@web.de>2014-04-03 07:54:58 +0200
commit62a10df76560d2361955e1072b4b0e1b2a62e477 (patch)
tree9a581742bcc14ce16feaa2d79bbe82276742c9e9 /tests
parente4e87f1cb2fe01a4462f7b0e3d347389f173beff (diff)
downloadNim-62a10df76560d2361955e1072b4b0e1b2a62e477.tar.gz
fixes yet another option type
Diffstat (limited to 'tests')
-rw-r--r--tests/objvariant/tyaoption.nim47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/objvariant/tyaoption.nim b/tests/objvariant/tyaoption.nim
new file mode 100644
index 000000000..635e60bb8
--- /dev/null
+++ b/tests/objvariant/tyaoption.nim
@@ -0,0 +1,47 @@
+discard """
+  output: '''some(str), some(5), none
+some(5!)
+some(10)'''
+"""
+
+import strutils
+
+type Option[A] = object
+  case isDefined*: Bool
+    of true:
+      value*: A
+    of false:
+      nil
+
+proc some[A](value: A): Option[A] =
+  Option[A](isDefined: true, value: value)
+
+proc none[A](): Option[A] =
+  Option[A](isDefined: false)
+
+proc `$`[A](o: Option[A]): String =
+  if o.isDefined:
+    "some($1)" % [$o.value]
+  else:
+    "none"
+
+let x = some("str")
+let y = some(5)
+let z = none[Int]()
+
+echo x, ", ", y, ", ", z
+
+proc intOrString[A : Int | String](o: Option[A]): Option[A] =
+  when A is Int:
+    some(o.value + 5)
+  elif A is String:
+    some(o.value & "!")
+  else:
+    o
+
+#let a1 = intOrString(none[String]())
+let a2 = intOrString(some("5"))
+let a3 = intOrString(some(5))
+#echo a1
+echo a2
+echo a3