Version française
Home     About     Download     Resources     Contact us    
Browse thread
Smells like duck-typing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <bhurt@j...>
Subject: Re: [Caml-list] Smells like duck-typing
Arnaud Spiwack wrote:

> Brian Hurt a écrit :
>
>> Dario Teixeira wrote:
>>
>>> Hi,
>>>
>>>  
>>>
>>>> That seems backwards from the way OO inheritance is supposed to 
>>>> work.  You don't go from a more feature-rich case to a less 
>>>> feature-rich case -- it's the other way around.
>>>>     
>>>
>>>
>>> Of course it is -- that is precisely why inheritance is the wrong
>>> formalism for my problem!  What I need is a "reverse inheritance"
>>> formalism, where a fully defined data structure sits at the root,
>>> and whose descendants are PRUNED versions of the parent.
>>>   
>>
>> The problem with this is that it violates one of the assumptions of 
>> typing, that if type A is (also) a type B, than anywhere you can use 
>> a type B, you can also use a type A.  This isn't an assumption 
>> limited to object oriented languages.  And this isn't true in your 
>> example- if type A is lacking members type B has, then it's possible 
>> to write situations where a "real" type B can be used, but not a type 
>> A- just use a field of type B that type A doesn't have.
>>
>> I think I'd recommend rethinking your approach to the problem.
>
> Well, what he is suggesting is to be able to derive a supertype A 
> given a type B. This is not fundamentaly incorrect. However I fail to 
> find any reasoning where it is made use of, in usual mathematics. So I 
> am, personnally, a bit puzzled by the suggestion, unable to say if it 
> might make sense or not (method exclusion is natural in the setting of 
> incomplete objets or traits, but it doesn't fit the situation very 
> much, since it usually produces something that requires to be completed).

Saying that A is a supertype of B is the equivelent of saying B is a 
subtype of A.  Same relation, different direction.  In OO lingo, how 
they say "B is a subtype of A" is that "B inherits from (is a subclass 
of) A".

In either case, while all B's are A's, not all A's are B's.  What he 
wants is a case where not all A's are B's, but because of the magic 
mindreading feature of the language (in conjunction with the DWIM 
feature of the hardware), you can treat all A's as B's, with the system 
magically filling in the missing values and methods.  This is what the 
mind reading is needed for- to know how to fill in the missing values.

I will note that Ocaml's row-level polymorphism allows you to invent new 
supertypes of a given subtype as needed (a real nice feature, IMHO).  
But what he's asking for is fundamentally nonsensical.

Brian