# Modify string interpolation

**URL:** <https://discourse.julialang.org/t/modify-string-interpolation/72904>\
**Category:** General Usage\
**Tags:** show, string-interpolation\
**Created:** [December 10, 2021, 5:38pm UTC](https://discourse.julialang.org/t/modify-string-interpolation/72904 "2021-12-10T17:38:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asmund\_Hjulstad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmund_hjulstad/32/31820_2.png) [@Asmund\_Hjulstad](https://discourse.julialang.org/u/Asmund_Hjulstad)\
**Post date:** [December 10, 2021, 5:38pm UTC](https://discourse.julialang.org/t/modify-string-interpolation/72904/1 "2021-12-10T17:38:00Z")

</div>

Is it possible to modify string interpolation using type-specific rules restricted to a specific context?

```julia
"""SELECT * WHERE {
?s <$(ns.aa. __str__ ())> ?o)
}
"""

```

outputs  
`"SELECT * WHERE {\n?s <http://example.org/aa> ?o)\n}\n"`

but it would be more fun if I could only write something like

sparql""“SELECT \* WHERE {  
?s $(ns.aa) ?o)  
}  
“””

Any thoughts on how to achieve this?

complete example below:

```julia
using PyCall

begin
	rdflib = pyimport_conda("rdflib","rdflib")
end;

begin
struct NameSpace
		_ns
		NameSpace(s::String) = new(pycall(rdflib.Namespace, PyObject, s))
end
	Base.getproperty(thens::NameSpace, s::Symbol) = 
		pycall(rdflib.Namespace.term, PyObject, getfield(thens, :_ns), string(s))
end

ns = NameSpace("http://example.org/")

s = """SELECT * WHERE {
?s <$(ns.aa. __str__ ())> ?o)
}
"""

println(s)

```

---

<div class="post-metadata">

**Author:** ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Post date:** [December 11, 2021, 11:56am UTC](https://discourse.julialang.org/t/modify-string-interpolation/72904/2 "2021-12-11T11:56:13Z")

</div>

Julia will use the [show mechanism](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing) to determine the string representation of your inserted variable during interpolation. So it is possible to define how certain types will be interpolated in your string. However i don’t no pycall therfore i don’t know about the type of `ns.aa`. You should own the type in order to define new `show` methods on it, otherwise this would be considered type piracy.

Here is a small example how to do this for your own types

```julia
julia> struct Foo
           x
       end
julia> f = Foo(4)
julia> Base.show(io::IO, f::Foo) = print(io, "This is my custom representation of $(f.x) wrapped in Foo")
julia> "longer string with interpolated $f"
"longer string with interpolated This is my custom representation of 4 wrapped in Foo"

```

PS: You can also define [non-standard string literals](https://docs.julialang.org/en/v1/manual/metaprogramming/#meta-non-standard-string-literals) such as `sparql"..."` where you can parse and modify the string however you want but I guess hooking into the show mechanism is the simpler solution…

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [December 11, 2021, 12:59pm UTC](https://discourse.julialang.org/t/modify-string-interpolation/72904/3 "2021-12-11T12:59:05Z")

</div>

I’m not sure where the `sparql` macro comes from, but my guess is that it doesn’t allow string interpolation, which seems to be what you want. This can be incorporated, but it requires some effort, as it isn’t the default behaviour. The code in [HypertextLiteral](https://docs.juliahub.com/HypertextLiteral/yIQgZ/0.1.0/#Content-Interpolation) might show you how.

---

<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:** [December 11, 2021, 2:00pm UTC](https://discourse.julialang.org/t/modify-string-interpolation/72904/4 "2021-12-11T14:00:11Z")

</div>

> [@Asmund\_Hjulstad](#):
>
> Is it possible to modify string interpolation using type-specific rules restricted to a specific context?

Yes. Two options:

1. Write a custom string macro `foo"..."`. Then you can interpret `$` any way you want. For example, PyCall does this for `py"..."` (defined [here](https://github.com/JuliaPy/PyCall.jl/blob/3c66d9ea6d6c2166e11dece15b12335c24648e59/src/pyeval.jl)) to make `$x` interpolate a value as a Python object rather than as a string.
2. Wrap a value in your own type, and define a custom `show(io, x)` method, since string interpolation uses `show` to convert values to strings.

---

<div class="post-metadata">

**Author:** ![Asmund\_Hjulstad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asmund_hjulstad/32/31820_2.png) [@Asmund\_Hjulstad](https://discourse.julialang.org/u/Asmund_Hjulstad)\
**Post date:** [December 12, 2021, 2:01pm UTC](https://discourse.julialang.org/t/modify-string-interpolation/72904/5 "2021-12-12T14:01:09Z")

</div>

There is no sparql macro yet, I was contemplating writing one. I am learning rdflib (python library) and Julia at the same time.
