Last active
October 23, 2024 19:10
-
-
Save andykingking/4982353b8c69ea301c698e97f6d34635 to your computer and use it in GitHub Desktop.
Using structs with Access behaviour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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