Re: Templates for DRY code

2018-01-08 Thread Mark via Digitalmars-d-learn
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

Re: Templates for DRY code

2018-01-06 Thread codephantom via Digitalmars-d-learn
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

Re: Templates for DRY code

2018-01-06 Thread Paul Backus via Digitalmars-d-learn
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 ~=

Re: Templates for DRY code

2018-01-06 Thread codephantom via Digitalmars-d-learn
On Saturday, 6 January 2018 at 03:08:19 UTC, Ali Çehreli wrote: I agree with your point as well. A better name can help there a little. void ConvertAndAppend(T, S)(ref T[] arr, S s) { arr ~= s.to!T; } problem solved ;-) btw. I never thought that I would be able (or actually..willing)

Re: Templates for DRY code

2018-01-05 Thread codephantom via Digitalmars-d-learn
On Saturday, 6 January 2018 at 03:08:19 UTC, Ali Çehreli wrote: 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 Code is something that humans write and read (and read far more than write). So I

Re: Templates for DRY code

2018-01-05 Thread Ali Çehreli via Digitalmars-d-learn
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

Re: Templates for DRY code

2018-01-05 Thread codephantom via Digitalmars-d-learn
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.

Templates for DRY code

2018-01-05 Thread Ali Çehreli via Digitalmars-d-learn
Nothing new here... I'm just reminded of how templates can help with DRY (don't repeat yourself) code. Here is a piece of code that uses an alias to be flexible on the element type: import std.conv; alias MyType = double;// <-- Nice, MyType is used twice below void main() {