[Issue 3474] PATCH: Implement opDollar for struct and class indexing operations

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 5 08:50:30 PST 2009


http://d.puremagic.com/issues/show_bug.cgi?id=3474



--- Comment #2 from Don <clugdbug at yahoo.com.au> 2009-11-05 08:50:28 PST ---
(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);
}

-- 
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