Return (a + b, "17 + 25") instead of (a + b, "a + b") for #stringify(a + b)

let a = 17
let b = 25
I would like to get (a + b, "17 + 25") instead of (a + b, "a + b") for #stringify(a + b)

I would like to read a, b values dynamically in the macro plugin. Please suggest how to read a, b values dynamically.

Thanks in advance.

Hello @narayana,

That’s not possible. Remember that macros are a syntactic transformation at compile-time, not runtime. And at compiler time, we can’t know the values of a and b in general, for example consider if you had

let a = loadValueFromTheInternet()

What would a be in that case at compile time? The answer is: We don’t know because loadValueFromTheInternet() will only be executed when your program is run, not during compilation.

1 Like

Maybe a slightly different macro and library design could get the desired effect. How about a macro that performs a transformation that turns x + y into a closure using string interpolations, like { "\(x) + \(y)" }? There would however need to be some purely syntactic rule in the macro to decide what to interpolate, maybe only bare identifier declaration references or something like that.


Thanks Alex and Joe for the valuable information.