Re: Methods for expanding class in another class/struct

2020-09-27 Thread IGotD- via Digitalmars-d-learn

On Saturday, 26 September 2020 at 11:30:23 UTC, k2aj wrote:


It does work, the problem is that scoped returns a Voldemort 
type, so you have to use 
typeof(scoped!SomeClass(someConstructorArgs)) to declare a 
field. Gets really annoying when doing it with any class that 
doesn't have a zero-argument constructor, especially in generic 
code.


class Foo {}

class Bar {
typeof(scoped!Foo()) foo;
this() {
foo = scoped!Foo();
}
}


Thanks, so what we really need is a new scope template that 
declares the the variables in the class and possible a template 
for running the constructor?


Re: Methods for expanding class in another class/struct

2020-09-26 Thread k2aj via Digitalmars-d-learn

On Saturday, 26 September 2020 at 10:26:15 UTC, IGotD- wrote:
One thing that struck me looking at the source code of scoped, 
would scoped work inside a class and not only for stack 
allocations?


It does work, the problem is that scoped returns a Voldemort 
type, so you have to use 
typeof(scoped!SomeClass(someConstructorArgs)) to declare a field. 
Gets really annoying when doing it with any class that doesn't 
have a zero-argument constructor, especially in generic code.


class Foo {}

class Bar {
typeof(scoped!Foo()) foo;
this() {
foo = scoped!Foo();
}
}


Re: Methods for expanding class in another class/struct

2020-09-26 Thread IGotD- via Digitalmars-d-learn
One thing that struck me looking at the source code of scoped, 
would scoped work inside a class and not only for stack 
allocations?





Methods for expanding class in another class/struct

2020-09-26 Thread IGotD- via Digitalmars-d-learn
We know that classes are all reference typed so that classes must 
be allocated on the heap. However, this memory could be taken 
from anywhere so basically this memory could be a static array 
inside the class. This is pretty much what the scoped template 
does when allocating a class on the stack. In practice allocating 
a class inside another class could be done in similar fashion.


Do we have a good example showing how to expand a class inside 
another class?
Shouldn't be have a standard template similar to scoped, that 
statically allocates space inside the class? This template should 
be available in the standard library.