Thread overview
Methods for expanding class in another class/struct
Sep 26, 2020
IGotD-
Sep 26, 2020
IGotD-
Sep 26, 2020
k2aj
Sep 27, 2020
IGotD-
September 26, 2020
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.
September 26, 2020
One thing that struck me looking at the source code of scoped, would scoped work inside a class and not only for stack allocations?


September 26, 2020
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();
    }
}
September 27, 2020
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?