Container templates

Meta jared771 at gmail.com
Wed Feb 19 11:44:11 PST 2014


On Wednesday, 19 February 2014 at 19:10:44 UTC, Frustrated wrote:
> Are there container templates that one can mixin to classes that
> give them container behavior?
>
> e.g.,
>
> instead of
>
> class A
> {
>      Array!int x;
> }
>
> I want
>
> class A
> {
>     mixin Array!int;
> }
>
> so that I can do something like a.Add(3) instead of a.x.Add(3).

One solution is to use alias this.

class A
{
     Array!int x;
     alias x this;
}

Then you can do a.Add(3) and the method call will be "rewritten" 
(I don't know if it's *actually* rewritten) as a.x.Add(3).

>myints nor myfloats need to be actual elements of the class. In
>fact, in this case it might be ok to override them, e.g.,
>a.add(1) and a.add(5f) above.

This throws a wrench into the above solution, as you can 
currently only have 1 alias this. However, your idea of inner 
classes would work, I think.


More information about the Digitalmars-d-learn mailing list