Polysemous Values Qustion

downs default_357-line at yahoo.de
Tue Sep 25 22:51:54 PDT 2007


Nathan Reed wrote:
> Robert Frser wrote:
>> Can someone giv me an example where polysemous values could apply to a
>> class (given the class has (multiple) opImplicitCastTo/From)
> 
> Here's another example where polysemous values could come in handy:
> 
> interface A { ... }
> interface B { ... }
> 
> class Foo : A, B { ... }
> class Bar : A, B { ... }
> 
> void main (string[] args)
> {
>     bool flag = ...
>     auto whatsMyType = flag ? new Foo : new Bar;
> }
> 
> Here, Foo and Bar both implement A and B, so the variable 'whatsMyType'
> could have either type A or type B.  If you do this in D now, it will
> fail with a compile error, since Foo and Bar have no least upper
> bound...but with polysemous values, whatsMyType could have type "A or B".
> 
> Thanks,
> Nathan Reed

I don't think it can.
Polysemous values are values that can be interpreted as more than one
type simultaneously. whatsMyType can be interpreted as either Foo or
Bar, but not both at the same time - whichever one is correct at
runtime, the other one isn't.
The proper thing to do here is to cast them to Object and figure it out
via dynamic casts. :)

 --downs



More information about the Digitalmars-d mailing list