Print string with multiple lines without identation

Is there some macro that allows me to write strings with multiple lines removing the identiation of the code? Meaning:

julia> println("function f(x)
                    y = x + 1
                    return y
                end")
function f(x)
            y = x + 1
            return y
         end

I would like that that printed what this prints:

julia> println("function f(x)
           y = x + 1
           return y
       end")
function f(x)
    y = x + 1
    return y
end

Use multiline string literals:

julia> println("""
               function f(x)
                   y = x + 1
                   return y
               end
               """)
function f(x)
    y = x + 1
    return y
end
5 Likes

Do you know what the rules are here? Does the minimum number of leading white spaces before the first non-whitespace character treated become the offset? I could imagine that example returning a string with lots of leading white space, but it doesn’t.

[…] triple-quoted strings are dedented to the level of the least-indented line […]

From Triple Quoted String Literals in the documentation.

3 Likes