rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Add :value macro capture designator

Open CAD97 opened this issue 4 years ago • 6 comments

Add a new macro_rules matcher, $name:value, with identical semantics to that of a function capture.

That is, it evaluates exactly once at the time of the macro call, and the macro manipulates a named temporary, identically to how a function call works. Importantly, this extends to the lifetime of temporaries, which is different from a macro expanding to let name = $name;.

Rendered

CAD97 avatar Apr 08 '21 02:04 CAD97

(You can view a rich diff in the Files Changed tab, but apparently only for edited files, not added ones. And I was excited to use a new way to make the rendered link...)

CAD97 avatar Apr 08 '21 02:04 CAD97

you probably wanted the file to be a .md file as opposed to what it currently is: a file with no extension. Also, the Rendered link should probably link to a file on a git branch, not to a particular commit, since then it will not need to be changed each time you push more commits.

programmerjake avatar Apr 08 '21 03:04 programmerjake

I think POV-Ray's macros may also be prior art: http://www.povray.org/documentation/3.7.0/r3_3.html#r3_3_2_8_3

programmerjake avatar Apr 08 '21 03:04 programmerjake

... I'm tired, obviously. I fixed the file extension 😅 and rendered link

CAD97 avatar Apr 08 '21 03:04 CAD97

Something I just realized: $:value (as written) forces the macro to evaluate as a block expression. While this is the intent w.r.t. passed in temporaries, it would be nice if $:value could also work for macros intended to be used in statement position (e.g. do not wrap their contents in another block and even potentially expand to let statements and/or items).

For statement position, it might have the correct behavior to bind to a let at the start of the expansion and call drop at the end of the expansion, but I'm not well-versed enough in temporary lifetime promotion to actually know. (I don't think so, as dropping the binding doesn't even do anything if the binding is a Copy reference.)

So while an expression-position expansion can be explained by the match trick, it's currently impossible (as I understand it) to both get fully correct temporary behavior that matches a function call, and expand in statement position.

CAD97 avatar Apr 09 '21 02:04 CAD97

This RFC has been largely rewritten to use the k#autoref behavior as in RFC #2442.

CAD97 avatar Apr 20 '22 23:04 CAD97

Closing this as I don't have the bandwidth to shepherd this. Anyone can feel free to borrow wording to revive this, if they're so inclined.

CAD97 avatar Mar 19 '23 00:03 CAD97