fixes yet another option type
This commit is contained in:
parent
e4e87f1cb2
commit
62a10df765
3 changed files with 61 additions and 8 deletions
|
|
@ -113,9 +113,15 @@ proc pickMaxInt(n: PNode): BiggestInt =
|
||||||
internalError(n.info, "pickMaxInt")
|
internalError(n.info, "pickMaxInt")
|
||||||
|
|
||||||
proc makeRange(typ: PType, first, last: BiggestInt): PType =
|
proc makeRange(typ: PType, first, last: BiggestInt): PType =
|
||||||
|
let minA = min(first, last)
|
||||||
|
let maxA = max(first, last)
|
||||||
|
let lowerNode = newIntNode(nkIntLit, minA)
|
||||||
|
if typ.kind == tyInt and minA == maxA:
|
||||||
|
result = getIntLitType(lowerNode)
|
||||||
|
else:
|
||||||
var n = newNode(nkRange)
|
var n = newNode(nkRange)
|
||||||
addSon(n, newIntNode(nkIntLit, min(first, last)))
|
addSon(n, lowerNode)
|
||||||
addSon(n, newIntNode(nkIntLit, max(first, last)))
|
addSon(n, newIntNode(nkIntLit, maxA))
|
||||||
result = newType(tyRange, typ.owner)
|
result = newType(tyRange, typ.owner)
|
||||||
result.n = n
|
result.n = n
|
||||||
addSonSkipIntLit(result, skipTypes(typ, {tyRange}))
|
addSonSkipIntLit(result, skipTypes(typ, {tyRange}))
|
||||||
|
|
|
||||||
|
|
@ -385,8 +385,8 @@ proc mutateType(t: PType, iter: TTypeMutator, closure: PObject): PType =
|
||||||
|
|
||||||
proc valueToString(a: PNode): string =
|
proc valueToString(a: PNode): string =
|
||||||
case a.kind
|
case a.kind
|
||||||
of nkCharLit..nkUInt64Lit: result = $(a.intVal)
|
of nkCharLit..nkUInt64Lit: result = $a.intVal
|
||||||
of nkFloatLit..nkFloat128Lit: result = $(a.floatVal)
|
of nkFloatLit..nkFloat128Lit: result = $a.floatVal
|
||||||
of nkStrLit..nkTripleStrLit: result = a.strVal
|
of nkStrLit..nkTripleStrLit: result = a.strVal
|
||||||
else: result = "<invalid value>"
|
else: result = "<invalid value>"
|
||||||
|
|
||||||
|
|
|
||||||
47
tests/objvariant/tyaoption.nim
Normal file
47
tests/objvariant/tyaoption.nim
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue