Use case for std.bind

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Feb 25 12:54:48 PST 2009


Denis Koroskin wrote:
> On Wed, 25 Feb 2009 17:19:30 +0300, Andrei Alexandrescu 
>> curry!(fun)(42)
>>
>> instead of the literals above.
> 
> I don't want to offend anyone, but this syntax sucks badly.
> 
> Just compare:
> 
> auto x = (int x) { return fun(42, x); };
> auto x = makeDelegate(curry!(fun)(42));
> 
> And you say that your solution is /shorter/? You must be kidding! 

Of course it is shorter. You seldom need makeDelegate. And the more 
parameters you add to the mix, the shorter it becomes.

> Delegate is THE way to pass closures in D, not some functional object 
> that has opCall().

Who told you that lied.

> Besides, I want my methods to be virtual and I can't 
> afford myself a template method that accepts functional objects of any 
> type.

Sure. Make a delegate out of it when you so need. Notice that *I* don't 
want to be shoehorned into making only virtual calls.


> bind is way *way* WAY more capable that what you propose as a 
> replacement. It allows parameter reordering, duplication etc with a 
> simple, uniform syntax:
> 
> auto x = bind(&fun, 42, _0);
> auto y = bind(&fun, _0, 42);
> auto z = bind(&fun, _1, _0);

I'm not sure how you can say that because you don't know about the 
replacement I want to propose. Even I don't fully know. I tried to 
launch a thread asking people for designs, met with a thundering silence 
(in contrast, ironically, there's been a lot of interest in creating 
ad-hoc syntax).

Anyway, I was thinking of:

auto x = bind!(fun, 0)(42);
auto y = bind!(fun, 1)(42);

Rewiring arguments would be the charter of a different functional.

> But it's not very intuitive and easy to read:
> 
> auto q = bind(&fun, _1, bind(&fun, bind(&fun, _0, 42), _1));

I agree :o). I wanted to come up with an equivalent, but couldn't 
actually figure what yours does. For example, where does the first 
parameter of the outer bind go? Did you mean _0 instead of the first _1?

> Besides, it is often ambiguous:
> 
> void foo(int x, int y);
> void foo(float x, int y);
> 
> auto x = makeDelegate(bind(&foo, _0, 42)); // error
> 
> With built-in D delegate syntax, there is no ambiguity and mistake:
> auto x = (int x) { foo(x, 42); };
> 
> That's *the best* syntax I can think of. Just keep it and let the 
> std.bind go. Don't waste your precious time of fixing something that 
> isn't broken (err.. std.bind IS broken, but that's not what I was 
> talking about :p)

Well no matter how the conversation went, I'm hugely drawn to a 
conclusion that has me do less work :o).

Andrei



More information about the Digitalmars-d mailing list