Static method conflicts with non-static method?
Paulo Pinto
pjmlp at progtools.org
Thu Apr 26 23:29:01 PDT 2012
On Friday, 27 April 2012 at 06:14:13 UTC, H. S. Teoh wrote:
> Is this a bug? Code:
>
> import std.stdio;
>
> struct S {
> static int func(int x) { return x+1; }
> int func(int x) { return x+2; }
> }
>
> void main() {
> S s;
> writeln(s.func(1));
> }
>
> DMD (latest git) output:
>
> test.d(10): Error: function test.S.func called with argument
> types:
> ((int))
> matches both:
> test.S.func(int x)
> and:
> test.S.func(int x)
>
> The error message is unhelpful, but basically the complaint is
> that the
> static method is conflicting with the non-static method.
>
> But I would've thought it is unambiguous; I'd expect that
> s.func should
> resolve to the non-static method, and S.func to the static
> method. After
> all, no object is needed to invoke the static method, and the
> static
> method cannot be invoked without an object.
>
>
> T
I always thought that D follows the same rules as Java, C++ and
C# have. Meaning you can use an instance object to call a static
method, even though it is not needed.
As such the call becomes ambiguous, because the compiler won't
know what it supposed to call.
I tried to look in the language reference, but did not find a
clear explanation for this, but I would expect such scenarios to
be forbidden. This type of code is a pain to maintain if it is
allowed.
--
Paulo
More information about the Digitalmars-d
mailing list