Skip to content

Instantly share code, notes, and snippets.

@alco
Created October 8, 2015 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alco/6fe04173243896c3a898 to your computer and use it in GitHub Desktop.
Save alco/6fe04173243896c3a898 to your computer and use it in GitHub Desktop.
defmodule Foo do
Module.register_attribute __MODULE__, :a, [persist: true]
Module.register_attribute __MODULE__, :a_list, [persist: true]
Module.register_attribute __MODULE__, :b, [persist: true, accumulate: true]
Module.register_attribute __MODULE__, :b_list, [persist: true, accumulate: true]
@a 1
@a 2
@a_list [1]
@a_list [2]
@b 1
@b 2
@b_list [1]
@b_list [2]
def a, do: @a
def a_list, do: @a_list
def b, do: @b
def b_list, do: @b_list
end
# Unexpected: why does [2] get persisted instead of 2?
IO.inspect Foo.a
#=> 2
IO.inspect Foo.__info__(:attributes) |> Keyword.get_values(:a)
#=> [[2]]
# OK
IO.inspect Foo.a_list
#=> [2]
IO.inspect Foo.__info__(:attributes) |> Keyword.get_values(:a_list)
#=> [[2]]
# Unexpected: why do lists get persisted when each @b is an integer?
IO.inspect Foo.b
#=> [2, 1]
IO.inspect Foo.__info__(:attributes) |> Keyword.get_values(:b)
#=> [[1], [2]]
# OK
IO.inspect Foo.b_list
#=> [[2], [1]]
IO.inspect Foo.__info__(:attributes) |> Keyword.get_values(:b_list)
#=> [[1], [2]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment