# Function subtypes

**URL:** <https://discourse.julialang.org/t/function-subtypes/92292>\
**Category:** General Usage\
**Created:** [December 29, 2022, 9:11pm UTC](https://discourse.julialang.org/t/function-subtypes/92292 "2022-12-29T21:11:30Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [December 30, 2022, 6:09pm UTC](https://discourse.julialang.org/t/function-subtypes/92292/12 "2022-12-30T18:09:02Z")

</div>

> [@nchisholm](#):
>
> Still, it would be nice to see dedicated syntax for this sort of thing, e.g.,

See these discussion: [Shorthand syntax for defining functors - Internals & Design - Julia Programming Language (julialang.org)](https://discourse.julialang.org/t/shorthand-syntax-for-defining-functors/88380)

> [@nchisholm](#):
>
> I’ll try writing a macro to simplify things and post whatever I come up with here if I am successful…

I’ve actually have written a macro to do this before:

```julia
julia> macro singleton(expr)
           expr = expr::Expr
           @assert Meta.isexpr(expr, :call) && first(expr.args) == :isa "Not an `isa` expression"

           _, name, supertype = expr.args
           typename = gensym(name)

           return quote
               struct $typename <: $(esc(supertype)) end
               const $(esc(name)) = $typename()
               Base.@ __doc__ ($(esc(name)))
           end
       end
@singleton (macro with 1 method)

julia> abstract type MyType <: Function end

julia> begin
           """
           Singleton object `foo` acts as a function with special properties...
           """
           @singleton foo isa MyType
       end
foo

julia> foo() = println("Hello world!")
(::var"##foo#292") (generic function with 1 method)

julia> foo(name) = println("Hello $(name)!")
(::var"##foo#292") (generic function with 2 methods)

julia> foo()
Hello world!

julia> foo("Steve")
Hello Steve!

julia> foo isa MyType
true

julia> foo isa Function
true

help?> foo
search: foo floor pointer_from_objref OverflowError RoundFromZero unsafe_copyto! functionloc StackOverflowError

  Singleton object foo acts as a function with special properties...

```

## Edit:

- Use `isa` instead of `<:`
- Removed `Base.show` definition
- Add support for docstrings with `Base.@ __doc__ `

---

_[View the full topic](https://discourse.julialang.org/t/function-subtypes/92292)._
