declaration-site extension methods possibly dangerous

Rémi Forax forax at univ-mlv.fr
Fri Dec 11 13:35:41 PST 2009


Le 11/12/2009 21:26, Fatih Coskun a écrit :
> Rémi Forax wrote:
>> Fatih Coskun a écrit :
>>> declaration-site extension methods can be easily confused with 
>>> abstract methods of an interface. I can imagine many developers 
>>> trying to override extension methods in their subclasses in order to 
>>> provide different implementations. But this will not work. 
>>> declaration-site extension methods cannot be overriden, they are 
>>> hidden instead.
>>
>> Why ?, The proposal doesn't say anything about that.
> From the proposal:
> "An extension method in an interface is hidden in a class implementing 
> that interface if the class defines a static or instance method with 
> the same signature."
>
> The proposal states that extension methods can be hidden, i.e. they 
> cannot be overriden.

Ok, It seems I have un-voluntarily skip that sentence when reading the 
proposal.
Anyway, I don't think that it's a good idea to be able to hide an 
extension method.

> It couldn't be otherwise, because extension methods are in fact just 
> static methods. Static methods are always statically bound (via the 
> static type of a variable), this means it is never possible to 
> override them in a dynamic manner like instance methods can be.

Yes, but the compiler can raise an error, as it do when you try to 
override a static method
with a non-static one.


>> I don't understand why use-site extension don't suffer from the same 
>> disease.
> use-site extension methods suffer from the same consequence in that 
> they cannot be overriden in a class implementing the interface. But 
> they do not suffer in the same way in that one can be easily confused 
> about this. Because use-site extension methods are not defined in the 
> interface, one will not be tempted to override them. The developer 
> will not be tempted to do something that can't be done.


Why, it's an extension method. By definition, you call it on an instance,
so you will want to override it. It's a problem of all extension methods.

>
> Fatih

Rémi


More information about the lambda-dev mailing list