API

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Mon May 5 17:33:21 PDT 2014


On Tue, May 06, 2014 at 12:28:19AM +0000, bearophile via Digitalmars-d wrote:
> Andrei Alexandrescu:
> 
> >So I'm looking at creation functions and in particular creation
> >functions for arrays.
> >
> >1. Follow the new int[n] convention:
> >
> >auto a = allok.make!(int[])(42);
> >assert(a.length == 42);
> >assert(a.equal(repeat(0, 42));
> >
> >2. Follow the [ literal ] convention:
> >
> >auto a = allok.make!(int[])(42);
> >assert(a.length == 1);
> >assert(a[0] == 42);
> 
> Both cases are needed. Unfortunately we don't have named arguments in
> D, otherwise you can use the same name for both operations, using
> different arguments.
[...]

Adam's trick with a Length struct neatly solves this problem. :-)

	struct Length { size_t n; }
	auto a = allok.make!(int[])(Length(42), 1);
	... // etc

It's a very clever idea, and I like it!


T

-- 
In theory, software is implemented according to the design that has been carefully worked out beforehand. In practice, design documents are written after the fact to describe the sorry mess that has gone on before.


More information about the Digitalmars-d mailing list