I have two functions
"This is my docstring for `add_1(x)`"
add_1(x) = x + 1
"This is my docstring for `add_2(x)`"
add_2(x) = x + 2
The code of the two functions is strikingly similar, so that in Julia there must be a clever way of automating their creation.
Side question: If these functions were generated programmatically, how would errors work out? Usually the stack trace shows a line number in the originating file, but here several functions are generated from the same line. Would it always show the same line number as well?
One solution would be this
["""
"This is my docstring for \`add_$i(x)\`"
add_$i(x) = x + $i
""" for i ∈ 1:3] .|> Meta.parse .|> eval
Regarding the line numbers, however, I am stuck. This is what you get by deliberately causing an error:
julia> add_3("arstoen")
ERROR: MethodError: no method matching +(::String, ::Int64)
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
+(::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:87
+(::LinearAlgebra.UniformScaling, ::Number) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/uniformscaling.jl:145
...
Stacktrace:
[1] add_3(x::String)
@ Main ./none:2
[2] top-level scope
@ REPL[8]:1
Does anyone else know a way of making the stacktrace sensible?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With