RejectedSoftware Forums

Sign up

How to use the ruby like string inline evaluation syntax

In vibe.d's diet templates it is possible to use

"result: #{funktion_call(variable)} " 

inside,
so that in the end

"result: "~function_call(variable)!toString~" " 

or something similar is evaluated.

How to use this in functionality in an other context?
Where in the vibe.d sources I can find this part of evaluation?

Any hint for me?
Regards mt.
ps. First I thought it is an D feature...

Re: How to use the ruby like string inline evaluation syntax

Am 31.01.2016 um 16:25 schrieb Martin Tschierschke:

In vibe.d's diet templates it is possible to use

 "result: #{funktion_call(variable)} "

inside,
so that in the end

 "result: "~function_call(variable)!toString~" "

or something similar is evaluated.

How to use this in functionality in an other context?
Where in the vibe.d sources I can find this part of evaluation?

Any hint for me?
Regards mt.
ps. First I thought it is an D feature...

It requires using a string mixin at the scope where the required
variables are accessible. A simple implementation:

import std.stdio;
import std.string;

// TODO: escape special characters properly with backslashes
string escape(string str) { return str; }

string writeInterpolated(string format)
{
    string ret;
    while (true) {
       auto idx = format.indexOf('{');
       if (idx < 0) {
          ret ~= "write(\""~format.escape()~"\");\n";
          break;
       }
       if (idx > 0) ret ~= "write(\""~format[0 .. idx].escape()~"\");\n";
       format = format[idx+1 .. $];
       auto eidx = format.indexOf('}');
       assert(eidx >= 0, "Missing closing '}');
       assert(eidx > 0, "Missing interpolation expression");
       ret ~= "write("~format[0 .. eidx]~");\n";
       format = format[eidx+1 .. $];
    }
    return ret ~ "writeln();";
}

void main()
{
    string w = "world";
    mixin(writeInterpolated("Hello, {w}!"));
}

Personally, I'm pretty happy with the printf inspired syntax that is
supported by format/writefln/formattedWrite. It also supports
positional arguments for things like i18n strings.

Re: How to use the ruby like string inline evaluation syntax

Thank you!

To convert the value of w to string, your mixin
uses write.

Should I use something with format:

 auto newstring = part ~ format("%s",w) ~ other_part;

or

 auto newstring =format("....part %s ...other..part",w);


to be sure to have an conversion to string available,
because I do not want to write?

Personally, I'm pretty happy with the printf inspired syntax that is
supported by format/writefln/formattedWrite. It also supports
positional arguments for things like i18n strings.

p.s. When I use tab in the Browser (Firefox latest@Ubuntu) while editing this form, the left edited part grows many folds.

Re: How to use the ruby like string inline evaluation syntax

Am 04.02.2016 um 16:25 schrieb Martin Tschierschke:

Thank you!

To convert the value of w to string, your mixin
uses write.

Should I use something with format:

  auto newstring = part ~ format("%s",w) ~ other_part;

or

  auto newstring =format("....part %s ...other..part",w);


to be sure to have an conversion to string available,
because I do not want to write?

I'd use the latter, as it would usually be more efficient. Since it's
run at CTFE though, this maight actually not the case, but it shouldn't
matter for such small strings.

Personally, I'm pretty happy with the printf inspired syntax that is
supported by format/writefln/formattedWrite. It also supports
positional arguments for things like i18n strings.

p.s. When I use tab in the Browser (Firefox latest@Ubuntu) while editing this form, the left edited part grows many folds.

Thanks, I've noticed that, too, after I finally fully switched over to
Linux for development tasks. I'll have to revisit the JavaScript code.