Re: Array operations

2025-05-05 Thread Kagamin via Digitalmars-d-learn
Actually it's a regression: https://forum.dlang.org/post/fwwhyqpdvmdyudtgi...@forum.dlang.org

Re: Array operations

2025-05-04 Thread Andy Valencia via Digitalmars-d-learn
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 [0

Re: Array operations

2025-05-03 Thread Jonathan M Davis via Digitalmars-d-learn
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,

Re: Array operations

2025-05-03 Thread Kagamin via Digitalmars-d-learn
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.

Re: Array operations

2025-05-03 Thread Kagamin via Digitalmars-d-learn
You link with release version of druntime, try to link with debug version.

Re: Array operations

2025-05-03 Thread Andy Valencia via Digitalmars-d-learn
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 [

Re: Array operations

2025-05-03 Thread Nick Treleaven via Digitalmars-d-learn
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 elem

Re: Array operations

2025-05-02 Thread monkyyy via Digitalmars-d-learn
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, re

Array operations

2025-05-02 Thread Andy Valencia via Digitalmars-d-learn
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 steppe

Re: Array operations with multidimensional arrays

2016-11-19 Thread Era Scarecrow via Digitalmars-d-learn
On Saturday, 19 November 2016 at 21:05:49 UTC, John Colvin wrote: On Saturday, 19 November 2016 at 19:36:50 UTC, Marduk wrote: Thanks a lot! Now I get what it means that array declarations are read from right to left. The way I think about it is this: int is a type. int[3] is an array of 3 in

Re: Array operations with multidimensional arrays

2016-11-19 Thread John Colvin via Digitalmars-d-learn
On Saturday, 19 November 2016 at 19:36:50 UTC, Marduk wrote: On Saturday, 19 November 2016 at 17:37:58 UTC, John Colvin wrote: On Saturday, 19 November 2016 at 10:20:16 UTC, Marduk wrote: Additionally, I would like to assign 2D sub-arrays of a 3D array, i.e. something like the following: int[

Re: Array operations with multidimensional arrays

2016-11-19 Thread Marduk via Digitalmars-d-learn
On Saturday, 19 November 2016 at 17:37:58 UTC, John Colvin wrote: On Saturday, 19 November 2016 at 10:20:16 UTC, Marduk wrote: Additionally, I would like to assign 2D sub-arrays of a 3D array, i.e. something like the following: int[3][2][2] a; a[0] = [[2,2], [2,2]]; You have the dimensions

Re: Array operations with multidimensional arrays

2016-11-19 Thread John Colvin via Digitalmars-d-learn
On Saturday, 19 November 2016 at 10:20:16 UTC, Marduk wrote: Additionally, I would like to assign 2D sub-arrays of a 3D array, i.e. something like the following: int[3][2][2] a; a[0] = [[2,2], [2,2]]; You have the dimensions the wrong way around. a is a 2 element array of 2 element arrays o

Array operations with multidimensional arrays

2016-11-19 Thread Marduk via Digitalmars-d-learn
In the documentation one can learn how to do array operations with 1D arrays. However, this does not scale up for 2D arrays. For example, the following does not work: int[2][2] a,b; a = [[1,1],[1,1]]; b[][] = a[][]*2; Additionally, I would like to assign 2D sub-arrays of a 3D array, i.e

Re: Array operations with array of structs

2015-07-11 Thread Peter via Digitalmars-d-learn
On Saturday, 11 July 2015 at 13:31:12 UTC, Peter wrote: So after looking into it a little bit... So now I'm trying to multiply the array by a double but it's giving incompatible type errors. opBinary, opBinaryRight, and opOpAssign are defined. I have: struct Vector3 { public double[3] _

Re: Array operations with array of structs

2015-07-11 Thread anonymous via Digitalmars-d-learn
On Saturday, 11 July 2015 at 13:31:12 UTC, Peter wrote: The postblit can only not take @nogc due to the array duplication which is understandable. I think the postblit might be redundant anyway since the struct is built on a static array so there is no possibility of two different Vect3s "point

Re: Array operations with array of structs

2015-07-11 Thread Peter via Digitalmars-d-learn
On Wednesday, 8 July 2015 at 06:05:54 UTC, ketmar wrote: do you see the gotcha? if you uncomment postblit or assigns, this build function fails to compile, as that operations aren't "pure nothrow @nogc @trusted", and they will be used for either assign or postblitting. So after looking into i

Re: Array operations with array of structs

2015-07-07 Thread ketmar via Digitalmars-d-learn
t.vdata[] = this.vdata[] + rhs.vdata[]; return result; } BOOM! adding (1) is enough to get the same error. this is what is going on. i don't know why DMD insists on such restrictions for array operations, though.

Re: Array operations with array of structs

2015-07-07 Thread Peter via Digitalmars-d-learn
On Monday, 6 July 2015 at 15:48:28 UTC, anonymous wrote: Ok, I disabled everything in the struct except what I posted and it ran. I then uncommented stuff to isolate the cause. I've added in the bits that cause the error below (plus some constructors just for reference). struct Vector3 {

Re: Array operations with array of structs

2015-07-06 Thread anonymous via Digitalmars-d-learn
On Monday, 6 July 2015 at 12:15:22 UTC, Peter wrote: dmd 2.066.1, windows 7 64bit Tested it on Windows 7, using dmd 2.066.1: works for me. The exact code I tested (just commented those "..." out): struct Vector3 { public double[3] _p; //... Vector3 opBinary(string op)(in Vecto

Re: Array operations with array of structs

2015-07-06 Thread Peter via Digitalmars-d-learn
On Monday, 6 July 2015 at 10:29:35 UTC, anonymous wrote: Works for me with various versions of dmd on linux. What compiler are you using, what version of it, what operating system, etc? dmd 2.066.1, windows 7 64bit

Re: Array operations with array of structs

2015-07-06 Thread anonymous via Digitalmars-d-learn
On Monday, 6 July 2015 at 03:02:59 UTC, Nicholas Wilson wrote: On Monday, 6 July 2015 at 01:16:54 UTC, Peter wrote: [...] unittest{ auto a = Vector3([2.0, 2.0, 0.0]); auto b = Vector3([1.0, 2.0, 1.0]); Vector3[] c = [a]; Vector3[] d = [b]; Vector3 e = a + b; // works Vec

Re: Array operations with array of structs

2015-07-06 Thread anonymous via Digitalmars-d-learn
On Monday, 6 July 2015 at 01:16:54 UTC, Peter wrote: Hi, I have a struct with arithmetic operations defined using opBinary but array operations with arrays of it don't work. struct Vector3 { public double[3] _p; ... Vector3 opBinary(string op)(in Vector3 rhs) const i

Re: Array operations with array of structs

2015-07-05 Thread Nicholas Wilson via Digitalmars-d-learn
On Monday, 6 July 2015 at 03:02:59 UTC, Nicholas Wilson wrote: On Monday, 6 July 2015 at 01:16:54 UTC, Peter wrote: Hi, I have a struct with arithmetic operations defined using opBinary but array operations with arrays of it don't work. struct Vector3 { public double[

Re: Array operations with array of structs

2015-07-05 Thread Nicholas Wilson via Digitalmars-d-learn
On Monday, 6 July 2015 at 01:16:54 UTC, Peter wrote: Hi, I have a struct with arithmetic operations defined using opBinary but array operations with arrays of it don't work. struct Vector3 { public double[3] _p; ... Vector3 opBinary(string op)(in Vector3 rhs) const i

Array operations with array of structs

2015-07-05 Thread Peter via Digitalmars-d-learn
Hi, I have a struct with arithmetic operations defined using opBinary but array operations with arrays of it don't work. struct Vector3 { public double[3] _p; ... Vector3 opBinary(string op)(in Vector3 rhs) const if (op == "+"){ Vector3 result;

Re: Array operations, dynamic arrays and length

2015-07-04 Thread jmh530 via Digitalmars-d-learn
On Thursday, 2 July 2015 at 19:27:57 UTC, J Miller wrote: I knew that automatic allocation doesn't happen, but I'm confused by the fact if you explicitly declare "c" with "int[] c;" and then assign "c[] = a[] * b[]", versus using "auto c = a[] * b[]", you get two different errors (array length

Re: Array operations, dynamic arrays and length

2015-07-02 Thread J Miller via Digitalmars-d-learn
On Thursday, 2 July 2015 at 12:59:03 UTC, Steven Schveighoffer wrote: On 7/2/15 8:21 AM, "Marc =?UTF-8?B?U2Now7x0eiI=?= " wrote: On Thursday, 2 July 2015 at 10:48:56 UTC, Steven Schveighoffer wrote: On 7/1/15 8:36 PM, J Miller wrote: Oh, and to make things really confusing, "auto e = a[] - b[

Re: Array operations, dynamic arrays and length

2015-07-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/2/15 8:21 AM, "Marc =?UTF-8?B?U2Now7x0eiI=?= " wrote: On Thursday, 2 July 2015 at 10:48:56 UTC, Steven Schveighoffer wrote: On 7/1/15 8:36 PM, J Miller wrote: Oh, and to make things really confusing, "auto e = a[] - b[]" and "int[] e = a[] - b[]" both cause "Error: array operation a[] - b

Re: Array operations, dynamic arrays and length

2015-07-02 Thread via Digitalmars-d-learn
https://issues.dlang.org/show_bug.cgi?id=14759

Re: Array operations, dynamic arrays and length

2015-07-02 Thread via Digitalmars-d-learn
On Thursday, 2 July 2015 at 10:48:56 UTC, Steven Schveighoffer wrote: On 7/1/15 8:36 PM, J Miller wrote: Oh, and to make things really confusing, "auto e = a[] - b[]" and "int[] e = a[] - b[]" both cause "Error: array operation a[] - b[] without destination memory not allowed". Using dmd 2.0

Re: Array operations, dynamic arrays and length

2015-07-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/1/15 8:36 PM, J Miller wrote: Oh, and to make things really confusing, "auto e = a[] - b[]" and "int[] e = a[] - b[]" both cause "Error: array operation a[] - b[] without destination memory not allowed". Using dmd 2.067.0. This is not a bug. You need to allocate memory before you can wri

Re: Array operations, dynamic arrays and length

2015-07-01 Thread J Miller via Digitalmars-d-learn
On Wednesday, 1 July 2015 at 21:15:13 UTC, Marc Schütz wrote: On Wednesday, 1 July 2015 at 19:09:36 UTC, Alex Parrill wrote: I don't think this is a bug. Since you don't initialize `c` to anything, it defaults to an empty slice. Array [] operations apply to each element of a sli

Re: Array operations, dynamic arrays and length

2015-07-01 Thread via Digitalmars-d-learn
On Wednesday, 1 July 2015 at 19:09:36 UTC, Alex Parrill wrote: I don't think this is a bug. Since you don't initialize `c` to anything, it defaults to an empty slice. Array [] operations apply to each element of a slice, but `c` doesn't have any elements, so it does nothing.

Re: Array operations, dynamic arrays and length

2015-07-01 Thread Alex Parrill via Digitalmars-d-learn
don't initialize `c` to anything, it defaults to an empty slice. Array [] operations apply to each element of a slice, but `c` doesn't have any elements, so it does nothing. Change `int[] c;` to `int[] c = new int[4];` and it works.

Re: Array operations, dynamic arrays and length

2015-07-01 Thread via Digitalmars-d-learn
On Tuesday, 30 June 2015 at 22:37:34 UTC, ixid wrote: int[] a = [1,1,1,1]; int[] b = [1,1,1,1]; int[] c; c[] = a[] - b[]; c.writeln; This outputs []. This feels wrong, it feels like something that should have exploded or set the length to 4. If the leng

Array operations, dynamic arrays and length

2015-06-30 Thread ixid via Digitalmars-d-learn
int[] a = [1,1,1,1]; int[] b = [1,1,1,1]; int[] c; c[] = a[] - b[]; c.writeln; This outputs []. This feels wrong, it feels like something that should have exploded or set the length to 4. If the lengths of a and b are mismatched it throws an exception.

Re: Array Operations question

2014-07-25 Thread bearophile via Digitalmars-d-learn
Márcio Martins: float[10] a; a[] = uniform(0.0f, 1.0f); This is going to set all values to the result of a single call to uniform(); Is there a way to express my intent that I want one result per array element? Currently D array operations can't be used for that kind of usage. S

Array Operations question

2014-07-25 Thread via Digitalmars-d-learn
Hello! I started exploring D today and one of the things that caught my attention was the "array operations" feature. While playing around and writing some code with it, I came across a problem I wasn't able to find a solution for. The following is a simple example of the a

Re: Scalar + array operations

2014-05-21 Thread Stefan Frijters via Digitalmars-d-learn
On Wednesday, 21 May 2014 at 17:07:27 UTC, Francesco Cattoglio wrote: On Wednesday, 21 May 2014 at 13:52:47 UTC, John Colvin wrote: On Wednesday, 21 May 2014 at 11:45:57 UTC, Stefan Frijters wrote: I would have expected the last case to work as well, but I get testarr.d(20): Error: incompatibl

Re: Scalar + array operations

2014-05-21 Thread Francesco Cattoglio via Digitalmars-d-learn
On Wednesday, 21 May 2014 at 13:52:47 UTC, John Colvin wrote: On Wednesday, 21 May 2014 at 11:45:57 UTC, Stefan Frijters wrote: I would have expected the last case to work as well, but I get testarr.d(20): Error: incompatible types for ((dfoo) * (ibar[])): 'double' and 'int[]' Is this by des

Re: Scalar + array operations

2014-05-21 Thread Stefan Frijters via Digitalmars-d-learn
On Wednesday, 21 May 2014 at 13:52:47 UTC, John Colvin wrote: Please file a bug, there's no reason for that not to work, it just needs to be implemented properly. Ok, thanks for confirming. Filed as https://issues.dlang.org/show_bug.cgi?id=12780 .

Re: Scalar + array operations

2014-05-21 Thread John Colvin via Digitalmars-d-learn
On Wednesday, 21 May 2014 at 11:45:57 UTC, Stefan Frijters wrote: When working on my current project (writing a numerical simulation code) I ran into the following issue when trying to multiply a vector (represented by a fixed-length array) by a scalar: import std.stdio; void main() { int

Re: Scalar + array operations

2014-05-21 Thread bearophile via Digitalmars-d-learn
Stefan Frijters: Is this by design? It was very surprising to me, especially since all other combinations do seem to work. I don't know if this situation is by design. At first sights it seems a limitation that could be removed. Bye, bearophile

Scalar + array operations

2014-05-21 Thread Stefan Frijters via Digitalmars-d-learn
When working on my current project (writing a numerical simulation code) I ran into the following issue when trying to multiply a vector (represented by a fixed-length array) by a scalar: import std.stdio; void main() { int ifoo = 2; int[3] ibar = 1; double dfoo = 2.0; double[3] dbar

Re: overload of array operations

2011-10-17 Thread bearophile
Timon Gehr: > I agree. It is odd that we have opSliceUnary but not opSliceBinary. If you think something useful is missing, then file a Bugzilla enhancement request. Bye, bearophile

Re: overload of array operations

2011-10-17 Thread Timon Gehr
On 10/15/2011 01:12 AM, Jay Norwood wrote: Jonathan M Davis Wrote: On Friday, October 14, 2011 15:29:17 Jay Norwood wrote: Jonathan M Davis Wrote: On Friday, October 14, 2011 11:30:25 Jay Norwood wrote: Is it possible to overload array operations Please be more specific. Are you asking

Re: overload of array operations

2011-10-17 Thread Timon Gehr
On 10/14/2011 09:43 PM, Jonathan M Davis wrote: On Friday, October 14, 2011 15:29:17 Jay Norwood wrote: Jonathan M Davis Wrote: On Friday, October 14, 2011 11:30:25 Jay Norwood wrote: Is it possible to overload array operations Please be more specific. Are you asking whether a struct or

Re: overload of array operations

2011-10-14 Thread Jonathan M Davis
On Friday, October 14, 2011 16:12 Jay Norwood wrote: > Jonathan M Davis Wrote: > > On Friday, October 14, 2011 15:29:17 Jay Norwood wrote: > > > Jonathan M Davis Wrote: > > > > On Friday, October 14, 2011 11:30:25 Jay Norwood wrote: > > > > >

Re: overload of array operations

2011-10-14 Thread Jay Norwood
Jonathan M Davis Wrote: > On Friday, October 14, 2011 15:29:17 Jay Norwood wrote: > > Jonathan M Davis Wrote: > > > On Friday, October 14, 2011 11:30:25 Jay Norwood wrote: > > > > Is it possible to overload array operations > > > > > > Please be m

Re: overload of array operations

2011-10-14 Thread Jonathan M Davis
On Friday, October 14, 2011 15:29:17 Jay Norwood wrote: > Jonathan M Davis Wrote: > > On Friday, October 14, 2011 11:30:25 Jay Norwood wrote: > > > Is it possible to overload array operations > > > > Please be more specific. Are you asking whether a struct or class

Re: overload of array operations

2011-10-14 Thread Jay Norwood
Jonathan M Davis Wrote: > On Friday, October 14, 2011 11:30:25 Jay Norwood wrote: > > Is it possible to overload array operations > > Please be more specific. Are you asking whether a struct or class can > overload > the indexing and slicing operators? If so, the answer

Re: overload of array operations

2011-10-14 Thread Jay Norwood
Jonathan M Davis Wrote: > On Friday, October 14, 2011 11:30:25 Jay Norwood wrote: > > Is it possible to overload array operations > > Please be more specific. Are you asking whether a struct or class can > overload > the indexing and slicing operators? If so, the answer

Re: overload of array operations

2011-10-14 Thread Jonathan M Davis
On Friday, October 14, 2011 11:30:25 Jay Norwood wrote: > Is it possible to overload array operations Please be more specific. Are you asking whether a struct or class can overload the indexing and slicing operators? If so, the answer is yes. http://d-programming-language.

overload of array operations

2011-10-14 Thread Jay Norwood
Is it possible to overload array operations

Re: Array Operations and type inference

2010-08-08 Thread simendsjo
On 07.08.2010 14:10, simendsjo wrote: I'm new to D2, so please.. :) (...) Thanks for all answers. Seems array operations is a bit buggy, so I'll rather look more into them at a later date.

Re: Array Operations and type inference

2010-08-08 Thread bearophile
Don: > That is bug 4578, which has been fixed, and will be in the next compiler > release. Good, there is no need to file it then. simendsjo post shows two more cases, this is the first: > { > double[3] a = [1,1,1]; > auto b = a[] + 3; // What happens here? >

Re: Array Operations and type inference

2010-08-08 Thread bearophile
4,4]); // fails as b.length == 0.. Should the > compiler > say something? > } That is wrong code, because array operations don't allocate new memory. The compiler (or runtime, because those are dynamic arrays) must complain here, but here it doesn't yet, and

Re: Array Operations and type inference

2010-08-07 Thread Don
Mafi wrote: Hey, here Mafi again, I thought about your snippets and here's what I have. Am 07.08.2010 14:10, schrieb simendsjo: { // Like the previous example, but with dynamic arrays.. double[] a = [1,1,1]; auto b = a; assert(a is b); b = a[] + 3; assert(a == [1,1,1]); //writeln(b); // acces

Re: Array Operations and type inference

2010-08-07 Thread Mafi
Hey, here Mafi again, I thought about your snippets and here's what I have. Am 07.08.2010 14:10, schrieb simendsjo: { double[3] a = [1,1,1]; double[3] b; b[] = a[] + 3; assert(a == [1,1,1]); assert(b == [4,4,4]); } { double[3] a = [1,1,1]; auto b = a; b[] = a[] + 3; assert(a == [1,1,1]); assert

Re: Array Operations and type inference

2010-08-07 Thread Mafi
Am 07.08.2010 14:10, schrieb simendsjo: I'm new to D2, so please.. :) The spec on Array Operations, http://www.digitalmars.com/d/2./arrays.html says: """A vector operation is indicated by the slice operator appearing as the lvalue of an =, +=, -=, *=, /=, %=, ^=, &=

Array Operations and type inference

2010-08-07 Thread simendsjo
I'm new to D2, so please.. :) The spec on Array Operations, http://www.digitalmars.com/d/2./arrays.html says: """A vector operation is indicated by the slice operator appearing as the lvalue of an =, +=, -=, *=, /=, %=, ^=, &= or |= operator.""" The f

Re: Confusing behavior involving array operations.

2010-05-14 Thread Robert Clipsham
be greatly appreciated. Thanks, Pillsy I'm using dmd 2.045 on linux here, and that example works fine, so I'd guess it's an OS X specific bug if you can't get it working with newer versions of dmd... There were some issues to do with array operations that have been fixed recent

Confusing behavior involving array operations.

2010-05-14 Thread Pillsy
I have the following program on Mac OS X 10.6.3 running dmd version 2.043: $ cat if.d import std.stdio; void main (string [] args) { foreach(arg; args) writeln(arg); auto vec = new double[10]; foreach(i, ref x; vec) { x = cast(double) i; } if (args.length > 1 && args[1] == "bar") {