Re: Idiomatic D code to avoid or detect devision by zero

2020-08-03 Thread Martin Tschierschke via Digitalmars-d-learn
On Friday, 31 July 2020 at 15:19:25 UTC, Andrea Fontana wrote: On Friday, 31 July 2020 at 13:55:18 UTC, Martin Tschierschke wrote: What would be the idiomatic way to write a floating point division occuring inside a loop and handle the case of division by zero. c = a/b; // b might be zero some

Re: Idiomatic D code to avoid or detect devision by zero

2020-08-03 Thread Martin Tschierschke via Digitalmars-d-learn
On Friday, 31 July 2020 at 14:18:15 UTC, Steven Schveighoffer wrote: On 7/31/20 9:55 AM, Martin Tschierschke wrote: What would be the idiomatic way to write a floating point division occuring inside a loop and handle the case of division by zero. c = a/b; // b might be zero sometimes, than set

Re: dynamic array .length vs .reserve - what's the difference?

2020-08-03 Thread wjoe via Digitalmars-d-learn
On Saturday, 1 August 2020 at 16:04:01 UTC, Steven Schveighoffer wrote: On 7/31/20 12:32 PM, wjoe wrote: On Friday, 31 July 2020 at 04:28:57 UTC, Ali Çehreli wrote: Another option, which is curiously said to be more performant in memory allocation than native arrays, is std.array.Appender. I'v

Are function literals deprecated?

2020-08-03 Thread Victor L Porton via Digitalmars-d-learn
I am writing a book about D (I already have 150 pages). Are function literals considered deprecated in regard of using delegates instead?

Re: Are function literals deprecated?

2020-08-03 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 3 August 2020 at 14:23:56 UTC, Victor L Porton wrote: Are function literals considered deprecated in regard of using delegates instead? No, they both work well for different purposes.

Re: Idiomatic D code to avoid or detect devision by zero

2020-08-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/3/20 5:53 AM, Martin Tschierschke wrote: On Friday, 31 July 2020 at 14:18:15 UTC, Steven Schveighoffer wrote: On 7/31/20 9:55 AM, Martin Tschierschke wrote: What would be the idiomatic way to write a floating point division occuring inside a loop and handle the case of division by zero. c

Re: Idiomatic D code to avoid or detect devision by zero

2020-08-03 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn
On Monday, 3 August 2020 at 14:50:36 UTC, Steven Schveighoffer wrote: On 8/3/20 5:53 AM, Martin Tschierschke wrote: I prefer putting additional bracket around For really long expressions you could also split it on multiple lines: c = (b_expression == 0) ? (d_longer_expression) : (a_expr

Re: safety and auto vectorization

2020-08-03 Thread Chad Joan via Digitalmars-d-learn
On Sunday, 2 August 2020 at 17:31:45 UTC, Bruce Carneal wrote: import std; void f0(int[] a, int[] b, int[] dst) @safe { dst[] = a[] + b[]; } void f1(int[] a, int[] b, int[] dst) @trusted { const minLen = min(a.length, b.length, dst.length); dst[0..minLen] = a[0..minLen] + b[0..minLe

Re: safety and auto vectorization

2020-08-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/2/20 1:31 PM, Bruce Carneal wrote: import std; void f0(int[] a, int[] b, int[] dst) @safe {     dst[] = a[] + b[]; } void f1(int[] a, int[] b, int[] dst) @trusted {     const minLen = min(a.length, b.length, dst.length);     dst[0..minLen] = a[0..minLen] + b[0..minLen];     assert(dst.

Re: safety and auto vectorization

2020-08-03 Thread Bruce Carneal via Digitalmars-d-learn
On Monday, 3 August 2020 at 18:55:36 UTC, Steven Schveighoffer wrote: On 8/2/20 1:31 PM, Bruce Carneal wrote: import std; void f0(int[] a, int[] b, int[] dst) @safe {     dst[] = a[] + b[]; } [snip of auto-vectorization example] I was surprised that f0 ran just fine with a.length and b.le

Re: safety and auto vectorization

2020-08-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/3/20 3:22 PM, Bruce Carneal wrote: Thanks Steve (and Chad).  Summary: underspecified, varying behavior across versions, buggy. Steve, what's the best way for me to report this?  Are spec issues lumped in with the other bugzilla reports? Yep. You can file under dlang.org with the spec

Re: Question about UDAs

2020-08-03 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Aug 03, 2020 at 03:00:08AM +, Cecil Ward via Digitalmars-d-learn wrote: > When practically speaking would you use UDAs? A real-world use-case? There are probably more use cases than this, but for me, their primary usefulness is in declarative programming and compile-time introspection

Re: Question about UDAs

2020-08-03 Thread Ali Çehreli via Digitalmars-d-learn
On 8/2/20 8:00 PM, Cecil Ward wrote: > Ali Çehreli’s book mentions them briefly with an example > but that doesn’t seem to qualify as a realistic use-case. The XML example I chose there qualifies as serialization like H. S. Teoh mentions. UDAs on user-defined type members are for marking them f

Re: Question about UDAs

2020-08-03 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Aug 03, 2020 at 08:16:57PM -0700, Ali Çehreli via Digitalmars-d-learn wrote: [...] > UDAs were added to D by a request from Manu Evans and that's when I > learned them. In one of Manu's use cases they would put a @Tweakable > attribute to certain struct members. The effect of that attribut