I tried to build an option type and a function that takes an union type:
#! strongSpaces
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
This code works fine, but when I uncomment either a1 or a3 it fails at runtime with an unreadable error message:
antoras@wookypooky ~/dev/nimrod % nimrod c --verbosity:0 -r maybe.nim
maybe.nim(39, 4) Hint: 'a3' is declared but not used [XDeclaredButNotUsed]
Error: execution of an external program failed; rerun with --parallelBuild:1 to see the error message
1 antoras@wookypooky ~/dev/nimrod % nimrod c --parallelBuild:1 --verbosity:0 -r maybe.nim :(
maybe.nim(39, 4) Hint: 'a3' is declared but not used [XDeclaredButNotUsed]
/home/antoras/dev/nimrod/nimcache/maybe.c: In function ‘intorstring_88437’:
/home/antoras/dev/nimrod/nimcache/maybe.c:280:9: error: incompatible types when assigning to type ‘option88106’ from type ‘option88624’
result = some_88617((NI64)(TMP153));
^
Error: execution of an external program failed
What does this error message mean?