# Instantiating function from type?

**URL:** <https://discourse.julialang.org/t/instantiating-function-from-type/107532>\
**Category:** General Usage\
**Tags:** type, function\
**Created:** [December 12, 2023, 10:37pm UTC](https://discourse.julialang.org/t/instantiating-function-from-type/107532 "2023-12-12T22:37:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 12, 2023, 10:37pm UTC](https://discourse.julialang.org/t/instantiating-function-from-type/107532/1 "2023-12-12T22:37:50Z")

</div>

Given the type `F` of a function, is there a good/documented way to instantiate the underlying function if possible?

`F()` doesn’t work, but I feel like it should:

```julia
julia> F = typeof(sin)
typeof(sin) (singleton type of function sin, subtype of Function)

julia> F()
ERROR: MethodError: no method matching typeof(sin)()
Stacktrace:
 [1] top-level scope
   @ REPL[52]:1

```

You can use the undocumented internal field `F.instance`:

```julia
julia> F.instance
sin (generic function with 14 methods)

```

so the best I can come up with is:

```julia
(::Type{F})() where {F<:Function} = Base.issingletontype(F) ? F.instance : throw(MethodError(F, ()))

```

Is there something better than `issingletontype` to determine whether a `Function` type corresponds to a generic function, as opposed to some other user-defined datatype?

(Obviously, this is type piracy … in a package this would better be named `functioninstance(F)` or something like that. But I’m thinking that something like this should go into Base.)

PS. On a related note, it feels like there should be a `GenericFunction <: Function` supertype for all `Function` types that come from bona-fide Julia functions, as opposed to user-defined functor types.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [December 12, 2023, 11:35pm UTC](https://discourse.julialang.org/t/instantiating-function-from-type/107532/2 "2023-12-12T23:35:29Z")

</div>

Not sure whether there exist other solutions, but what you suggest here is more or less the same as the solution provided by @Mason in this older thread:

> [@How to get a function instance from a function type?](https://discourse.julialang.org/t/how-to-get-a-function-instance-from-a-function-type/79585/2):
>
> No, because not all functions have a one-to-one relationship with their types. For instance, f = let x = 1 y -\> x + y end captures runtime data and cannot be reconstructed from it’s type alone. That said, in the case of functions which are singleton instances, you could do if isdefined(tf, :instance) ft.instance else error("This type does not have a single instance") end

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 12, 2023, 11:46pm UTC](https://discourse.julialang.org/t/instantiating-function-from-type/107532/3 "2023-12-12T23:46:17Z")

</div>

You can use incomplete initialization to get a valid instance of any singleton type, instead of using the undocumented `instance` field. I have a tiny package for it here: [Neven Sajko / InstanceOfSingletonType.jl · GitLab](https://gitlab.com/nsajko/InstanceOfSingletonType.jl). Registering it.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 12, 2023, 11:54pm UTC](https://discourse.julialang.org/t/instantiating-function-from-type/107532/4 "2023-12-12T23:54:01Z")

</div>

> [@stevengj](#):
>
> `F()` doesn’t work, but I feel like it should

Related thread:

> [@Constructors for \`\<:Function\` types when possible, like in C++](https://discourse.julialang.org/t/constructors-for-function-types-when-possible-like-in-c/86611):
>
> I was thinking about how C++20 [added](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0624r2.pdf) support for default constructors for stateless (with no captures) lambdas, meaning that nowadays all function object families may be default-constructible in C++. Then I realized that in Julia I can’t even do typeof(sqrt)(), or something like this: func(::F) where {F \<: Function} = F()(5.0) func(sqrt) # Or this func(::Type{F}) where {F \<: Function} = F()(5.0) func(typeof(sqrt)) Seems like passing “pure” functions as types, instead of as objects would make…

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 12, 2023, 11:56pm UTC](https://discourse.julialang.org/t/instantiating-function-from-type/107532/5 "2023-12-12T23:56:07Z")

</div>

There is [`Core.Compiler.singleton_type`](https://github.com/JuliaLang/julia/blob/d27ed8f34f0092671e6ca612ef3309d6d3d177c2/base/compiler/utilities.jl#L347-L357).

```julia
julia> const F = typeof(sin)
typeof(sin) (singleton type of function sin, subtype of Function)

julia> Core.Compiler.singleton_type(F)
sin (generic function with 14 methods)

```
