---
title: "Comment by Mark Kittisopikul on \"Fruity Composable Design Patterns in Julia\" | Hashnode"
description: "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 effic"
url: "https://hashnode.com/posts/fruity-composable-design-patterns-in-julia/64958d80d5dec1509f8f963c/comment/6498cd575244606d9b5fd35f"
---

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

[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}
```
