February 19, 2012 Re: When are associative arrays meant to throw a RangeError? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Murphy | On 19/02/2012 15:05, Daniel Murphy wrote:
> "Ben Davis"<entheh@cantab.net> wrote in message
> news:jhr0qf$24sj$1@digitalmars.com...
>> On 19/02/2012 03:31, Daniel Murphy wrote:
>>> Except for this magic initialization, AAs behave the same as classes - ie
>>> a
>>> reference type.
>>
>> That's not quite true, because 'length' is passed around by value
>> alongside the reference, leading to semantics you could never reproduce
>> with classes, unless I'm mistaken.
>
> AAs, not Arrays.
Ah, well then I did this test earlier:
int[string] assoc=null;
writefln("%s",assoc.length);
//prints 0
Why did that work?
|
February 19, 2012 Re: When are associative arrays meant to throw a RangeError? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Davis | The call is rewriten to _aa_len(aa) and checks for null. This can almost be done with a normal class, except the compiler inserts a null check into each member function, iirc. I guess that's another bit of magic that can't be handled simply. It can still be done with a struct: struct AAPimpl { AAImpl aa; size_t length() @property { if (!aa) return 0; return aa.length(); } } I expect something like this will end up being the solution. "Ben Davis" <entheh@cantab.net> wrote in message news:jhr4d1$2b4n$1@digitalmars.com... > On 19/02/2012 15:05, Daniel Murphy wrote: >> "Ben Davis"<entheh@cantab.net> wrote in message news:jhr0qf$24sj$1@digitalmars.com... >>> On 19/02/2012 03:31, Daniel Murphy wrote: >>>> Except for this magic initialization, AAs behave the same as classes - >>>> ie >>>> a >>>> reference type. >>> >>> That's not quite true, because 'length' is passed around by value alongside the reference, leading to semantics you could never reproduce with classes, unless I'm mistaken. >> >> AAs, not Arrays. > > Ah, well then I did this test earlier: > > int[string] assoc=null; > writefln("%s",assoc.length); > //prints 0 > > Why did that work? |
Copyright © 1999-2021 by the D Language Foundation