Red's Take on String Interpolation

So Red is trying to attack string interpolation via a macro, which apparently can happen "before word binding" in some way.

That kind of ties their hands representationally because macros always are signaled by ISSUE!s, so the callsites that want to use string interpolation must have a macro.

>> probe expand-directives [#composite {"(player)" "(vfile)" --audio-file "(afile)"}]
[rejoin [{"} (player) {" "} (vfile) {" --audio-file "} (afile) {"}]]

Boris's remark about the usefulness of being able to do string interpolation in Rebol sums up probably how most of those who reject the idea at first would eventually come around:

can add that first time I saw Gregg's #composite my reaction was "why would we ever want that?". But then with every print or rejoin I was using in my code I had this thought "what if..". Then I started using my experimental implementation, and after some time I believe it's a total must have.

But I still think this should be approached as a binding question. It's very difficult to play with bricks in the "native" way that Rebol has been and overlay a predictable and useful idea of a "binding environment". And keeping an open mind about it is necessary.

2 Likes

Red is continuing their investment in a kind of string interpolation:

FEAT: String interpolation by hiiamboris · Pull Request #5085 · red/red · GitHub

They are defaulting to parentheses instead of $, and escaping parentheses with backslash (\on the inside, curiously\)

It's worth thinking about what a good default is, though ultimately I think it should be customizable (as with historical REWORD). I had been considering plain parentheses for the same reasons at first, but leaned to $ just because of how popular it is in other languages for interpolation.

My concept is different because it is not a hardcoded preprocessor behavior, but making binding information available on strings as a map of words to values. See "Rebol and Scopes: Well, why not?" for thoughts on that distinction and the challenges it presents.

1 Like

There's a pull request regarding this, where they give some good bad examples:

Here are just a few common examples written using rejoin function:

avcmd: rejoin [{"} player {" "} vfile {" --audio-file "} afile {"}]

print rejoin ["Download OK: '" remote 
    "' [" info/Content-Type ", " info/Content-Length " bytes]"]

do make error! rejoin [token1 " cannot follow " token2 
     " in the " name " part of " mold .mask]

Try to look at these expressions and visualize how the resulting string will look like, and if I've got all spaces and quotes right. Not a human task, eh?

1 Like