Why are opCall's not implicitely assignable?
Hasan Aljudy
hasan.aljudy at gmail.com
Mon Sep 25 15:49:16 PDT 2006
Your example demonstrates very well why overriding assignments shouldn't
be allowed.
# Car mycar;
# mycar = COLOR.RED; //!!!!!!!
How can a car turn to a red color? This is totally meaningless and bogus.
Color is a property of the car:
# mycar.color = COLOR.RED;
roof is a property of the car, but this doesn't justify using
# mycar.roof = COLOR.RED;
because you're not changing the property roof (well, you're changing its
state) but rather, you're changing the "color" of the roof; so you're
changing a property of roof, thus:
# mycar.roof.color = COLOR.RED;
What you're trying to do makes code totally unreadable.
Karen Lanrap wrote:
> Mike Parker wrote:
>
>> Cars have color. These could all be considered object
>> properties. Can you name an object that has the property of
>> opCall?
>
> Assume we have some colors
>
> enum COLOR{ LIGHTBLUE, DARKBLUE, RED};
>
> and a car with some colorable parts
>
> class Car
> {
> Roofliner roof;
> Seat[4] seats;
> Carpet carpet;
> this()
> {
> roof= new Roofs.Handmade;
> seats[0]= new Seats.Handicapped(OVERWEIGHT);
> seats[1]= new Seats.Standard;
> seats[2]= new Seats.Auxiliar(LEFT, TINY);
> // ...
>
> Now I want to assign colors to the colorable parts
>
> Car car;
> void main()
> {
> car= new Car;
> car.roof= COLOR.LIGHTBLUE;
> seats[0]= COLOR.DARKBLUE;
> seats[1]= COLOR.RED
> // ...
>
> But you say that I am not allowed to do this, because the colorable
> parts are no properties of the car?
>
> I have to write
>
> Car car;
> void main()
> {
> car= new Car;
> with(car)
> {
> roof.color= COLOR.LIGHTBLUE;
> seats[0].color= COLOR.DARKBLUE;
> seats[1].color= COLOR.RED
> // ...
>
> instead? Thereby explicitely saying that I mean the color each time?
>
> How does this fit with "auto c='c';" implicit type inference? Why do
> I have to write
>
> COLOR col= seats[0].color;
>
> when it would suffice to write:
>
> COLOR col= seats[0];
>
> because of appropriate opCalls?
>
>
More information about the Digitalmars-d-learn
mailing list