| Thread overview |
|---|
December 25, 2014 Templates, constructors and default arguments | ||||
|---|---|---|---|---|
| ||||
I'm wondering how to best implement the following pattern: the constructor of a class has some required and some optional arguments; and one of the (optional) arguments also controls if any additional arguments should be passed.
A hypothetical/simplified example that I came up with: there's a Dataset class which requires size and rank to be set. Size is required; rank defaults to 1. There's also a "filebacked" boolean option that defaults to false; if specified, a bunch of additional arguments are available (like filename, mode, etc), some of which have default values. Ideally, I'd want to be able to construct it like this:
// "new Dataset" could instead be a static factory method like "Dataset.create"
// this is is purely hypothetical
new Dataset; // fails, size required
new Dataset(size); // filebacked=false, rank=1
new Dataset(size, rank); // filebacked=false
new Dataset(size, rank, "foo"); // fails, filename not applicable
new Dataset!false(size); // rank=1
new Dataset!true(size, rank); // fails, filename missing
new Dataset!true(size, rank, "foo"); // mode = "w+"
new Dataset!true(size, rank, "foo", "w+");
If the "filebacked" argument only affects construction of the object and not its runtime behaviour, creating subclasses to solve this seems somewhat wrong. In fact, this argument doesn't even have to be a compile-time value but for the sake of being able to catch errors at compile time it probably should be.
Templating the class like this
class Dataset(filebacked = false)
doesn't work since then "new Dataset(size)" is disallowed, in favor of "Dataset!()(size)".
Adding a template factory function with variadic arg tuple like so
Dataset create(bool filebacked = false, Args...)(uint size, uint rank = 1, Args args)
doesn't work either because of the presence of default parameters ("default argument expected for args").
I wonder if there's any way to hack around this?
| ||||
December 25, 2014 Re: Templates, constructors and default arguments | ||||
|---|---|---|---|---|
| ||||
Posted in reply to aldanor Attachments: | On Thu, 25 Dec 2014 02:07:51 +0000 aldanor via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > I'm wondering how to best implement the following pattern: the constructor of a class has some required and some optional arguments; and one of the (optional) arguments also controls if any additional arguments should be passed. > > A hypothetical/simplified example that I came up with: there's a Dataset class which requires size and rank to be set. Size is required; rank defaults to 1. There's also a "filebacked" boolean option that defaults to false; if specified, a bunch of additional arguments are available (like filename, mode, etc), some of which have default values. Ideally, I'd want to be able to construct it like this: > > // "new Dataset" could instead be a static factory method like > "Dataset.create" > // this is is purely hypothetical > new Dataset; // fails, size required > new Dataset(size); // filebacked=false, rank=1 > new Dataset(size, rank); // filebacked=false > new Dataset(size, rank, "foo"); // fails, filename not applicable > new Dataset!false(size); // rank=1 > new Dataset!true(size, rank); // fails, filename missing > new Dataset!true(size, rank, "foo"); // mode = "w+" > new Dataset!true(size, rank, "foo", "w+"); > > If the "filebacked" argument only affects construction of the object and not its runtime behaviour, creating subclasses to solve this seems somewhat wrong. In fact, this argument doesn't even have to be a compile-time value but for the sake of being able to catch errors at compile time it probably should be. > > Templating the class like this > > class Dataset(filebacked = false) > > doesn't work since then "new Dataset(size)" is disallowed, in favor of "Dataset!()(size)". > > Adding a template factory function with variadic arg tuple like so > > Dataset create(bool filebacked = false, Args...)(uint size, > uint rank = 1, Args args) > > doesn't work either because of the presence of default parameters ("default argument expected for args"). > > I wonder if there's any way to hack around this? you can create two or more constrained templates, for example? like this: import iv.writer; void create(bool filebacked : false) (usize rank) { writefln!"filebacked=false, rank=%s"(rank); } void create(bool filebacked : true) (usize rank, string fn, string mode="w+") { writefln!"filebacked=true, rank=%s; fn=%s; mode=%s"(rank, fn, mode); } void create (usize rank=1) { writef!"rank=%s : "(rank); create!false(rank); } void main () { create(); // "rank=1 : filebacked=false, rank=1" create(42); // "rank=42 : filebacked=false, rank=42" create!false(66); // "filebacked=false, rank=66" create!true(99, "t"); // "filebacked=true, rank=99; fn=t; mode=w+" } happy hacking! ;-) | |||
December 25, 2014 Re: Templates, constructors and default arguments | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Thursday, 25 December 2014 at 02:28:47 UTC, ketmar via Digitalmars-d-learn wrote:
> happy hacking! ;-)
Thanks once again! I think this mostly solves it. Would it be possible to somehow do the same trick with this()? (I guess due to having to write Type!() when default template arguments are omitted?)
| |||
December 25, 2014 Re: Templates, constructors and default arguments | ||||
|---|---|---|---|---|
| ||||
Posted in reply to aldanor Attachments: | On Thu, 25 Dec 2014 03:07:55 +0000 aldanor via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > On Thursday, 25 December 2014 at 02:28:47 UTC, ketmar via Digitalmars-d-learn wrote: > > happy hacking! ;-) > > Thanks once again! I think this mostly solves it. Would it be possible to somehow do the same trick with this()? (I guess due to having to write Type!() when default template arguments are omitted?) unfortunately, you can have templated constructors, but you can't instantiate them manually. ;-) i.e. class A { this(T) () { ... } } can be compiled, but you can't call that constructor. there is simply no syntax for it. you still can do templated constructors with type deducing though: class A { this(T) (T arg) { ... } } auto n = new A(42); // this will call this!int(42) auto n = new A("alice"); // this will call this!string("alice") auto n = new A(true); // this will call this!bool(true) but you can't write: auto n = new A!bool(false); // will not compile the only thing you can do is make constructors private and using fabric. alas. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply