Static method conflicts with non-static method?

Steven Schveighoffer schveiguy at yahoo.com
Fri Apr 27 03:48:29 PDT 2012


On Fri, 27 Apr 2012 01:07:03 -0400, H. S. Teoh <hsteoh at quickfur.ath.cx>  
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.

It's not a bug, it's intended behavior.  Static members are accessible via  
an instance pointer.  My personal belief is that it shouldn't be this way,  
especially since you can make factory static methods that look like they  
are doing something on an instance.  I brought this up a while ago, but  
since when did anyone listen to me? :)

Here is a relevant discussion:

http://forum.dlang.org/post/j3fi1u$1uge$1@digitalmars.com

One of the counter-arguments from Andrei was that static methods can be  
used as "methods" on types for generic programming.  With the advent of  
UFCS, this argument has much less teeth.  Maybe it should be revisited...

-Steve


More information about the Digitalmars-d mailing list