allocate hash table manually
Jari-Matti Mäkelä
jmjmak at utu.fi.invalid
Wed Sep 6 07:29:54 PDT 2006
Mildred wrote:
> Le mar 05/09/2006 __ 20:01 Johan Granberg __ __crit:
>> I think this is what I have used.
>>
>> class Value {
>> ...
>> protected Value[char[]] _environment = null;
>> ...
>> public void env_make(Stack S){
>> Value[char[]] e;
>> _environment = e;
>> }
>> ...
>> }
>
> Thanks, that helps me.
>
> Isn't there a solution with the new keyword, it is the normal way to
> allocate on the heap, no ?
An empty hash is already "null" by default. Basically you're doing
something stupid here - see this example:
template Hash(K,V) {
V[K] Hash() {
V[K] tmp;
return tmp;
}
}
void main() {
int[int] a;
assert(a is null);
a[1] = 1;
assert(a !is null);
a = Hash!(int, int);
assert(a is null);
a[1] = 1;
assert(a !is null);
a = Hash!(int, int);
assert(a is null);
}
The compiler indicates a hash is empty by returning a null. Here Hash!
creates an empty hash and returns it - hash array literals can be
implemented in the same fashion.
More information about the Digitalmars-d
mailing list