Use case for std.bind

Daniel Keep daniel.keep.lists at gmail.com
Tue Feb 24 07:27:18 PST 2009



Lars Kyllingstad wrote:
> 
> I'm on thin ice here, I know, but consider the following simple example:
> 
>   pure real foo(real a, real b, real c)
>   {
>     auto tmp = sqrt(b + c);
>     return tmp + a;
>   }
> 
>   auto bar = foo(_, x, y);
> 
> Since bar now carries with it the last two arguments, it should in
> principle only have to calculate the square root once, i.e. the last
> line above would be equivalent to something like
> 
>   struct Bar
>   {
>     auto tmp;
> 
>     pure real bar(real a) { return tmp + a; }
>   }
> 
>   auto bar = &Bar(sqrt(x+y)).bar;
> 
> 
> -Lars

There was a proposal made by Walter (or possibly Andrei) involving
partial specialisation.

pure real foo(real a, static real b, static real c)
{
  return a + sqrt(b + c);
}

void main()
{
  auto x = foo(1, 2, 3);
}

Would automagically become:

pure real foo_2_3(real a)
{
  return a + (value of sqrt(b+c), assuming sqrt was CTFE-enabled);
}

void main()
{
  auto x = foo_2_3(1);
}

Still waiting for it, though :)

  -- Daniel



More information about the Digitalmars-d mailing list