www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Redefining __dollar for a class ;)

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
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
next sibling parent reply John Reimer <terminal.node gmail.com> writes:
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
parent reply nick <nick.atamas gmail.com> writes:
Does $ serve any purpose other than making text output slightly easier 
in the "typing extra characters" department?
Jan 31 2006
parent Sean Kelly <sean f4.ca> writes:
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
prev sibling next sibling parent reply Carlos Santander <csantander619 gmail.com> writes:
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
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
parent Carlos Santander <Carlos_member pathlink.com> writes:
In article <drs4fk$at$1 digitaldaemon.com>, Jarrett Billingsley says...
"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.

You're right. The non-parameters restriction should exist, though. -- Carlos Santander Bernal
Feb 02 2006
prev sibling parent reply Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
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
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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