# Are there any plans for Julia to adopt an effects system?

**URL:** <https://discourse.julialang.org/t/are-there-any-plans-for-julia-to-adopt-an-effects-system/110729>\
**Category:** Internals & Design\
**Created:** [February 25, 2024, 8:05am UTC](https://discourse.julialang.org/t/are-there-any-plans-for-julia-to-adopt-an-effects-system/110729 "2024-02-25T08:05:29Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [February 25, 2024, 4:25pm UTC](https://discourse.julialang.org/t/are-there-any-plans-for-julia-to-adopt-an-effects-system/110729/5 "2024-02-25T16:25:17Z")

</div>

I might be missing something crucial, but I think we can already do this today with ScopedValues. For example, taking the error-handling example [from the blog post @mnemnion linked](https://overreacted.io/algebraic-effects-for-the-rest-of-us/), we can write this in Julia as

```julia
using ScopedValues
function getName(user)
    name = user.name
    if name === nothing
        name = perform("ask_name")
    end
    return name
end

function makeFriends(user1, user2)
    push!(user1.friendNames, getName(user2))
    push!(user2.friendNames, getName(user1))
end
   
const arya = (; name=nothing, friendNames=[])
const gendry = (; name="Gendry", friendNames=[])

const HANDLER = ScopedValue{Union{Nothing, Function}}(nothing)

function perform(effect)
    handler = HANDLER[]
    isnothing(handler) && error("Could not perform effect $effect; no handler")
    return handler(effect)
end

with(HANDLER => function(effect)
    if effect === "ask_name"
        return "Arya Stark"
    end
    error("No handler for effect $effect")
end) do

    makeFriends(arya, gendry)
end

@show arya.friendNames gendry.friendNames;

```

which results in

```julia
arya.friendNames = Any["Gendry"]
gendry.friendNames = Any["Arya Stark"]

```

(ScopedValues are in Base in v1.11 due to [this PR](https://github.com/JuliaLang/julia/pull/50958), and available in earlier versions via [https://github.com/vchuravy/ScopedValues.jl](https://github.com/vchuravy/ScopedValues.jl).)

edit: I see Conditions.jl uses ScopedValues, so I guess their relevance here is old news 🙂

---

_[View the full topic](https://discourse.julialang.org/t/are-there-any-plans-for-julia-to-adopt-an-effects-system/110729)._
