Closed ventor3000 closed 11 years ago
+1 It is annoying to cast back to PAnimal every time
Nicely formatted:
type
TAnimal=object {.inheritable.}
PAnimal=ref TObject
TDog=object of TAnimal
PDog=ref TDog
TCat=object of TAnimal
PCat=ref TCat
TAnimalArray=array[0..2,PAnimal]
proc newDog():PDog = new(result)
proc newCat():PCat = new(result)
proc test(a:openarray[PAnimal])=
echo("dummy")
test(newDog(),newCat()) #does not work
var myarray:TAnimalArray=[newDog(),newCat(),newDog()] #does not work
var myarray2:TAnimalArray=[newDog(),newDog(),newDog()] #does not work either
Yep, implement covariance :-P
Feature request.
The following code, which implicitly tells Nimrod that vars test and test2 are TAnimalArray:s does not work. Implicit casting of the elements in array to TAnimal should be done.
type TAnimal=object PAnimal=ref TObject
proc newDog():PDog = new(result)
proc newCat():PCat = new(result)
proc test(a:openarray[PAnimal])= echo("dummy")
test(newDog(),newCat()) #does not work
var myarray:TAnimalArray=[newDog(),newCat(),newDog()] #does not work
var myarray2:TAnimalArray=[newDog(),newDog(),newDog()] #does not work either