www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static vs non-static

reply "Zhenya" <zheny list.ru> writes:
Hi!
Sorry,if it already was discussed,but

import std.stdio;

struct Foo
{
	static void bar()
	{
		writeln("static");
	}
	void bar()
	{
		writeln("non-static");
	}
}

int main()
{
     Foo gun;
     gun.bar();//fails here
}

Is it all right,that compiler dosn't prefer non-static function 
in ths case?
Jan 13 2013
next sibling parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Sunday, 13 January 2013 at 15:58:56 UTC, Zhenya wrote:
 Hi!
 Sorry,if it already was discussed,but

 import std.stdio;

 struct Foo
 {
 	static void bar()
 	{
 		writeln("static");
 	}
 	void bar()
 	{
 		writeln("non-static");
 	}
 }

 int main()
 {
     Foo gun;
     gun.bar();//fails here
 }

 Is it all right,that compiler dosn't prefer non-static function 
 in ths case?
Main should be void in this case. Yes, it is a problem - dmd allows to call static functions on instance. When both match, it issues ambiguity error.
Jan 13 2013
next sibling parent reply "Zhenya" <zheny list.ru> writes:
On Sunday, 13 January 2013 at 16:18:36 UTC, Maxim Fomin wrote:
 On Sunday, 13 January 2013 at 15:58:56 UTC, Zhenya wrote:
 Hi!
 Sorry,if it already was discussed,but

 import std.stdio;

 struct Foo
 {
 	static void bar()
 	{
 		writeln("static");
 	}
 	void bar()
 	{
 		writeln("non-static");
 	}
 }

 int main()
 {
    Foo gun;
    gun.bar();//fails here
 }

 Is it all right,that compiler dosn't prefer non-static 
 function in ths case?
