Skip to content

Latest commit

 

History

History
49 lines (38 loc) · 1.38 KB

README.md

File metadata and controls

49 lines (38 loc) · 1.38 KB

StringInterpolation.jl

String interpolation is an awesome feature of Julia, but string interpolation for non-standard string literals is not automatic and requires significant boilerplate code to make it work.

This package simply ressurects an old Base method interp_parse and adds a macro @interpolate. For example:

julia> Pkg.clone("https://github.com/EricForgy/StringInterpolation.jl.git")
julia> using StringInterpolation
julia> x = "World"
julia> @interpolate "Hello \$x"
"Hello World"

Note the $ is escaped in the string we want to interpolate.

The intended use for this package is when building non-standard string literals. For example:

macro test_str(s)
    return quote
        str = @interpolate $s
        # Do what you want to do with interpolated string here.
        sprint(print,str)
    end
end

Example

The following non-standard string literal simply makes 3 copies of the interpolated string:

macro triple_str(s)
    return quote
        str = @interpolate $s
        sprint(print,str^3)
    end
end

Then, you can use the macro as follows:

julia> x = "World"; println(triple"Hello \$x\n")
Hello World
Hello World
Hello World