def save(deck, filename) do
binary = :erlang.term_to_binary(deck)
File.write(filename, binary)
end
File.read("{filename}")
# will return
{:ok, <<....>>} # This is very common return type
# So, it's common
{status, binary} = File.read("{filename}")
def load(filename) do
{status, binary} = File.read(filename)
:erlang.binary_to_term binary
end
# but, it doesn't treat error case, so below is recommended.
def load(filename) do
{status, binary} = File.read(filename)
# case is elixir-style, not if statement.
case status do
{:ok, binary} -> :erlang.binary_to_term binary
{:error, _reason} -> "That file does not exist"
# underscore means mentioning to compiler that I don't care this value. It's only for patter matching.
end
end