Article: Writing Julia style multiple dispatch code in D

data pulverizer via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Wed Aug 30 16:45:13 PDT 2017


On Wednesday, 30 August 2017 at 22:49:54 UTC, jmh530 wrote:
> On Wednesday, 30 August 2017 at 22:30:12 UTC, data pulverizer 
> wrote:
>> On Wednesday, 30 August 2017 at 22:10:38 UTC, Jean-Louis Leroy 
>> wrote:
>>> On Wednesday, 30 August 2017 at 21:30:29 UTC, data pulverizer 
>>> wrote:
>>>> In the light of this I think your package just became more 
>>>> interesting to me.
>>>
>>> I think that your work and mine are complementary :-)
>>
>> Here is one strange difference between inheriting from an 
>> interface and a class:
>>
>> ```
>> interface Animal{}
>> class Dog: Animal{}
>> class Cat: Animal{}
>>
>>
>> void main()
>> {
>> 	Animal[] x;
>> 	x ~= new Cat();
>> 	x ~= new Dog();
>> 	x ~= new Cat();
>> 	writeln(typeid(x[0])); // Gives Animal
>> }
>> ```
>>
>> But if Animal is set to a class the typeid gives Cat, why does 
>> this happen? Does this mean that inheriting from an interface 
>> is not really polymorphism?
>
> Is there a reason you're not using
> writeln(typeid(typeof(x[0])));
> I pretty much always write it that way.

typeid() will give you the run-time type while typeof() gives the 
declared (compile time) type, typeid(typeof()) will not give you 
the run-time type - which in our case is what we want if we are 
using sub-typing polymorphism.


More information about the Digitalmars-d-announce mailing list