1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
discard """
output: '''
34
b
wohoo
baz
'''
"""
block tobject2:
# Tests the object implementation
type
TPoint2d {.inheritable.} = object
x, y: int
TPoint3d = object of TPoint2d
z: int # added a field
proc getPoint( p: var TPoint2d) =
writeLine(stdout, p.x)
var p: TPoint3d
TPoint2d(p).x = 34
p.y = 98
p.z = 343
getPoint(p)
block tofopr:
type
TMyType {.inheritable.} = object
len: int
data: string
TOtherType = object of TMyType
proc p(x: TMyType): bool =
return x of TOtherType
var
m: TMyType
n: TOtherType
doAssert p(m) == false
doAssert p(n)
block toop:
type
TA = object of RootObj
x, y: int
TB = object of TA
z: int
TC = object of TB
whatever: string
proc p(a: var TA) = echo "a"
proc p(b: var TB) = echo "b"
var c: TC
p(c)
block tfefobjsyntax:
type
Foo = object
a, b: int
s: string
FooBar = object of RootObj
n, m: string
Baz = object of FooBar
proc invoke(a: ref Baz) =
echo "baz"
# check object construction:
let x = (ref Foo)(a: 0, b: 45, s: "wohoo")
echo x.s
var y: ref FooBar = (ref Baz)(n: "n", m: "m")
invoke((ref Baz)(y))
block t3012:
type
A {.inheritable.} = object
C {.inheritable.} = ref object
type
AA = ref object of A
CC = ref object of C
block t7244:
type
Foo = ref object of RootRef
Bar = ref object of Foo
proc test(foo: var Foo) = discard
proc test(bar: var Bar) = test(Foo(bar))
import std/macros
#bug #20856
macro ensureImplWorksOnConstr(t: typed): untyped =
expectKind(t, nnkObjConstr)
doAssert t[0].getTypeInst.getImpl.repr == "A = object"
doAssert t[0].getImpl.repr == "A = object"
type A = object
ensureImplWorksOnConstr(A())
|