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

Nick Treleaven ntrel-public at yahoo.co.uk
Mon Sep 10 10:38:19 PDT 2012


On 09/09/2012 18:25, Philippe Sigaud wrote:
> 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.

I assumed he only needed class construction. I forgot that we already 
have std.container.make (which also works with structs):

make!A(5).i.writeln();

I noticed jmdavis has a pull request for what you describe:
https://github.com/D-Programming-Language/phobos/pull/756


More information about the Digitalmars-d mailing list