On Monday, 29 March 2021 at 15:13:04 UTC, Gavin Ray wrote:
Brief question, is it possible to write this so that the "alias fn" here appears as the final argument?

auto my_func(alias fn)(string name, string description, auto otherthing)

The above seems to work, since the type of "fn" can vary and it gets called inside of "my_func", but from an ergonomics point I'd love to be able to put the function last and write it inline:

  my_func("name", "description", "otherthing", (x, y, z) {
    auto foo = 1;
    return foo + 2;
  })


Currently I am calling it like:

  auto some_func = (x, y, z) {
    auto foo = 1;
    return foo + 2;
  };

  my_func!some_func("name", "description", "otherthing");

Which is fine, it's just that from a readability perspective it doesn't really allow for writing the function inline and you need to declare it separately.

Not a huge deal, just learning and thought I would ask =)

Thank you!

Also with delegates (lazy), you get the type checks however you must have to declare parameters on call site, which can be PITA in the future when doing refactoring will be necessary.

Better plan ahead as the number of changes will explode when you make quite a number of these and decide to change params/returns.

```
  import std.stdio;

void my_func(T, XS)(string a, string b, string c, lazy T function(XS)[] t...)
  {
// call function, just the first one, can call all of them as well
    t[0](a);

// can get the result too, mind the future refactoring needs tho
    // T res = t[0]();
  }

  void main()
  {
    my_func("name", "description", "otherthing", (string x) {
      writeln(x);
      return x;
    });

    // no function, compile error
    // my_func("name", "description", "otherthing");
  }
```

Reply via email to