---
title: "Discussion on \"Fruity Composable Design Patterns in Julia\" | Hashnode"
description: "Discussion on \"Fruity Composable Design Patterns in Julia\". Searching for the best value-based factory method that is easy to extend in the Julia programming language. With fruits."
url: "https://hashnode.com/posts/fruity-composable-design-patterns-in-julia/64958d80d5dec1509f8f963c"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Fruity Composable Design Patterns in Julia](https://scientificcoder.com/fruity-composable-design-patterns-in-julia?utm_source=hashnode&utm_medium=feed)

Searching for the best value-based factory method that is easy to extend in the Julia programming language. With fruits.

Jun 23, 2023 · 8 min read · scientificcoder.com  
Read the article

-   [#design-patterns](https://hashnode.com/tag/design-patterns)
-   [#julia](https://hashnode.com/tag/julia)
-   [#software-development](https://hashnode.com/tag/software-development)
-   [#coding](https://hashnode.com/tag/coding)
-   [#factory-design-pattern](https://hashnode.com/tag/factory-design-pattern)

## Written by

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

@matthijscox · The Netherlands

  
[View profile](https://hashnode.com/@matthijscox)

Scientific Software Engineer

A scientific software developer with over a decade of experience in academia, startups and industry. My mission is to turn you into an elite numerical computing professional.

## Responses 2

Markdown supported · ⌘↵ to post

[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 :)

[Mark Kittisopikul](https://hashnode.com/@markkitt)

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

Value based dispatch with a string presents some challenges.

If you use a Symbol, an interned string, directly, then value based dispatch can be made very efficient due to constant propagation.

```
julia> @btime ValueFruitFactory.fruit(:orange)
  1.424 ns (0 allocations: 0 bytes)
Main.ValueFruitFactory.Orange()

julia> f() = ValueFruitFactory.fruit(:orange)
f (generic function with 1 method)

julia> @btime f()
  1.423 ns (0 allocations: 0 bytes)
Main.ValueFruitFactory.Orange()

julia> @code_typed f()
CodeInfo(
1 ─     return $(QuoteNode(Main.ValueFruitFactory.Orange()))
) => Main.ValueFruitFactory.Orange
```

Compare the typed code with the string version.

```
julia> @code_typed g()
CodeInfo(
1 ─ %1 = $(Expr(:foreigncall, :(:jl_string_ptr), Ptr{UInt8}, svec(Any), 0, :(:ccall), "orange"))::Ptr{UInt8}
│   %2 = Core.sizeof("orange")::Int64
│   %3 = $(Expr(:foreigncall, :(:jl_symbol_n), Ref{Symbol}, svec(Ptr{UInt8}, Int64), 0, :(:ccall), :(%1), :(%2), "orange"))::Symbol
│   %4 = invoke Main.ValueFruitFactory.fruit(%3::Symbol)::Union{Main.ValueFruitFactory.Apple, Main.ValueFruitFactory.Orange}
└──      return %4
) => Union{Main.ValueFruitFactory.Apple, Main.ValueFruitFactory.Orange}
```
