|
1 | 1 | # Changelog for Elixir v1.8
|
2 | 2 |
|
| 3 | +Elixir v1.8 comes with many improvements at the infrastructure level, improving compilation time, speeding up common patterns, and adding features around introspection of the system. |
| 4 | + |
| 5 | +## Custom struct inspections |
| 6 | + |
| 7 | +Elixir now provides a derivable implementation of the `Inspect` protocol. In a nutshell, this means it is really easy to filter data from your data structures whenever they are inspected. For example, imagine you have a user struct with security and privacy sensitive information: |
| 8 | + |
| 9 | +```elixir |
| 10 | +defmodule User do |
| 11 | + defstruct [:id, :name, :age, :email, :encrypted_password] |
| 12 | +end |
| 13 | +``` |
| 14 | + |
| 15 | +By default, if you inspect a user via `inspect(user)`, it will include all fields. This can cause fields such as `:email` and `:encrypted_password` to appear in logs, error reports, etc. You could always define a custom implementation of the `Inspect` protocol for such cases but Elixir v1.8 makes it simpler by allowing you to derive the `Inspect` protocol: |
| 16 | + |
| 17 | +```elixir |
| 18 | +defmodule User do |
| 19 | + @derive {Inspect, only: [:id, :name, :age]} |
| 20 | + defstruct [:id, :name, :age, :email, :encrypted_password] |
| 21 | +end |
| 22 | +``` |
| 23 | + |
| 24 | +Now all user structs will be printed with all remaining fields collapsed: |
| 25 | + |
| 26 | + #User<id: 1, name: "Jane", age: 33, ...> |
| 27 | + |
| 28 | +You can also pass `@derive {Inspect, except: [...]}` in case you want to keep all fields by default and exclude only some. |
| 29 | + |
| 30 | +## Time zone database support |
| 31 | + |
| 32 | +In Elixir v1.3, Elixir added four types, known as Calendar types, to work with dates and times: `Time`, `Date`, `NaiveDateTime` (without time zone) and `DateTime` (with time zone). Over the last releases we have added many enhancements to the Calendar types but the `DateTime` module always evolved at a slower pace since Elixir did not provide support for a time zone database. |
| 33 | + |
| 34 | +Elixir v1.8 now defines a `Calendar.TimeZoneDatabase` behaviour, allowing developers to bring in their own time zone databases. By defining an explicit contract for time zone behaviours, Elixir can now extend the `DateTime` API, adding functions such as `DateTime.shift_zone/3`. By default, Elixir ships with a time zone database called `Calendar.UTCOnlyTimeZoneDatabase` that only handles UTC. |
| 35 | + |
| 36 | +Other Calendar related improvements include the addition of `Date.day_of_year/1`, `Date.quarter_of_year/1`, `Date.year_of_era/1`, and `Date.day_of_era/1`. |
| 37 | + |
| 38 | +## Faster compilation and other performance improvements |
| 39 | + |
| 40 | +Due to improvements to the compiler made over the last year, Elixir v1.8 should compile code about 5% faster on average. This is yet another release where we have been able to reduce compilation times and provide a more joyful development experience to everyone. |
| 41 | + |
| 42 | +The compiler also emits more efficient code for range checks in guards (such as `x in y..z`), for charlists with interpolation (such as `'foo #{bar} baz'`), and when working with records via the `Record` module. |
| 43 | + |
| 44 | +Finally, EEx templates got their own share of optimizations, emitting more compact code that runs faster. |
| 45 | + |
| 46 | +## Improved instrumentation and ownership with `$callers` |
| 47 | + |
| 48 | +The `Task` module is one of the most common ways to spawn light-weight processes to perform work concurrently. Whenever you spawn a new process, Elixir annotates the parent of that process through the `$ancestors` key. This information can be used by instrumentation tools to track the relationship between events occurring within multiple processes. However, many times, tracking only the `$ancestors` is not enough. |
| 49 | + |
| 50 | +For example, we recommend developers to always start tasks under a supervisor. This provides more visibility and allows us to control how those tasks are terminated when a node shuts down. In your code, this can be done by invoking something like: `Task.Supervisor.start_child(MySupervisor, task_specification)`. This means that, although your code is the one who invokes the task, the actual parent of the task would be the supervisor, as the supervisor is the one spawning it. We would list the supervisor as one of the `$ancestors` for the task, but the relationship between your code and the task is lost. |
| 51 | + |
| 52 | +In Elixir v1.8, we now track the relationship between your code and the task via the `$callers` key in the process dictionary, which aligns well with the existing `$ancestors` key. Therefore, assuming the `Task.Supervisor` call above, we have: |
| 53 | + |
| 54 | + [your code] -- calls --> [supervisor] ---- spawns --> [task] |
| 55 | + |
| 56 | +which means we store the following relationships: |
| 57 | + |
| 58 | + [your code] [supervisor] <-- ancestor -- [task] |
| 59 | + ^ | |
| 60 | + |---------------------- caller ---------------------| |
| 61 | + |
| 62 | +When a task is spawned directly from your code, without a supervisor, then the process running your code will be listed under both `$ancestors` and `$callers`. |
| 63 | + |
| 64 | +This small feature is very powerful. It allows instrumentation and monitoring tools to better track and relate the events happening in your system. This feature can also be used by tools like the "Ecto Sandbox". The "Ecto Sandbox" allows developers to run tests concurrently against the database, by using transactions and an ownership mechanism where each process explicitly gets a connection assigned to it. Without `$callers`, every time you spawned a task that queries the database, the task would not know its caller, and therefore it would be unable to know which connection was assigned to it. This often meant features that relies on tasks could not be tested concurrently. With `$callers`, figuring out this relationship is trivial and you have more tests using the full power of your machine. |
| 65 | + |
3 | 66 | ## v1.8.0-dev
|
4 | 67 |
|
5 | 68 | ### 1. Enhancements
|
|
61 | 124 | * [Calendar] Allow printing dates with more than 9999 years
|
62 | 125 | * [Exception] Exclude deprecated functions in "did you mean?" hints
|
63 | 126 | * [Float] Handle subnormal floats in `Float.ratio/1`
|
64 |
| - * [Kernel] Remove `Guard test tuple_size(...) can never succeed` dialyzer warning on try |
| 127 | + * [Kernel] Remove `Guard test tuple_size(...) can never succeed` Dialyzer warning on `try` |
65 | 128 | * [Kernel] Expand operands in `size*unit` bitstring modifier instead of expecting `size` and `unit` to be literal integers
|
66 | 129 | * [Kernel] Do not deadlock on circular struct dependencies in typespecs
|
67 | 130 | * [Kernel] Raise proper error message when passing flags to the Erlang compiler that Elixir cannot handle
|
|
0 commit comments