> `var a:seq[A] = @[B(val:1), B(val:2)]` > can not work as Nim is a statically
> typed language, seq[A] and seq[B] are different types, so assignment is not
> allowed.
There's nothing about static typing that forbids this; the collection class
just needs the appropriate generic assignment
with the conversion of first element in the `seq` `B(val:1).A` you tell the
compiler, that you create a `seq[A]`. For the remaining elements you don't need
the conversion:
var a:seq[A] = @[B(val:1).A, B(val:2)]
Run
and you can even omit the type declaration for `a`:
The thing is that in example1 case there is an implicit conversion from Cat to
Animal, while in example2 that conversion has to be explicit.
I would expect the implicit conversion happening in both cases. Probably there
is a rationale for that no happening.
Try this.
type
A = ref object of RootObj
B = ref object of A
val:int
proc example*(x:seq[A]) =
for i in x:
echo repr i
var a:seq[A] = @[B(val:1).A, B(val:2),A()] #B(val:1).A instead of #B(val:1)
echo repr a
example(a)
I understand the rationale of seq[Animal] being a different type than seq[Cat].
But I believe this is counter-intuitive when Cat it is actually an animal.
In fact, it looks strange to me the following behaviour:
type
Animal = ref object of RootObj
Cat = ref object of
snej, seq is in modern Nim a value object, not a ref. In old Nimrod we could
assign nil to a seq, but that is not possible today.
mantielero,
var a:seq[A] = @[B(val:1), B(val:2)]
Run
can not work as Nim is a statically typed language, seq[A] and seq[B] are
different
I’m a Nim newbie, haven’t used seq much, and I’m making the assumption that seq
is a reference type, ie. passed by reference not by value. If that’s wrong,
ignore this!)
You can’t assign an instance of seq[B] to a variable of type seq[A], because
you could then use that variable to add a non-B
I am still struggling with the type system .
For instance:
type
A = ref object of RootObj
B = ref object of A
val:int
proc example*(x:seq[A]) =
for i in x:
echo repr i
var a:seq[A] = @[B(val:1), B(val:2)] #<--- This fails, but
My bad. It was an error somewhere else.
Thank both. It is clear. In fact I just realised that the problem was cause by
something different (that I still don't understand), but probably it has
something to do with the stack and the heap.
I have something like the following:
let points = newPoint( @[ pt(0,0,0),
> Is there a way to make it accept all the instances inheriting from Curve?
As that question arises often, I tried to explain that in my book (with
geometric shapes).
It should just work.
type
A = ref object of RootObj
B = ref object of A
C = ref object of B
proc foo(xs: openarray[B]) =
for x in xs:
if x of C: echo "C"
elif x of B: echo "B"
elif x of A: echo "A"
foo([C(), B(), C()])
I have a function with the following signature:
proc newCurveLoop*( curves:seq[Curve]; tag:int = -1 ):Loop
Run
On the other hand I have:
Curve* = ref object of Tag
Line* = ref object of Curve
ini*, fin*:Point
Run
The type safety makes
13 matches
Mail list logo