You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, it is not possible to build a seq, array or openArray of the super type with the initialiser syntax when only a single subtype element is used. It works fine though when at least two different subtypes are passed into the initialiser.
type
Base = ref object of RootObj
A = ref object of Base
B = ref object of Base
# Sequences
var s: seq[Base]
s = @[A(), B()] # OK
#s = @[A()] # FAILS TO COMPILE:
# Error: type mismatch: got (seq[A]) but expected 'seq[Base]'
# Arrays
var arr: array[0..1, Base]
arr = [A(), B()] # OK
#arr = [A(), A()] # FAILS TO COMPILE:
# Error: type mismatch: got (array[0..1, A]) but expected 'array[0..1, Base]'
# Open arrays
proc openArrayTest(oa: openArray[Base]) = discard
openArrayTest([A(), B()]) # OK
#openArrayTest([A()]) # FAILS TO COMPILE:
# Error: type mismatch: got (array[0..0, A])
# but expected one of:
# proc openArrayTest(oa: openArray[Base])
openArrayTest(@[A(), B()]) # OK
#openArrayTest(@[A()]) # FAILS TO COMPILE:
# Error: type mismatch: got (seq[A])
# but expected one of:
# proc openArrayTest(oa: openArray[Base])
The text was updated successfully, but these errors were encountered:
That's not a bug. The subtype relation cannot be lifted from A <: B to C[A] <: C[B] it would be unsound. You need to cast your 'A' to 'Base' explicitly.
Currently, it is not possible to build a seq, array or openArray of the super type with the initialiser syntax when only a single subtype element is used. It works fine though when at least two different subtypes are passed into the initialiser.
The text was updated successfully, but these errors were encountered: