September 18, 2020 Re: why $ is need to access array [negative index]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On Friday, 18 September 2020 at 20:56:30 UTC, mw wrote:
> On Friday, 18 September 2020 at 20:48:47 UTC, bachmeier wrote:
>> I'm inclined to say typing a single character is not a hardship in exchange for extreme clarity of the code. Keep in mind that
>
> It not about saving "typing a single character".
>
> In the example I showed,
>
> signs[sign] = ...; // the sign can be -1, 0, 1
>
> in D, to write the same code, you have to test the sign and branch:
>
> if (sign >= 0) {
> signs[ sign] = ...;
> } else {
> signs[$+sign] = ...; // remember + here
> }
I'm confused. The justification for a major language change would be convenience of porting Python code to D?
|
September 18, 2020 Re: why $ is need to access array [negative index]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On 9/18/20 4:46 PM, mw wrote:
> On Friday, 18 September 2020 at 20:06:01 UTC, Steven Schveighoffer wrote:
>>
>> I would say no. The indexing code lowers to a machine instruction. Making it so a negative value means something else means every single indexing operation is going to have to check whether it's negative, and if so do something completely different.
>
> Currently we have range check on every single indexing operation already; so the trade-off here is: adding one more check v.s. the convenience it buys.
-boundscheck=off
This currently disables the bounds checks. If your proposed feature went in, then it would still have to check the index for sign.
-Steve
|
September 18, 2020 Re: why $ is need to access array [negative index]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | On Friday, 18 September 2020 at 21:02:11 UTC, bachmeier wrote:
>> in D, to write the same code, you have to test the sign and branch:
>>
>> if (sign >= 0) {
>> signs[ sign] = ...;
>> } else {
>> signs[$+sign] = ...; // remember + here
>> }
>
> I'm confused. The justification for a major language change would be convenience of porting Python code to D?
You are indeed confused: the justification is either compiler writes that branching code, or user have to write it.
|
September 18, 2020 Re: why $ is need to access array [negative index]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On 9/18/20 4:56 PM, mw wrote:
> On Friday, 18 September 2020 at 20:48:47 UTC, bachmeier wrote:
>> I'm inclined to say typing a single character is not a hardship in exchange for extreme clarity of the code. Keep in mind that
>
> It not about saving "typing a single character".
>
> In the example I showed,
>
> signs[sign] = ...; // the sign can be -1, 0, 1
>
> in D, to write the same code, you have to test the sign and branch:
>
> if (sign >= 0) {
> signs[ sign] = ...;
> } else {
> signs[$+sign] = ...; // remember + here
> }
>
>
Well, if you don't care about verbosity.
signs[(sign + $) % $] = ...;
-Steve
|
September 18, 2020 Re: why $ is need to access array [negative index]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | On Friday, 18 September 2020 at 20:57:04 UTC, bachmeier wrote:
> I forgot to add when discussing C, that D already has negative index values, which mean something completely different than Python's usage:
>
> double[] x = [1, 2, 3, 4];
> double * y = &(x.ptr)[2];
> writeln(y[-2]); // 1
This is using array index syntax on a raw pointer.
|
September 18, 2020 Re: why $ is need to access array [negative index]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Friday, 18 September 2020 at 21:11:58 UTC, Steven Schveighoffer wrote:
> Well, if you don't care about verbosity.
>
> signs[(sign + $) % $] = ...;
I want compiler writes this verbosity, instead of the user :-)
|
September 18, 2020 Re: why $ is need to access array [negative index]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On Friday, 18 September 2020 at 21:14:19 UTC, mw wrote:
> On Friday, 18 September 2020 at 21:11:58 UTC, Steven Schveighoffer wrote:
>> Well, if you don't care about verbosity.
>>
>> signs[(sign + $) % $] = ...;
>
> I want compiler writes this verbosity, instead of the user :-)
And ... this [(sign + $) % $] is not right, it's defensive programming: and will hide real bugs, e.g.
when array index > array.length.
See how easy user code contains bugs. The compiler should really do the hard work to make programmer's life easier.
|
September 18, 2020 Re: why $ is need to access array [negative index]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On Friday, 18 September 2020 at 21:14:19 UTC, mw wrote:
> On Friday, 18 September 2020 at 21:11:58 UTC, Steven Schveighoffer wrote:
>> Well, if you don't care about verbosity.
>>
>> signs[(sign + $) % $] = ...;
>
> I want compiler writes this verbosity, instead of the user :-)
It's not free, and not always desirable. This code would silently
accept sign=100 for example.
|
September 18, 2020 Re: why $ is need to access array [negative index]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On 9/18/20 5:14 PM, mw wrote:
> On Friday, 18 September 2020 at 21:11:58 UTC, Steven Schveighoffer wrote:
>> Well, if you don't care about verbosity.
>>
>> signs[(sign + $) % $] = ...;
>
> I want compiler writes this verbosity, instead of the user :-)
>
A custom type will do this for you. Just write one.
Would be as easy as:
arr.pyIdx[-1]; // uses python indexing.
-Steve
|
September 18, 2020 Re: why $ is need to access array [negative index]? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mw | On Fri, Sep 18, 2020 at 09:14:19PM +0000, mw via Digitalmars-d wrote: > On Friday, 18 September 2020 at 21:11:58 UTC, Steven Schveighoffer wrote: > > Well, if you don't care about verbosity. > > > > signs[(sign + $) % $] = ...; > > I want compiler writes this verbosity, instead of the user :-) Yawn. struct MyArray(T) { T[] impl; alias impl this; this(T[] data) { impl = data; } ref T opIndex(ptrdiff_t idx) { return (idx < 0) ? impl[$ + idx] : impl[idx]; } } MyArray!int x = [ 1, 2, 3 ]; assert(x[-1] == 3); // not verbose anymore T -- Живёшь только однажды. |
Copyright © 1999-2021 by the D Language Foundation