www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - $ for length?

reply "Steve Teale" <steve.teale britseyeview.com> writes:
How difficult would it be to allow the '$' to be used instead of
length in something like:

Thing[] ta;
for (size_t i = 0; i < ta.$; i++)

It can be used in slices, and indexes, so it might well be
unambiguous here.
Feb 20 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Steve Teale:

 How difficult would it be to allow the '$' to be used instead of
 length in something like:

 Thing[] ta;
 for (size_t i = 0; i < ta.$; i++)

 It can be used in slices, and indexes, so it might well be
 unambiguous here.

You can vote it: https://d.puremagic.com/issues/show_bug.cgi?id=7840 Bye, bearophile
Feb 20 2014
prev sibling next sibling parent "w0rp" <devw0rp gmail.com> writes:
On Thursday, 20 February 2014 at 10:28:43 UTC, Steve Teale wrote:
 How difficult would it be to allow the '$' to be used instead of
 length in something like:

 Thing[] ta;
 for (size_t i = 0; i < ta.$; i++)

 It can be used in slices, and indexes, so it might well be
 unambiguous here.

I don't like it. It's a small difference in the number of characters between the two on a line, and .length is much easier to read, especially for people new to the language. I think given D's smaller userbase, anything which makes a line of code easier to read for people coming from other languages without getting in the way of other things is a bonus.
Feb 20 2014
prev sibling next sibling parent "Chris" <wendlec tcd.ie> writes:
On Thursday, 20 February 2014 at 13:17:37 UTC, w0rp wrote:
 On Thursday, 20 February 2014 at 10:28:43 UTC, Steve Teale 
 wrote:
 How difficult would it be to allow the '$' to be used instead 
 of
 length in something like:

 Thing[] ta;
 for (size_t i = 0; i < ta.$; i++)

 It can be used in slices, and indexes, so it might well be
 unambiguous here.

I don't like it. It's a small difference in the number of characters between the two on a line, and .length is much easier to read, especially for people new to the language. I think given D's smaller userbase, anything which makes a line of code easier to read for people coming from other languages without getting in the way of other things is a bonus.

Why not have both?
Feb 20 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
w0rp:

 length is much easier to read, especially for people new to the 
 language.

But such new people find code like: a[$ - 1] so they need to know the meaning of $. Bye, bearophile
Feb 20 2014
prev sibling next sibling parent "Mengu" <mengukagan gmail.com> writes:
On Thursday, 20 February 2014 at 13:17:37 UTC, w0rp wrote:
 On Thursday, 20 February 2014 at 10:28:43 UTC, Steve Teale 
 wrote:
 How difficult would it be to allow the '$' to be used instead 
 of
 length in something like:

 Thing[] ta;
 for (size_t i = 0; i < ta.$; i++)

 It can be used in slices, and indexes, so it might well be
 unambiguous here.

I don't like it. It's a small difference in the number of characters between the two on a line, and .length is much easier to read, especially for people new to the language. I think given D's smaller userbase, anything which makes a line of code easier to read for people coming from other languages without getting in the way of other things is a bonus.

well, at least it's not len(ta).
Feb 20 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Mengu:

 well, at least it's not len(ta).

A free function len() is quite useful if you want to use it in higher order functions: map(len, ["red", "yo"]) In D: ["red", "yo"].map!(s => s.length) ["red", "yo"].map!walkLength Bye, bearophile
Feb 20 2014
prev sibling next sibling parent "Steve Teale" <steve.teale britseyeview.com> writes:
On Thursday, 20 February 2014 at 13:52:20 UTC, Chris wrote:

 Why not have both?

I was not suggesting otherwise.
Feb 20 2014
prev sibling next sibling parent "Frustrated" <c1514843 drdrb.com> writes:
On Thursday, 20 February 2014 at 10:28:43 UTC, Steve Teale wrote:
 How difficult would it be to allow the '$' to be used instead of
 length in something like:

 Thing[] ta;
 for (size_t i = 0; i < ta.$; i++)

 It can be used in slices, and indexes, so it might well be
 unambiguous here.

it's ugly, how about ta.opLength = $ta = ta.length?
Feb 20 2014
prev sibling next sibling parent "Kagamin" <spam here.lot> writes:
On Thursday, 20 February 2014 at 10:28:43 UTC, Steve Teale wrote:
 How difficult would it be to allow the '$' to be used instead of
 length in something like:

 Thing[] ta;
 for (size_t i = 0; i < ta.$; i++)

 It can be used in slices, and indexes, so it might well be
 unambiguous here.

Early versions of D already had the feature: you could write arr[0..length], but the feature proved to be ambiguous and was removed. $ is meant to be a special indexing syntax (like two dots), it's not a member, not a length.
Feb 20 2014
prev sibling next sibling parent "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Thursday, 20 February 2014 at 18:39:43 UTC, Kagamin wrote:

 $ is meant to be a special indexing syntax (like two dots), 
 it's not a member, not a length.

Errr, what? http://dlang.org/operatoroverloading.html#Dollar
Feb 20 2014
prev sibling next sibling parent "Kagamin" <spam here.lot> writes:
On Thursday, 20 February 2014 at 18:35:26 UTC, Frustrated wrote:
 it's ugly, how about ta.opLength = $ta = ta.length?

Or #ta - what miniD does. See http://d.puremagic.com/issues/show_bug.cgi?id=2660
Feb 20 2014
prev sibling next sibling parent "Kagamin" <spam here.lot> writes:
On Thursday, 20 February 2014 at 18:43:29 UTC, Stanislav Blinov
wrote:
 On Thursday, 20 February 2014 at 18:39:43 UTC, Kagamin wrote:

 $ is meant to be a special indexing syntax (like two dots), 
 it's not a member, not a length.

Errr, what? http://dlang.org/operatoroverloading.html#Dollar

It repeats what I said:
 Within the arguments to array index and slicing operators

Well, you can't write a.+(b) - that's a difference between syntax and member.
Feb 20 2014
prev sibling next sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Thursday, 20 February 2014 at 18:45:49 UTC, Kagamin wrote:
 On Thursday, 20 February 2014 at 18:35:26 UTC, Frustrated wrote:
 it's ugly, how about ta.opLength = $ta = ta.length?

Or #ta - what miniD does. See http://d.puremagic.com/issues/show_bug.cgi?id=2660

Comes from Lua. It may or may not represent the number of elements in a table, since we don't want to correlate to those semantics it is probably best to stay away from it.
Feb 20 2014
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Thursday, 20 February 2014 at 10:28:43 UTC, Steve Teale wrote:
 How difficult would it be to allow the '$' to be used instead of
 length in something like:

 Thing[] ta;
 for (size_t i = 0; i < ta.$; i++)

 It can be used in slices, and indexes, so it might well be
 unambiguous here.

My first reaction is disgust, second is I'd probably get use to it, third is wondering if I would actually use it given the option (probably not).
Feb 20 2014