Can someone answer my question o Stack Overflow: How to create a non-standard string literal with parameters in Julia?
how would you feel about putting the parameters inside the string with a special delimiter? so instead of
You’d have, for example choosing delimiter
Thank you, @adienes, for instant help! But I’m looking for a way to do exactly that. If it’s not possible, I would accept as solution as well.
I’m not sure I’m qualified to say what is or is not “possible,” strictly speaking, but I do suspect what you are asking for is very difficult.
A string macro
R"..." is essentially the same as
@R_str("...") over a raw string. So what this would be is a way to programmatically generate definitions for
macro R1_str ... end macro R2_str ... end ... macro R234_str ... end
Which, as you can see, might end up being a lot of definitions. It would sort of be like defining
plus1(x) = ... plus2(x) = ... plus234(x) = ...
Instead of just
Yes, you got it. I would like to generate multiple definitions. A guy answered me on SO suggesting me using flags. I believe that is fine but doen’t solve my problem. I have a suite of tests with random integers like:
R1"abc" R34"abc R244"abc"
and I would have to create all macro definitions one by one…
The sugestion was:
julia> macro R_str(p,flag) rotate(flag, p) end @R_str (macro with 1 method) julia> R"hello"3 "llohe" julia> R"abc"1 "cab" julia> R"abc"244 "cab"
You can use metaprogramming for your tests, e.g.
for n in rand(1:200, 20) @eval @test @R_str("abc", $n) == rotate("abc", $n) end
macro R_str(s, n); rotate(s, n); end really seems like the idiomatic Julia solution here.
(The main utility of making this a string macro is for when
n are literals, so that the
rotate(s, n) call happens at compile-time. Otherwise you might as well just call
rotate(s, n) directly.)
@stevengj you mean If I use integers as flags, right?
I found a solution:
for n in 0:26 @eval macro $(Symbol(:R, n, :_str))(s) rotate($n, s) end end
I am sure that using flags is the best approach but this does exactly what I need.