Function subtypes

Hmm… :thinking:. How about leaving things more like your original macro:

abstract type MyAbstractType end

@singleton foo isa MyAbstractType

where the @singleton expands to

struct var"#foo" <: MyAbstractType end
const foo = var"#foo"()

Then it would be just a matter adjusting Base.show methods to nicely pretty-print things depending on whether the singleton instance isa Function or not.

julia> foo
foo (singleton)

julia> typeof(foo)   # internally var"#foo", but that's transparent to the user
typeof(foo)

julia> abstract type MyFuncType <: Function end

julia> @singleton bar <: MyFuncType
bar (generic function with 0 methods)

julia> typeof(bar)  # internally var"#bar"
typeof(bar)

This would not require any change to the language itself—only a new macro and adjustment of Base.show.

1 Like

You’re right. A macro makes more sense. I’m gonna edit my original answer (where the macro is defined) to use isa instead of <:, which i also think its better, and omit the Base.show definition.

1 Like