digitalmars.D - opStarAssign?
- Janice Caron (10/10) Nov 22 2007 So, are we going to get opStarAssign()?
- Gregor Richards (5/7) Nov 22 2007 Because the names for operator overloads should be based on what
- Janice Caron (6/12) Nov 22 2007 Yeah. Um. I was employing irony.
- Leonard Dahlmann (3/19) Nov 23 2007 What about *p *=, *p /=, *p ~=, ...?
- Jarrett Billingsley (16/35) Nov 23 2007 Which is why we need reference returns. Then you could do:
- Robert Fraser (2/44) Nov 23 2007 I believe there was some mention of reference types at the conference.
- Janice Caron (14/16) Nov 23 2007 I don't see why. We've done just fine without a[n]*=, a[n]/=, a[n]~=
- 0ffh (3/8) Nov 23 2007 Wouldn't "p.opAssign(p.opDeref()+n);" be quite sufficient?
- Janice Caron (8/15) Nov 23 2007 I think that would end up being equivalent to
- 0ffh (7/22) Nov 23 2007 Is that so?
- Frits van Bommel (6/18) Nov 24 2007 Even if they're rare, they *do* exist (for example, a variant type that
- 0ffh (16/23) Nov 24 2007 You mean like:
- 0ffh (11/11) Nov 24 2007 I wonder: There are quite a few requests floating
- 0ffh (9/10) Nov 24 2007 Okay, maybe it's a bit too insane! =)
- Frits van Bommel (5/16) Nov 24 2007 ...
- 0ffh (13/23) Nov 25 2007 Hummm... not quite. The good thing about Lisps, IMHO, is that they faile...
- =?ISO-8859-1?Q?Ma=ebl?= (12/12) Nov 25 2007 Take a look at OCaml / CamlP4, this is exactly what you are looking for,...
- 0ffh (10/14) Nov 25 2007 Thanks for the tip. I took a look at O'Caml earlier, but to be frank,
- =?ISO-8859-1?Q?Ma=ebl?= (1/1) Nov 25 2007 I'm pretty sure that imperative language can (and will!) completely bene...
- 0ffh (5/13) Nov 25 2007 That's entirely possible... maybe I am missing out on that.
- oldrev (13/31) Nov 25 2007 Anonymous AST macro is the ultimate way to solve it:
- Dejan Lekic (2/2) Nov 25 2007 Why would anyone need this in a general-purpose, OO language, is beyond
- 0ffh (11/13) Nov 25 2007 A few examples of what would be possible to add to any program
- =?ISO-8859-1?Q?Ma=ebl?= (1/1) Nov 25 2007 Plus, if it is implemented as a pre-processor (CamlP4-like) you could ma...
- 0ffh (6/9) Nov 25 2007 You wouldn't even need CTFE for it, if you'd just add a compiler switch ...
- Janice Caron (2/4) Nov 24 2007 Both of those lines look like *x= to me.
- Frits van Bommel (4/15) Nov 24 2007 I think you missed the intention of the ':' at the end. He wasn't
- Bill Baxter (11/33) Nov 23 2007 But the nice thing about the current approach is that it makes it easy
- Dejan Lekic (2/2) Nov 25 2007 Mrs. Caron, I think You are mixing opStar with opMul* those two have
So, are we going to get opStarAssign()? I can do n = *p; but not *p = n; Also, are these overloads going to be renamed (e.g. opDeref(), opDerefAssign()) as people here have suggested. I can think of many names I might give to a dereferencing function - among them val(), deref(), getValue(), getElement(), and so on, but for some reason "opStar" would not be high on my list of choices.
Nov 22 2007
Janice Caron wrote:but for some reason "opStar" would not be high on my list of choices.Because the names for operator overloads should be based on what operator they're overloading, not the syntax for it. e.g. it's opMul, not opStar. opDiv, not opSlash. - Gregor Richards
Nov 22 2007
On 11/22/07, Gregor Richards <Richards codu.org> wrote:Janice Caron wrote:Yeah. Um. I was employing irony. Anyway, I'd hope Walter is considering changing the name, given the unanimous support here for doing that. The other part of my question though, was, is opWhateverAssign() on the cards any time soon. Any sign of an overload for *p = ?but for some reason "opStar" would not be high on my list of choices.Because the names for operator overloads should be based on what operator they're overloading, not the syntax for it. e.g. it's opMul, not opStar. opDiv, not opSlash.
Nov 22 2007
Janice Caron Wrote:On 11/22/07, Gregor Richards <Richards codu.org> wrote:What about *p *=, *p /=, *p ~=, ...? Wouldn't these need overloads, too?Janice Caron wrote:Yeah. Um. I was employing irony. Anyway, I'd hope Walter is considering changing the name, given the unanimous support here for doing that. The other part of my question though, was, is opWhateverAssign() on the cards any time soon. Any sign of an overload for *p = ?but for some reason "opStar" would not be high on my list of choices.Because the names for operator overloads should be based on what operator they're overloading, not the syntax for it. e.g. it's opMul, not opStar. opDiv, not opSlash.
Nov 23 2007
"Leonard Dahlmann" <leo.dahlmann gmail.com> wrote in message news:fi6o8p$2i49$1 digitalmars.com...Janice Caron Wrote:Which is why we need reference returns. Then you could do: class A { int myInt; ref int opDeref() { return myInt; } } A a = new A(); *a = 5; // sets myInt to 5 *a += 3; // sets myInt to 8 This would also solve the similar problems with opIndexAssign and with properties.On 11/22/07, Gregor Richards <Richards codu.org> wrote:What about *p *=, *p /=, *p ~=, ...? Wouldn't these need overloads, too?Janice Caron wrote:Yeah. Um. I was employing irony. Anyway, I'd hope Walter is considering changing the name, given the unanimous support here for doing that. The other part of my question though, was, is opWhateverAssign() on the cards any time soon. Any sign of an overload for *p = ?but for some reason "opStar" would not be high on my list of choices.Because the names for operator overloads should be based on what operator they're overloading, not the syntax for it. e.g. it's opMul, not opStar. opDiv, not opSlash.
Nov 23 2007
Jarrett Billingsley wrote:"Leonard Dahlmann" <leo.dahlmann gmail.com> wrote in message news:fi6o8p$2i49$1 digitalmars.com...I believe there was some mention of reference types at the conference.Janice Caron Wrote:Which is why we need reference returns. Then you could do: class A { int myInt; ref int opDeref() { return myInt; } } A a = new A(); *a = 5; // sets myInt to 5 *a += 3; // sets myInt to 8 This would also solve the similar problems with opIndexAssign and with properties.On 11/22/07, Gregor Richards <Richards codu.org> wrote:What about *p *=, *p /=, *p ~=, ...? Wouldn't these need overloads, too?Janice Caron wrote:Yeah. Um. I was employing irony. Anyway, I'd hope Walter is considering changing the name, given the unanimous support here for doing that. The other part of my question though, was, is opWhateverAssign() on the cards any time soon. Any sign of an overload for *p = ?but for some reason "opStar" would not be high on my list of choices.Because the names for operator overloads should be based on what operator they're overloading, not the syntax for it. e.g. it's opMul, not opStar. opDiv, not opSlash.
Nov 23 2007
On 11/23/07, Leonard Dahlmann <leo.dahlmann gmail.com> wrote:What about *p *=, *p /=, *p ~=, ...? Wouldn't these need overloads, too?I don't see why. We've done just fine without a[n]*=, a[n]/=, a[n]~= and so on, so why should * be any different? My preference would be to have the compiler rewrite *p += n; as p.opDerefAssign(p.opDeref() + n); (Ditto with .length, [] and so on). That way only two functions are needed, but you still get the full suite of expressability. Note that if the compiler is able to inline both opDeref() and opDerefAssign() then you'd still get the efficiency of ... er ... opDerefAddAssign ... without having the clutter of a zillion different functions. Of course, having functions return references is another approach too.
Nov 23 2007
Janice Caron wrote:My preference would be to have the compiler rewrite *p += n; as p.opDerefAssign(p.opDeref() + n);Wouldn't "p.opAssign(p.opDeref()+n);" be quite sufficient? regards, frank
Nov 23 2007
On 11/23/07, 0ffh <frank frankhirsch.youknow.what.todo.net> wrote:Janice Caron wrote:I think that would end up being equivalent to p = *p + n; whereas what we actually want is the equivalent of *p = *p + n; but I could be wrong. Maybe it depends on what we consider opAssign ought to be doing. In any case, we /do/ have opIndexAssign(), so opDerefAssign() ought to exist by analogy.My preference would be to have the compiler rewrite *p += n; as p.opDerefAssign(p.opDeref() + n);Wouldn't "p.opAssign(p.opDeref()+n);" be quite sufficient?
Nov 23 2007
Janice Caron wrote:On 11/23/07, 0ffh <frank frankhirsch.youknow.what.todo.net> wrote:Si.Janice Caron wrote:I think that would end up being equivalent to p = *p + n;My preference would be to have the compiler rewrite *p += n; as p.opDerefAssign(p.opDeref() + n);Wouldn't "p.opAssign(p.opDeref()+n);" be quite sufficient?whereas what we actually want is the equivalent of *p = *p + n;Is that so? I'd guess that cases where both "x=y;" and "*x=y;" make sense are rare. So overloading opAssign for the parameter type might be quite sufficient.but I could be wrong. Maybe it depends on what we consider opAssign ought to be doing. In any case, we /do/ have opIndexAssign(), so opDerefAssign() ought to exist by analogy.Well it's good that you know what ought to be - I'm sure I don't. ;-) regards, frank
Nov 23 2007
0ffh wrote:Janice Caron wrote: > I think that would end up being equivalent to > p = *p + n; Si. > whereas what we actually want is the equivalent of > *p = *p + n; Is that so? I'd guess that cases where both "x=y;" and "*x=y;" make sense are rare. So overloading opAssign for the parameter type might be quite sufficient.Even if they're rare, they *do* exist (for example, a variant type that can store both normal types and pointers). So for full functionality as well as plain consistency (also very important in a programming language) there should be an opDerefAssign. (And opDeref instead of opStar, obviously)
Nov 24 2007
Frits van Bommel wrote:0ffh wrote:You mean like: int y=0; Variant x=&y; *x=3; printf("%i\n",y); // prints 3 Wouldn't this be quite sufficient, without the need to "*x=": int y=0; Variant x=&y; int* z;z=x; *z=3; printf("%i\n",y); // prints 3 Variant is obviously a very special case. I wonder if we really need this... I think I see quite some potential for serious shootings of feet here... okay, I'm all for it now! =) regards, frankI'd guess that cases where both "x=y;" and "*x=y;" make sense are rare. So overloading opAssign for the parameter type might be quite sufficient.Even if they're rare, they *do* exist (for example, a variant type that can store both normal types and pointers).
Nov 24 2007
I wonder: There are quite a few requests floating around that would require changes to the D language. If the D language is going to be changed, why not go all the way and make the change to end all change requests: A small compile-time API for AST walking and manipulation. I don't just mean macros, but full scale metaprogramming. It would even be possible to do refactoring and convert the AST back to source from within the language. The only problem I really see is that it would require the AST structure to be part of the language specification. regards, frank
Nov 24 2007
0ffh wrote:[...]Okay, maybe it's a bit too insane! =) I imagine it would tax the compile time execution capabilities of the compilers, maybe beyond their limits. But how about a compiler switch to spit out the AST in some text format (say, in Annotated Term Format), and a possibility to feed those files back to the compiler, instead of D source? I imagine that couldn't be too hard? regards, frank
Nov 24 2007
0ffh wrote:I wonder: There are quite a few requests floating around that would require changes to the D language. If the D language is going to be changed, why not go all the way and make the change to end all change requests: A small compile-time API for AST walking and manipulation. I don't just mean macros, but full scale metaprogramming. It would even be possible to do refactoring and convert the AST back to source from within the language. The only problem I really see is that it would require the AST structure to be part of the language specification.... I think you just reinvented LISP :P. (Note: all those brackets are in there to encode the AST structure into the language)
Nov 24 2007
Frits van Bommel wrote:0ffh wrote:Hummm... not quite. The good thing about Lisps, IMHO, is that they failed to acquire a syntax (apart from Dylan). That, together with the fact how ridiculously easy it is to write a simple Lisp interpreter, gives you a powerful metaprogramming language for a rather small investment. Another feature of the lack of syntax in Lisp is that the syntax the mathematicians have invented for other functional languages makes my hair stand on end... =) It is perfectly possible to have a functional language with a C-like syntax, as I gather will shortly be demonstrated by D2. It's also perfectly possible to have a syntax and still do heavy duty metaprogramming, as Stratego has already shown. regards, frankI wonder: [...].... I think you just reinvented LISP :P. (Note: all those brackets are in there to encode the AST structure into the language)
Nov 25 2007
Take a look at OCaml / CamlP4, this is exactly what you are looking for, and this is done in a truely nice and (extremely) fast (as always when it comes to OCaml ...) way. In my opinion, CamlP4 is one of the biggest advantage of OCaml, because it truly permits to implement full DSL, or to couple a DSL to OCaml code in a transparent way, so for instance you can define a logic DSL, and have things like <ocaml code...> let F = Logic( A -> B \/ C ) in my_solver F ; <ocaml code again...> and this would translate to ... let F = Formula_Or(Formula_Imp(A,B),C) in my_solver F ; ... and this certainly is useful in many ways
Nov 25 2007
Maël wrote:Take a look at OCaml / CamlP4, this is exactly what you are looking for, and this is done in a truely nice and (extremely) fast (as always when it comes to OCaml ...) way. In my opinion, CamlP4 is one of the biggest advantage of OCaml, because it truly permits to implement full DSL, or to couple a DSL to OCaml code in a transparent way, so for instance you can define a logic DSL, and have things like [...] and this certainly is useful in many waysThanks for the tip. I took a look at O'Caml earlier, but to be frank, functional languages turn me off... =) I have been imperating since I was a small boy, and I'm reasonably good at it. But when I look at, say Haskell, Clean, Caml, the syntax alone is enough to turn me off. I find those even more repulsive than Lisp. That's not to say that I do not appreciate how powerful those languages are. But it's perfectly possible to have metaprogramming in an imperative languages with C-style syntax, you don't need to go functional for that. regards, frank
Nov 25 2007
I'm pretty sure that imperative language can (and will!) completely benefit from such an API, and I wasn't trying to tell you to go with OCaml, but to take some inspiration of the great work that has been done for CamlP4. As for the syntax, it becomes natural only when you exercise it frequently, but once you understand it, I tell you that frankly, you miss it in imperative languages. However, I've heard that Walter was trying to build an hybrid imperative/functionnal language (note that OCaml already is such an hybrid, although the switch between functionnal and imperative, and the object system aren't very "natural" to work with to be honest) and I'd really be happy it this was the case!
Nov 25 2007
Maël wrote:As for the syntax, it becomes natural only when you exercise it frequently, but once you understand it, I tell you that frankly, you miss it in imperative languages.That's entirely possible... maybe I am missing out on that. But then again, by missing out on that I won't miss it in C or D! ;-)However, I've heard that Walter was trying to build an hybrid imperative/functionnal language (note that OCaml already is such an hybrid, although the switch between functionnal and imperative, and the object system aren't very "natural" to work with to be honest) and I'd really be happy it this was the case!Me too! (Is this a candidate for moderation now?) regards, frank
Nov 25 2007
0ffh Wrote:Ma�l wrote:Anonymous AST macro is the ultimate way to solve it: macro(args, "=>", expr) { mixin("delegate(" ~ args.stringof ~ ")" ~ "{ return " ~ expr.stringof ~ "; };"); } void foo(int delegate(int, int)){ ... } foo(int x, int y => x + y); Hope it's helpful :) Regards, WeiTake a look at OCaml / CamlP4, this is exactly what you are looking for, and this is done in a truely nice and (extremely) fast (as always when it comes to OCaml ...) way. In my opinion, CamlP4 is one of the biggest advantage of OCaml, because it truly permits to implement full DSL, or to couple a DSL to OCaml code in a transparent way, so for instance you can define a logic DSL, and have things like [...] and this certainly is useful in many waysThanks for the tip. I took a look at O'Caml earlier, but to be frank, functional languages turn me off... =) I have been imperating since I was a small boy, and I'm reasonably good at it. But when I look at, say Haskell, Clean, Caml, the syntax alone is enough to turn me off. I find those even more repulsive than Lisp. That's not to say that I do not appreciate how powerful those languages are. But it's perfectly possible to have metaprogramming in an imperative languages with C-style syntax, you don't need to go functional for that. regards, frank
Nov 25 2007
Why would anyone need this in a general-purpose, OO language, is beyond me...
Nov 25 2007
Dejan Lekic wrote:Why would anyone need this in a general-purpose, OO language, is beyond me...A few examples of what would be possible to add to any program you have the source of, by just including an appropriate module: - Stack backtraces - Runtime statistics & profiling - Array access range checking - Integral overflow checks - New numeric types that behave like primitive types without any need for syntax overloading - Anything regards, frank
Nov 25 2007
Plus, if it is implemented as a pre-processor (CamlP4-like) you could make powerful domain-specific languages and directly embbed them in D programs
Nov 25 2007
Maël wrote:Plus, if it is implemented as a pre-processor (CamlP4-like) you could make powerful domain-specific languages and directly embbed them in D programsYou wouldn't even need CTFE for it, if you'd just add a compiler switch to output the AST in some text format (say, in Annotated Term Format), and a possibility to feed those files back to the compiler, instead of D source. Hacking the compiler would become a breeze. =) regards, frank
Nov 25 2007
On 11/24/07, 0ffh <frank frankhirsch.youknow.what.todo.net> wrote:*x=3; Wouldn't this be quite sufficient, without the need to "*x=":Both of those lines look like *x= to me.
Nov 24 2007
Janice Caron wrote:On 11/24/07, 0ffh <frank frankhirsch.youknow.what.todo.net> wrote:I think you missed the intention of the ':' at the end. He wasn't referring to the previous lines, but the following ones: 0ffh wrote:*x=3; Wouldn't this be quite sufficient, without the need to "*x=":Both of those lines look like *x= to me.int y=0; Variant x=&y; int* z;z=x; *z=3; printf("%i\n",y); // prints 3
Nov 24 2007
Janice Caron wrote:On 11/23/07, Leonard Dahlmann <leo.dahlmann gmail.com> wrote:But the nice thing about the current approach is that it makes it easy to allow controlled or monitored access of your data. If you return a reference you have no idea what they're going to do with it. Of course you don't always care. If you're providing a container class then returning a reference to the data element is exactly the behavior you want. But if you don't want to expose your data in that way, then some automatic rewrite rules like you suggest would be nice. I think both approaches have legitimate uses. Implementing one does not eliminate the need for the other. --bbWhat about *p *=, *p /=, *p ~=, ...? Wouldn't these need overloads, too?I don't see why. We've done just fine without a[n]*=, a[n]/=, a[n]~= and so on, so why should * be any different? My preference would be to have the compiler rewrite *p += n; as p.opDerefAssign(p.opDeref() + n); (Ditto with .length, [] and so on). That way only two functions are needed, but you still get the full suite of expressability. Note that if the compiler is able to inline both opDeref() and opDerefAssign() then you'd still get the efficiency of ... er ... opDerefAddAssign ... without having the clutter of a zillion different functions. Of course, having functions return references is another approach too.
Nov 23 2007
Mrs. Caron, I think You are mixing opStar with opMul* those two have totally different purpose, as You probably know...
Nov 25 2007