September 18
On Friday, 18 September 2020 at 20:46:00 UTC, mw wrote:
>
> 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.
>

We want as few range checks as possible because indexing arrays are not too seldom done it loops. Adding an extra check could affect performance in this case.

September 18
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
> }

signs[1+sign]
September 18
On 9/18/20 5:17 PM, mw wrote:
> 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.

It depends on how you want to define indexing.

Let me write that a different way:

"And ... this processing of negative indexes is not right, it's defensive programming: and will hide real bugs, e.g.

when array index < 0"

D array indexing is set in stone. Like really hard, billion-year-old stone.

Again, if you want a different indexing scheme, write a type. D makes it really easy!

-Steve
September 18
On Friday, 18 September 2020 at 21:06:22 UTC, mw wrote:
> You are indeed confused: the justification is either compiler writes that branching code, or user have to write it.

Good.

Let the user write it every time.

I don't want the compiler inserting branches in to my code simply because of the essentially-never chance I might not want to put a $ in front of a -<integer> statement for array access.

Further to write it every time: Write an array template. It's simple. Here's the important parts, the rest isn't too difficult to write from here

struct Array( T )
{
  ref auto opIndex( int Index ) { if( Index < 0 ) Index = slice.length + Index; return slice[ Index ]; }
  T[] slice;
}
September 20
On Friday, 18 September 2020 at 21:31:53 UTC, IGotD- wrote:
> On Friday, 18 September 2020 at 20:46:00 UTC, mw wrote:
>>
>> 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.
>>
>
> We want as few range checks as possible because indexing arrays are not too seldom done it loops. Adding an extra check could affect performance in this case.

You dont need an extra check, you just do an unsigned comparison, negative values will interpreted as larger. IE...

int idx = length-i;
assert(cast(unsigned) idx < length)

catches anything outside the valid range.

Yes you have the issue that the index could wrap around and end up valid, but you already have that anyway. I mean if (idx-1) is going to cause a problem so will (idx+0xFFFFFFFF)


September 20
On Sunday, 20 September 2020 at 14:01:58 UTC, claptrap wrote:
>
> You dont need an extra check, you just do an unsigned comparison, negative values will interpreted as larger. IE...
>
> int idx = length-i;
> assert(cast(unsigned) idx < length)
>
> catches anything outside the valid range.
>
> Yes you have the issue that the index could wrap around and end up valid, but you already have that anyway. I mean if (idx-1) is going to cause a problem so will (idx+0xFFFFFFFF)

No, if -1 is going to be interpreted as the element just before the end of the array you will need to have an extra check additional from the extra range check.

Still completely irrelevant for D as indexes are of type size_t which is unsigned.
September 20
On Sunday, 20 September 2020 at 14:06:10 UTC, IGotD- wrote:
> On Sunday, 20 September 2020 at 14:01:58 UTC, claptrap wrote:
>>
>> You dont need an extra check, you just do an unsigned comparison, negative values will interpreted as larger. IE...
>>
>> int idx = length-i;
>> assert(cast(unsigned) idx < length)
>>
>> catches anything outside the valid range.
>>
>> Yes you have the issue that the index could wrap around and end up valid, but you already have that anyway. I mean if (idx-1) is going to cause a problem so will (idx+0xFFFFFFFF)
>
> No, if -1 is going to be interpreted as the element just before the end of the array you will need to have an extra check additional from the extra range check.
>
> Still completely irrelevant for D as indexes are of type size_t which is unsigned.

Nevermind I missunderstood the OP
Next ›   Last »
1 2 3