Make code_llvm return an object that overloads show
Motivation is i want clipboard(@code_llvm foo()) to work. Which it currently doesn't as @code_llvm foo() returns nothing and just prints directly.
I suspect that the code_* functions are older than our current show infrastructure.
And they don't follow its conventions, that rather than printing directly, prefer returning objects that overload show.
This fixes it for @code_llvm, while leaving in place the methods that take an IO as the first argument
I wonder if we might change the fields of LLVMCode in the future. Thus my suggestions follow.
- Clearly document that the internal fields of
LLVMCodeare not public API. - Provide a
convert(String, ::LLVMCode)andString(::LLVMCode)methods as a public API to obtain a string representation ofLLVMCode.
I suspect that the code_* functions are older than our current show infrastructure.
Not really, no, they are likely newer. These functions are similar to much of the infrastructure for expressing show in specific formats, such as Base.Ryu.writefixed or join or dump or such. The oddballs are really the other code_typed/code_lowered functions, since (unlike these and code_warntype), they intermix both IO formatting questions (e.g. debuginfo flags) and computational questions (e.g. optimize=false) as they return objects with custom show methods.
I wonder if we might change the fields of LLVMCode in the future.
Unlikely. Note that this is InteractiveUtils.LLVMCode, not part of Core.Compiler, nor part of Base's reflection functionality.
Odds are if we were to do something more complex than a string, it would be there. and If we did we would likely rename this type to avoid confusion.
- Clearly document that the internal fields of LLVMCode are not public API.
All internal fields are not public API unless documented otherwise. This type is internal and not public.
- Provide a convert(String, ::LLVMCode) and String(::LLVMCode) methods as a public API to obtain a string representation of LLVMCode.
string(::LLVMCode) and repr(::LLVMCode) should already work to do that (you get them for free by implementing show).
and those are already defined as the way to get string representations of things.
and those are already defined as the way to get string representations of things
That seems not quite a correct string representation then, since the result of show should usually be somewhat round-trip capable. So the show would typically be LLVMCode("llvm_module\ngoes\nhere\nunreadable....skipping 1234 bytes...{0} = {1}")
To me, this looks more like a deficiency with clipboard than with all methods that output things to an IO.
Maybe we need a version of macro version @sprint, so it can be written as clipboard(@sprint @code_llvm foo()) ?
When using the function form of IO functions such as code_llvm, you would typically write this: clipboard(sprint(code_llvm, foo, ()))
As an alternative solution, would it make sense to allow for an optional io=BLA option to @code_llvm etc.?