Julia: Make Function a parametric type

Created on 13 Jan 2019  路  2Comments  路  Source: JuliaLang/julia

Currently, every function f has its own type that is displayed as typeof(f) and is the only possible value for its type. This arrangement seems overly granular to me. One place where this causes issue is executing subtypes(Function): on my machine, it ran for > 2min, after which I gave up and interrupted the execution.

To address this, I propose to make Function a parametric type. Something like

abstract type AbstractFunction end
struct Function{f} <: AbstractFunction end

or better yet simply

abstract type Function{f} end

this would solve the current problem with subtypes(Function) (while this is relatively a minor annoyance, I suspect there are more benefits to the proposal than I currently am able to recognize). With this, we also can define

typeof(f::Function) = Function{f}

that would return

julia> typeof(sin)
Function{sin}

which looks nicer to me than the current

julia> typeof(sin)
typeof(sin)

All 2 comments

Not in the order of importance.

  1. Function types are not all singleton types so Function{f} is not always a valid type.
  2. Why are you using subtypes(Function) to begin with? If you are using it to see what are all the types, then you are simply suggesting making it much harder to do while having no effect on simplfying returning the result you want. If you don't actually need the result then, well, changing this won't have any effect on what you want to do.
  3. The type must be defined before the object. typeof must not be circular. If you are talking about a overridable (generic) function, then you can do that just fine by defining your own function and type. It'll work for any singleton functions and doesn't conflict with the current design.

    julia> struct A{f} end
    
    julia> A{sin}
    A{sin}
    

In general, typeof (and the type hierarchy) is not designed based on the reasoning you have here which is why the proposal here doesn't make much sense (mostly I'm refering to the circular definition part). FWIW, the Function type doesn't serve much purpose anymore. Any dispatch, which is what types are designed for, that relies on Function are most likely wrong due to being too specific. They should usually accept non-function callable objects too.

There are other reasons parametric function type may be wanted (https://github.com/JuliaLang/julia/issues/17168). However, the fact that functions in julia is a collection of largely independent methods makes this hard to do. Any discussion of other related advantage of adding type parameters to Function or Method should go there instead.

typeof(f::Function) can't just return whatever you want; it has to return a DataType that describes the layout, as for all values. Closures are general data structures with fields and type parameters like other objects.

subtypes(Function) might be too slow, and perhaps we should address that directly. I suppose under this proposal it would return an empty array? Ok, that would be fast, but why would it be useful? I don't see why it solves anything.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tkoolen picture tkoolen  路  3Comments

TotalVerb picture TotalVerb  路  3Comments

yurivish picture yurivish  路  3Comments

wilburtownsend picture wilburtownsend  路  3Comments

m-j-w picture m-j-w  路  3Comments