I’ve seen a lot of style guides that suggest being sparing with closures. Which is concerning, because I use a ton of closures. Part of the motivation for that advice seems to be because stack traces are hard to learn from when they have elements on [5] (::var"#1#2")(::Float64)
or whatever in them.
I unfortunately need to make a lot of closures that are not at top level scope, so it isn’t super easy to make a normal struct and then dynamically add methods to it. But even inside my function bodies I can of course make anonymous structs and give them methods, like this:
test = (a=1, b="yes", c=1.0, f=log)
const MyStruct = typeof(test)
eval_f(s::MyStruct, x) = s.f(x)
I’m hoping that this may at least be used to get more information in stack traces. But when I call eval_f(test, -1.0)
, my stacktrace gives me:
ERROR: DomainError with -1.0:
log will only return a [...].
Stacktrace:
[...]
[4] eval_f(s::NamedTuple{(:a, :b, :c, :f), Tuple{Int64, String, Float64, typeof(log)}}, x::Float64)
@ Main ./REPL[4]:1
[...]
Is there any way for me to make this line in the stacktrace instead say eval_f(s::MyStruct, x::Float64)
for methods and type aliases that are not created at top level? It seems sort of unlikely, but I thought I’d ask.