Main should be void in this case. Yes, it is a problem - dmd allows to call static functions on instance. When both match, it issues ambiguity error.
Hmmm...So it will remain as it is? It hurts me a little bit(
Jan 13 2013
parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Sunday, 13 January 2013 at 16:23:27 UTC, Zhenya wrote:
 On Sunday, 13 January 2013 at 16:18:36 UTC, Maxim Fomin wrote:
 Yes, it is a problem - dmd allows to call static functions on 
 instance. When both match, it issues ambiguity error.
Hmmm...So it will remain as it is? It hurts me a little bit(
Frankly speaking I do not know - I keep an eye on D project for some period of time and remember how some features I considered stable were easily changed. If Walter is for current behavior - than it will remain as it is (unless Walter is overpersuaded). What I would do is not writing code which is looking for problems. BTW, take look at this thread http://forum.dlang.org/thread/pkodcsxwwehumhtkrlty forum.dlang.org (posts 1-3).
Jan 13 2013
parent reply "Zhenya" <zheny list.ru> writes:
On Sunday, 13 January 2013 at 17:17:54 UTC, Maxim Fomin wrote:
 On Sunday, 13 January 2013 at 16:23:27 UTC, Zhenya wrote:
 On Sunday, 13 January 2013 at 16:18:36 UTC, Maxim Fomin wrote:
 Yes, it is a problem - dmd allows to call static functions on 
 instance. When both match, it issues ambiguity error.
Hmmm...So it will remain as it is? It hurts me a little bit(
Frankly speaking I do not know - I keep an eye on D project for some period of time and remember how some features I considered stable were easily changed. If Walter is for current behavior - than it will remain as it is (unless Walter is overpersuaded). What I would do is not writing code which is looking for problems. BTW, take look at this thread http://forum.dlang.org/thread/pkodcsxwwehumhtkrlty forum.dlang.org (posts 1-3).
Thank you.
Jan 13 2013
parent reply "Andrey" <andr-sar yandex.ru> writes:
Don't know if this will be useful in any manner, but it came this 
silly way:

class MyClass {

	struct _static {

  		static void myfun() {
  			writeln("static myfun");
  		}
	}

  	void myfun() {
  		writeln("myfun");
  	}

}

void main() {

auto obj = new MyClass();
obj.myfun(); //myfun
obj._static.myfun(); //static
MyClass._static.myfun(); //static

}
Jan 13 2013
parent reply "Zhenya" <zheny list.ru> writes:
On Sunday, 13 January 2013 at 17:59:28 UTC, Andrey wrote:
 Don't know if this will be useful in any manner, but it came 
 this silly way:

 class MyClass {

 	struct _static {

  		static void myfun() {
  			writeln("static myfun");
  		}
 	}

  	void myfun() {
  		writeln("myfun");
  	}

 }

 void main() {

 auto obj = new MyClass();
 obj.myfun(); //myfun
 obj._static.myfun(); //static
 MyClass._static.myfun(); //static

 }
Aha...Thank you,very interesting idea to hide function in struct: struct Foo { static struct bar { static void opCall() { writeln("static"); } void bar() { writeln("non-static"); } } } This is the workaround I looked for.
Jan 13 2013
parent "Zhenya" <zheny list.ru> writes:
On Sunday, 13 January 2013 at 18:16:40 UTC, Zhenya wrote:
 On Sunday, 13 January 2013 at 17:59:28 UTC, Andrey wrote:
 Don't know if this will be useful in any manner, but it came 
 this silly way:

 class MyClass {

 	struct _static {

 		static void myfun() {
 			writeln("static myfun");
 		}
 	}

 	void myfun() {
 		writeln("myfun");
 	}

 }

 void main() {

 auto obj = new MyClass();
 obj.myfun(); //myfun
 obj._static.myfun(); //static
 MyClass._static.myfun(); //static

 }
Aha...Thank you,very interesting idea to hide function in struct: struct Foo { static struct bar { static void opCall() { writeln("static"); } void bar() { writeln("non-static"); } } } This is the workaround I looked for.
Oops...I made mistake:struct and function with the same name isn't allowed. Okay, I will try to get around it somehow...
Jan 13 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Maxim Fomin:

 dmd allows to call static functions on instance.
I think that's a D design mistake (and I think Jonathan Davis agrees with me), but Walter prefers the current behavour. Bye, bearophile
Jan 13 2013
next sibling parent reply "Zhenya" <zheny list.ru> writes:
On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:
 Maxim Fomin:

 dmd allows to call static functions on instance.
I think that's a D design mistake (and I think Jonathan Davis agrees with me), but Walter prefers the current behavour. Bye, bearophile
Maybe you could suggest some workaround? for example I tried to use global function,but in that case it never will be executed...
Jan 13 2013
parent "Andrey" <andr-sar yandex.ru> writes:
Definitely this is a bad design. Even in PHP you can't call 
static functions from class instance. :-)

Having two functions with the same name and argument list within 
one class is a bad idea too.
Jan 13 2013
prev sibling next sibling parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:
 Maxim Fomin:

 dmd allows to call static functions on instance.
I think that's a D design mistake (and I think Jonathan Davis agrees with me), but Walter prefers the current behavour. Bye, bearophile
Then I agree with both of you, however I found this issue not very dramatical (comparing with other problems), probably because I haven't hit some problems with it.
Jan 13 2013
parent "Zhenya" <zheny list.ru> writes:
On Sunday, 13 January 2013 at 17:04:21 UTC, Maxim Fomin wrote:
 On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:
 Maxim Fomin:

 dmd allows to call static functions on instance.
I think that's a D design mistake (and I think Jonathan Davis agrees with me), but Walter prefers the current behavour. Bye, bearophile
Then I agree with both of you, however I found this issue not very dramatical (comparing with other problems), probably because I haven't hit some problems with it.
I just was writting something like n-dimensional dispatcher,described in Andrei's Modern C++ design,and I needed to write classinfo analog by hands.
Jan 13 2013
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, January 13, 2013 17:39:21 bearophile wrote:
 Maxim Fomin:
 dmd allows to call static functions on instance.
I think that's a D design mistake (and I think Jonathan Davis agrees with me), but Walter prefers the current behavour.
C++ and Java (and probably C# too) have this problem as well, so it's not really any surprise that D does it, and I wouldn't expect Walter to change his mind on it (especially since it risks breaking code for something that he would probably consider fairly minor). I do think that it's a design mistake (in all of these languages), but I wouldn't expect it to change. - Jonathan M Davis
Jan 13 2013
prev sibling parent reply "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:
 Maxim Fomin:

 dmd allows to call static functions on instance.
I think that's a D design mistake (and I think Jonathan Davis agrees with me), but Walter prefers the current behavour.
I'll have to disagree and it should remain an error. Consider his example, if it were allowed and you do: Gun gun; Gun.bar(); gun.bar(); What would happen? The first would call the static and the second would call the non-static. Overloading-wise their signatures are identical (static doesn't count). The difference in the calls is a single letter and can easily go under the radar; Without an actual difference in the signature it would be easy to confuse the two. Now had one required an input then they couldn't be messed up. Let's go with a slightly more confounding example. struct S { int x; int opIndex(int i){return i;} //non-instance is always 0 static int length() {return 0;} int length() const {return x;} } Assuming a function should call this (Say a foreach? once there's more methods?), which is the correct length to call? If the static is preferred then length is always 0, if non-static is preferred (although slimmer) the length of 0 being returned by accident is always there. If the static length is private, then inside the struct S you always have to reference 'this.length' vs 'S.length' to keep them separate just to be sure. I recall reading somewhere that in Java that technically calling a static function by a instance was an error but instead only gave you a warning; Kinda makes sense since methods affect it's instance but static functions don't have an instance to work with; They were more for functions that needed to be free but couldn't due to the strict 'everything is an object' setup.
Jan 15 2013
parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Tuesday, 15 January 2013 at 22:03:24 UTC, Era Scarecrow wrote:
 On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:
 Maxim Fomin:

 dmd allows to call static functions on instance.
I think that's a D design mistake (and I think Jonathan Davis agrees with me), but Walter prefers the current behavour.
I'll have to disagree and it should remain an error. Consider his example, if it were allowed and you do: Gun gun; Gun.bar(); gun.bar();
The problem is that code depends on presence of static and no-static functions. If no one is preferred, compiler would issue error (like dmd behaves today), if one of them is preferred (for e.x. non-static) the semantic would be silently changed if you write gun.bar() and then add non-static function to class.
Jan 15 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-01-13 16:58, Zhenya wrote:
 Hi!
 Sorry,if it already was discussed,but

 import std.stdio;

 struct Foo
 {
 static void bar()
 {
 writeln("static");
 }
 void bar()
 {
 writeln("non-static");
 }
 }

 int main()
 {
 Foo gun;
 gun.bar();//fails here
 }

 Is it all right,that compiler dosn't prefer non-static function in ths
 case?
There's a bugzilla issue for this. Search for "classinfo". -- /Jacob Carlborg
Jan 13 2013