When to use the handle_params callback ThinkingElixir 085: Computer Vision in Elixir with Cocoa Xu

attr_reader has been released.

Defines module attribute getter automatically.

AttrReader

In Elixir, Module variable is often used as a constant. But I didn’t want to bother to define a getter when I wanted to refer to it with Test code etc. If you use AttrReader, you can use it without having to define the getter of the module attribute.

Links

Usage

Defines by use

iex> defmodule UseAttrReader do
...>   @foo "foo"
...>   use AttrReader
...>   @bar :bar
...> end

iex> UseAttrReader.foo()
"foo"

iex> UseAttrReader.bar()
:bar

Defines by macro

iex> defmodule UseAttrReaderMacro do
...>   require AttrReader
...>   AttrReader.define @foo
...>   AttrReader.define @bar, "bar"
...>   AttrReader.define @baz, :baz
...> end

iex> UseAttrReader.foo()
nil

iex> UseAttrReader.bar()
"bar"

iex> UseAttrReader.baz()
:baz