Fengyang Wang
Fengyang Wang

Reputation: 12051

String macro with trailing backslash \

Is it possible to pass a single trailing \ character to a string macro?

macro test_str(s)
    s
end

test"\\"  # results in \\, that is two backslashes
test"\"   # does not parse ... the " is treated as escaped

Upvotes: 2

Views: 129

Answers (2)

Fengyang Wang
Fengyang Wang

Reputation: 12051

One way would be to implement the functionality as part of the string macro itself. Ignoring performance, an easy way to do that is just replace(s, "\\\\", "\\").

macro test_str(s)
    replace(s, "\\\\", "\\")
end

Then

julia> test"\\"
"\\"

is indeed a single backslash.

Upvotes: 1

Frames Catherine White
Frames Catherine White

Reputation: 28192

It is a work around, but you could invoke the macro directly -- as a macro rather than as a string macro

@test_str("\\") works fine.

Upvotes: 5

Related Questions