www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static function conflicts with Non-Static?!

reply "Namespace" <rswhite4 googlemail.com> writes:
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();

		return f;
	}

	void Load() {

	}
}

void main() {
	Foo f = Foo.Load();
}
[/code]
Jun 02 2012
next sibling parent reply "Zhenya" <zheny list.ru> writes:
On Saturday, 2 June 2012 at 08:06:57 UTC, 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();

 		return f;
 	}

 	void Load() {

 	}
 }

 void main() {
 	Foo f = Foo.Load();
 }
 [/code]
xDDD
Jun 02 2012
parent reply "Zhenya" <zheny list.ru> writes:
I'm not sure, but it seems that this is a bug.
Jun 02 2012
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, June 02, 2012 10:14:51 Zhenya wrote:
 I'm not sure, but it seems that this is a bug.
It's not. If nothing else, it's perfectly legal to call a static function with an instance. e.g. class C { static void func() {} } auto c = new C; c.func(); So, that creates an ambiguity if a static and non-static function could have the same name. Now, it could just assume that the instance was meant in this case, but it doesn't work that way. It's just illegal to overload a static function with a non-static function. Personally, I wish that it weren't legal to call a static function with an object and that you had to explicitly use the class, but that's not the way sure). - Jonathan M Davis
Jun 02 2012
next sibling parent "Zhenya" <zheny list.ru> writes:
Understand)
Jun 02 2012
prev sibling next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
On Saturday, 2 June 2012 at 08:24:16 UTC, Jonathan M Davis wrote:
 On Saturday, June 02, 2012 10:14:51 Zhenya wrote:
 I'm not sure, but it seems that this is a bug.
It's not. If nothing else, it's perfectly legal to call a static function with an instance. e.g. class C { static void func() {} } auto c = new C; c.func(); So, that creates an ambiguity if a static and non-static function could have the same name. Now, it could just assume that the instance was meant in this case, but it doesn't work that way. It's just illegal to overload a static function with a non-static function. Personally, I wish that it weren't legal to call a static function with an object and that you had to explicitly use the class, but that's not the way though I'm not sure). - Jonathan M Davis
Strange thing. But I understand, thanks. :)
Jun 02 2012
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 Personally, I wish that it weren't legal to call a static 
 function with an
 object and that you had to explicitly use the class,
I agree. Bye, bearophile
Jun 02 2012
parent reply Kevin Cox <kevincox.ca gmail.com> writes:
On Jun 2, 2012 6:38 AM, "bearophile" <bearophileHUGS lycos.com> wrote:
 Jonathan M Davis:


 Personally, I wish that it weren't legal to call a static function with
an
 object and that you had to explicitly use the class,
I agree. Bye, bearophile
Same here, D 3.0?
Jun 02 2012
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 02.06.2012 14:39, Kevin Cox wrote:
 On Jun 2, 2012 6:38 AM, "bearophile" <bearophileHUGS lycos.com
 <mailto:bearophileHUGS lycos.com>> wrote:
  >
  > Jonathan M Davis:
  >
  >
  >> Personally, I wish that it weren't legal to call a static function
 with an
  >> object and that you had to explicitly use the class,
  >
  >
  > I agree.
  >
  > Bye,
  > bearophile

 Same here, D 3.0?
Just rename the function. In contrast it is not easy to code up a workaround that will "re-enable" static as instance method back if we to ban it. Generic programming benefits from it in certain scenarios, while I personally wouldn't mind either way. -- Dmitry Olshansky
Jun 02 2012
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, June 02, 2012 14:49:39 Dmitry Olshansky wrote:
 Generic programming benefits from it in certain scenarios
This is brought up periodically, and I don't really buy it. _Maybe_ there's a scenario where it would help, but typeof makes it trivial to get the type so that you can call a static function with the type, and I'd consider it questionable to insist on it with the idea that some classes would have a static version of the function and some a non-static version. That just seems like bad design. - Jonathan M Davis
Jun 02 2012
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 02 Jun 2012 06:49:39 -0400, Dmitry Olshansky  
<dmitry.olsh gmail.com> wrote:

 On 02.06.2012 14:39, Kevin Cox wrote:
 On Jun 2, 2012 6:38 AM, "bearophile" <bearophileHUGS lycos.com
 <mailto:bearophileHUGS lycos.com>> wrote:
  >
  > Jonathan M Davis:
  >
  >
  >> Personally, I wish that it weren't legal to call a static function
 with an
  >> object and that you had to explicitly use the class,
  >
  >
  > I agree.
  >
  > Bye,
  > bearophile

 Same here, D 3.0?
