Redefining __dollar for a class ;)
Deewiant
deewiant.doesnotlike.spam at gmail.com
Sat Jul 29 04:54:55 PDT 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>
More information about the Digitalmars-d
mailing list