digitalmars.D - Redefining __dollar for a class ;)
- Jarrett Billingsley (29/29) Jan 31 2006 This came to me when writing an opSlice for a class - how about redefini...
- John Reimer (4/8) Jan 31 2006 I've gradually learned to appreciate it myself. It certainly beats
- nick (2/2) Jan 31 2006 Does $ serve any purpose other than making text output slightly easier
- Sean Kelly (3/5) Jan 31 2006 It allows for the length property to be referenced in anonymous temporar...
- Carlos Santander (6/45) Feb 01 2006 Maybe with a restriction that __dollar must not take any parameters and ...
- Jarrett Billingsley (4/7) Feb 01 2006 I think it should be allowed to return any type. After all, you can
- Carlos Santander (4/11) Feb 02 2006 You're right. The non-parameters restriction should exist, though.
- Deewiant (10/45) Jul 29 2006 This came to mind as I filed Issue 269 in the 'Zilla, so it deserves a b...
- Jarrett Billingsley (5/22) Jul 29 2006 That makes sense. The compiler would still be able to issue an error if...
This came to me when writing an opSlice for a class - how about redefining __dollar (i.e. $ to mean "length of array") for classes? The closest I've come is this: class A { void opSlice(int lo, int hi) { writefln(lo, ", ", hi); } } int __dollar(A a) { return 5; } void main() { A a = new A; a[0 .. $(a) - 1]; } Which prints "0, 4". I would make __dollar a method of A, but DMD doesn't like a.$. I know this will probably never, ever make it in, but it's something interesting to ponder :) Though it could happen.. if slice expressions had a kind of "implied with" for when they were used with classes, then we could define __dollar in a class, and a[0 .. $ - 1] would be the equivalent of a[0 .. a.__dollar - 1] (which does currently work if __dollar is a method of A, but doesn't save many keystrokes). (By the way, after having used $ for a while, I think I'm in the pro-dollar camp now.)
Jan 31 2006
Jarrett Billingsley wrote:(By the way, after having used $ for a while, I think I'm in the pro-dollar camp now.)I've gradually learned to appreciate it myself. It certainly beats using length in every instance. -JJR
Jan 31 2006
Does $ serve any purpose other than making text output slightly easier in the "typing extra characters" department?
Jan 31 2006
nick wrote:Does $ serve any purpose other than making text output slightly easier in the "typing extra characters" department?It allows for the length property to be referenced in anonymous temporaries. Sean
Jan 31 2006
Jarrett Billingsley escribió:This came to me when writing an opSlice for a class - how about redefining __dollar (i.e. $ to mean "length of array") for classes? The closest I've come is this: class A { void opSlice(int lo, int hi) { writefln(lo, ", ", hi); } } int __dollar(A a) { return 5; } void main() { A a = new A; a[0 .. $(a) - 1]; } Which prints "0, 4". I would make __dollar a method of A, but DMD doesn't like a.$. I know this will probably never, ever make it in, but it's something interesting to ponder :) Though it could happen.. if slice expressions had a kind of "implied with" for when they were used with classes, then we could define __dollar in a class, and a[0 .. $ - 1] would be the equivalent of a[0 ... a.__dollar - 1] (which does currently work if __dollar is a method of A, but doesn't save many keystrokes). (By the way, after having used $ for a while, I think I'm in the pro-dollar camp now.)Maybe with a restriction that __dollar must not take any parameters and it must return an integral type? Or will any numeric type work? Regardless, I like that idea. -- Carlos Santander Bernal
Feb 01 2006
"Carlos Santander" <csantander619 gmail.com> wrote in message news:drrn2u$2ob4$2 digitaldaemon.com...Maybe with a restriction that __dollar must not take any parameters and it must return an integral type? Or will any numeric type work? Regardless, I like that idea.I think it should be allowed to return any type. After all, you can overload the slice operator to do so.
Feb 01 2006
In article <drs4fk$at$1 digitaldaemon.com>, Jarrett Billingsley says..."Carlos Santander" <csantander619 gmail.com> wrote in message news:drrn2u$2ob4$2 digitaldaemon.com...You're right. The non-parameters restriction should exist, though. -- Carlos Santander BernalMaybe with a restriction that __dollar must not take any parameters and it must return an integral type? Or will any numeric type work? Regardless, I like that idea.I think it should be allowed to return any type. After all, you can overload the slice operator to do so.
Feb 02 2006
Jarrett Billingsley wrote:This came to me when writing an opSlice for a class - how about redefining __dollar (i.e. $ to mean "length of array") for classes? The closest I've come is this: class A { void opSlice(int lo, int hi) { writefln(lo, ", ", hi); } } int __dollar(A a) { return 5; } void main() { A a = new A; a[0 .. $(a) - 1]; } Which prints "0, 4". I would make __dollar a method of A, but DMD doesn't like a.$. I know this will probably never, ever make it in, but it's something interesting to ponder :) Though it could happen.. if slice expressions had a kind of "implied with" for when they were used with classes, then we could define __dollar in a class, and a[0 .. $ - 1] would be the equivalent of a[0 .. a.__dollar - 1] (which does currently work if __dollar is a method of A, but doesn't save many keystrokes).This came to mind as I filed Issue 269 in the 'Zilla, so it deserves a bump. A possible and possibly simpler solution is that when using classInstance[$] it would just call the class's length method, which is essentially what it does with arrays. This way, foo[$] would always be equivalent to foo[foo.length]. Using __dollar like in your code smells bad to me, since names prefixed with __ are meant for the compiler's use only. If manual overloading of $ were to be allowed (i.e. not in the implicit way like I suggested above), it'd likely have to be something like opDollar. And hey, we got opSliceAssign eventually, so we might get this as well! <g>
Jul 29 2006
"Deewiant" <deewiant.doesnotlike.spam gmail.com> wrote in message news:eaficf$9lh$1 digitaldaemon.com...This came to mind as I filed Issue 269 in the 'Zilla, so it deserves a bump. A possible and possibly simpler solution is that when using classInstance[$] it would just call the class's length method, which is essentially what it does with arrays. This way, foo[$] would always be equivalent to foo[foo.length].That makes sense. The compiler would still be able to issue an error if no length method existed. That does seem better.Using __dollar like in your code smells bad to me, since names prefixed with __ are meant for the compiler's use only. If manual overloading of $ were to be allowed (i.e. not in the implicit way like I suggested above), it'd likely have to be something like opDollar. And hey, we got opSliceAssign eventually, so we might get this as well! <g>Yeah, I whined about that one too, didn't I.. :)
Jul 29 2006