Re: Avoid subtracting form .length

2024-11-18 Thread Paolo Invernizzi via Digitalmars-d-learn
On Thursday, 14 November 2024 at 13:08:36 UTC, Manfred Nowak wrote: On Saturday, 9 November 2024 at 04:02:46 UTC, Jonathan M Davis wrote: [...] b[0 .. a.length-1] = a[]; You'll get a RangeError being thrown when you run the code. [...] Therefore a fix might be to not deminish the length:

Re: Avoid subtracting form .length

2024-11-15 Thread Manfred Nowak via Digitalmars-d-learn
On Thursday, 14 November 2024 at 13:24:47 UTC, Dom DiSc wrote: No, no. My problem is: [...] So it seems, that the intended problem was transposed into some example, thereby oblittering that problem with some coding errors. I checked that the source array has at least one element. Yes, but on

Re: Avoid subtracting form .length

2024-11-14 Thread Dom DiSc via Digitalmars-d-learn
On Thursday, 14 November 2024 at 17:27:54 UTC, user1234 wrote: You define a contract that Dscanner does not understand. The more simple solution, as you seem to be careful about bounds, is to disable the specific check that worries you. Ok. Somewhat sad, but at least it works. Thanks for all t

Re: Avoid subtracting form .length

2024-11-14 Thread user1234 via Digitalmars-d-learn
On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote: I _very_ often use this pattern: ``` fun(ref int[] a) { assert(a.length && a.length<=100); int[100] b; b[0 .. a.length-1] = a[]; b[a.length .. 100] = 5; } ``` I consider this perfectly safe, but DScanner gives warnings for

Re: Avoid subtracting form .length

2024-11-14 Thread user1234 via Digitalmars-d-learn
On Thursday, 14 November 2024 at 06:53:01 UTC, Salih Dincer wrote: On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote: I _very_ often use this pattern: ```d fun(ref int[] a) { assert(a.length && a.length<=100); int[100] b; b[0 .. a.length-1] = a[]; b[a.length .. 100] = 5; }

Re: Avoid subtracting form .length

2024-11-14 Thread Dom DiSc via Digitalmars-d-learn
No, no. My problem is: I want to access the last element of an array and assign it to the element of same index in another array (so I can't use $), and I checked that the source array has at least one element. The questions are: - Why is it considered dangerous to use the expression a.length-

Re: Avoid subtracting form .length

2024-11-14 Thread Manfred Nowak via Digitalmars-d-learn
On Saturday, 9 November 2024 at 04:02:46 UTC, Jonathan M Davis wrote: [...] b[0 .. a.length-1] = a[]; You'll get a RangeError being thrown when you run the code. [...] Therefore a fix might be to not deminish the length: b[ 0 .. a.length]= a[]; -manfred

Re: Avoid subtracting form .length

2024-11-14 Thread matheus via Digitalmars-d-learn
On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote: ... My version: import std.stdio, std.array, std.range, std.algorithm; void foo(ref int[] a, int filler){ int[5] b; auto len = min(b.length,a.length); b[0..len] = a[0..len]; b[len..5] = filler; writeln("a = ", a); w

Re: Avoid subtracting form .length

2024-11-13 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote: I _very_ often use this pattern: ```d fun(ref int[] a) { assert(a.length && a.length<=100); int[100] b; b[0 .. a.length-1] = a[]; b[a.length .. 100] = 5; } ``` I consider this perfectly safe, but DScanner gives warnings for

Re: Avoid subtracting form .length

2024-11-08 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, November 8, 2024 4:27:40 PM MST Dom DiSc via Digitalmars-d-learn wrote: > I _very_ often use this pattern: > > ``` > fun(ref int[] a) > { > assert(a.length && a.length<=100); > int[100] b; > b[0 .. a.length-1] = a[]; > b[a.length .. 100] = 5; > } > ``` > > I consider thi

Re: Avoid subtracting form .length

2024-11-08 Thread Anonymouse via Digitalmars-d-learn
On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote: I _very_ often use this pattern: ``` fun(ref int[] a) { assert(a.length && a.length<=100); int[100] b; b[0 .. a.length-1] = a[]; b[a.length .. 100] = 5; } ``` I use `a.length +(-1)`. It's not pretty but at least dscanner i

Re: Avoid subtracting form .length

2024-11-08 Thread monkyyy via Digitalmars-d-learn
On Friday, 8 November 2024 at 23:27:40 UTC, Dom DiSc wrote: I _very_ often use this pattern: ``` fun(ref int[] a) { assert(a.length && a.length<=100); int[100] b; b[0 .. a.length-1] = a[]; b[a.length .. 100] = 5; } ``` I consider this perfectly safe, but DScanner gives warnings for