new A().b() currently requires extra brackets/parentheses

Philippe Sigaud philippe.sigaud at gmail.com
Sun Sep 9 10:25:08 PDT 2012


On Sun, Sep 9, 2012 at 5:16 PM, Nick Treleaven <ntrel-public at yahoo.co.uk> wrote:

> Maybe you could use a template:
>
> auto create(T, Args...)(Args args)
> {
>     return new T(args);
> }
>
> class A
> {
>     int i;
>     this(int i){this.i = i;}
> }
>
> import std.stdio;
>
> void main()
> {
>     create!A(5).i.writeln();
> }

A generic 'create' template would be good for generic code: on a
reference type, use 'new' and pass args, whereas for a value type, use
the type directly. Of course, this should manage builtins (like int,
int[], int[3]) correctly.


More information about the Digitalmars-d mailing list