Skip to content

Instantly share code, notes, and snippets.

@andykingking
andykingking / coin.ex
Last active June 18, 2024 18:10
Using structs with Access behaviour
# An example struct.
defmodule Coin do
# Using Kernel.put_in/3 and other methods requires the target to have the Access behaviour.
@behaviour Access
# Structs by default do not implement this. It's easy to delegate this to the Map implementation however.
defdelegate get(coin, key, default), to: Map
defdelegate fetch(coin, key), to: Map
defdelegate get_and_update(coin, key, func), to: Map
defdelegate pop(coin, key), to: Map