www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array operations

reply Andy Valencia <dont spam.me> writes:
In the following code, two questions.  First, is there any 
difference between "x[] = y" and "x[] = y[]"?  It appears not.  
Second, in assigning from arrays of differing sizes, Phobos 
causes an illegal instruction, rather than the sort of exception 
I'd have expected.  I'm curious why they stepped away from D's 
exception architecture?

Thanks!
Andy

```d
import std.stdio : writeln;

void main() {
     int[] x = new int[8];
     x[] = 1;
     int[] y = new int[4];
     y[] = 2;
     x[0 .. 4] = y;
     writeln(x);
     x[] = 1;
     x[0 .. 4] = y[];
     writeln(x);
     x[] = y[];
     writeln(x);
}
```
May 02
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Friday, 2 May 2025 at 22:19:53 UTC, Andy Valencia wrote:
 In the following code, two questions.  First, is there any 
 difference between "x[] = y" and "x[] = y[]"?
With basic slices no; I could construct something with overloads tho ```d import std.stdio : writeln; auto foo(ref int[] a, ref int[] b){ a=b; } auto bar(ref int[] a, ref int[] b){ a=b[]; } void foobar(alias F)(){ int[] x = new int[4]; int[] y = new int[8]; F(x,y); (x.ptr-y.ptr).writeln; (x.length-y.length).writeln; } void main() { foobar!foo; foobar!bar; } ``` x and y are as far as I know, bit for bit identical and nothing can cause spookieness in either case
  I'm curious why they stepped away from D's exception 
 architecture?
Slices are references... mostly; its spooky when they overlap with dynamic arrays; with this instruction your going to be reassigning a pointer.
May 02
prev sibling parent reply Nick Treleaven <nick geany.org> writes:
On Friday, 2 May 2025 at 22:19:53 UTC, Andy Valencia wrote:
 In the following code, two questions.  First, is there any 
 difference between "x[] = y" and "x[] = y[]"?  It appears not.
`y` is already a slice `int[]`, so slicing it does not change the type. Slicing without indices selects all elements, it does not change the elements the expression refers to.
 Second, in assigning from arrays of differing sizes, Phobos 
 causes an illegal instruction, rather than the sort of 
 exception I'd have expected.  I'm curious why they stepped away 
 from D's exception architecture?
It throws a RangeError rather than an Exception. One advantage is that allows indexing an array to be used in `nothrow` code. Also, bounds checks can be turned off, in which case nothing would be thrown, and code attempting to catch this occurence would never be called. Indexing past the end of a slice is a programming error which is not supposed to be recoverable from. If the slice length mismatch is caused by a runtime value, that's still a programmer error because the runtime value was not checked for validity.
May 03
parent reply Andy Valencia <dont spam.me> writes:
On Saturday, 3 May 2025 at 11:18:00 UTC, Nick Treleaven wrote:
 Second, in assigning from arrays of differing sizes, Phobos 
 causes an illegal instruction, rather than the sort of 
 exception I'd have expected.  I'm curious why they stepped 
 away from D's exception architecture?
It throws a RangeError rather than an Exception. One advantage is that allows indexing an array to be used in `nothrow` code. Also, bounds checks can be turned off, in which case nothing would be thrown, and code attempting to catch this occurence would never be called.
At least on 1.40.1 of the ldc2 distro for x86-64, uses the "illegal instruction" instruction. But it does make sense that array operations want to be outside the "nothrow" guarantee, as that would make nothrow almost useless. Thanks, Andy ``` (gdb) r Starting program: /home/vandys/dlang/tst41 [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". [2, 2, 2, 2, 1, 1, 1, 1] [2, 2, 2, 2, 1, 1, 1, 1] Program received signal SIGILL, Illegal instruction. 0x00007ffff7f5a9e8 in core.internal.util.array._enforceSameLength(const(char[]), const(ulong), const(ulong)) () from /home/vandys/ldc2-1.39.0-linux-x86_64/bin/../lib/libdruntime-ldc-shared.so.109 (gdb) x/i $pc => 0x7ffff7f5a9e8 <_D4core8internal4util5array18_enforceSameLengthFNbNfxAaxmxmZv+168>: ud2 (gdb) ```
May 03
next sibling parent Kagamin <spam here.lot> writes:
You link with release version of druntime, try to link with debug 
version.
May 03
prev sibling next sibling parent Kagamin <spam here.lot> writes:
But yeah, core.internal.util.array is wrong design, it should be 
boolean function, and the compiler should generate 
assert(areTypedArraysConformable()); at the caller side.
May 03
prev sibling next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Saturday, May 3, 2025 8:23:00 AM Mountain Daylight Time Andy Valencia via
Digitalmars-d-learn wrote:
 At least on 1.40.1 of the ldc2 distro for x86-64, uses the
 "illegal instruction" instruction.
That sounds like an ldc bug then. With dmd, your program gives [2, 2, 2, 2, 1, 1, 1, 1] [2, 2, 2, 2, 1, 1, 1, 1] core.exception.RangeError q.d(13): Range violation ---------------- ??:? onRangeError [0x29f6b6] ??:? _d_arrayboundsp [0x29f685] ??:? _Dmain [0x27c099] - Jonathan M Davis
May 03
parent Andy Valencia <dont spam.me> writes:
On Sunday, 4 May 2025 at 00:15:23 UTC, Jonathan M Davis wrote:
 That sounds like an ldc bug then. With dmd, your program gives

 [2, 2, 2, 2, 1, 1, 1, 1]
 [2, 2, 2, 2, 1, 1, 1, 1]
 core.exception.RangeError q.d(13): Range violation
 ----------------
 ??:? onRangeError [0x29f6b6]
 ??:? _d_arrayboundsp [0x29f685]
 ??:? _Dmain [0x27c099]
Ah, you are right: https://github.com/ldc-developers/ldc/issues/3793 dmd here give the RangeError also (as does gdc). Thanks, Andy
May 04
prev sibling parent Kagamin <spam here.lot> writes:
Actually it's a regression: 
https://forum.dlang.org/post/fwwhyqpdvmdyudtgipyg forum.dlang.org
May 05