Hmm… . 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
.