Perfect forwarding

Manu turkeyman at gmail.com
Thu Jul 30 01:31:59 UTC 2020


On Wed, Jul 29, 2020 at 10:35 PM Jean-Louis Leroy via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

> On Wednesday, 29 July 2020 at 10:38:29 UTC, Manu wrote:
> > On Wed, Jul 29, 2020 at 9:40 AM Jean-Louis Leroy via
> > Digitalmars-d < digitalmars-d at puremagic.com> wrote:
> >
> >> On Tuesday, 28 July 2020 at 22:48:16 UTC, Manu wrote:
> >> > It is my opinion that if the solution involves a text-mixin,
> >> > the author gets an instant FAIL.
> >>
> >> But:
> >>
> >> > The major hangup in this exercise is dealing with 'storage
> >> > class', which is
> >> > impossible because it's not part of the language, and
> >> > instantly
> >> > forces synthesising strings.
> >>
> >> Guaranteed failure then? ;-)
> >>
> >
> > Yes.
>
> This is a problem I spent a lot of time on while trying to
> support all the variations of functions in openmethods (and the
> problem is even harder because I need to alter the storage
> classes of a subset of the parameters).
>
> Doesn't this cut it?
>
> module challenge;
>
> template forward(alias fun)
> {
>      import std.format;
>      import std.traits;
>      enum name = __traits(identifier, fun);
>      static foreach (ovl; __traits(getOverloads, __traits(parent,
> fun), name)) {
>          mixin(q{
>                  auto ref %s(Parameters!ovl args) {
>                      return __traits(parent, fun).%s(args);
>                  }
>              }.format(name, name));
>      }
> }
>
> void myfun(int, ref double, out string);
>
> int myfun(in string, inout double);
>
> pragma(msg, typeof(__traits(getOverloads, forward!myfun,
> "myfun")[0]));
> pragma(msg, typeof(__traits(getOverloads, forward!myfun,
> "myfun")[1]));
>
> Output:
> void(int, ref double, out string)
> int(const(string), inout(double))
>
> This deals with storage classes all right.
>
> As for the string mixin, it would be nice to be able to do with
> it, or at least to narrow it to the function name, but in this
> respect D is more C than Lisp.
>
>
I mean... std.format in a forward macro? I find that embarrassing. Compile
times are important.  Of course, the usual casualties occur; debugging
steps through string mixins, etc.
Also, your solution doesn't perform any interesting transformation on
args... forwarding functions usually transform some argument(/s) in some
way, or inject additional arguments.
Hard and really lame to write any of the actual meat of the exercise inside
a string.

I'm not saying your solution is bad, just that the problem is bad, and it
really sucks that this thread exists. I've been complaining about this
since day-one. 'Storage class' is the problem as usual.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20200730/69c7d6a7/attachment.htm>


More information about the Digitalmars-d mailing list