Immutability and other attributes, please review

Dmitry Olshansky dmitry.olsh at gmail.com
Fri Jun 15 03:01:35 PDT 2012


On 15.06.2012 12:50, Roman D. Boiko wrote:
> On Friday, 15 June 2012 at 08:31:08 UTC, dennis luehring wrote:
>> Am 15.06.2012 08:25, schrieb Jacob Carlborg:
>>> On 2012-06-14 17:32, Roman D. Boiko wrote:
>>>
>>>> I agree, just looking how to accomplish my goals. I decided to get rid
>>>> of casting, and will store everything on heap. I don't know how to
>>>> put a
>>>> variable of type float to the heap, and thought that it would be
>>>> nice to
>>>> allow the user to pass anything inside, but copy when that is not an
>>>> l-value.
>>>
>>> Do you really need to put a float on the heap? Just pass it around as a
>>> value type, it's not like it's a big struct.
>>>
>>
>> i think he needs to - else he encapsulate the float into an struct
>> which is then also on the heap
>>
>> (ast/whatever)
>> nodex -> float
>> nodey -> string
>> nodez -> int
>> nodew -> double
>>
>> etc.
>>
>> in this example a node can contain from n types values - how would you
>> solve that? or better what is the smallest(maybe fastest) representation
>>
>> there need to be a queryable tree in the end - so there is a need to
>> hold the values - yes he can copy by value - but into whom?
>>
>> in the end it will be an variant-like type (with members for each
>> type) on heap - whichs is not that good because that will cost much
>> more mem
>>
>> or something like an class FloatValue -> NodeValue -> Value
>> oop-hierachy whichs also will get onto the heap
>>
>> i would try to use something like an base-types pool for all the small
>> float,double,int,string etc values... and pointer to this pool - or
>> just use the heap :)
>
> Well, my case is actually more complicated:
> https://github.com/roman-d-boiko/dct/blob/master/fe/syntax.d
>
> Node is a struct that holds:
> * instances of Syntax **classes** (these are immutable AST nodes without
> identity; please don't confuse Node which is a struct and node which is
> a general term for part of a tree)
> * information about their positions of Syntax nodes in source code,
> * and links to parent Node instances.
>
Suggest you revisit toNode function  as it does return something strange :)

Just looking at first - last  lines:
pure nothrow auto toNode(R)(R range, immutable(Maybe!Node) parent) 
if(isInputRange!R && hasLength!R)

	....
		auto result = new Node[](range.length);
		foreach(e; range) result ~= toNode2(e);
		return cast(immutable) range; // <<<<---- WAT
	}

And no need to cast to immutable, compiler will infer things 
automatically since it's pure. (and correctly for both mutable/immutable)


-- 
Dmitry Olshansky


More information about the Digitalmars-d-learn mailing list