diff options
-rwxr-xr-x | rod/types.nim | 14 | ||||
-rw-r--r-- | tests/accept/compile/tgenericvariant.nim | 23 |
2 files changed, 29 insertions, 8 deletions
diff --git a/rod/types.nim b/rod/types.nim index ed9d5271a..32f9c7a94 100755 --- a/rod/types.nim +++ b/rod/types.nim @@ -430,8 +430,12 @@ proc TypeToString(typ: PType, prefer: TPreferedDesc = preferName): string = if (prefer == preferName) and (t.sym != nil): return t.sym.Name.s case t.Kind - of tyGenericInst: - result = typeToString(lastSon(t), prefer) + of tyGenericBody, tyGenericInst, tyGenericInvokation: + result = typeToString(t.sons[0]) & '[' + for i in countup(1, sonsLen(t) -1 -ord(t.kind != tyGenericInvokation)): + if i > 1: add(result, ", ") + add(result, typeToString(t.sons[i])) + add(result, ']') of tyArray: if t.sons[0].kind == tyRange: result = "array[" & rangeToStr(t.sons[0].n) & ", " & @@ -439,12 +443,6 @@ proc TypeToString(typ: PType, prefer: TPreferedDesc = preferName): string = else: result = "array[" & typeToString(t.sons[0]) & ", " & typeToString(t.sons[1]) & ']' - of tyGenericInvokation, tyGenericBody: - result = typeToString(t.sons[0]) & '[' - for i in countup(1, sonsLen(t) - 1): - if i > 1: add(result, ", ") - add(result, typeToString(t.sons[i])) - add(result, ']') of tyArrayConstr: result = "Array constructor[" & rangeToStr(t.sons[0].n) & ", " & typeToString(t.sons[1]) & ']' diff --git a/tests/accept/compile/tgenericvariant.nim b/tests/accept/compile/tgenericvariant.nim new file mode 100644 index 000000000..51d01355a --- /dev/null +++ b/tests/accept/compile/tgenericvariant.nim @@ -0,0 +1,23 @@ +type + TMaybe[T] = object + case empty: Bool + of False: value: T + else: nil + +proc Just*[T](val: T): TMaybe[T] = + result.empty = False + result.value = val + +proc Nothing[T](): TMaybe[T] = + result.empty = True + +proc safeReadLine(): TMaybe[string] = + var r = stdin.readLine() + if r == "": return Nothing[string]() + else: return Just(r) + +when isMainModule: + var Test = Just("Test") + echo(Test.value) + var mSomething = safeReadLine() + echo(mSomething.value) |