Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 20, 2014 $ for length? | ||||
---|---|---|---|---|
| ||||
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. |
February 20, 2014 Re: $ for length? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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 |
February 20, 2014 Re: $ for length? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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.
|
February 20, 2014 Re: $ for length? | ||||
---|---|---|---|---|
| ||||
Posted in reply to w0rp | 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?
|
February 20, 2014 Re: $ for length? | ||||
---|---|---|---|---|
| ||||
Posted in reply to w0rp | 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
|
February 20, 2014 Re: $ for length? | ||||
---|---|---|---|---|
| ||||
Posted in reply to w0rp | 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).
|
February 20, 2014 Re: $ for length? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mengu | 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
|
February 20, 2014 Re: $ for length? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | On Thursday, 20 February 2014 at 13:52:20 UTC, Chris wrote:
>
> Why not have both?
I was not suggesting otherwise.
|
February 20, 2014 Re: $ for length? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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?
|
February 20, 2014 Re: $ for length? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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.
|
Copyright © 1999-2021 by the D Language Foundation