proposal: a new string litteral to embed variables in a string

Jacob Carlborg doob at me.com
Thu Nov 7 12:18:39 PST 2013


On 2013-11-07 12:05, Timothee Cour wrote:

> I would love to have AST macros too, but how do they help for the
> problem at hand? (eg use cases in OT + DSL). Like others mentioned, the
> feature is not crazy, it is present in most languages.

If AST macros worked like my vision [1] then it can be done by doing 
something like this. The declaration of the macro would look like this:

macro interpolate (Context context, Ast!(string) str)

* Scan "str" for occurrences of $variableName and $(expression) or 
whatever syntax is chosen

* Replace all occurrences with "%s"

* Create the following AST:

format(replacedStr, variableName, expression);

Use it like:

int a = 3;
int b = 4;

auto str = interpolate("a=$a b=$b");

Will generate the same AST as:

auto str = format("a=%s b=%s", a, b);

[1] https://dl.dropboxusercontent.com/u/18386187/ast_macros.html

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list