Hello again,
I have the following situation, with a myFoo variable of type Foo that is
instantiated as one of the foo subtypes:
type
Foo = ref object of RootObj
x: string = "I'm Foo"
Bar1 = ref object of Foo
y: string = "I'm Bar1"
Bar2 = ref object of Foo
z: string = "I'm Bar2"
FooManager = ref object
myFoo: Foo
var manager: FooManager = FooManager(
myFoo: Bar1()
)
method showContents(b: Bar1) =
echo b.y
method showContents(b: Bar2) =
echo b.z
showContents(manager.myFoo)
Run
That doesn't work because myFoo is a Foo type, not a Bar1 or Bar2 type, so I
have to convert it by hand like so:
method showContents(f: Foo) =
if f of Bar1:
showContents(f.Bar1)
elif f of Bar2:
showContents(f.Bar2)
Run
which defeats the point.
(I also tried using converters but couldn't make it work.)
Is there anyway to make this work without having to check the type of myFoo?