Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
September 05, 2006 allocate hash table manually | ||||
---|---|---|---|---|
| ||||
Hi,
I'm a new user od D language but immediately, I felt that was the
language I always looking for. So I'm very happy to use it.
I'm trying to convert my recent project to C (using a garbage collector
as library) to D. It's a script language with a lisp syntax.
My question is how do I use new to allocate a hash table. My code is as follows :
class Value {
...
protected Value[char[]] _environment = null;
...
public void env_make(Stack S){
_environment = new Value[char[]];
}
...
}
But the compiler says : need size of rightmost array, not type char[]
My problem is that I want some Value objects to have an environment,
and I want others objects without environment (so I use null).
How can I do that ?
Thanks
Mildred
--
Mildred <xmpp:mildred@jabber.fr> <http://mildred632.free.fr/> Clef GPG : <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key> Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
|
September 05, 2006 Re: allocate hash table manually | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mildred | Mildred wrote:
> Hi,
>
> I'm a new user od D language but immediately, I felt that was the
> language I always looking for. So I'm very happy to use it.
> I'm trying to convert my recent project to C (using a garbage collector
> as library) to D. It's a script language with a lisp syntax.
>
> My question is how do I use new to allocate a hash table. My code is as
> follows :
>
> class Value {
> ...
> protected Value[char[]] _environment = null;
> ...
> public void env_make(Stack S){
> _environment = new Value[char[]];
> }
> ...
> }
>
> But the compiler says : need size of rightmost array, not type char[]
>
> My problem is that I want some Value objects to have an environment,
> and I want others objects without environment (so I use null).
> How can I do that ?
>
> Thanks
> Mildred
>
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;
}
...
}
|
September 06, 2006 Re: allocate hash table manually | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Granberg | 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 ? -- Mildred <xmpp:mildred@jabber.fr> <http://mildred632.free.fr/> Clef GPG : <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key> Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B] |
September 06, 2006 Re: allocate hash table manually | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mildred | 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.
|
September 06, 2006 Re: allocate hash table manually | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jari-Matti Mäkelä | Jari-Matti Mäkelä wrote:
> An empty hash is already "null" by default. Basically you're doing something stupid here
<snip>
Sorry, I was a bit rude. I always rely on the implicit initialization a bit too much. I hope the Hash-template will give the functionality you were looking for.
|
September 06, 2006 Re: allocate hash table manually | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jari-Matti Mäkelä | Jari-Matti Mäkelä wrote: > 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); > } Dammit, here's an optimized version (stupid me): > void main() { > int[int] a; > assert(a is null); > a[1] = 1; > assert(a !is null); > a = null; // "creates" a new hash > assert(a is null); > a[1] = 1; > assert(a !is null); > a = null; // "creates" yet another new hash > assert(a is null); > } |
Copyright © 1999-2021 by the D Language Foundation