if I check the type of an identifier which is a function:
julia> typeof(zero)
typeof(zero)
wouldn't generic function "zero"
be more helpful?
but that's not the type
OK - but I feel short-changed by getting typeof(zero)
echoed back. I don't think I got any more information...
if I do methods(zero)
I seem to get the full description, but it's more than I need. What should I be asking?
I guess that depends on what you want to know
Basically, when you type
function foo(x)
x + 1
end
that gets turned into something essentially equivalent to
struct var"typeof(foo)"
end
function (foo :: var"typeof(foo)")(x)
x + 1
end
const foo = var"typeof(foo)"()
all julia functions are just callable structs
julia, unlike Haskell, doesn't have the arguments to a function as part of the type signature
and when you define a closure, then the closed over data will end up as fields of that struct
e.g.
julia> let x = 1
f(y) = x + y
dump(f)
end
f (function of type var"#f#4"{Int64})
x: Int64 1
a "function" in julia is just the name - the individual things that have code in them are "methods", which are associated with a tuple of argument types
this means that the above closure was equivalent to writing
struct var"f#4"{T}
x::T
end
function (f :: var"f#4")(y)
f.x + y
end
f = var"f#4"(x)
That's going to take me a while to digest :-) - very interesting...
So what do we call the informational string I get sent when I create a method/function - looks like this (can't remember exactly) generic function with 2 methods
. Can I ask for that given the identifier?
I'm trying to avoid making unintended software jokes, like I did with zero
.
I'd guess a display string?
it's really just an informational piece of text
"generic" in this context means that you can add methods to it, if I'm not mistaken (and if I am, I'll be corrected probably)
@Sukera
help me solve the zero, one pun
dump
is a very useful function when you need to understand what something is
julia> dump(zero)
zero (function of type typeof(zero))
also, ?zero
in general, ?
followed by any symbol in the REPL will display the associated documentation (or an autogenerated one, if none exists)
Great - thanks.
I really appreciate this generous coaching !!
Peter Goodall has marked this topic as resolved.
@Mason Protter Now I get what you were showing me with closure/struct
Last updated: Nov 06 2024 at 04:40 UTC