Elixir V1.12 was released with improved scripting, tighter Erlang/OTP 24 integration, stepped scope, and a dozen new functions in the standard library. Overall, this is a small release that continues our tradition of bringing quality of life improvements to Elixir developers every 6 months. Some of these improvements are directly related to recent efforts to introduce numerical computation into Elixir.

Elixir V1.12 requires Erlang/OTP 22+. We also recommend running Mix local.rebar after installation to upgrade to the latest version of Rebar, which includes support for Erlang OTP/24+.

Note: This bulletin contains the Asciinema fragment. You may need to enable third-party JavaScript on this site in order to see them. If JavaScript is disabled, the NoScript tag with the appropriate link will be displayed.

Script improvements:Mix.install/2System.trap_signal/3

Elixir V1.12 brings new convenience to users who use Elixir for scripting (via.exs files). Elixir has been able to manage dependencies for quite some time, but it can only be done in Mix projects. In particular, the Elixir team is wary of global dependencies, because any scripts that rely on system packages are fragile and hard to reproduce when your system changes.

Mix.install/2, intended to be a sweet spot between single-file scripts and full Mix projects. With Mix.install/2, you can list your dependencies at the top of the script. When you first execute your script, Elixir downloads, compiles, and caches your dependencies before running your script. Future calls to the script will simply read the compiled artifacts from the cache.

Mix.install([:jason]) IO.puts(Jason.encode! (%{hello: :world}))Copy the code

Mix.install/2 also does protocol integration, which gives script developers an option to execute their code in the most efficient format. Note that Mix.install/2 is experimental at present and may change in future releases.

Also, Mix.Install /2 works well with Livebook, a newly announced project that brings interactive and collaborative notebook projects to Elixir. With Livebook and Mix.install/2, you can bring dependencies into your code notebook and make sure they are fully repeatable. Watch the Livebook announcement to learn more.

Another improvement to the script is the ability to capture exit signals through system.trap_signal /3. All you need is the name of the signal and the callback that will be called when the signal fires. For example, ExUnit makes use of this feature to print all currently running tests when you abort the test suite with SIGQUIT (Ctrl+\\). You can see this in action when you run tests in the Plug project below.

This is especially useful when your tests get stuck and you want to know which one is to blame.

Important: Capturing signals can have a significant impact on system shutdown and production behavior, so libraries are strongly discouraged from setting their own traps. Instead, they should let users configure them themselves. The only time it is acceptable for libraries to set their own traps is when using Elixir in script mode, such as in.exs files and through Mix tasks.

Tighter Erlang/OTP 24 integration

Erlang/OTP 24 comes with JIT compilation, so Elixir developers don’t have to do anything to take advantage of it. There are many other features to look forward to in Erlang/OTP 24, and Elixir V1.12 provides integration with many of them: such as 16-bit floating-point support in bitstrings, and performance improvements during compiler and code evaluations.

Another nice feature of Erlang/OTP 24 is the implementation of EEP 54, which provides extended error messages for many of the functions in Erlang’s STdlib. Elixir V1.12 takes full advantage of this feature to improve error reporting from Erlang. For example, in earlier VERSIONS of OTP, inserting an invalid argument into an ETS table that no longer exists would only result in an error, ArgumentError.

However, in Elixir V1.12 and Erlang/OTP 24.

Finally, note that Rebar V2 is no longer suitable for Erlang/OTP 24+. Starting with Elixir V1.4, Mix defaults to Rebar V3, so most developers don’t need to make any changes. However, if you explicitly set Manager: :rebar in your dependencies, you should move to Rebar V3 by removing the :manager option. Compatibility with unsupported versions of Rebar will be removed from Mix in the future.

Step scope

Elixir had support for ranges prior to its V1.0 release. Ranges only support integers and are inclusive, using the mathematical symbol a.. B. Range in Elixir is either increased by 1.. 10, or a reduced 10.. 1. The direction of the range is always inferred from the first and last positions. A range is always lazy because its value is emitted when it is enumerated rather than precomputed.

Unfortunately, due to this inference, it is impossible to empty the scope. For example, if you want to create a list of n elements, you can’t use a list from 1.. Because 1.. 0 (for n=0) is a decreasing range with two elements.

Elixir V1.12 via First.. The last//step notation supports a stepped range. For example: 1.. 10//2 will emit the numbers 1,3,5,7, and 9. You can think of the // operator as equivalent to “range division” because it effectively divides the number of elements in a range by step, rounding imprecisely. Steps can be positive (increasing range) or negative (decreasing range). Step scopes bring more expressive power to Elixir scopes, and they elegantly solve the problem of empty scopes because they allow the direction of the steps to be explicitly stated rather than inferred.

As of Elixir V1.12, implicit decrement ranges have been soft deprecated and will be warned in future versions of Elixir according to our deprecation policy.

then/2tap/2

Two new functions have been added to the Kernel module to make it easier to work with pipes. Tap /2 passes the given argument to an anonymous function, returning the argument itself. Then /2 passes the given argument to an anonymous function, returning the result. The following.

"hello world"
|> tap(&IO.puts/1)
|> then(&Regex.scan(~r/\w+/, &1))
Copy the code

That’s this.

"hello world"
|> (fn x ->
      IO.puts(x)
      x
    end).()
|> (&Regex.scan(~r/\w+/, &1)).()
Copy the code

Tap /2 and then/2 are both implemented as macros, and compiler improvements on Erlang/OTP 24 ensure that the anonymous functions in the middle are optimized away, ensuring that the above idiom does not have any performance impact on your code.

The improvement of IEx

IEx gets two important quality of life improvements in this release. Clicking TAB after a function call shows all of the function’s arguments, and you can now paste code with pipes in your shell. Take a look at these two features in action.

Additional features

Elixir V1.12 also adds many features to the standard library. Enum module has received the new functions, such as Enum. Count_until / 2, Enum. The product / 1, Enum. Zip_with / 2, and so on. The Integer module now includes integer.pow /2 and integer.extended_gcd /2.

The Code module gets a cursor_context/2 function, which is now used to support IEx auto-completion, which is used by projects such as Livebook to provide intelligent hints.

The EEx application has also been extended to provide metadata for text segments. This enabled the Surface and Phoenix LiveView team to implement a new templating language called HEEx that validates HTML and EEX. Finally, the Registry module supports the :compressed option, which is useful for GraphQL applications that manage hundreds of thousands of subscriptions via Absinthe.

For a complete list of all changes, see the full release notes. Check out the Installation section for an Elixir installation, and read our getting started guide to learn more.

Have a good time!

© 2012-2021 Elixir Team. All rights reserved.