# How to create a non-standard string literal with parameters in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037>\
**Category:** New to Julia\
**Tags:** macros, metaprogramming\
**Created:** [January 16, 2023, 6:07pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037 "2023-01-16T18:07:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flavio\_Barros](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/flavio_barros/32/45443_2.png) [@Flavio\_Barros](https://discourse.julialang.org/u/Flavio_Barros)\
**Post date:** [January 16, 2023, 6:07pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037/1 "2023-01-16T18:07:19Z")

</div>

Can someone answer my question o Stack Overflow: [How to create a non-standard string literal with parameters in Julia?](https://stackoverflow.com/questions/75137954/how-to-create-a-non-standard-string-literal-with-parameters-in-julia)

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 16, 2023, 6:14pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037/2 "2023-01-16T18:14:20Z")

</div>

how would you feel about putting the parameters inside the string with a special delimiter? so instead of

```julia
R1"abc"
R234"abc"

```

You’d have, for example choosing delimiter `:`,

```julia
R"1$:abc"
R"234$:abc"

```

---

<div class="post-metadata">

**Author:** ![Flavio\_Barros](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/flavio_barros/32/45443_2.png) [@Flavio\_Barros](https://discourse.julialang.org/u/Flavio_Barros)\
**Post date:** [January 16, 2023, 6:17pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037/3 "2023-01-16T18:17:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 16, 2023, 6:22pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037/4 "2023-01-16T18:22:03Z")

</div>

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

```julia
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

```julia
plus1(x) = ...
plus2(x) = ...
plus234(x) = ...

```

Instead of just `plus(x,y)`

---

<div class="post-metadata">

**Author:** ![Flavio\_Barros](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/flavio_barros/32/45443_2.png) [@Flavio\_Barros](https://discourse.julialang.org/u/Flavio_Barros)\
**Post date:** [January 16, 2023, 6:24pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037/5 "2023-01-16T18:24:55Z")

</div>

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:

```julia
R1"abc"
R34"abc
R244"abc"

```

and I would have to create all macro definitions one by one…

The sugestion was:

```julia
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"

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 16, 2023, 6:40pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037/6 "2023-01-16T18:40:58Z")

</div>

> [@Flavio\_Barros](#):
>
> I have a suite of tests with random integers like … and I would have to create all macro definitions one by one…

You can use metaprogramming for your tests, e.g.

```julia
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 `s` and `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.)

---

<div class="post-metadata">

**Author:** ![Flavio\_Barros](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/flavio_barros/32/45443_2.png) [@Flavio\_Barros](https://discourse.julialang.org/u/Flavio_Barros)\
**Post date:** [January 16, 2023, 6:45pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037/7 "2023-01-16T18:45:33Z")

</div>

> [@adienes](#):
>
> ```julia
> macro R1_str ... end
> macro R2_str ... end
> ...
> macro R234_str ... end
> 
> ```

@stevengj you mean If I use integers as flags, right?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 16, 2023, 6:46pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037/8 "2023-01-16T18:46:00Z")

</div>

> [@Flavio\_Barros](#):
>
> @stevengj you mean If I use integers as flag right?

Right, so `R"abc"13` etcetera.

---

<div class="post-metadata">

**Author:** ![Flavio\_Barros](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/flavio_barros/32/45443_2.png) [@Flavio\_Barros](https://discourse.julialang.org/u/Flavio_Barros)\
**Post date:** [January 16, 2023, 7:29pm UTC](https://discourse.julialang.org/t/how-to-create-a-non-standard-string-literal-with-parameters-in-julia/93037/9 "2023-01-16T19:29:59Z")

</div>

I found a solution:

```julia
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.
