Nested Structs (Solution)

js.mdnq js_adddot+mdng at gmail.com
Wed Dec 12 16:58:25 PST 2012


On Thursday, 13 December 2012 at 00:37:16 UTC, Max Samukha wrote:
> On Thursday, 13 December 2012 at 00:02:01 UTC, js.mdnq wrote:
>>
>> Half done? Has it even been implemented at all?
>
> http://dlang.org/class.html#nested
>
> It is implemented for nested classes but not structs.
>
>>
>> In any case my method seems to provide a solution to the 
>> problem in the mean time. I have updated the code to work 
>> correct(had a small bug) and it uses an alias to make it 
>> easier to update in the future if D finally does support this 
>> construct in full.
>>
>> http://dpaste.dzfl.pl/64025e0a
>>
>> If outer is the appropriate keyword then Parent can easily be 
>> changed.
>
> The name doesn't matter. I just wanted to annoy Walter again 
> with yet another bit of evidence that completeness of a feature 
> is important even if it doesn't seem to have an obvious use 
> case. People will do what is logical and end up with hacks like 
> that.
>
>> Little would have to be done to the structs to make it work. 
>> In fact, the method should work if D ever decides to support 
>> such a feature with little change. Compile times could be 
>> increased though by removing the templating, but unfortunately 
>> that might require a lot of work. A mixin might simplify it.

Ok, maybe thats why my posts have not received any attention 
except by you ;)

Checking the sizes of classes, it seems that inner classes store 
a ptr to the outer class.

This is exactly what I am trying to avoid.

So, In fact, my method seems to save more space if I'm not 
mistake. Of course, they are not as safe since they don't keep 
the outer class ptr with them as they move around.

http://dpaste.dzfl.pl/b20e1412

It's strange that in my case the size of Q is 24 rather than 20 
on dpaste.

In any case F should be 8 rather than 12 which suggests it is 
storing outer as a ptr inside the class.

When using my code, the struct size is 1 (the size of an empty 
struct) and the class size is 20. Not sure why a nested class 
increases the size of the parent class on my setup but not 
dpaste, but regardless, obviously D is storing a ptr in the 
classes, which are not necessary in many cases.

I think I have an idea how to make it a bit more elegant that I 
will try and if it works I'll start using it in my code.


More information about the Digitalmars-d mailing list