Polysemous Values Qustion
Jari-Matti Mäkelä
jmjmak at utu.fi.invalid
Wed Sep 26 01:12:36 PDT 2007
downs wrote:
> 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.
Types of "new Foo" and "new Bar" can be [Foo, Object, A, B]. (ok, maybe also
void*)
If in the type world ?: is a function (x,y) -> (z) where z belongs to both x
and y, "flag ? new Foo : new Bar" can very well be [Object, A, B], and by
the same logic so can also "whatsMyType", because typewise = is probably a
function (x) -> (x).
More information about the Digitalmars-d
mailing list