Just rename the function. In contrast it is not easy to code up a workaround that will "re-enable" static as instance method back if we to ban it. Generic programming benefits from it in certain scenarios, while I personally wouldn't mind either way.
http://d.puremagic.com/issues/show_bug.cgi?id=6579 See revised proposal. -Steve
Jun 25 2012
prev sibling parent "Regan Heath" <regan netmail.co.nz> writes:
On Sat, 02 Jun 2012 09:23:50 +0100, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

see "Static Members": "A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member." http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx It does cause some people some consternation. Some more background: http://underground.infovark.com/2008/03/28/calling-a-static-method-from-an-object-instance-in-c/ IMO it's a good decision not to allow it. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Jun 06 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Jun 02, 2012 at 01:23:50AM -0700, Jonathan M Davis wrote:
[...]
 Personally, I wish that it weren't legal to call a static function
 with an object and that you had to explicitly use the class, but
 that's not the way that it is in D, C++, and Java (and probably the

[...] I've always been of the opinion that static methods should _only_ be callable via the class name. If an instance is not needed, then it shouldn't be used in the first place. Allowing the use of a class instance where it's not needed is needlessly confusing and prevents legal overloading of static methods vs. non-static methods. T -- If Java had true garbage collection, most programs would delete themselves upon execution. -- Robert Sewell
Jun 02 2012
prev sibling next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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
Jun 02 2012
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-06-02 10: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();

 return f;
 }

 void Load() {

 }
 }

 void main() {
 Foo f = Foo.Load();
 }
 [/code]
http://d.puremagic.com/issues/show_bug.cgi?id=3345 -- /Jacob Carlborg
Jun 02 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, June 02, 2012 18:40:38 Jacob Carlborg wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=3345
Ah, there it is! I was _sure_ that a report for it existed, but I couldn't find it (probably because I was searching for overload and didn't think to search for name). - Jonathan M Davis
Jun 02 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-06-02 19:59, Jonathan M Davis wrote:
 On Saturday, June 02, 2012 18:40:38 Jacob Carlborg wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=3345
Ah, there it is! I was _sure_ that a report for it existed, but I couldn't find it (probably because I was searching for overload and didn't think to search for name). - Jonathan M Davis
In this case I search for "classinfo". -- /Jacob Carlborg
Jun 02 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, June 02, 2012 08:41:17 H. S. Teoh wrote:
 On Sat, Jun 02, 2012 at 01:23:50AM -0700, Jonathan M Davis wrote:
 [...]
 
 Personally, I wish that it weren't legal to call a static function
 with an object and that you had to explicitly use the class, but
 that's not the way that it is in D, C++, and Java (and probably the

[...] I've always been of the opinion that static methods should _only_ be callable via the class name. If an instance is not needed, then it shouldn't be used in the first place. Allowing the use of a class instance where it's not needed is needlessly confusing and prevents legal overloading of static methods vs. non-static methods.
The trick is getting Walter to agree. I don't know how entrenched his thinking on the matter is, but changing it _would_ be a breaking change, which makes it harder to convince him to make the change, even if he nominally agrees. And, of course, it's easier if someone actually goes and makes the required changes to the compiler and submits a pull request, but someone would obviously have to take the time to do that. - Jonathan M Davis
Jun 02 2012
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 6/2/12, Jonathan M Davis <jmdavisProg gmx.com> wrote:
 The trick is getting Walter to agree.
The trick is getting all the people that bought TDPL to burn their books, because by the time all these new changes are set in place the book will have as much dead weight to it as dsource.org. It specifically says on page 197: "If you use an object instead of the class name when accessing a static member, that's fine, too."
Jun 02 2012
parent "Regan Heath" <regan netmail.co.nz> writes:
On Sat, 02 Jun 2012 19:25:05 +0100, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 On 6/2/12, Jonathan M Davis <jmdavisProg gmx.com> wrote:
 The trick is getting Walter to agree.
The trick is getting all the people that bought TDPL to burn their books, because by the time all these new changes are set in place the book will have as much dead weight to it as dsource.org. It specifically says on page 197: "If you use an object instead of the class name when accessing a static member, that's fine, too."
They could print/make available errata pages :p R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Jun 06 2012