digitalmars.D.learn - Avoid subtracting form .length
- Dom DiSc (14/14) Nov 08 I _very_ often use this pattern:
- monkyyy (2/16) Nov 08 write a utility function? ~~after turning off dscanner~~
- Anonymouse (3/13) Nov 08 I use `a.length +(-1)`. It's not pretty but at least dscanner is
- Jonathan M Davis (7/21) Nov 08 Well, I don't know what it's warning about, but the code is wrong, becau...
- Manfred Nowak (7/9) Nov 14 On Saturday, 9 November 2024 at 04:02:46 UTC, Jonathan M Davis
- Dom DiSc (9/9) Nov 14 No, no. My problem is: I want to access the last element of an
- Manfred Nowak (13/17) Nov 15 So it seems, that the intended problem was transposed into
- Paolo Invernizzi (7/17) Nov 18 Good Lord! Manfred Nowak? Is that really you?!
- Salih Dincer (25/39) Nov 13 Why not use foreach in your code? Is there a specific reason for
- user1234 (5/26) Nov 14 I dont know if that's the reason but slice assignments are likely
- matheus (25/26) Nov 14 My version:
- user1234 (5/19) Nov 14 You define a contract that Dscanner does not understand. The more
- Dom DiSc (3/6) Nov 14 Ok. Somewhat sad, but at least it works.
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 this, no matter if a check is present or not. What's the recommended fix for this? I have no idea what else to use.
Nov 08
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 this, no matter if a check is present or not. What's the recommended fix for this? I have no idea what else to use.write a utility function? ~~after turning off dscanner~~
Nov 08
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 is happy.
Nov 08
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 this perfectly safe, but DScanner gives warnings for this, no matter if a check is present or not. What's the recommended fix for this? I have no idea what else to use.Well, I don't know what it's warning about, but the code is wrong, because the lengths of the slices don't match in b[0 .. a.length-1] = a[]; You'll get a RangeError being thrown when you run the code. - Jonathan M Davis
Nov 08
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
Nov 14
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-1 ? - What else is proposed to implement the desired effect without warning?
Nov 14
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 only for the purpose of the development of the code, because the used `assert' will probably not be compiled into a release binary. [...]- What else is proposed to implement the desired effect without warning?Maybe using an `if`-statement, which is propper for release too, instead of an `assert`-expression, which is propper for develeopment only? Please reconsider the docs at 11.19.10.9.1: https://dlang.org/spec/expression.html#assert_expressions
Nov 15
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: [...]Good Lord! Manfred Nowak? Is that really you?! Everyone, we have the history of D in its full essence, dating back to 2004! :-P It's great to see you here again, even if only from time to time!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
Nov 18
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 this, no matter if a check is present or not. What's the recommended fix for this? I have no idea what else to use.Why not use foreach in your code? Is there a specific reason for this? If I'm not mistaken, this is what you want to do: ```d import std.array, std.range, std.stdio; void fun(size_t size, int padding = 42)(ref int[] arr) { assert(arr.length <= size, "Size Error!"); int[size] sarr = padding; foreach(i, ref e; arr) sarr[i] = e;/* sarr[0 .. arr.length+ 1] = arr[]; sarr[arr.length .. size] = padding;//*/ sarr.writefln!" %s"; } void main() { int[] arr = iota(5).array; arr.length.writeln(": ", arr); arr.fun!10; } /* 5: [0, 1, 2, 3, 4] [0, 1, 2, 3, 4, 42, 42, 42, 42, 42] //*/ ``` SDB79
Nov 13
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 dont know if that's the reason but slice assignments are likely faster, e.g for builtin types that certainly lead to generate a stdc `memove`s. (that must be indirectly done in DRT function `_d_array_slice_copy`).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 this, no matter if a check is present or not. What's the recommended fix for this? I have no idea what else to use.Why not use foreach in your code? Is there a specific reason for this? If I'm not mistaken, this is what you want to do: [...] SDB79
Nov 14
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); writeln("b = ", b); } void main(){ int[] a; a = iota(2).array; foo(a, 77); writeln(""); a = iota(11).array; foo(a, 77); } output: a = [0, 1] b = [0, 1, 77, 77, 77] a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] b = [0, 1, 2, 3, 4] Matheus.
Nov 14
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 this, no matter if a check is present or not. What's the recommended fix for this? I have no idea what else to use.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. You can do that in an ".ini" file that must stand in the project root directory IIRC.
Nov 14
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 the replies.
Nov 14