Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 24, 2006 Array implicit conversions subvert type system | ||||
---|---|---|---|---|
| ||||
Can't remember if I raised this before (it was loitering among the testcases from my last peek at D), but it's still true in 0.174. "Derived[] to Base[]" assignments really shouldn't happen without either a copy or an explit cast. --- class Fruit {}; class Apple : Fruit {}; class Orange : Fruit {}; void main() { Apple[] apples; apples.length = 3; Fruit[] fruits = apples; Orange orange = new Orange; fruits[1] = orange; if (apples[1] == orange) printf("Oops.\n"); } |
November 25, 2006 Re: Array implicit conversions subvert type system | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Capp | Mike Capp wrote:
> Can't remember if I raised this before (it was loitering among the testcases
> from my last peek at D), but it's still true in 0.174. "Derived[] to Base[]"
> assignments really shouldn't happen without either a copy or an explit cast.
>
> ---
>
> class Fruit {};
> class Apple : Fruit {};
> class Orange : Fruit {};
>
> void main()
> {
> Apple[] apples;
> apples.length = 3;
> Fruit[] fruits = apples;
> Orange orange = new Orange;
> fruits[1] = orange;
>
> if (apples[1] == orange) printf("Oops.\n");
> }
No, purposeful, contrived subversion of the type system subverts the type system.
- Gregor Richards
|
November 25, 2006 Re: Array implicit conversions subvert type system | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gregor Richards | Gregor Richards wrote:
> No, purposeful, contrived subversion of the type
> system subverts the type system.
Purposeful, contrived subversion of the type system should be marked as such with a cast, IMHO. That's what casts are for. It's hardly a burden for the programmer, and it prevents accidents.
This crops up in basic OO FAQs for a reason. It's a real mistake that real people make.
|
November 25, 2006 Re: Array implicit conversions subvert type system | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Capp | void eatFruit(Fruit[] yum) { ... } void pickApples() { Apple[] a; for (int i = 0; i < 100; i++) { a ~= new Apple(); } eatFruit(a); // nope, can't eat it, it's Apples, not fruit } |
Copyright © 1999-2021 by the D Language Foundation