digitalmars.D - Re: Static operator overloads are possible why?
- Giles Bathgate <gilesbathgate gmail.com> Jul 05 2007
- Manfred Nowak <svv1999 hotmail.com> Jul 05 2007
- GilesBathgate <gilesbathgate gmail.com> Jul 05 2007
- GilesBathgate <gilesbathgate gmail.com> Jul 05 2007
- Derek Parnell <derek psych.ward> Jul 05 2007
- Giles Bathgate <gilesbathgate gmail.com> Jul 06 2007
- Derek Parnell <derek psych.ward> Jul 06 2007
- David L. Davis <SpottedTiger yahoo.com> Jul 06 2007
- Manfred Nowak <svv1999 hotmail.com> Jul 08 2007
You can access static class members through variables which are of that class's type. This applies to all static methods and fields; static operator overloads are no exception. I'm not entirely sure why you can do this; it's probably "the way C++ does it."
I don't know what you mean by this I tried public class Test { public char[] Name; public Test opAddAssign(Test value) { writefln(value.Name); writefln(Test.Name); //... } } It didnt workThat being said, I'm not sure why you decided to try a static overload instead of just new'ing t. Another solution would be to make your multicast delegate a struct type instead, which wouldn't have to be new'ed; I think DFL (very .Net like form library for D) does this.
DFL huh? there is another thing that went under the radar... Its a shame there is no "distribution" of D Anyway I am not looking for a solution to the problem of how to create a multicast delegate class in D I am trying to solve the meta problem, of how to access an lvalue in a static operator overload.
Jul 05 2007
Giles Bathgate wrotehow to access an lvalue in a static operator overload.
parameters or declared `static' too. Therefore change | public char[] Name; to public static char[] Name; Or as a complete example: public class Test { static char[] baseName = "Test"; char[] name; static void opAddAssign(Test value) { writefln(value.name); Test.baseName~= ".".dup; baseName~= value.name.dup; } static void opCall(){ writefln( ":", baseName); } } void main() { Test b = new Test(); b.name = "foo"; Test += b; Test(); Test t; t(); } import std.stdio; Or in other words: for the entities of a class that are declared `static' the D-compiler automatically establishes a singleton and that singleton can be accessed 1) by the name of the class or 2) every instance of the class So: your questions'should static operator overloads even be allowed?' and if they were to be allowed should they not have two arguments?
-manfred
Jul 05 2007
operators declared `static' can only access entities that are actual parameters or declared `static' too.
Exactly and since the lvalue is not a parameter then I cannot access it! The language dosen't support what I want to do, and I am preposing it should because its very useful if not essential to be able to access the lvalue in an operator overload.
Jul 05 2007
Or in other words: for the entities of a class that are declared `static' the D-compiler automatically establishes a singleton and that singleton can be accessed 1) by the name of the class or 2) every instance of the class
Yeah I know, I am a programmer. If you can translate this C# code into D I will give you a medal public class Test { public string Name; public static Test operator +(Test lvalue, Test rvalue) { if (lvalue == null) { lvalue = new Test(); lvalue.Name = "foo"; } if (rvalue == null) { rvalue = new Test(); rvalue.Name = "bar"; } Console.Write(lvalue.Name); Console.Write(rvalue.Name); return rvalue; } } static void Main(string[] args) { Test T = null; Test B = null; T += B; }
Jul 05 2007
On Thu, 05 Jul 2007 15:24:34 -0400, GilesBathgate wrote:If you can translate this C# code into D I will give you a medal
Where do I apply for the medal :-) import std.stdio; class Test { string Name; static Test opAddAssign(ref Test lvalue, ref Test rvalue) { if (lvalue is null) { lvalue = new Test(); lvalue.Name = "foo"; } if (rvalue is null) { rvalue = new Test(); rvalue.Name = "bar"; } writefln("%s", lvalue.Name); writefln("%s", rvalue.Name); return rvalue; } } void main() { Test T; Test B; Test.opAddAssign(T,B); } -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Jul 05 2007
void main() { Test T; Test B; Test.opAddAssign(T,B); }
Cheaters do not win medals. Its hardly an operator overload if you have to write Test.opAddAssign(T,B); instead of T += B; sorry. Do you see my point yet?
Jul 06 2007
On Fri, 06 Jul 2007 05:46:01 -0400, Giles Bathgate wrote:void main() { Test T; Test B; Test.opAddAssign(T,B); }
Cheaters do not win medals. Its hardly an operator overload if you have to write Test.opAddAssign(T,B); instead of T += B; sorry. Do you see my point yet?
Hang on a minute, all you asked for was someone to "translate" the C# code into D. And that is what I did. You did not specify that the resultant translation must use the "+=" operator. Do you see my point? <G> -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Jul 06 2007
Give the man his medal! :) (It's not creating, if you follow the rules) Derek Parnell Wrote:On Fri, 06 Jul 2007 05:46:01 -0400, Giles Bathgate wrote:void main() { Test T; Test B; Test.opAddAssign(T,B); }
Cheaters do not win medals. Its hardly an operator overload if you have to write Test.opAddAssign(T,B); instead of T += B; sorry. Do you see my point yet?
Hang on a minute, all you asked for was someone to "translate" the C# code into D. And that is what I did. You did not specify that the resultant translation must use the "+=" operator. Do you see my point? <G> -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Jul 06 2007
GilesBathgate wroteIf you can translate this C# code into D I will give you a medal
As some replies might have shown, it is unclear what you might mean with "translate". <elaboration> In a game with main language german a german tourist in China asks a passerby "Sprechen Sie deutsch?", which means "Do you speak german?" The correct _translation_ to english would of course be "Do you speak german?" But why would a native english speaker ask some one in China whether he speaks german? So: the correct _transduction_ would be "Do you speak english?" Because you are a programmer this elaboration should be enough to put you on the tip of the iceberg. </elaboration> -manfred
Jul 08 2007