Skip to content

Instantly share code, notes, and snippets.

@andykingking
Last active October 23, 2024 19:10
Show Gist options
  • Save andykingking/4982353b8c69ea301c698e97f6d34635 to your computer and use it in GitHub Desktop.
Save andykingking/4982353b8c69ea301c698e97f6d34635 to your computer and use it in GitHub Desktop.
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
defstruct [:name, :value]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment