Static function conflicts with Non-Static?!

Dmitry Olshansky dmitry.olsh at gmail.com
Sat Jun 2 01:17:04 PDT 2012


On 02.06.2012 12:06, Namespace wrote:
> Is that a joke? :D
>
> This Code throw the error, that a call of "Load" matches both functions.
> How is that possible? Even in php that works fine. Any workarounds?
> I can not believe that such a simple error still exists in D.
>
> [code]
> import std.stdio;
>
> class Foo {
> public:
> static Foo Load() {
> Foo f = new Foo();
> f.Load();

In D like in Java and C++ and *ahem* unlike PHP it seems static 
functions can be called with instance variable.

Now compile may probably pick instance function here, but imagine you 
decide to refactor you instance Load function. So you rename it Create 
or OnLoad.. whatever. Now .Load suddenly calls static "overload".
So it may be another anti-hijacking thing in D.

>
> return f;
> }
>
> void Load() {
>
> }
> }
>
> void main() {
> Foo f = Foo.Load();
> }
> [/code]


-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list