[Issue 3474] PATCH: Implement opDollar for struct and class indexing operations
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Nov 5 09:18:45 PST 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3474
--- Comment #3 from Andrei Alexandrescu <andrei at metalanguage.com> 2009-11-05 09:18:44 PST ---
(In reply to comment #2)
> (In reply to comment #1)
> > Great!
> >
> > Any chance we can use "length" instead of "opDollar" throughout? It would
> > instantly match what arrays are currently doing and it would be a closer
> > description of the semantics of the operation.
>
> Yes, the name doesn't make any difference.
> Although length() looks natural, I think length!(1)() looks a bit clumsy?
> The one-dimensional case is a degenerate form of the multi-dimensional case.
> Moreover, I suspect the template is not the interface you want in most cases:
> you often want to iterate over the dimensions. Probably in most cases, it'd be
> defined like:
>
> int opDollar(int n)() { return dim[n]; }
>
> Also there may be problems if you want to be able to use .length to change the
> dimensions.
>
> BTW you can use aliases. This works:
>
> struct Foo
> {
> int x;
> int length() { return x; }
> alias length opDollar;
> int opIndex(int k) { return x*k; }
> }
>
> void main()
> {
> Foo f = Foo(7);
> int x = foo[$-5];
> assert(x== 2*7);
> }
I understand and have no further comments. Let's go with opDollar.
(One suggestion if I may :o). In the 1-dimensional case, of a type defines no
opDollar but does define length... you may want to rewrite $ to length. But
probably that's too complicated a rule already.)
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list