constructor is not callable using argument types ()

bearophile bearophileHUGS at lycos.com
Mon Dec 10 06:15:29 PST 2012


Suliman:

> But if I have a lot of property like^
> string name
> int age
> ....
>
> it's not good to specify all of them in paranceses:
> auto file = new FileName("test.txt", 21, ETC);
>
> How can I specify them in another way?

There are several ways to help that. A simple way is to use 
default arguments:

this(string fileName, int id=21, Foo f=ETC) {...}

An alternative is to define various constructors with different 
number of arguments.

Other solutions include the use of a little configuration struct, 
or the chaining of setters that return "this":

FileName setId(int id_) { this.id = id_; return this; }

Then if you define several of such setters, you can chain only 
the ones you want in a single line.

auto f = new FileName("test.txt");
f.setId(21).setF(ETC);

Probably there are other solutions.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list