# Julia version of state-machine pattern

**URL:** https://discourse.julialang.org/t/julia-version-of-state-machine-pattern/125484
**Category:** General Usage
**Tags:** parametric-types
**Created:** [February 2, 2025, 9:42pm UTC](https://discourse.julialang.org/t/julia-version-of-state-machine-pattern/125484 "2025-02-02T21:42:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [February 2, 2025, 9:42pm UTC](https://discourse.julialang.org/t/julia-version-of-state-machine-pattern/125484/1 "2025-02-02T21:42:17Z")

</div>

There is an interesting tweet-thread about a Swift pattern which should have a corresponding Julia version:

> <https://x.com/BHolmesDev/status/1885720264098316316>

Anyone wants to show a Julia version?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [February 3, 2025, 12:03pm UTC](https://discourse.julialang.org/t/julia-version-of-state-machine-pattern/125484/2 "2025-02-03T12:03:18Z")

</div>

Here you go:

```julia
struct Park end
struct Drive end
struct Game end

struct Car{State}
    engineSystem::EngineSystem
    infotainmentSystem::InfotainmentSystem
end

function switchToDrive(c::Car{Park})
    activate(c.engineSystem)
    return Car{Drive}(c.engineSystem, c.infotainmentSystem)
end

```

Passing `Car{Drive}` would be a detectable `MethodError`. Note also that this is very far from how actual cars work internally, and using dispatch for this feels very much like a “if all you have is a hammer, everything looks like a nail” kind of situation. It would be much cleaner to use a proper sum-type for this. The julia version also can’t do data hiding the same way Swift can with its `private` field.
