| Thread overview | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 01, 2006 Redefining __dollar for a class ;) | ||||
|---|---|---|---|---|
| ||||
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.)
| ||||
February 01, 2006 Re: Redefining __dollar for a class ;) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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
| |||
February 01, 2006 Re: Redefining __dollar for a class ;) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to John Reimer | Does $ serve any purpose other than making text output slightly easier in the "typing extra characters" department? | |||
February 01, 2006 Re: Redefining __dollar for a class ;) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to nick | 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
| |||
February 02, 2006 Re: Redefining __dollar for a class ;) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 | |||
February 02, 2006 Re: Redefining __dollar for a class ;) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander | "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. | |||
February 02, 2006 Re: Redefining __dollar for a class ;) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 | |||
July 29, 2006 Re: Redefining __dollar for a class ;) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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>
| |||
July 29, 2006 Re: Redefining __dollar for a class ;) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Deewiant | "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.. :) | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply