fix #5654
This commit is contained in:
parent
fb3ff64450
commit
e11b3520ff
3 changed files with 47 additions and 2 deletions
|
|
@ -267,7 +267,7 @@ proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
|
||||||
# little HACK to support the new 'var T' as return type:
|
# little HACK to support the new 'var T' as return type:
|
||||||
linefmt(p, cpsStmts, "$1 = $2;$n", rdLoc(dest), rdLoc(src))
|
linefmt(p, cpsStmts, "$1 = $2;$n", rdLoc(dest), rdLoc(src))
|
||||||
return
|
return
|
||||||
let ty = skipTypes(dest.t, abstractRange)
|
let ty = skipTypes(dest.t, abstractRange + tyUserTypeClasses)
|
||||||
case ty.kind
|
case ty.kind
|
||||||
of tyRef:
|
of tyRef:
|
||||||
genRefAssign(p, dest, src, flags)
|
genRefAssign(p, dest, src, flags)
|
||||||
|
|
|
||||||
|
|
@ -164,7 +164,7 @@ proc mapType(typ: PType): TCTypeKind =
|
||||||
of tySet: result = mapSetType(typ)
|
of tySet: result = mapSetType(typ)
|
||||||
of tyOpenArray, tyArray, tyVarargs: result = ctArray
|
of tyOpenArray, tyArray, tyVarargs: result = ctArray
|
||||||
of tyObject, tyTuple: result = ctStruct
|
of tyObject, tyTuple: result = ctStruct
|
||||||
of tyUserTypeClass, tyUserTypeClassInst:
|
of tyUserTypeClasses:
|
||||||
internalAssert typ.isResolvedUserTypeClass
|
internalAssert typ.isResolvedUserTypeClass
|
||||||
return mapType(typ.lastSon)
|
return mapType(typ.lastSon)
|
||||||
of tyGenericBody, tyGenericInst, tyGenericParam, tyDistinct, tyOrdinal,
|
of tyGenericBody, tyGenericInst, tyGenericParam, tyDistinct, tyOrdinal,
|
||||||
|
|
@ -1131,6 +1131,9 @@ proc genTypeInfo(m: BModule, t: PType): Rope =
|
||||||
of tyStatic:
|
of tyStatic:
|
||||||
if t.n != nil: result = genTypeInfo(m, lastSon t)
|
if t.n != nil: result = genTypeInfo(m, lastSon t)
|
||||||
else: internalError("genTypeInfo(" & $t.kind & ')')
|
else: internalError("genTypeInfo(" & $t.kind & ')')
|
||||||
|
of tyUserTypeClasses:
|
||||||
|
internalAssert t.isResolvedUserTypeClass
|
||||||
|
return genTypeInfo(m, t.lastSon)
|
||||||
of tyProc:
|
of tyProc:
|
||||||
if t.callConv != ccClosure:
|
if t.callConv != ccClosure:
|
||||||
genTypeInfoAuxBase(m, t, t, result, rope"0")
|
genTypeInfoAuxBase(m, t, t, result, rope"0")
|
||||||
|
|
|
||||||
42
tests/concepts/trandom_vars.nim
Normal file
42
tests/concepts/trandom_vars.nim
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
discard """
|
||||||
|
output: "11.0"
|
||||||
|
"""
|
||||||
|
|
||||||
|
type
|
||||||
|
# A random number generator
|
||||||
|
Random = object
|
||||||
|
random: proc(): float
|
||||||
|
# A generic typeclass for a random var
|
||||||
|
RandomVar[A] = concept x
|
||||||
|
var rng: Random
|
||||||
|
rng.sample(x) is A
|
||||||
|
# A few concrete instances
|
||||||
|
Uniform = object
|
||||||
|
a, b: float
|
||||||
|
ClosureVar[A] = object
|
||||||
|
f: proc(rng: var Random): A
|
||||||
|
|
||||||
|
# How to sample from various concrete instances
|
||||||
|
proc sample(rng: var Random, u: Uniform): float = u.a + (u.b - u.a) * rng.random()
|
||||||
|
|
||||||
|
proc sample[A](rng: var Random, c: ClosureVar[A]): A = c.f(rng)
|
||||||
|
|
||||||
|
proc uniform(a, b: float): Uniform = Uniform(a: a, b: b)
|
||||||
|
|
||||||
|
# How to lift a function on values to a function on random variables
|
||||||
|
proc map[A, B](x: RandomVar[A], f: proc(a: A): B): ClosureVar[B] =
|
||||||
|
proc inner(rng: var Random): B =
|
||||||
|
f(rng.sample(x))
|
||||||
|
|
||||||
|
result.f = inner
|
||||||
|
|
||||||
|
import future
|
||||||
|
|
||||||
|
proc fakeRandom(): Random =
|
||||||
|
result.random = () => 0.5
|
||||||
|
|
||||||
|
let x = uniform(1, 10).map((x: float) => 2 * x)
|
||||||
|
|
||||||
|
var rng = fakeRandom()
|
||||||
|
|
||||||
|
echo rng.sample(x)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue