digitalmars.D.learn - static vs non-static
- Zhenya (21/21) Jan 13 2013 Hi!
- Maxim Fomin (4/25) Jan 13 2013 Main should be void in this case.
- Zhenya (3/32) Jan 13 2013 Hmmm...So it will remain as it is?
- Maxim Fomin (9/14) Jan 13 2013 Frankly speaking I do not know - I keep an eye on D project for
- Zhenya (2/17) Jan 13 2013 Thank you.
- bearophile (5/6) Jan 13 2013 I think that's a D design mistake (and I think Jonathan Davis
- Zhenya (4/10) Jan 13 2013 Maybe you could suggest some workaround?
- Andrey (4/4) Jan 13 2013 Definitely this is a bad design. Even in PHP you can't call
- Maxim Fomin (4/10) Jan 13 2013 Then I agree with both of you, however I found this issue not
- Zhenya (4/17) Jan 13 2013 I just was writting something like n-dimensional
- Jonathan M Davis (7/12) Jan 13 2013 C++ and Java (and probably C# too) have this problem as well, so it's no...
- Era Scarecrow (34/38) Jan 15 2013 I'll have to disagree and it should remain an error.
- Maxim Fomin (6/18) Jan 15 2013 The problem is that code depends on presence of static and
- Jacob Carlborg (4/25) Jan 13 2013 There's a bugzilla issue for this. Search for "classinfo".
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
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
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:Hmmm...So it will remain as it is? It hurts me a little bit(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
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: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).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
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:Thank you.On Sunday, 13 January 2013 at 16:18:36 UTC, Maxim Fomin wrote: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).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
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
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
On Sunday, 13 January 2013 at 18:16:40 UTC, Zhenya wrote:On Sunday, 13 January 2013 at 17:59:28 UTC, Andrey wrote:Oops...I made mistake:struct and function with the same name isn't allowed. Okay, I will try to get around it somehow...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
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
On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:Maxim Fomin:Maybe you could suggest some workaround? for example I tried to use global function,but in that case it never will be executed...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
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
On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:Maxim Fomin: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.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
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: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.Maxim Fomin: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.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
On Sunday, January 13, 2013 17:39:21 bearophile wrote:Maxim Fomin: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 Davisdmd 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.
Jan 13 2013
On Sunday, 13 January 2013 at 16:39:22 UTC, bearophile wrote:Maxim Fomin: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.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.
Jan 15 2013
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: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.Maxim Fomin: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();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.
Jan 15 2013
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