Templates for DRY code

codephantom me at noyb.com
Sun Jan 7 01:59:19 UTC 2018


On Saturday, 6 January 2018 at 23:32:42 UTC, Paul Backus wrote:
> On Saturday, 6 January 2018 at 03:38:35 UTC, codephantom wrote:
>> or even..
>>
>> a.append( s.to!ConvertToElementType(a) );
>>
>> That's not valid code of course, but the semantics are all 
>> contained in that single chunk.
>
> This works:
>
> import std.range.primitives: ElementType;
>
> a ~= s.to!(ElementType!(typeof(a)));

I guess this brings us back to Ali's point about finding the 
balance between being explicit and fully automatic.

I certainly prefer:
  a.append(s);
vs
  a ~= s.to!(ElementType!(typeof(a)));  // this hurts by brain ;-)

The only problem with the Ali's suggestion of using append, is we 
always bring background knowledge everytime we read and write 
code, and, we all know that you cannot append a string to an 
array of doubles (that is our background knowledge).

I guess if we knew that you can do that now, then 'a.append(s);' 
would be just fine.

But I'm not a big fan of 'implicit' conversions in type safe 
languages, even when those conversion are type safe. So there's 
yet another balance to get right.



More information about the Digitalmars-d-learn mailing list