---
title: "Comment by Mason Protter on \"Fruity Composable Design Patterns in Julia\" | Hashnode"
description: "I played around with this a few years ago in github.com/MasonProtter/PatternDispatch.jl julia&gt; module PatternFruitFactory using PatternDispatch abstract type"
url: "https://hashnode.com/posts/fruity-composable-design-patterns-in-julia/64958d80d5dec1509f8f963c/comment/6499c01a5244606d9b5ff904"
---

[Discussion](https://hashnode.com/posts/fruity-composable-design-patterns-in-julia/64958d80d5dec1509f8f963c)

Comment [Fruity Composable Design Patterns in Julia](https://hashnode.com/posts/fruity-composable-design-patterns-in-julia/64958d80d5dec1509f8f963c)

[Mason Protter](https://hashnode.com/@Mason265)

[Jun 26, 2023](https://hashnode.com/posts/fruity-composable-design-patterns-in-julia/64958d80d5dec1509f8f963c/comment/6499c01a5244606d9b5ff904)

I played around with this a few years ago in github.com/MasonProtter/PatternDispatch.jl

```
julia> module PatternFruitFactory
       using PatternDispatch

       abstract type Fruit end
       struct Apple <: Fruit end
       struct Orange <: Fruit end

       @pattern fruit("apple") = Apple()
       @pattern fruit("orange") = Orange()
       @pattern fruit(s) = error("unknown fruit $s")
       end

julia> @btime PatternFruitFactory.fruit("orange")
  3.449 ns (0 allocations: 0 bytes)
Main.PatternFruitFactory.Orange()
```

These are extensible at a later time:

```
julia> @btime PatternFruitFactory.fruit("banana")
  7.537 ns (0 allocations: 0 bytes)
Main.PatternFruitFactory.Banana()
```

PatternDispatch.jl does some stuff that's unfriendly to precompilation though so it doesn't fit very well into packages. Very much just a proof of concept.

[Matthijs Cox](https://hashnode.com/@matthijscox)

[Jun 27, 2023](https://hashnode.com/posts/fruity-composable-design-patterns-in-julia/64958d80d5dec1509f8f963c/comment/6499c01a5244606d9b5ff904#reply-649aa23064e19e74c4064c1f)

cool, thanks for yet another suggestion :)
