Stream: helpdesk (published)

Topic: ✔ typeof(aFunction)


view this post on Zulip Peter Goodall (Nov 16 2021 at 20:10):

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?

view this post on Zulip Sebastian Pfitzner (Nov 16 2021 at 20:10):

but that's not the type

view this post on Zulip Peter Goodall (Nov 16 2021 at 20:18):

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?

view this post on Zulip Sukera (Nov 16 2021 at 20:20):

I guess that depends on what you want to know

view this post on Zulip Mason Protter (Nov 16 2021 at 20:20):

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)"()

view this post on Zulip Mason Protter (Nov 16 2021 at 20:20):

all julia functions are just callable structs

view this post on Zulip Sukera (Nov 16 2021 at 20:20):

julia, unlike Haskell, doesn't have the arguments to a function as part of the type signature

view this post on Zulip Mason Protter (Nov 16 2021 at 20:20):

and when you define a closure, then the closed over data will end up as fields of that struct

view this post on Zulip Mason Protter (Nov 16 2021 at 20:21):

e.g.

julia> let x = 1
           f(y) = x + y
           dump(f)
       end
f (function of type var"#f#4"{Int64})
  x: Int64 1

view this post on Zulip Sukera (Nov 16 2021 at 20:21):

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

view this post on Zulip Mason Protter (Nov 16 2021 at 20:24):

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)

view this post on Zulip Peter Goodall (Nov 16 2021 at 20:27):

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.

view this post on Zulip Sukera (Nov 16 2021 at 20:28):

I'd guess a display string?

view this post on Zulip Sukera (Nov 16 2021 at 20:29):

it's really just an informational piece of text

view this post on Zulip Sukera (Nov 16 2021 at 20:29):

"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)

view this post on Zulip Peter Goodall (Nov 16 2021 at 20:29):

@Sukera
help me solve the zero, one pun

view this post on Zulip Mason Protter (Nov 16 2021 at 20:29):

dump is a very useful function when you need to understand what something is

julia> dump(zero)
zero (function of type typeof(zero))

view this post on Zulip Sukera (Nov 16 2021 at 20:30):

also, ?zero

view this post on Zulip Sukera (Nov 16 2021 at 20:30):

in general, ? followed by any symbol in the REPL will display the associated documentation (or an autogenerated one, if none exists)

view this post on Zulip Peter Goodall (Nov 16 2021 at 20:30):

Great - thanks.
I really appreciate this generous coaching !!

view this post on Zulip Notification Bot (Nov 16 2021 at 20:31):

Peter Goodall has marked this topic as resolved.

view this post on Zulip Peter Goodall (Nov 16 2021 at 20:41):

@Mason Protter Now I get what you were showing me with closure/struct


Last updated: Nov 06 2024 at 04:40 UTC