What function is called to convert a type to a string as part of string interpolation?

I’m currently using the following code

example_string = "$(logdata.level)"

to convert from a Base.CoreLogging.LogLevel to a String.

However I would guess that there is a function call which can be used instead to do this?

Just string, but you usually define the called print methods instead of string methods.

julia> Meta.@lower "$(logdata.level)"
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = logdata
│   %2 = Base.getproperty(%1, :level)
│   %3 = Base.string(%2)
└──      return %3
))))

But presumably I can’t do

print(logdata.level)

?

So it has to be String(logdata.level)? Sorry just slightly confused by your previous comment

Try in an interactive REPL. string and print’s docstrings in help mode also explains the print methods a bit better. It’ll explain your intuition that instantiating a String does not involve the 1-argument print to stdout.

It’s calling string(logdata.level), which (by default) calls print to an IOBuffer under the hood (and print in turn calls show by default).

There is also a String(x) constructor, but it is much more limited in what types x it supports — basically, just other strings and vectors of bytes, i.e. things which can be “losslessly” represented as a String. For example, you can do string(3) to get "3", but not String(3).

3 Likes

Do you happen to know where I can find the documentation for string as opposed to String? Thanks for the explanation

See the manual:

1 Like

Excellent!

1 Like