Re: const after initialization / pointers, references and values

2014-08-22 Thread Vicente via Digitalmars-d
First of all thanks for your replies, they are useful. @Philippe: A pure function is ok for initializing default_nodes but not for nodes because a pure function can't read a file. The static this has the problem it needs know the initializer at compile time but I wanted to choose the

Re: const after initialization / pointers, references and values

2014-08-22 Thread Vicente via Digitalmars-d
Indeed the ref can be applied to the return type: http://dlang.org/function.html#ref-functions So, does the following code copy any data from nodes? If that is the case this solution avoids the class storage, avoids pointers and nodes is encapsulated as read-only, that's great. The program I'm

Re: const after initialization / pointers, references and values

2014-08-22 Thread Wyatt via Digitalmars-d
On Friday, 22 August 2014 at 18:21:06 UTC, Vicente wrote: @Wyatt: Certainly ref parameters help a lot, but I'm trying to get a Node by returning (a reference to) it. Does the ref keyword apply to the return type? I poked it a bit and came out with this. I _think_ it's working as expected:

Re: const after initialization / pointers, references and values

2014-08-22 Thread Vicente via Digitalmars-d
On Friday, 22 August 2014 at 20:12:39 UTC, Wyatt wrote: I poked it a bit and came out with this. I _think_ it's working as expected: ... auto ref opSlice(){return nodes[];}; ... -Wyatt Assuming it's working as expected, that is exactly what I was looking for! But the following

Re: const after initialization / pointers, references and values

2014-08-22 Thread Vicente via Digitalmars-d
If the foreach loop is replaced by: foreach(ref node_ref; np) Then it works like a charm!

Re: const after initialization / pointers, references and values

2014-08-21 Thread Wyatt via Digitalmars-d
On Wednesday, 20 August 2014 at 21:26:55 UTC, Philippe Sigaud via Digitalmars-d wrote: If you want reference semantics but do not want to have pointers in your code, yes classes are your best choice. Alternatively, isn't this a good place to use ref parameters? Or is there some semantic I'm

Re: const after initialization / pointers, references and values

2014-08-21 Thread Philippe Sigaud via Digitalmars-d
On Wed, Aug 20, 2014 at 11:49 PM, Jakob Ovrum via Digitalmars-d digitalmars-d@puremagic.com wrote: Certainly the easiest, but I don't think it's always the best. If light-weightedness is desired, make the struct contain the reference, effectively making the struct a reference type Well, yes.

const after initialization / pointers, references and values

2014-08-20 Thread Vicente via Digitalmars-d
Hello, I have some questions on how to do a few things in D. Below is an example code on which the questions are based. I need read access to a big and complex (i.e.: nested) data structure of type Node. But first it needs to be initialized. That can be done with a default initializer or from a

Re: const after initialization / pointers, references and values

2014-08-20 Thread Philippe Sigaud via Digitalmars-d
After initialization there is no need to modify the data anymore. My first question is: can the data be changed to const once initialized? You can have const or immutable data, that can be initialized once. I tend to use pure functions to do that: struct Node { Node[] block;

Re: const after initialization / pointers, references and values

2014-08-20 Thread Jakob Ovrum via Digitalmars-d
On Wednesday, 20 August 2014 at 21:26:55 UTC, Philippe Sigaud via Digitalmars-d wrote: If you want reference semantics but do not want to have pointers in your code, yes classes are your best choice. Certainly the easiest, but I don't think it's always the best. If light-weightedness is