How to declare "type of function, passed as an argument, which should have it's type inferred"? (or if D had an "any" type)

Gavin Ray user at example.com
Mon Mar 29 15:13:04 UTC 2021


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!


More information about the Digitalmars-d-learn mailing list