D as a prototyping language (for C/C++ projects)

Ary Borenszweig ary at esperanto.org.ar
Wed Feb 27 12:53:55 PST 2013


On 2/27/13 5:12 PM, H. S. Teoh wrote:
>> The most basic, non-templated implementation of "tap" would look like
>> this:
>>
>> Object tap (alias func) (Object o)
>> {
>>      func(o);
>>      return o;
>> }
>
> What would a templated version add to this functionality?
>
>
>> class Point
>> {
>>      int x;
>>      int y;
>> }
>>
>> Point createPoint ()
>> {
>>      return (new Point).tap!((p) { p.x = 3; p.y = 4 });
>> }
>
> I guess I'm skeptical about the value of using tap in this context,
> since you could just call the function on the object, then set its
> values, then return it. So this is just syntactic sugar.

Tap is nice when you want to print-debug something and you have a chain 
of calls:

auto foo = x.map!(...).reduce!(...).nWayUnion!(...);

Now something is not working correctly and you want to see what's after 
the "reduce!" step:

auto foo = x.map!(...).reduce!(...).tap!(r) { writefln(r); 
}).nWayUnion!(...);

Otherwise you'd have to break it in many lines and then put them back 
together.


More information about the Digitalmars-d mailing list