opDollar and length
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?
Re: opDollar and length
On Sunday, 28 December 2014 at 18:12:42 UTC, Joseph Rushton Wakeling via Digitalmars-d-learn 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? To allow slicing for types that don't have a length property but are terminated by a sentinel value, like null terminated strings or single linked lists. It's usefull for multi-dimensional containers as well.
Re: opDollar and length
On Sunday, December 28, 2014 18:21:41 Tobias Pankrath via Digitalmars-d-learn wrote: On Sunday, 28 December 2014 at 18:12:42 UTC, Joseph Rushton Wakeling via Digitalmars-d-learn 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? To allow slicing for types that don't have a length property but are terminated by a sentinel value, like null terminated strings or single linked lists. It's usefull for multi-dimensional containers as well. Indeed, though there's definitely been discussion of making it so that opDollar is not necessary under at least some circumstances when length is defined: https://issues.dlang.org/show_bug.cgi?id=7177 But unfortunately, the nitty gritty details are just complicated enough that it hasn't happened yet. I really wish that it would though so that we can require that random access ranges define opDollar and then actually be able to use opDollar for random access ranges in generic code. - Jonathan M Davis
Re: opDollar and length
On Sun, 28 Dec 2014 19:02:59 +0100 Joseph Rushton Wakeling via Digitalmars-d-learn digitalmars-d-learn@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. signature.asc Description: PGP signature
Re: opDollar and length
On 28/12/14 19:21, Tobias Pankrath via Digitalmars-d-learn wrote: To allow slicing for types that don't have a length property but are terminated by a sentinel value, like null terminated strings or single linked lists. It's usefull for multi-dimensional containers as well. Ah, clear. Thanks very much. (Must remember to look into the multi-dimensional container issue in more detail, that looks like something worth understanding in depth.)