# Shorthand for dispatch types

**URL:** https://discourse.julialang.org/t/shorthand-for-dispatch-types/53631
**Category:** Internals & Design
**Tags:** proposal, type, dispatch
**Created:** [January 19, 2021, 6:40pm UTC](https://discourse.julialang.org/t/shorthand-for-dispatch-types/53631 "2021-01-19T18:40:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)
#### Post date: [January 19, 2021, 6:40pm UTC](https://discourse.julialang.org/t/shorthand-for-dispatch-types/53631/1 "2021-01-19T18:40:37Z")

</div>

What do you think of providing a shorthand for creating types that are defined just for dispatching to relevant methods. For example ([https://bkamins.github.io/julialang/2020/11/27/abt.html](https://bkamins.github.io/julialang/2020/11/27/abt.html)):

```julia
abstract type ABRule end
struct Greedy <: ABRule end
struct Thompson <: ABRule end
struct Unif <: ABRule end

```

or ([JuMP.jl/src/print.jl](https://JuMP))

```julia
abstract type PrintMode end
abstract type REPLMode <: PrintMode end
abstract type IJuliaMode <: PrintMode end

```

These two could be written down e.g. as:

```julia
structs Greedy | Thompson | Unif <: abstract type ABRule

```

and

```julia
abstract types REPLMode | IJuliaMode <: abstract type PrintMode

```

Here I used Haskell notation with `|` as delimiter. I guess a tuple is would be more fitting here. These shortcuts could also be provided by a macro. What do you think?

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [January 19, 2021, 7:15pm UTC](https://discourse.julialang.org/t/shorthand-for-dispatch-types/53631/2 "2021-01-19T19:15:20Z")

</div>

Yeah, I’ve thought about making a macro for this. Maybe call it `@struct_enum`…

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [January 20, 2021, 9:20pm UTC](https://discourse.julialang.org/t/shorthand-for-dispatch-types/53631/3 "2021-01-20T21:20:41Z")

</div>

It would probably make sense to use the same syntax as `@enum`, so you could write either

```julia
@struct_enum Fruit Apple Orange Kiwi

```

or

```julia
@struct_enum Fruit begin
    Apple
    Orange
    Kiwi
end

```

---

<div class="post-metadata">

### Author: ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)
#### Post date: [January 21, 2021, 7:34pm UTC](https://discourse.julialang.org/t/shorthand-for-dispatch-types/53631/4 "2021-01-21T19:34:54Z")

</div>

Then maybe just piggyback on @enum

```julia
@enum struct Greedy Thompson Unif <: ABRule

```

and

```julia
@enum abstract type REPLMode IJuliaMode <: PrintMode

```

but also

```julia
@enum struct begin
    Greedy 
    Thompson
    Unif 
end <: ABRule

```

Are there any guidelines on reusing macros in different contexts?

I think there needs to be a way for distinguishing between abstract and concrete types as I can see people use both.

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [January 21, 2021, 8:08pm UTC](https://discourse.julialang.org/t/shorthand-for-dispatch-types/53631/5 "2021-01-21T20:08:39Z")

</div>

Generally it’s not a good idea to overload other people’s macros. The arguments to macros are usually of type `Symbol` or `Expr` (or literal values of type `Int`, `String`, etc). It’s difficult to use multiple dispatch for macros in an effective and unambiguous way (unless you have control over all of the methods yourself).

You can see [here](https://github.com/JuliaLang/julia/blob/c242166ab0c093907434c145e1f3522f7af9469e/base/Enums.jl#L123) that the `@enum` macro only has one method defined.

I personally don’t think that a `@struct_enum` macro would need to support abstract types. Normally if you’re switching on a type, you’re using singletons that can be instantiated. For example, you might do something like this:

```julia
struct A end
struct B end

foo(x, ::A) = x + 1
foo(x, ::B) = x + 2

foo(1, A())
foo(1, B())

```

You can’t instantiate abstract types, so if you want to switch on abstract types, you would have to write it like this:

```julia
abstract type A end
abstract type B end

foo(x, ::Type{A}) = x + 1
foo(x, ::Type{B}) = x + 2

foo(1, A)
foo(2, B)

```

In my experience, singleton structs are usually more efficient than passing around the type objects themselves. In other words, `A` and `B` are objects of type `DataType`, whereas `A()` and `B()` are basically empty objects that are easy for the compiler to compile away.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [January 21, 2021, 10:24pm UTC](https://discourse.julialang.org/t/shorthand-for-dispatch-types/53631/6 "2021-01-21T22:24:22Z")

</div>

For something like Rust enum types, you may find this [GitHub - MasonProtter/SumTypes.jl: An implementation of Sum types in Julia](https://github.com/MasonProtter/SumTypes.jl#sumtypesjl) helpful.

```julia
julia> @sum_type Fruit begin
           Apple()
           Banana()
           Orange()
       end

julia> Apple()
Fruit: Apple()

julia> Banana()
Fruit: Banana()

julia> Orange()
Fruit: Orange()

```

Not exactly what you’re looking for since these things are a closed set and require unwrapping, but perhaps still useful.
