declaration-site extension methods possibly dangerous
Fatih Coskun
fatih at coskuns-castle.de
Fri Dec 11 12:26:40 PST 2009
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. 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.
> 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.
Fatih
More information about the lambda-dev
mailing list