Jump to page: 1 2
Thread overview
$ for length?
Feb 20, 2014
Steve Teale
Feb 20, 2014
bearophile
Feb 20, 2014
w0rp
Feb 20, 2014
Chris
Feb 20, 2014
Steve Teale
Feb 20, 2014
bearophile
Feb 20, 2014
Mengu
Feb 20, 2014
bearophile
Feb 20, 2014
Frustrated
Feb 20, 2014
Kagamin
Feb 21, 2014
Jesse Phillips
Feb 20, 2014
Kagamin
Feb 20, 2014
Stanislav Blinov
Feb 20, 2014
Kagamin
Feb 21, 2014
Jesse Phillips
February 20, 2014
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
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
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
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
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
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
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
On Thursday, 20 February 2014 at 13:52:20 UTC, Chris wrote:

>
> Why not have both?

I was not suggesting otherwise.
February 20, 2014
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
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.
« First   ‹ Prev
1 2