January 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

}
January 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.
January 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:
>> 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...
January 13, 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
January 13, 2013
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
January 15, 2013
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.
January 16, 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:
>> 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.
1 2
Next ›   Last »