Elixir 1.3, recently announced by José Valim, deprecates imperative assignments and adds new types and accessors, improves its Mix build tool and the ExUnit unit testing framework.
Elixir 1.3 deprecates imperative assignments to variables that are accessed in an outer scope and will emit a warning each time it finds one such use. The rationale behind this is that assigning to a variable that is used in an outer scope amounts to adding an implicit return value to the inner scope. Take for example, the following function definition:
def format(message, opts) do
path =
if (file = opts[:file]) && (line = opts[:line]) do
relative = Path.relative_to_cwd(file)
message = Exception.format_file_line(relative, line) <> " " <> message
relative
end
{path, message}
end
The if
block, besides returning a value for path
, is also changing the value in message
, which is then returned along with path
from the enclosing function. This is considered bad usage in Elixir 1.3 and should be refactored into the following code, which makes it explicit that the if
block returns two values:
def format(message, opts) do
path =
if (file = opts[:file]) && (line = opts[:line]) do
relative = Path.relative_to_cwd(file)
message = Exception.format_file_line(relative, line) <> " " <> message
{relative, message}
end
{path, message}
end
Another language feature meant to make it simpler to traverse nested data structures, is new access selectors. The following snippet show how you can traverse a map containing an embedding map associated to the languages
key, and convert all of its elements for the name
key to uppercase:
iex> update_in myMap, [:languages, Access.all(), :name], &String.upcase/1
Elixir’s build tool, Mix, is now able to do cross reference checking, which is useful, for example, in detecting calls to not existing modules and functions, or finding all places that calls a function belonging to a given module, or generating a dependency graph. Additionally, Mix streamlined its output, thus making it easier to spot warnings. Another improvement to Mix in Elixir 1.3 is better dependency tracking, which translates into faster compile times.
ExUnit, Elixir’s unit testing framework, leverages Mix cross-ref checks to add a new flag, --stale
which ensures that only unit tests that may have changed since the last run are executed. Other improvements in ExUnit
include better assertion output and improved test organization using named blocks.
There are many more changes in Elixir 1.3. You can read the full list in the release notes.