fully specialized generic failed at array construction
type
SBase[T, V] = ref object of RootObj
val: T
color: V
SRC = ref object of SBase[string, int]
SRD = ref object of SBase[string, int]
var a = SBase[string, int](val: "base", color: 1)
var b = SRC(val: "rc", color: 2)
var c = SRD(val: "rd", color: 3)
var x = [a, b, c] #ok
var y = [b, c, a] #failed
var z = [c, b, a] #failed
what need to be included in the test:
- the above case
- secondary form of ref -> e.g. from new(ref someobject)
- ptr form, and it's secondary form
- ~~ordinary object -> perhaps generate runtime error because of object slicing~~
see the evolution of this bug in #7601, #7818, and #7906
note: if possible, uncomment test at block test_t4799_6 of #4799
a,b,c are not defined in the test
a,b,c are not defined in the test
edited
I think this one should close , relevant issues are closed
nope. the compiler(devel v1.7.1) still rejects the above example. inside the compiler, the type comparator related to array and perhaps seq, is still buggy.
I think the result is expected, x is implicitly casting to base type, y and z can't casting from base type to derived type
ah, right. the example is misleading. it should be like this:
type
SBase[T, V] = ref object of RootObj
val: T
color: V
SRC = ref object of SBase[string, int]
SRD = ref object of SBase[string, int]
var a = SBase[string, int](val: "base", color: 1)
var b = SRC(val: "rc", color: 2)
var c = SRD(val: "rd", color: 3)
var x = [a, b, c] #ok
var y = [b, c] #failed
var z = [c, b] #failed
This works (and is normally what you're supposed to do):
type
SBase[T, V] = ref object of RootObj
val: T
color: V
SRC = ref object of SBase[string, int]
SRD = ref object of SBase[string, int]
var a = SBase[string, int](val: "base", color: 1)
var b = SRC(val: "rc", color: 2)
var c = SRD(val: "rd", color: 3)
var x = [a, b, c]
var y = [SBase[string, int](b), c, a]
var z = [SBase[string, int](c), b, a]
The problem is the error message:
(13, 10) Error: type mismatch: got 'SRC' for 'b' but expected 'ref SBase[system.string, system.int]'
It performs type inference to find the common supertype of b, c, a and c, b, a and infers a correct array type, but doesn't actually update the b and c nodes to convert to their supertype. Maybe a final fitNode call to every array element.