Skip to content

Instantly share code, notes, and snippets.

@andykingking
Last active April 13, 2024 14:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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