Templates for DRY code

Ali Çehreli acehreli at yahoo.com
Sat Jan 6 03:08:19 UTC 2018


On 01/05/2018 06:14 PM, codephantom wrote:
 > On Saturday, 6 January 2018 at 01:33:11 UTC, Ali Çehreli wrote:
 >> One solution is to wrap ~= in a function template. Now the conversion
 >> is automatically made to the element type of the array:
 >> ...
 >> .....
 >> I think append() could be a part of std.array but I can't find
 >> anything like that in Phobos.
 >>
 >> Ali
 >
 > The problem with this, in my opinion, is that your template 'append' is
 > doing a conversion behind the scenes..ie. I wouldn't know that 'append'
 > actually means 'convert & then append'.
 >
 > When I read:
 >
 >   double[] a;
 >   string s = "1.2";
 >   a.append(s);
 >
 > I think to myself...wtf is going on here? How can you append a string to
 > an array of doubles?
 >
 > That's when I'd have go and find the append template and work out what
 > it is really doing.
 >

I agree with your point as well. A better name can help there a little.

It's hard to find a balance between fully explicit and fully automatic. 
I find myself going back and forth between those two extremes.

Ali



More information about the Digitalmars-d-learn mailing list