Use case for std.bind

Yigal Chripun yigal100 at gmail.com
Tue Feb 24 07:52:52 PST 2009


Daniel Keep wrote:
>>> auto a = b(_, c(_, x), _);
>>>
>>> What are the types of a, b and c?  What if b has multiple overloads or
>>> is templated?
>>>
>>> Context-dependant grammar (let alone context-dependant single character
>>> symbols): just say "no".
>>>
>>>    -- Daniel
>> Good point. How about this alternative syntax, then:
>>
>>    auto bar3 = foo(int, b, int, d);
>>
>> Will this also cause problems?
>>
>> -Lars
>
> This doesn't address the problem of what, exactly, the context of the
> curried arguments is.  What's more, you're putting a type where an
> expression is expected; I don't see how you get "currying" from
> "repeating an argument type."
>
> The problem I have with these suggestions are that you're basically
> arguing for an incredibly inflexible, context-dependant, completely
> unintuitive syntax for something you already have working syntax for.  I
> just don't see the point.
>
>    -- Daniel

given the following functions:
int foo(int a, int b) {..}
int bar(int x, int delegate(int) dg, int z) {..}

here's the example you gave above in current D2: (not tested)

auto temp = int(int a) { return foo(a, somevar); };
auto dg = int(int a, int c) { return bar(a, temp, c); };

or you can do it in one go:

auto dg = int(int a, int c) {
return bar(a, (int x){ return foo(x, somevar); }, c); };

surely using instead the following is much clearer:

auto dg = bar(_, foo(_, somevar), _);

all it does is generate the delegates for you.

what do you mean by "the context of the curried arguments"?

regarding your point on context-dependency: that is a good point indeed 
and can be solved by either using Lars' alternative syntax or by making 
ambiguities compile time errors.

given the following functions:

void foo(int x, int y) { ... }
void foo(float x, int y) { ... }

here's what happens:

auto dg = foo(_, 0); // compile time error - ambiguity
void delegate(int)   dg = foo(_, 0); // select first overload
void delegate(float) dg = foo(_, 0); // select second overload

this is similar to &foo only that if there are overloads of foo:
auto dg = &foo; // this IMHO shouldn't compile
IIRC, the current behavior is to use the first foo.

with your example, the compiler knows the signature of bar and can 
choose the correct overload of foo based on its type.
if there's more than one overload of bar than the programmer needs to 
specify the type of bar to disambiguate.




More information about the Digitalmars-d mailing list