opDollar and length

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 28 12:42:57 PST 2014


On Sun, 28 Dec 2014 19:02:59 +0100
Joseph Rushton Wakeling via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:

> A question that suddenly occurred to me, and I realized I didn't know the answer.
> 
> Why is it necessary/desirable to define separate .length and .opDollar methods 
> for custom types?
'because you can't write this:

  auto len = myvar.$;

and it's very tedious to write this:

  auto slc = myvar[myvar.length-5..myvar.length-3];

sure, compiler can use `opDollar` when you requested `length` and there
is no such member. but it adds another quirk into language and may
backstab you eventually. there is nothing hard in adding
`alias length = opDollar;` (or vice versa) to type definition.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141228/f380e473/attachment.sig>


More information about the Digitalmars-d-learn mailing list