Re: How to allocate arrays of objects?

2016-02-11 Thread Jacob Carlborg via Digitalmars-d-learn
On 2016-02-11 05:34, Adam D. Ruppe wrote: On Thursday, 11 February 2016 at 04:31:12 UTC, cy wrote: as[0..$] = new A(); before accessing .stuff on as[0]. Loop through it and allocate each one rather than trying to do it in a one liner like that. What about this? as[] = new A(); Or will

Re: String joining an array of structs or class instances implementing toString?

2016-02-11 Thread pineapple via Digitalmars-d-learn
Oh pardon the constructor I was playing with as a learning experience and forgot to get rid of.

Re: Does D optimize sqrt(2.0)?

2016-02-11 Thread crimaniak via Digitalmars-d-learn
On Thursday, 11 February 2016 at 07:41:55 UTC, Enjoys Math wrote: If I just type out sqrt(2.0) in D, is that automatically made into a constant for me? Thanks. for DMD -O : import std.math; immutable foo = sqrt(2.0); pure float precalculated() { return foo; } pure float

Re: String joining an array of structs or class instances implementing toString?

2016-02-11 Thread Ali Çehreli via Digitalmars-d-learn
On 02/11/2016 04:44 AM, pineapple wrote: It feels like there should be an out-of-the box way to do this but I haven't been able to find it? Help? This is the thing that I want to do: struct example{ const string str; //this(string str){ this.str = str; } string toString(){

Procedural drawing using ndslice

2016-02-11 Thread Claude via Digitalmars-d-learn
Hello, I come from the C world and try to do some procedural terrain generation, and I thought ndslice would help me to make things look clean, but I'm very new to those semantics and I need help. Here's my problem: I have a C-style rough implementation of a function drawing a disk into a

Re: Is this nogc? dmd and gdc disagree

2016-02-11 Thread rcorre via Digitalmars-d-learn
On Thursday, 11 February 2016 at 04:20:13 UTC, tsbockman wrote: On Thursday, 11 February 2016 at 03:09:51 UTC, rcorre wrote: I recently tried compiling enumap with GDC, and found that it disagrees with DMD on whether a function is @nogc. Here's a semi-reduced test-case: Here's an @nogc

Re: String joining an array of structs or class instances implementing toString?

2016-02-11 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Thursday, 11 February 2016 at 12:44:15 UTC, pineapple wrote: It feels like there should be an out-of-the box way to do this but I haven't been able to find it? Help? This is the thing that I want to do: struct example{ const string str; //this(string str){ this.str = str; }

Re: Is this nogc? dmd and gdc disagree

2016-02-11 Thread rcorre via Digitalmars-d-learn
On Thursday, 11 February 2016 at 12:41:16 UTC, rcorre wrote: On Thursday, 11 February 2016 at 04:20:13 UTC, tsbockman wrote: Using zip and slices guarantees that the structure returned will be only 5*size_t.sizeof bytes, regardless of the types of K and V. I'm on the DMD 2.070 release, so

Re: Does D optimize sqrt(2.0)?

2016-02-11 Thread Johannes Pfau via Digitalmars-d-learn
On Thursday, 11 February 2016 at 07:41:55 UTC, Enjoys Math wrote: If I just type out sqrt(2.0) in D, is that automatically made into a constant for me? Thanks. For GDC the answer is yes:

String joining an array of structs or class instances implementing toString?

2016-02-11 Thread pineapple via Digitalmars-d-learn
It feels like there should be an out-of-the box way to do this but I haven't been able to find it? Help? This is the thing that I want to do: struct example{ const string str; //this(string str){ this.str = str; } string toString(){ return this.str; } } public void

Re: String joining an array of structs or class instances implementing toString?

2016-02-11 Thread pineapple via Digitalmars-d-learn
On Thursday, 11 February 2016 at 12:53:20 UTC, Edwin van Leeuwen wrote: I'd do it like this: import std.algorithm : map; pars.map!((part) => part.toString) // Turn them to strings .join(" ").writeln; // Join them. Thanks! Does the map function iterate without constructing an extra list

Re: String joining an array of structs or class instances implementing toString?

2016-02-11 Thread Edwin van Leeuwen via Digitalmars-d-learn
On Thursday, 11 February 2016 at 13:43:49 UTC, pineapple wrote: Thanks! Does the map function iterate without constructing an extra list in-memory? Yes, it is lazy, so it only calls toString when the result is actually used (by the join call). In case you do need to create an extra list

Re: Is this nogc? dmd and gdc disagree

2016-02-11 Thread tsbockman via Digitalmars-d-learn
On Thursday, 11 February 2016 at 12:55:02 UTC, rcorre wrote: Though it appears (in 2.070 at least) that zip's range primitives aren't nogc: I took a look at the source code for `zip()`, and the cause of this deficiency is that `Zip` takes a `StoppingPolicy` as a runtime parameter, rather

Re: Procedural drawing using ndslice

2016-02-11 Thread John Colvin via Digitalmars-d-learn
On Thursday, 11 February 2016 at 13:05:41 UTC, Claude wrote: Hello, I come from the C world and try to do some procedural terrain generation, and I thought ndslice would help me to make things look clean, but I'm very new to those semantics and I need help. Here's my problem: I have a

Re: Procedural drawing using ndslice

2016-02-11 Thread Ali Çehreli via Digitalmars-d-learn
On 02/11/2016 05:05 AM, Claude wrote: Hello, I come from the C world and try to do some procedural terrain generation, and I thought ndslice would help me to make things look clean, but I'm very new to those semantics and I need help. Here's my problem: I have a C-style rough implementation of

Re: String joining an array of structs or class instances implementing toString?

2016-02-11 Thread Meta via Digitalmars-d-learn
On Thursday, 11 February 2016 at 12:53:20 UTC, Edwin van Leeuwen wrote: I'd do it like this: import std.algorithm : map; pars.map!((part) => part.toString) // Turn them to strings .join(" ").writeln; // Join them. You can shorten this by using std.conv.to. Also keep in mind that `join` will

Re: How to allocate arrays of objects?

2016-02-11 Thread Meta via Digitalmars-d-learn
On Thursday, 11 February 2016 at 09:02:30 UTC, Jacob Carlborg wrote: On 2016-02-11 05:34, Adam D. Ruppe wrote: On Thursday, 11 February 2016 at 04:31:12 UTC, cy wrote: as[0..$] = new A(); before accessing .stuff on as[0]. Loop through it and allocate each one rather than trying to do it in