Compare commits

..
87 Commits
Author SHA1 Message Date
José Valim 4e6d67c169 Add branch docs publishing 2023-05-30 11:41:49 +02:00
jendis 614ae049e6 Fix elixir test assertion for erlang >=OTP-24.1 (#11311) 2021-10-14 16:43:34 +02:00
MatteoandJosé Valim b1492cb0cf Fix logger translator with otp-24 (#11222)
* Handle new significant key in sasl reports

* Add OTP-24.0 to CI matrix

Co-authored-by: José Valim <jose.valim@dashbit.co>
2021-08-31 23:46:27 +02:00
Wojtek Mach 237a71311c Update warnings for OTP master (#10807) 2021-08-31 19:27:51 +02:00
José Valim e40b280fd8 Print task times on MIX_DEBUG=1 2021-06-01 11:43:49 +02:00
José Valim 308255bda8 Release v1.11.4 2021-03-16 12:25:59 +01:00
José Valim 1e81a5b9a5 Run formatter 2021-02-28 01:33:19 +01:00
José Valim 0627d7b0d4 Pass env and target to Mix.Config readers 2021-02-27 21:52:56 +01:00
Wojtek Mach 99ec7522d3 Backport OTP 24 compatibility patches to v1.11 (#10743)
* Update Erlang warnings translation (#10694)

Since https://github.com/erlang/otp/commit/8ecc648df22dbfe9dd68a3e2b968ba2b6a2ca7fe
instead of `{eval_failure, Reason}` we'd get `{eval_failure, Call, Reason}`

* Fix translating erl compiler errors to diagnostics on OTP 24 (#10719)

The compiler previously emitted just `line` and now emits `{line,
column}`.

The diagnostic struct accepts as position either:

    nil
    line
    {start_line, start_col, end_line, end_col}

so we could have used the `column` to create the 4-tuple but we don't
have enough information to do that correctly, we don't know where the
line ends.

* Fix remaining warning translations for OTP 24 (#10720)

Yecc error message changed so we need to loosen up our assertion:

OTP 23

    iex> iex
    Erlang/OTP 23 [erts-11.1.7] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

    Interactive Elixir (1.11.3) - press Ctrl+C to exit (type h() ENTER for help)
    iex(1)> :yecc.file('f.yrl')
    f.yrl:1: syntax error before: '.'
    :error

OTP 24

    iex> iex
    Erlang/OTP 24 [DEVELOPMENT] [erts-11.1.7] [source-802d2c5083] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

    Interactive Elixir (1.12.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
    iex(1)> :yecc.file('f.yrl')
    f.yrl:1:5: syntax error before: .

    :error

* Support 16bit floats in bitstrings (#10740)

On OTP 24:

    iex> <<x::float-16>> = <<60, 0>>
    iex> x
    1.0
    iex> <<x::float-16>>
    <<60, 0>>

Before OTP 24 we'd get errors or wouldn't match:

    iex> <<1.0::float-16>>
    ** (ArgumentError) argument error while evaluating iex at line 1

    <<x::float-16>> = <<60, 0>>
    ** (MatchError) no match of right hand side value: <<60, 0>>

    iex> (fn <<x::float-16>> -> x; _ -> :nomatch end).(<<60, 0>>)
    :nomatch

* Handle :compile.file/2 returning :error on OTP 24 (#10742)

Before OTP 24 when given invalid args, :compile.file/2 would return:

    iex(1)> :compile.file('a.erl', [{:d, 'foo', 'bar'}, :report])
    {:error, :badarg}

On OTP 24:

    iex(1)> :compile.file('a.erl', [{:d, 'foo', 'bar'}, :report])

    *** Internal compiler error ***
    exception error: bad argument
      in function  io_lib:format/2
         called as io_lib:format("badly formed '~s'",[{"foo","bar"}])
      in call from sys_messages:list_errors/3 (sys_messages.erl, line 53)
      in call from lists:foreach/2 (lists.erl, line 1342)
      in call from compile:comp_ret_err/1 (compile.erl, line 546)
      in call from compile:'-internal_fun/2-anonymous-0-'/2 (compile.erl, line 229)
      in call from compile:'-do_compile/2-anonymous-0-'/1 (compile.erl, line 219)
    :error
2021-02-20 14:33:11 +01:00
José Valim d30c5c0185 Release v1.11.3 2021-01-04 16:47:18 +01:00
José Valim 3fc334ebc3 Support all literals in guards, closes #10613 2021-01-02 17:19:30 +01:00
Julian Doherty df8955c0dd Improve Enum.chunk_while/4 docs to clarify after_fun semantics (#10595)
- add `{:halt, acc}` tuple return doc for `chunk_fun`
- explain `after_fun` semantics in more detail
- format return tuples for `chunk_fun` and `after_fun` as markdown lists
- added note that `acc` in `after_fun` return tuple is ignored
- note that `Enum.chunk_while/4` returns a list of *chunks*, not a list
  of lists (emitted chunks can be anything, not necessarily just lists)
2021-01-02 17:09:25 +01:00
José Valim 6b87a54825 Clarify writing documentation guide, closes #10610 2021-01-02 17:09:20 +01:00
José Valim 72ec902554 Revert "Add support for new rebar3 output env option (#10018)"
This option will be enabled on Elixir v1.12 instead.

This reverts commit fc9f7690c1.
2020-12-21 13:01:34 +01:00
José Valim 2c9fff0f93 Update CHANGELOG 2020-12-17 13:23:40 +01:00
José Valim c42f7af02b Do not match on error_info on OTP 24 2020-12-17 13:21:32 +01:00
José Valim 9c77960a26 Consider macros and module attributes on the left side of ++ on ExUnit.Diff
Closes #10520
2020-12-17 13:17:19 +01:00
José Valim a20f413841 Do not warn for attributes used in after_compile
Closes #10579.
2020-12-17 13:08:07 +01:00
José Valim ffbae20014 Improve xref error message, closes #10582 2020-12-17 08:18:16 +01:00
José Valim 9c81578ab5 Update CHANGELOG 2020-12-13 09:24:38 +01:00
José Valim a9f7b8e18c Run formatter 2020-12-13 09:15:02 +01:00
José Valim 61c3c34f44 Do not discard context from with, closes #10538 2020-12-13 09:14:39 +01:00
José Valim d8faf5bb7b Fix bootstrapping 2020-12-13 09:13:47 +01:00
José Valim 9d03032cfd Add line and column to EEx tokenizer 2020-12-13 09:06:15 +01:00
José Valim 3c4c68b3d5 Fix variable overriding in records, closes #10540 2020-12-13 09:05:09 +01:00
José Valim bed0a68176 Do not use unicode chars if ANSI is disabled
See https://github.com/elixir-lang/elixir/issues/10481#issuecomment-742448132
2020-12-10 12:19:19 +01:00
José Valim 60642c340d Improve error message on do-block in module attr, closes #10544 2020-12-10 12:19:14 +01:00
Eric Meadows-Jönsson 35e4263934 Fix docs tests on OTP 23 (#10510) 2020-11-29 10:19:53 +01:00
José Valim 43327512da Do not use private and removed ram_file:compress/1 (#10439) 2020-11-29 10:19:22 +01:00
José Valim 32692f14ff Fix test on Erlang master 2020-11-29 09:51:00 +01:00
Eric Meadows-Jönsson f80bd3c478 Fix intermittent test failure caused by archive warning (#10493) 2020-11-29 09:43:36 +01:00
José Valim 23934b48ba Fix failure on Erlang/OTP master 2020-11-29 09:43:28 +01:00
Dominic Letz c370a8a724 Add :compressed to stream_modes spec (#10490)
:compressed is already supported here so that gzipped files
can be streamed `File.stream!("log.gz", [:compressed])`.
2020-11-29 09:43:17 +01:00
José Valim 8c2237f80d Handle in memory modules in Xref.calls, closes #10502 2020-11-21 08:34:30 +01:00
José Valim a5e9c39023 Do not raise when checking for ambiguity with static_atoms_encoder, closes #10501 2020-11-20 12:43:14 +01:00
José Valim 6d918f5fb4 Wrap around :file instead of calling internals 2020-11-20 07:14:43 +01:00
Eksperimental 4347468d8a Update GitHub actions/checkout to v2 (#10496) 2020-11-18 13:21:49 +01:00
José Valim f044def5a1 Handle recursive vars in map values (#10498) 2020-11-18 12:42:23 +01:00
José Valim 05c9ccad12 Check for stacktrace at compile-time and runtime 2020-11-03 20:18:50 +01:00
José Valim 7533e56c1b Release v1.11.2 2020-11-03 15:23:56 +01:00
José Valim 3f083795b6 Do not trace protocol impl checks
A more complete solution is found in master but this
approach is less disruptive in the v1.11 branch.
2020-11-02 11:35:51 +01:00
José Valim 1bbc959d1a Prune tracers when fetching __ENV__ inside functions, closes #10466 2020-10-30 13:30:17 +01:00
José Valim 1603f21b0a Fix race condition when loading test helper, closes #10464 2020-10-29 19:29:06 +01:00
José Valim 02c3957667 Clarify wording about siblings in umbrella 2020-10-22 10:53:14 +02:00
José Valim eeab9f5aa1 Ensure manually set tracers are kept on compilation
Closes #10451.
2020-10-22 08:50:10 +02:00
José Valim 3f3e987a23 Handle aliases with code recursion
It addresses a regression introduced in 97e8b0242d.

Closes https://github.com/nerves-project/nerves/issues/557.
2020-10-20 20:33:58 +02:00
José Valim 53c8b27ee2 Fix bootstrap issue in Code module 2020-10-17 20:10:26 +02:00
José Valim 3550dd10af Do not crash when getting docs for missing erts appdir
Closes #10437.
2020-10-17 19:46:51 +02:00
José Valim 7e44b88122 Raise for is_record guards, closes #10436 2020-10-17 19:33:10 +02:00
José Valim 5c5259826f Release v1.11.1 2020-10-16 09:31:35 +02:00
Xavier Noria ccdefd904f Improves the docs of config/runtime.exs and Config.config_env/0 (#10414) 2020-10-16 09:23:13 +02:00
José Valim ff95a59ec0 Add :append to stream_mode, closes #10421 2020-10-16 09:22:44 +02:00
José Valim 4081590742 Add inet6 fallback to Mix usage of httpc, closes #10423 2020-10-16 09:22:16 +02:00
José Valim 693df5fe8c Properly handle tags inside typespec in Erlang docs, closes #10430 2020-10-16 09:22:11 +02:00
José Valim b4605374b5 Do not deadlock Logger if handler crashes on sync mode, closes #10420 2020-10-12 09:27:32 +02:00
Eksperimental 36ec08e0c8 Fix warning message in app requirement (#10417)
It was missing a closing square bracket.

Previously it was saying

  3. In case you don't want to add a requirement to :crypto, you may optionally skip this warning by adding [xref: [exclude: :crypto] to your "def project" in mix.exs

Now

  3. In case you don't want to add a requirement to :crypto, you may optionally skip this warning by adding [xref: [exclude: :crypto]] to your "def project" in mix.exs
2020-10-11 00:15:59 +02:00
José Valim ae0ac9b3c9 Ignore tracers if lexical tracker is dead or explicitly nil 2020-10-11 00:15:41 +02:00
Milton Mazzarri e3479831ba Do not show warning when super is in GenServer.child_spec/1 (#10418)
The original goal[1] was to deprecate the usage of `super` on every `GenServer`
callback, but `child_spec/1` is not a callback, it's a default implementation
that can be re-implemented.

This commit removes the _deprecation warning_ when someone use `super` in
`GenServer.child_spec/1`.

Fixes: #10415

[1] https://github.com/elixir-lang/elixir/commit/c024b0eeb2dc69a759fe5ceeb1c8216d58ae9386#diff-ed0344c13ed25389035f337d94902894R301-R316
2020-10-11 00:15:40 +02:00
José Valim ffbcc004ec Do not crash when :reduce is set to nil in comprehensions, closes #10411 2020-10-08 15:37:53 +02:00
José Valim e0d61c7875 Fix order dependent tests in logger suite 2020-10-08 09:43:51 +02:00
Michał Łępicki b2059031a6 Fix returning errors in Module.Types.Expr.of_expr (#10410) 2020-10-08 09:37:13 +02:00
José Valim ed2829d8ea Keep warnings when traversing clauses 2020-10-07 23:53:23 +02:00
José Valim 081f99cd37 Consider targets when filtering apps, closes #10405 2020-10-06 19:31:36 +02:00
José Valim 7837cc5c59 Release v1.11.0 2020-10-06 15:43:09 +02:00
Daniil Fedotov b96fc39422 Restrict Logger.configure on config_change callback (#10388)
The `config_change` callback is called from `application_controller` process,
which is receiving env updates on releases. `Logger.configure` does a blocking
call to the `Logger` process, which calls `Application.put_env`

This causes a crash on logger config update.

The fix is to call `Logger.configure` only for the config that have not yet
been persisted.
2020-10-03 08:33:31 +02:00
Waldemar Rachwał e1cb898198 Take --dot-iex into account after shell respawn (#10384) 2020-10-01 20:36:14 +02:00
Dave Lucia 88e2894925 Enum.max_by/3 and Enum.min_by/3 dialyzer fixes (#10383) 2020-10-01 13:23:19 +02:00
José Valim f7cb5ae053 Fix inference of open maps with non-singleton keys
Closes #10371.
2020-09-29 13:11:22 +02:00
José Valim b4845e6854 Update iso.ex 2020-09-25 13:34:38 +02:00
José Valim 0f1200f99b Add a note on async_stream+take 2020-09-25 13:33:15 +02:00
José Valim cec1a7190b Make config/runtime.exs deep merge, closes #10365 2020-09-25 13:32:54 +02:00
Gabriel Alves 53d1b98a05 Raise instead of crashing on when trying to use list operators in guards (#10363) 2020-09-23 14:30:39 +02:00
José Valim 4828f11cd2 Keep invoking loadconfig on cli boot 2020-09-22 15:47:35 +02:00
José Valim 501271cad0 Make replace and replace! in Keyword consistent, closes #10345 2020-09-18 16:13:16 +02:00
José Valim fd4a6ad5cf Handle empty map in other places, closes #10343 2020-09-18 10:53:56 +02:00
José Valim 196aab87ca Align doc chunks return with EEP 48 2020-09-18 10:41:39 +02:00
José Valim 59b266506a Append space on middle and start expr if necessary, closes #10340 2020-09-17 17:11:28 +02:00
José Valim 1c8e625d77 Speed up compilation with multiple describe 2020-09-17 14:24:54 +02:00
José Valim f9d7adbfdf Introduce register_test/6 to speed up test definition 2020-09-17 10:57:00 +02:00
José Valim f1721c8a58 Reduce amount of injected code in case 2020-09-17 10:29:40 +02:00
José Valim 57abca4c16 Properly handle reduce in types, closes #10224 2020-09-14 23:02:07 +02:00
José Valim 8e9ece0d62 More typing improvements (#10333)
1. Move push_expr_stack to unify and keep helpers typing free

2. Add get_var! when accessing variables so it helps us find
   variables that have not been properly processed

3. Properly process all variables found missing on step 2
2020-09-14 22:45:34 +02:00
José Valim 54ba096d34 Remove boolean and number as builtin types (#10329) 2020-09-14 22:45:34 +02:00
José Valim 6042fd0bcc Infer -> Unify 2020-09-14 22:45:34 +02:00
Eksperimental 475b73b46f Use proper casing in CHANGELOG (#10331) 2020-09-14 15:24:51 +02:00
José Valim 3cf6069ad9 Fix typo in CHANGELOG 2020-09-14 09:43:19 +02:00
José Valim 48cee5aaa5 Release v1.11.0-rc.0 2020-09-14 09:16:34 +02:00
294 changed files with 7574 additions and 16307 deletions
+3 -4
View File
@@ -12,21 +12,20 @@ test_template: &DEFAULT_TEST_SETTINGS
test_freebsd_task:
<<: *DEFAULT_TEST_SETTINGS
name: FreeBSD 13.0
name: FreeBSD 12.1
alias: FreeBSD Stable
freebsd_instance:
image_family: freebsd-13-0
image_family: freebsd-12-1
cpu: 8
memory: 7424Mi
env:
CHECK_REPRODUCIBLE: true
LC_ALL: en_US.UTF-8
PATH: $PATH:/usr/local/lib/erlang22/bin
install_script:
- pkg install -y erlang-runtime22 git gmake
- pkg install -y erlang git gmake
- rm -rf .git
- gmake compile
+2 -3
View File
@@ -1,10 +1,9 @@
[
inputs: [
"lib/*/{lib,unicode,test}/**/*.{ex,exs}",
"lib/*/*.exs",
"lib/ex_unit/examples/*.exs",
".formatter.exs"
"lib/*/mix.exs"
],
locals_without_parens: [
# Formatter tests
assert_format: 2,
+3 -5
View File
@@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
otp_release: ['OTP-24.0', 'OTP-23.3', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0']
otp_release: ['OTP-24.0', 'OTP-23.0', 'OTP-22.3', 'OTP-22.0', 'OTP-21.3.8', 'OTP-21.0']
development: [false]
include:
- otp_release: master
@@ -51,7 +51,7 @@ jobs:
run: make test_elixir
- name: Check reproducible builds
run: taskset 1 make check_reproducible
if: matrix.otp_release == 'OTP-24.0'
if: matrix.otp_release == 'OTP-23.0'
test_windows:
name: Windows, OTP-${{ matrix.otp_release }}, Windows Server 2019
@@ -83,9 +83,7 @@ jobs:
- name: Erlang test suite
run: make --keep-going test_erlang
- name: Elixir test suite
run: |
del c:/Windows/System32/drivers/etc/hosts
make --keep-going test_elixir
run: make --keep-going test_elixir
check_posix_compliant:
name: Check POSIX-compliant
+438 -251
View File
@@ -1,309 +1,496 @@
# Changelog for Elixir v1.12
# Changelog for Elixir v1.11
Elixir v1.12 is out with improvements to scripting, tighter Erlang/OTP 24 integration, stepped ranges, and dozen of new functions across the standard library. Overall this is a small release, which continues our tradition of bringing Elixir developers quality of life improvements every 6 months.
Over the last releases, the Elixir team has been focusing on the compiler, both in terms of catching more mistakes at compilation time and making it faster. Elixir v1.11 has made excellent progress on both fronts. This release also includes many other goodies, such as tighter Erlang integration, support for more guard expressions, built-in datetime formatting, and other calendar enhancements.
Elixir v1.12 requires Erlang/OTP 22+. We also recommend running `mix local.rebar` after installation to upgrade to the latest Rebar versions, which includes support for Erlang OTP/24+.
## Tighter Erlang integration
## Scripting improvements: `Mix.install/2` and `System.trap_signal/3`
Following Elixir v1.10, we have further integrated with Erlang's new logger by adding four new log levels: `notice`, `critical`, `alert`, and `emergency`, matching all log levels found in the Syslog standard. The `Logger` module now supports structured logging by passing maps and keyword lists to its various functions. It is also possible to specify the log level per module, via the `Logger.put_module_level/2` function. Log levels per application will be added in future releases.
Elixir v1.12 brings new conveniences for those using Elixir for scripting (via `.exs` files). Elixir has been capable of managing dependencies for a quite long time, but it could only be done within Mix projects. In particular, the Elixir team is wary of global dependencies as any scripts that rely on system packages are brittle and hard to reproduce whenever your system changes.
IEx also has been improved to show the documentation for Erlang modules directly from your Elixir terminal. This works with Erlang/OTP 23+ and requires Erlang modules to have been compiled with documentation chunks.
`Mix.install/2` is meant to be a sweetspot between single-file scripts and full-blown Mix projects. With `Mix.install/2`, you can list your dependencies on top of your scripts. When you execute the script for the first time, Elixir will download, compile, and cache your dependencies before running your script. Future invocations of the script will simply read the compiled artefacts from the cache:
## Compiler checks: application boundaries
Elixir v1.11 builds on top of the recently added compilation tracers to track application boundaries. From this release, Elixir will warn if you invoke a function from an existing module but this module does not belong to any of your listed dependencies.
These two conditions may seem contradictory. After all, if a module is available, it must have come from a dependency. This is not true in two scenarios:
* Modules from Elixir and Erlang/OTP are always available - even if their applications are not listed as a dependency
* In an umbrella project, because all child applications are compiled within the same VM, you may have a module from a sibling project available, even if you don't depend on said sibling
This new compiler check makes sure that all modules that you invoke are listed as part of your dependencies, emitting a warning like below otherwise:
```text
:ssl.connect/2 defined in application :ssl is used by the current
application but the current application does not directly depend
on :ssl. To fix this, you must do one of:
1. If :ssl is part of Erlang/Elixir, you must include it under
:extra_applications inside "def application" in your mix.exs
2. If :ssl is a dependency, make sure it is listed under "def deps"
in your mix.exs
3. In case you don't want to add a requirement to :ssl, you may
optionally skip this warning by adding [xref: [exclude: :ssl]
to your "def project" in mix.exs
```
This comes with extra benefits in umbrella projects, as it requires applications to depend on the siblings they depend on, which will fail if there are any cyclic dependencies.
## Compiler checks: data constructors
In Elixir v1.11, the compiler also tracks structs and maps fields across a function body. For example, imagine you wanted to write this code:
def drive?(%User{age: age}), do: age >= 18
If there is either a typo on the `:age` field or the `:age` field was not yet defined, the compiler will fail accordingly. However, if you wrote this code:
def drive?(%User{} = user), do: user.age >= 18
The compiler would not catch the missing field and an error would only be raised at runtime. With v1.11, Elixir will track the usage of all maps and struct fields within the same function, emitting warnings for cases like above:
```text
warning: undefined field `age` in expression:
# example.exs:7
user.age
expected one of the following fields: name, address
where "user" was given the type %User{} in:
# example.exs:7
%User{} = user
Conflict found at
example.exs:7: Check.drive?/1
```
The compiler also checks binary constructors. Consider you have to send a string over the wire with length-based encoding, where the string is prefixed by its length, up to 4MBs. Your initial attempt may be this:
def run_length(string) when is_binary(string) do
<<byte_size(string)::32, string>>
end
However, the code above has a bug. Each segment given between `<<>>` must be an integer, unless specified otherwise. With Elixir v1.11, the compiler will let you know so:
```text
warning: incompatible types:
binary() !~ integer()
in expression:
<<byte_size(string)::integer()-size(32), string>>
where "string" was given the type integer() in:
# foo.exs:4
<<byte_size(string)::integer()-size(32), string>>
where "string" was given the type binary() in:
# foo.exs:3
is_binary(string)
HINT: all expressions given to binaries are assumed to be of type integer()
unless said otherwise. For example, <<expr>> assumes "expr" is an integer.
Pass a modifier, such as <<expr::float>> or <<expr::binary>>, to change the
default behaviour.
Conflict found at
foo.exs:4: Check.run_length/1
```
Which can be fixed by adding `::binary` to the second component:
def run_length(string) when is_binary(string) do
<<byte_size(string)::32, string::binary>>
end
While some of those warnings could be automatically fixed by the compiler, future versions will also perform those checks across functions and potentially across modules, where automatic fixes wouldn't be desired (nor possible).
## Compilation time improvements
Elixir v1.11 features many improvements to how the compiler tracks file dependencies, such that touching one file causes less files to be recompiled. In previous versions, Elixir tracked three types of dependencies:
* compile time dependencies - if A depends on B at compile time, such as by using a macro, whenever B changes, A is recompiled
* struct dependencies - if A depends on B's struct, whenever B's struct definition changed, A is recompiled
* runtime dependencies - if A depends on B at runtime, A is never recompiled
However, because dependencies are transitive, if A depends on B at compile time and B depends on C at runtime, A would depend on C at compile time. Therefore, it is very important to reduce the amount of compile time dependencies.
Elixir v1.11 replaces "struct dependencies" by "exports dependencies". In other words, if A depends on B, whenever B public's interface changes, A is recompiled. B's public interface is made by its struct definition and all of its public functions and macros.
This change allows us to mark `import`s and `require`s as "exports dependencies" instead of "compile time" dependencies. This simplifies the dependency graph considerably. For example, [in the Hex.pm project](https://github.com/hexpm/hexpm), changing the `user.ex` file in Elixir v1.10 would emit this:
```text
$ touch lib/hexpm/accounts/user.ex && mix compile
Compiling 90 files (.ex)
```
In Elixir v1.11, we now get:
```text
$ touch lib/hexpm/accounts/user.ex && mix compile
Compiling 16 files (.ex)
```
To make things even better, Elixir v1.11 also introduces a more granular file tracking for path dependencies. In previous versions, a module from a path dependency would always be treated as a compile time dependency. This often meant that if you have an umbrella project, changing an application would cause many modules in sibling applications to recompile. Fortunately, Elixir v1.11 will tag modules from dependencies as exports if appropriate, yielding dramatic improvements to those using path dependencies.
To round up the list of compiler enhancements, the `--profile=time` option added in Elixir v1.10 now also includes the time to compile each individual file. For example, in the Plug project, one can now get:
```text
[profile] lib/plug/conn.ex compiled in 935ms
[profile] lib/plug/ssl.ex compiled in 147ms (plus 744ms waiting)
[profile] lib/plug/static.ex compiled in 238ms (plus 654ms waiting)
[profile] lib/plug/csrf_protection.ex compiled in 237ms (plus 790ms waiting)
[profile] lib/plug/debugger.ex compiled in 719ms (plus 947ms waiting)
[profile] Finished compilation cycle of 60 modules in 1802ms
[profile] Finished group pass check of 60 modules in 75ms
```
While implementing those features, we have also made the `--long-compilation-threshold` flag more precise. In previous versions, `--long-compilation-threshold` would consider both the time a file spent to compile and the time spent waiting on other files. In Elixir v1.11, it considers only the compilation time. This means less false positives and you can now effectively get all files that take longer than 2s to compile by passing `--long-compilation-threshold 2`.
## `mix xref graph` improvements
To bring visibility to the compiler tracking improvements described in the previous section, we have also added new features to `mix xref`. `mix xref` is a task that describes cross-references between files in your projects. The `mix xref graph` subsection focuses on the dependency graph between them.
First we have made the existing `--label` flag to consider transitive dependencies. Using `--sink FILE` and `--label compile` can be a powerful combo to find out which files will change whenever the given `FILE` changes. For example, in the Hex.pm project, we get:
```text
$ mix xref graph --sink lib/hexpm/accounts/user.ex --label compile
lib/hexpm/billing/hexpm.ex
└── lib/hexpm/billing/billing.ex (compile)
lib/hexpm/billing/local.ex
└── lib/hexpm/billing/billing.ex (compile)
lib/hexpm/emails/bamboo.ex
├── lib/hexpm/accounts/email.ex (compile)
└── lib/hexpm/accounts/user.ex (compile)
lib/hexpm/emails/emails.ex
└── lib/hexpm_web/views/email_view.ex (compile)
lib/hexpm_web/controllers/api/docs_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/key_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/organization_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/organization_user_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/owner_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/package_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/release_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/repository_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/api/retirement_controller.ex
└── lib/hexpm_web/controllers/auth_helpers.ex (compile)
lib/hexpm_web/controllers/blog_controller.ex
└── lib/hexpm_web/views/blog_view.ex (compile)
lib/hexpm_web/endpoint.ex
├── lib/hexpm_web/plug_parser.ex (compile)
└── lib/hexpm_web/session.ex (compile)
```
All the files at the root will recompile if `lib/hexpm/accounts/user.ex` changes. Their children describe the *why*. For example, the `repository_controller.ex` file will recompile if user changes because it has a compile time dependency on `auth_helpers.ex`, which depends on `user.ex`. This indirect compile time dependency is often the source of recompilations and Elixir v1.11 now makes it trivial to spot them, so they can be eventually addressed.
Another improvement to `mix xref graph` is the addition of `--format cycles`, which will print all cycles in your compilation dependency graph. A `--min-cycle-size` flag can be used if you want to discard short cycles.
## `config/runtime.exs` and `mix app.config`
Elixir v1.9 introduced a new configuration file, specific to releases, called `config/releases.exs`. A release is a self-contained artifact with the Erlang VM, Elixir and your application, ready to run in production.
The addition of `config/releases.exs` has been a very useful one but, unfortunately, it applies only to releases. Developers not using releases must use the `config/config.exs` file, which often loaded too early at compilation time. For any dynamic configuration, developers had to resort to third-party tools or workarounds to achieve the desired results.
Elixir v1.11 addresses this issue by introducing a new configuration file, called `config/runtime.exs`. This new configuration file is loaded exactly before your application starts, when the code is already fully compiled. It is loaded in development, test, and production, regardless if you are using Mix or releases. Therefore it provides a unified API for runtime configuration in Elixir.
`config/runtime.exs` works the same as any other configuration file. However, given `config/runtime.exs` is meant to run with or without Mix, developers must not use `Mix.env()` or `Mix.target()` in `config/runtime.exs`. Instead, they must use the new `config_env()` and `config_target()`, which have been added to the `Config` module.
While `config/releases.exs` will continue to be supported, developers can migrate to `config/runtime.exs` without loss of functionality. For example, a `config/releases.exs` file such as this one
```elixir
Mix.install([:jason])
IO.puts Jason.encode!(%{hello: :world})
# config/releases.exs
import Config
config :foo, ...
config :bar, ...
```
`Mix.install/2` also performs protocol consolidation, which gives script developers an option to execute their code in the most performant format possible.
could run as is as `config/runtime.exs`. However, given `config/runtime.exs` runs in all environments, you may want to restrict part of your configuration to the `:prod` environment:
**Note:** `Mix.install/2` is currently experimental and it may change in future releases.
```elixir
# config/runtime.exs
import Config
Another improvement to scripting is the ability to trap exit signals via `System.trap_signal/3`. All you need is the signal name and a callback that will be invoked when the signal triggers. For example, ExUnit leverages this functionality to print all currently running tests when you abort the test suite via SIGQUIT (`Ctrl+\\ `):
```
$ mix test
.......................................................................
.....................^\
Aborting test suite, the following have not completed:
* test query building [test/ecto/query_test.exs:48]
* test placeholders in Repo.insert_all [test/ecto/repo_test.exs:502]
Showing results so far...
78 doctests, 1042 tests, 0 failures
if config_env() == :prod do
config :foo, ...
config :bar, ...
end
```
This is particularly useful when your tests get stuck and you want to know which one is the culprit.
If both files are available, releases will pick the now preferred `config/runtime.exs` instead of `config/releases.exs`.
**Important**: Trapping signals may have strong implications on how a system shuts down and behave in production and therefore it is extremely discouraged for libraries to set their own traps. Instead, they should redirect users to configure them themselves. The only cases where it is acceptable for libraries to set their own traps is when using Elixir in script mode, such as in `.exs` files and via Mix tasks.
To wrap it all up, `Mix` also includes a new task called `mix app.config`. This task loads all applications and configures them, without starting them. Whenever you write your own Mix tasks, you will typically want to invoke either `mix app.start` or `mix app.config` before running your own code. Which one is better depends if you want your applications running or only configured.
## Tighter Erlang/OTP 24 integration
## Other improvements
Erlang/OTP 24 ships with JIT compilation support and Elixir developers don't have to do anything to reap its benefits. There are many other features in Erlang/OTP 24 to look forwards to and Elixir v1.12 provides integration with many of them: such as support for 16bit floats in bitstrings as well as performance improvements in the compiler and during code evaluation.
Elixir v1.11 adds the `is_struct/2`, `is_exception/1`, and `is_exception/2` guards. It also adds support for the `map.field` syntax in guards.
Another excellent feature in Erlang/OTP 24 is the implementation of [EEP 54](http://erlang.org/eeps/eep-0054.html), which provides extended error information for many functions in Erlang's stdlib. Elixir v1.12 fully leverages this feature to improve reporting for errors coming from Erlang. For example, in earlier OTP versions, inserting an invalid argument into a ETS table that no longer exists would simply error with `ArgumentError`:
The Calendar module ships with a new `Calendar.strftime/3` function, which provides datetime formatting based on the `strftime` format. The `Date` module got new functions for working with weeks and months, such as `Date.beginning_of_month/1` and `Date.end_of_week/2`. Finally, all calendar types got conversion functions from and to gregorian timestamps, such as `Date.from_gregorian_days/2` and `NaiveDateTime.to_gregorian_seconds/1`.
```
Interactive Elixir (1.11.0)
iex(1)> ets = :ets.new(:example, [])
#Reference<0.3845811859.2669281281.223553>
iex(2)> :ets.delete(ets)
true
iex(3)> :ets.insert(ets, :should_be_a_tuple)
** (ArgumentError) argument error
(stdlib 3.15) :ets.insert(#Reference<0.3845811859.2669281281.223553>, :should_be_a_tuple)
```
Mix also includes two new tasks: `mix app.config`, for application runtime configuration, and `mix test.coverage`, which generates aggregated coverage reports for umbrella projects and for test suites partitioned across processes.
However, in Elixir v1.12 with Erlang/OTP 24:
## v1.11.4 (2021-03-16)
```
Interactive Elixir (1.12.0)
iex(1)> ets = :ets.new(:example, [])
#Reference<0.105641012.1058144260.76455>
iex(2)> :ets.delete(ets)
true
iex(3)> :ets.insert(ets, :should_be_a_tuple)
** (ArgumentError) errors were found at the given arguments:
* 1st argument: the table identifier does not refer to an existing ETS table
* 2nd argument: not a tuple
(stdlib 3.15) :ets.insert(#Reference<0.105641012.1058144260.76455>, :should_be_a_tuple)
```
Finally, note Rebar v2 no longer works on Erlang/OTP 24+. Mix defaults to Rebar v3 since v1.4, so no changes should be necessary by the huge majority of developers. However, if you are explicitly setting `manager: :rebar` in your dependency, you want to move to Rebar v3 by removing the `:manager` option. Support for unsupported Rebar versions will be removed from Mix in the future.
## Stepped ranges
Elixir has support for ranges from before its v1.0 release. Ranges support only integers and are inclusive, using the mathematic notation `a..b`. Ranges in Elixir are either increasing `1..10` or decreasing `10..1` and the direction of the range was always inferred from the first and last positions. Ranges are always lazy as its values are emitted as they are enumerated rather than being computed upfront.
Unfortunately, due to this inference, it is not possible to have empty ranges. For example, if you want to create a list of `n` elements, you cannot express it with a range from `1..n`, as `1..0` (for `n=0`) is a decreasing range with two elements.
Elixir v1.12 supports stepped ranges via the `first..last//step` notation. For example: `1..10//2` will emit the numbers `1`, `3`, `5`, `7`, and `9`. You can consider the `//` operator as an equivalent to "range division", as it effectively divides the number of elements in the range by `step`, rounding up on inexact scenarios. Steps can be either positive (increasing ranges) or negative (decreasing ranges). Stepped ranges bring more expressive power to Elixir ranges and they elegantly solve the empty range problem, as they allow the direction of the steps to be explicitly declared instead of inferred.
As of Elixir v1.12, implicitly decreasing ranges are soft-deprecated and warnings will be emitted in future Elixir versions based on our [deprecation policy](https://hexdocs.pm/elixir/compatibility-and-deprecations.html#deprecations).
## Additional functions
Elixir v1.12 has the additional of many functions across the standard library. The `Enum` module received additions such as `Enum.count_until/2`, `Enum.product/1`, `Enum.zip_with/2`, and more. The `Integer` module now includes `Integer.pow/2` and `Integer.extended_gcd/2`. Finally, the `Kernel` module got two new functions, `Kernel.then/2` and `Kernel.tap/2`, which are specially useful in `|>` pipelines.
## v1.12.3 (2021-09-05)
### 1. Bug fixes
#### Elixir
* [Code] Make sure that bindings in the default context returned by `Code.eval_*` functions are not returned as tagged tuples
* [Kernel] Do not crash when handling ambiguity errors
* [Range] Still match on old range patterns throughout the stdlib
#### IEx
* [IEx.Autocomplete] Do not error autocompletion with module attribute
#### Mix
* [Mix] Rename inconsistent `:exit_code` option to `:exit_status` on `Mix.raise/2`
## v1.12.2 (2021-07-01)
### 1. Bug fixes
#### Elixir
* [Kernel] Ensure deprecated macros emit warnings
#### Mix
* [mix deps] Ensure unconstrained rebar deps generate valid mix specifications
### 2. Enhancements
#### Elixir
* [elixirc] Change the output of `--profile time` to make it easier to detect outliers
* [Application] Do not add compile time deps on args to `Application.compile_env/2` and `Application.compile_env!/2`
* [Enum] Optimize `Enum.into/3` and `Map.new/2`
#### Mix
* [mix compile] Compile most recently changed files first
* [mix compile, mix run, mix test] Speed up the time taken to load dependencies. This should make the usage of Mix inside projects quite more responsive
## v1.12.1 (2021-05-28)
### 1. Bug fixes
#### Elixir
* [Code] Make sure `Code.format_string!/2` formats multiline expression inside interpolation on the first run
* [Macro] Revert keeping of underscores between digits in camelize
#### Mix
* [Mix] Make sure `Mix.install/2` expand paths for deps
* [mix deps.get] Silence false positives on `httpc` warnings
* [mix test] Do not run the whole suite when there are no --failed tests as it won't behave as expected inside umbrellas
## v1.12.0 (2021-05-19)
This release introduces fixes for better support of Erlang/OTP 24+.
### 1. Enhancements
#### EEx
* [EEx.Engine] Add `c:EEx.Engine.handle_text/3` callback that receives text metadata
* [EEx.Engine] Emit warnings for unused "do" expression in EEx
#### Elixir
* [Code] Add `Code.cursor_context/2` to return the context of a code snippet
* [Code] Do not add newlines around interpolation on code formatting. Note this means formatted code that has interpolation after the line length on Elixir v1.12 won't be considered as formatted on earlier Elixir versions
* [Code] Do not add brackets when keywords is used in the access syntax
* [Calendar] Support basic datetime format in `Calendar.ISO` parsing functions
* [Code] Improve evaluation performance on systems running on Erlang/OTP 24+
* [Date] Support steps via `Date.range/3`
* [DateTime] Add `offset` to `DateTime.to_iso8601/2` (now `to_iso8601/3`)
* [Enum] Add `Enum.count_until/2` and `Enum.count_until/3`
* [Enum] Add `Enum.product/1`
* [Enum] Add `Enum.zip_with/2`, `Enum.zip_with/3`, `Enum.zip_reduce/3`, and `Enum.zip_reduce/4`
* [Enum] Add support for functions as the second argument of `Enum.with_index/2`
* [Exception] Show `error_info` data for exceptions coming from Erlang
* [Float] Add `Float.pow/2`
* [Integer] Add `Integer.pow/2` and `Integer.extended_gcd/2`
* [IO] Add `IO.stream/0` and `IO.binstream/0` which default to STDIO with line orientation
* [List] Add default value for `List.first/1` and `List.last/1`
* [Kernel] Add `first..last//step` as support for stepped ranges
* [Kernel] Also warn for literal structs on `min/2` and `max/2`
* [Kernel] Add `Kernel.tap/2` and `Kernel.then/2`
* [Kernel] Do not add runtime dependencies to remotes in typespecs
* [Kernel] When there is an unused variable warning and there is a variable with the same name previously defined, suggest the user may have wanted to use the pin operator
* [Kernel] Improve error messages on invalid character right after a number
* [Kernel] Show removal and deprecated tips from Erlang/OTP
* [Macro] Add export dependencies on `Macro.struct!/2`
* [Macro] Support `:newline` to customize newlines escaping in `Macro.unescape_string/2`
* [Module] Raise on invalid `@dialyzer` attributes
* [Module] Add `Module.get_definition/2` and `Module.delete_definition/2`
* [Module] Allow `@on_load` to be a private function
* [Module] Validate `@dialyzer` related module attributes
* [Module] Add `Module.reserved_attributes/0` to list all reserved attributes by the language
* [Range] Add `Range.new/3` and `Range.size/1`
* [Regex] Add offset option to `Regex.scan/3` and `Regex.run/3`
* [Registry] Support `:compression` on `Registry` tables
* [Registry] Support `Registry.values/3` for reading values under a given key-pid pair
* [Stream] Add `Stream.zip_with/2` and `Stream.zip_with/3`
* [String] Add `:turkic` mode option to String case functions
* [String] Update to Unicode 13.0
* [System] Add `System.trap_signal/3` and `System.untrap_signal/2`
* [System] Add `System.shell/2` to invoke a command that is interpreted by the shell
* [Tuple] Add `Tuple.sum/1` and `Tuple.product/1`
* [URI] Support RFC3986 compliant encoding and decoding of queries via the `:rfc3986` option
#### ExUnit
* [ExUnit] Intercept SIGQUIT (via Ctrl+\\) and show a list of all aborted tests as well as intermediate test results
* [ExUnit] Interpolate module attributes in match assertions diffs
* [ExUnit] Print how much time is spent on `async` vs `sync` tests
* [ExUnit] Improve error messages for doctests
* [ExUnit] Compile doctests faster (often by two times)
* [ExUnit] Add `ExUnit.async_run/0` and `ExUnit.await_run/1`
#### IEx
* [IEx] Make IEx' parser configurable to allow special commands
* [IEx] Show function signature when pressing tab after the opening parens of a function
* [IEx] If an IEx expression starts with a binary operator, such as `|>`, automatically pipe in the result of the last expression
* [Kernel] Update formatting when printing warnings and errors from Erlang/OTP 24+
* [Kernel] Support float-16 on bitstrings
#### Mix
* [Mix] Add `Mix.install/2` for dynamically installing a list of dependencies
* [Mix] Support `:exit_code` option in `Mix.raise/2`
* [Mix] Discard `MIX_ENV` and `MIX_TARGET` values if they are empty strings
* [Mix] Print the time taken to execute a task with on `MIX_DEBUG=1`
* [mix compile.erlang] Compile multiple files in parallel
* [mix escript.build] Deep merge configuration and ensure argv is set when executing `config/runtime.exs`
* [mix release] Add `RELEASE_PROG` to releases with the name of the executable starting the release
* [mix release] Support `remote.vm.args` to customize how the connecting VM boots
* [mix test] Run all available tests if there are no pending `--failed` tests. This provides a better workflow as you no longer need to toggle the `--failed` flag between runs
* [mix test] Support `mix test --warnings-as-errors`, treating warnings as errors and returning a non-zero exit status (only applies to test files, not compilation)
* [mix local.rebar] This task will now install rebar3 version 3.14.4, compiled with Erlang/OTP 21
## v1.11.3 (2021-01-04)
### 1. Enhancements
#### Elixir
* [Macro] Add `Macro.unique_var/2` and `Macro.generate_unique_arguments/2`
### 2. Bug fixes
#### Elixir
* [CLI] Ensure `-e ""` (with an empty string) parses correctly on Windows
* [Inspect] Do not override user supplied `:limit` option for derived implementations
* [Kernel] Allow heredoc inside a heredoc interpolation
* [Kernel] Preserve CRLF on heredocs
* [Kernel] Public functions without documentation now appear as an empty map on `Code.fetch_docs/1`, unless they start with underscore, where they remain as `:none`. This aligns Elixir's implementation with EEP48
* [Kernel] Do not crash when complex literals (binaries and maps) are used in guards
* [Kernel] Properly parse keywords (such as `end`) followed by the `::` operator
* [Kernel] Do not ignore unimplemented signatures from generated functions
* [Kernel] Improve error message when an expression follows a keyword list without brackets
* [Macro] `Macro.decompose_call/1` now also consider tuples with more than 2 elements to not be valid calls
* [Macro] Fix `Macro.to_string/1` double-escaping of escape characters in sigils
* [Macro] Fix `Macro.underscore/1` on digits preceded by capitals: "FOO10" now becomes "foo10" instead of "fo_o10"
* [Macro] Preserve underscores between digits on `Macro.underscore/1`
* [OptionParser] Properly parse when numbers follow-up aliases, for example, `-ab3` is now parsed as `-a -b 3`
* [Path] Fix `Path.relative_to/2` when referencing self
* [Path] Do not crash when a volume is given to `Path.absname/1`, such as "c:"
* [Task] Ensure `Task.async_stream/2` with `ordered: false` discard results as they are emitted, instead of needlessly accumulating inside the stream manager
* [Task] Raise if `:max_concurrency` is set to 0 on streaming operations
* [URI] Do not discard empty paths on `URI.merge/2`
* [Code] Do not raise when checking for operator ambiguity when `:static_atoms_encoder` is set in `Code.string_to_quoted`
* [Kernel] Emit undefined function warnings from `with`
* [Kernel] Do not fail type checking when literals are used in guards
* [Module] Do not warn for attributes used in `@after_compile`
* [Record] Make sure nested record names do not clobber each other
#### ExUnit
* [ExUnit.Case] Make `@tag tmp_dir` an absolute directory, avoiding inconsistencies if the test changes the current working directory
* [ExUnit.Diff] Fix cases where the diffing algorithm would fail to print a pattern correct
* [ExUnit.Assertions] Do not crash if there are macros and module attributes on the left side of `++`
#### IEx
* [IEx] Fix auto-completion inside remote shells
* [IEx.Helpers] Do not use Unicode chars if ANSI is disabled
#### Mix
* [mix app.config] Do not emit false positive warnings when configured dependencies that have `runtime: false` set
* [mix compile.elixir] Ensure that a manifest is generated even with no source code
* [mix compile.elixir] Make sure export dependencies trigger recompilation when the dependency is removed as well as when the whole file is removed
* [mix compile.elixir] Do not emit false positive warnings when a path dependency adds a module that is then used by the current application in the same `mix compile` cycle
* [mix test] Ensure protocols within the current project are consolidated when `--cover` is given
* [mix release] Improve compliance of release scripts with stripped down Linux installations
* [mix release] Preserve file mode when copying non-beam ebin files
* [mix xref] Ensure args are passed to the underlying `mix compile` call
* [mix deps.compile] Fix compatibility with rebar v3.14
* [mix release] Do not use private `ram_file:compress/1`
* [mix xref] Do not crash when retrieving calls for modules in memory
### 3. Soft-deprecations (no warnings emitted)
## v1.11.2 (2020-11-03)
### 1. Bug fixes
#### Elixir
* [Kernel] Using `first..last` to match on ranges is soft-deprecated and will warn on future Elixir versions. Use `first..last//step` instead
* [Kernel] Using `first..last` to create decreasing ranges is soft-deprecated and will warn on future versions. Use `first..last//-1` instead
* [Code] Do not crash when getting docs for missing `erts` appdir
* [Kernel] Raise meaningful error if `:erlang.is_record` is used in guards
* [Kernel] Prune tracers when fetching `__ENV__` inside functions
### 4. Hard-deprecations
#### Mix
* [mix] Fix regression where aliases could not call themselves recursively
* [mix compile] Do not discard tracers that are set programatically
## v1.11.1 (2020-10-16)
### 1. Bug fixes
#### Elixir
* [Code] Ignore tracers if lexical tracker is dead or explicitly nil when evaling code with an environment
* [GenServer] Do not show warning when using `super` in `GenServer.child_spec/1`
* [Kernel] Do not crash when :reduce is set to `nil` in comprehensions
* [Kernel] Fix a scenario where undefined function warnings were not being emitted
#### IEx
* [IEx.Helpers] Properly handle tags inside typespec when showing Erlang docs
#### Logger
* [Logger] Do not deadlock Logger if handler crashes on sync mode
#### Mix
* [Mix] Add inet6 fallback to Mix usage of httpc
* [mix compile.app] Do not list apps that do not match the current target
## v1.11.0 (2020-10-06)
### 1. Enhancements
#### EEx
* [EEx.Engine] `use EEx.Engine` is deprecated in favor of explicit delegation
* [EEx] Track column information in EEx templates when enabled in the compiler
* [EEx] Show column information in EEx error messages
* [EEx] Support `:indentation` option when compiling EEx templates for proper column tracking
#### Elixir
* [Kernel] The binary operator `^^^` is deprecated. If you are using `Bitwise.^^^/2`, use `Bitwise.bxor/2` instead
* [Kernel] Deprecate `@foo()` in favor of `@foo`
* [System] Deprecate `System.stacktrace/0` (it was already deprecated outside of catch/rescue and now it is deprecated everywhere)
#### Mix
* [mix compile] The `:xref` compiler is deprecated and it has no effect. Please remove it from your mix.exs file.
### 5. Backwards incompatible changes
A backward incompatible change was found on Elixir v1.12.0 after the release. This was not planned and it is documented below:
* [Access] Add `Access.at!/1`
* [Calendar] Add `Calendar.strftime/3` for datetime formatting
* [Calendar] Add linear integer representations to Calendar modules: `Date.from_gregorian_days/2`, `Date.to_gregorian_days/1`, `NaiveDateTime.from_gregorian_seconds/3`, `NaiveDateTime.to_gregorian_seconds/1`, `Time.from_seconds_after_midnight/1`, and `Time.to_seconds_after_midnight/1`
* [Calendar] Add `new!` to Date/Time/NaiveDateTime/DateTime (`new` has also been added to `DateTime` for completeness)
* [Calendar] Support custom starting day of the week in `Date.day_of_week/2`
* [Calendar] Add `Date.beginning_of_month/1` and `Date.end_of_month/1`
* [Calendar] Add `Date.beginning_of_week/2` and `Date.end_of_week/2`
* [Code] Add `:column` to `Code.string_to_quoted*/2`
* [Code] Add `Code.can_await_module_compilation?/0` to check if the parallel compiler is enabled and it can await for other modules to be compiled
* [Config] Support `config_env/0` and `config_target/0` in `config` files
* [Config] Allow `import_config` to be disabled for some configuration files
* [Enum] Allow a sorting function on `Enum.min_max_by/3,4`, including the new `compare/2` conventions
* [Kernel] Add `is_struct/2` guard
* [Kernel] Add `is_exception/1` and `is_exception/2` guards
* [Kernel] Support `map.field` syntax in guards
* [Kernel] Add `+++` and `---` with right associativity to the list of custom operators
* [Kernel] Warn if a variable that looks like a compiler variable (such as `__MODULE__`) is unused
* [Kernel.ParallelCompiler] Report individual file compilation times when `profile: :time` is given
* [Kernel.ParallelCompiler] Improve precision of `:long_compilation_threshold` so it takes only compilation times into account (and not waiting times)
* [Registry] Add `Registry.delete_meta/2`
* [Task] Add `Task.await_many/2`
#### ExUnit
* [ExUnit.Formatter] `{:suite_finished, load_time, run_time}` message has been updated to `{:suite_finished, %{load: load_time, run: run_time, async: async_time}}`
* [ExUnit] Add support for coloring on Windows 10 consoles/shells
* [ExUnit] Add `ExUnit.fetch_test_supervisor/0`
* [ExUnit] Add `@tag :tmp_dir` support to ExUnit. The temporary directory is automatically created and pruned before each test
* [ExUnit] Add file and line to ExUnit's `--trace`
* [ExUnit.Assertion] Allow receive timeouts to be computed at runtime
* [ExUnit.Case] Add `register_test/6` to speed up compilation of custom tests
* [ExUnit.Doctest] Allow users to add tags to doctests
## v1.11
#### IEx
The CHANGELOG for v1.11 releases can be found [in the v1.11 branch](https://github.com/elixir-lang/elixir/blob/v1.11/CHANGELOG.md).
* [IEx] Add support for coloring on Windows 10 consoles/shells
* [IEx.Helpers] Show docs from Erlang modules that have been compiled with the docs chunk
#### Logger
* [Logger] Add `notice`, `critical`, `alert`, and `emergency` log levels
* [Logger] Support structured logging by logging maps or keyword lists
* [Logger] Allow level to be set per module with `Logger.put_module_level/2`
* [Logger] Include `erl_level` in Logger's metadata
#### Mix
* [mix] Add `MIX_BUILD_ROOT` to config `_build` dir
* [mix] Introduce `MIX_XDG` as a simpler mechanism to opt-in to the XDG specification
* [mix] Allow requirements for a Mix task to be listed via the `@requirements` module attribute
* [mix] Allow optional dependencies to be defined in `:extra_applications` and `:applications`
* [mix app.config] Add new `mix app.config` task that compiles applications and loads runtime configuration
* [mix archive.install] Support `--repo` option on Hex packages
* [mix compile] Support the `__mix_recompile__?/0` callback for custom behaviour on when Mix should recompile a given module
* [mix compile.elixir] Mark modules for path dependencies as "Export dependencies" if they changed but their public interface is the same
* [mix compile.elixir] Track application boundaries in the Elixir compiler. If you invoke code from Erlang or Elixir standard libraries and you don't depend on the proper applications, a warning will be emitted. A warning will also be emitted if you invoke code from an umbrella sibling that you don't depend on - effectively forbidding cyclic dependencies between apps
* [mix deps] Sort the dependencies alphabetically before printing
* [mix deps] Use `origin/HEAD` as the default Git ref in dependencies
* [mix deps] Redact Git `username`/`password` in output log
* [mix deps] Support rebar3's `git_subdir` resource type
* [mix deps.compile] Allow local deps to be skipped on `mix deps.compile`
* [mix deps.unlock] Print which dependencies get unlocked when using the `--unused` flag
* [mix escript.install] Support `--repo` option on Hex packages
* [mix new] Add `@impl` to application generated by `mix new --sup`
* [mix release] Enable overriding `sys.config` location via `RELEASE_SYS_CONFIG` env var
* [mix release] Boot a release under configuration in interactive mode and then swap to embedded mode (if running on Erlang/OTP 23+)
* [mix release] Add `rel_templates_path` to configure the source of template files such as "env.sh.eex", "vm.args.eex" and "overlays"
* [mix release] Allow some chunks to be kept in the `:strip_beams` config
* [mix test] Allow `:ignore_modules` inside `:test_coverage` option
* [mix test.coverage] Add `mix test.coverage` that aggregates coverage results from umbrellas and OS partitioning
* [mix xref] Make the `--label` option for `mix xref graph` transitive by default and add `--only-direct` for only direct dependencies
* [mix xref] Add `--format cycles` support for `mix xref graph`
* [mix xref] Add support to `mix xref graph` for using `--source` and `--sink` at the same time
### 2. Bug fixes
#### EEx
* [EEx] Make trimming behaviour via the `:trim` option more consistent
#### Elixir
* [Application] Warn if non-atom keys are given to `put_env`, `get_env`, `fetch_env`, and `delete_env`
* [Code] Do not send language keyword through the `:static_atoms_encoder` in `Code.string_to_quoted`
* [Kernel] Validate values given to `:line` in quote to avoid emitting invalid ASTs
* [Kernel] Report the correct line number when raising inside a macro
* [Kernel] Fix an issue where `elixirc` would not accept paths with backslash (`\`) separators on Windows
* [Kernel] Properly parse `&//2` (i.e. the capture of the division operator)
* [Kernel] Raise `CompileError` when trying to define reserved types
* [Kernel] Improve compiler error message when using `|` in a `def` signature
* [Kernel] Improve error message when trying to use invalid list operators in guards
* [Kernel.SpecialForms] Add `|/2` to the list of special forms to avoid inconsistent behaviour on overrides
* [Keyword] Enforce keys to be atoms in `Keyword.keys/1`
* [Record] Keep lexical ordering when creating records
* [Registry] Do not crash when a process with key-value has been registered using `:via` and it fails to start on `init`
* [URI] `URI.decode_query/2` emits an empty string for parameters without values, according to [URL's living standard](https://url.spec.whatwg.org/#application/x-www-form-urlencoded) - note this behaviour is not specified in the spec implemented by the URI module, so the living standard was chosen
* [Version] Add defaults and enforce keys in `Version` struct
#### ExUnit
* [ExUnit.CaptureIO] Fix race condition where a dead capture would still be considered as active
* [ExUnit.Diff] Do not crash when failing to eval/inspect struct
* [ExUnit.Diff] Properly diff numbers in respect to `==` and `===` operators
#### IEx
* [IEx] Fix tokenizer emitting repeated warnings in the REPL
* [IEx] Ensure `--dot-iex` is preserved when restarting the evaluator and after shell respawn
* [IEx.Pry] Ensure `IEx.pry` can be triggered more than twice when invoked from the same process
#### Mix
* [mix cmd] Fix a bug where only the first --app option would be executed
* [mix compile] Fix an issue where new protocol implementations would not propagate when running `mix compile` from an umbrella root
* [mix deps.compile] Use `gmake` instead of `make` when compiling deps on NetBSD/DragonFlyBSD
* [mix release] Load `.app` from dependencies path when it is a project dependency
* [mix release] Always include "rel/overlays" in the list of overlays directories if available
* [mix release] Change `erts/bin/erl` binary mode to `0o755`
* [mix test] Compare to test coverage threshold inclusively
#### Logger
* [Logger] Print metadata for all types that implement String.Chars
### 3. Soft-deprecations (no warnings emitted)
### Elixir
* [Exception] `Exception.exception?/1` is deprecated in favor of `Kernel.is_exception/1`
* [Regex] `Regex.regex?/1` is deprecated in favor of `Kernel.is_struct/2`
### Logger
* [Logger] `warn` log level is deprecated in favor of `warning`
### Mix
* [mix release] `config/releases.exs` is deprecated in favor of a more general purpose `config/runtime.exs`
### 4. Hard-deprecations
#### Elixir
* [Supervisor] Deprecate `Supervisor.start_child/2` and `Supervisor.terminate_child/2` in favor of `DynamicSupervisor`
* [Supervisor.Spec] Deprecate `Supervisor.Spec.worker/3` and `Supervisor.Spec.supervisor/3` in favor of the new typespecs
* [System] Deprecate `System.stacktrace/0` in favor of `__STACKTRACE__`
#### Mix
* [Mix.Project] Deprecate `Mix.Project.compile/2` in favor of `Mix.Task.run("compile", args)`
## v1.10
The CHANGELOG for v1.10 releases can be found [in the v1.10 branch](https://github.com/elixir-lang/elixir/blob/v1.10/CHANGELOG.md).
+4 -5
View File
@@ -2,8 +2,7 @@ PREFIX ?= /usr/local
TEST_FILES ?= "*_test.exs"
SHARE_PREFIX ?= $(PREFIX)/share
MAN_PREFIX ?= $(SHARE_PREFIX)/man
CANONICAL := 1.12/
CANONICAL ?= master/
CANONICAL := v1.11/
ELIXIRC := bin/elixirc --ignore-module-conflict $(ELIXIRC_OPTS)
ERLC := erlc -I lib/elixir/include
ERL_MAKE := if [ -n "$(ERLC_OPTS)" ]; then ERL_COMPILER_OPTIONS=$(ERLC_OPTS) erl -make; else erl -make; fi
@@ -28,9 +27,9 @@ SOURCE_DATE_EPOCH_FILE = $(SOURCE_DATE_EPOCH_PATH)/SOURCE_DATE_EPOCH
#==> Functions
define CHECK_ERLANG_RELEASE
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 22)])' -s erlang halt | grep -q '^true'; \
erl -noshell -eval '{V,_} = string:to_integer(erlang:system_info(otp_release)), io:fwrite("~s", [is_integer(V) and (V >= 21)])' -s erlang halt | grep -q '^true'; \
if [ $$? != 0 ]; then \
echo "At least Erlang/OTP 22.0 is required to build Elixir"; \
echo "At least Erlang/OTP 21.0 is required to build Elixir"; \
exit 1; \
fi
endef
@@ -83,7 +82,7 @@ $(PARSER): lib/elixir/src/elixir_parser.yrl
# Since Mix depends on EEx and EEx depends on Mix,
# we first compile EEx without the .app file,
# then Mix, and then compile EEx fully
# then Mix and then compile EEx fully
elixir: stdlib lib/eex/ebin/Elixir.EEx.beam mix ex_unit logger eex iex
stdlib: $(KERNEL) VERSION
+7 -7
View File
@@ -1,4 +1,4 @@
<img src="https://github.com/elixir-lang/elixir-lang.github.com/raw/master/images/logo/logo.png" width="200" alt="Elixir">
# ![Elixir](https://github.com/elixir-lang/elixir-lang.github.com/raw/master/images/logo/logo.png)
[![CI](https://github.com/elixir-lang/elixir/workflows/CI/badge.svg?branch=master)](https://github.com/elixir-lang/elixir/actions?query=branch%3Amaster+workflow%3ACI) [![Build status](https://api.cirrus-ci.com/github/elixir-lang/elixir.svg?branch=master)](https://cirrus-ci.com/github/elixir-lang/elixir)
@@ -29,7 +29,7 @@ For the many different ways to install Elixir,
[see our installation instructions on the website](https://elixir-lang.org/install.html).
To compile from source, you can follow the steps below.
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang). After that, clone this repository to your machine, compile and test it:
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang). Then clone this repository to your machine, compile and test it:
```sh
git clone https://github.com/elixir-lang/elixir.git
@@ -48,10 +48,10 @@ If Elixir fails to build (specifically when pulling in a new version via
If tests pass, you can use Interactive Elixir by running `bin/iex` in your terminal.
However, if tests fail, it is likely that you have an outdated Erlang/OTP version
(Elixir requires Erlang/OTP 22.0 or later). You can check your Erlang/OTP version
(Elixir requires Erlang/OTP 21.0 or later). You can check your Erlang/OTP version
by calling `erl` in the command line. You will see some information similar to:
Erlang/OTP 22 [erts-9.0] [smp:2:2] [async-threads:10] [kernel-poll:false]
Erlang/OTP 21 [erts-9.0] [smp:2:2] [async-threads:10] [kernel-poll:false]
If you have properly set up your dependencies and tests still fail,
you may want to open up a bug report, as explained next.
@@ -131,9 +131,9 @@ With tests running and passing, you are ready to contribute to Elixir and
We have saved some excellent pull requests we have received in the past in
case you are looking for some examples:
* [Implement Enum.member? - Pull request](https://github.com/elixir-lang/elixir/pull/992)
* [Add String.valid? - Pull request](https://github.com/elixir-lang/elixir/pull/1058)
* [Implement capture_io for ExUnit - Pull request](https://github.com/elixir-lang/elixir/pull/1059)
* [Implement Enum.member? - Pull Request](https://github.com/elixir-lang/elixir/pull/992)
* [Add String.valid? - Pull Request](https://github.com/elixir-lang/elixir/pull/1058)
* [Implement capture_io for ExUnit - Pull Request](https://github.com/elixir-lang/elixir/pull/1059)
### Reviewing changes
+1 -2
View File
@@ -6,8 +6,7 @@ Elixir applies bug fixes only to the latest minor branch. Security patches are a
| Elixir version | Support
| -------------- | ------------------------------
| 1.12 | Bug fixes and security patches
| 1.11 | Security patches only
| 1.11 | Bug fixes and security patches
| 1.10 | Security patches only
| 1.9 | Security patches only
| 1.8 | Security patches only
+1 -1
View File
@@ -1 +1 @@
1.12.3
1.11.4
+14 -14
View File
@@ -70,7 +70,7 @@ readlink_f () {
ERL=""
# Stores erl arguments preserving spaces/quotes (mimics an array)
erl_set () {
erl () {
eval "E${E}=\$1"
E=$((E + 1))
}
@@ -137,34 +137,34 @@ while [ $I -le $LENGTH ]; do
;;
--cookie)
S=2
erl_set "-setcookie"
erl_set "$2"
erl "-setcookie"
erl "$2"
;;
--sname|--name)
S=2
erl_set "$(echo "$1" | cut -c 2-)"
erl_set "$2"
erl "$(echo "$1" | cut -c 2-)"
erl "$2"
;;
--erl-config)
S=2
erl_set "-config"
erl_set "$2"
erl "-config"
erl "$2"
;;
--vm-args)
S=2
erl_set "-args_file"
erl_set "$2"
erl "-args_file"
erl "$2"
;;
--boot)
S=2
erl_set "-boot"
erl_set "$2"
erl "-boot"
erl "$2"
;;
--boot-var)
S=3
erl_set "-boot_var"
erl_set "$2"
erl_set "$3"
erl "-boot_var"
erl "$2"
erl "$3"
;;
--pipe-to)
S=3
-3
View File
@@ -99,21 +99,18 @@ if !par!=="+elixirc" (set parsElixir=!parsElixir! +elixirc && set runMode="elixi
rem ******* EVAL PARAMETERS ************************
if ""==!par:-e=! (
set "VAR=%~1"
if not defined VAR (set VAR= )
set parsElixir=!parsElixir! -e "!VAR:"=\"!"
shift
goto startloop
)
if ""==!par:--eval=! (
set "VAR=%~1"
if not defined VAR (set VAR= )
set parsElixir=!parsElixir! --eval "!VAR:"=\"!"
shift
goto startloop
)
if ""==!par:--rpc-eval=! (
set "VAR=%~2"
if not defined VAR (set VAR= )
set parsElixir=!parsElixir! --rpc-eval %1 "!VAR:"=\"!"
shift
shift
+1 -2
View File
@@ -12,9 +12,8 @@ Usage: $(basename "$0") [elixir switches] [compiler switches] [.ex files]
--ignore-module-conflict Does not emit warnings if a module was previously defined
--no-debug-info Does not attach debug info to compiled modules
--no-docs Does not attach documentation to compiled modules
--profile time Profile the time to compile modules
--verbose Prints compilation status
--warnings-as-errors Treats warnings as errors and return non-zero exit status
--warnings-as-errors Treats warnings as errors and return non-zero exit code
Options given after -- are passed down to the executed code.
Options can be passed to the Erlang runtime using \$ELIXIR_ERL_OPTIONS.
+3 -4
View File
@@ -14,16 +14,15 @@ goto run
:documentation
echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
echo.
echo -h, --help Prints this message and exits
echo -o The directory to output compiled files
echo -v, --version Prints Elixir version and exits
echo.
echo --help, -h Prints this message and exits
echo --ignore-module-conflict Does not emit warnings if a module was previously defined
echo --no-debug-info Does not attach debug info to compiled modules
echo --no-docs Does not attach documentation to compiled modules
echo --profile time Profile the time to compile modules
echo --verbose Prints compilation status
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit status
echo --version, -v Prints Elixir version and exits
echo --warnings-as-errors Treats warnings as errors and returns non-zero exit code
echo.
echo ** Options given after -- are passed down to the executed code
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
+25 -72
View File
@@ -17,11 +17,11 @@ defmodule EEx do
## API
This module provides three main APIs for you to use:
This module provides 3 main APIs for you to use:
1. Evaluate a string (`eval_string/3`) or a file (`eval_file/3`)
directly. This is the simplest API to use but also the
slowest, since the code is evaluated at runtime and not precompiled.
slowest, since the code is evaluated and not compiled before.
2. Define a function from a string (`function_from_string/5`)
or a file (`function_from_file/5`). This allows you to embed
@@ -40,14 +40,13 @@ defmodule EEx do
They are:
* `:file` - the file to be used in the template. Defaults to the given
file the template is read from or to `"nofile"` when compiling from a string.
* `:line` - the line to be used as the template start. Defaults to `1`.
file the template is read from or to "nofile" when compiling from a string.
* `:line` - the line to be used as the template start. Defaults to 1.
* `:indentation` - (since v1.11.0) an integer added to the column after every
new line. Defaults to `0`.
new line. Defaults to 0.
* `:engine` - the EEx engine to be used for compilation.
* `:trim` - if `true`, trims whitespace left and right of quotation as
long as at least one newline is present. All subsequent newlines and
spaces are removed but one newline is retained. Defaults to `false`.
* `:trim` - if true, trims whitespace left/right of quotation tags up until
newlines. At least one newline is retained. Defaults to false.
## Engine
@@ -106,13 +105,10 @@ defmodule EEx do
"""
@doc """
Generates a function definition from the given string.
Generates a function definition from the string.
The first argument is the kind of the generated function (`:def` or `:defp`).
The `name` argument is the name that the generated function will have.
`template` is the string containing the EEx template. `args` is a list of arguments
that the generated function will accept. They will be available inside the EEx
template. `options` is a list of EEx compilation options (see the module documentation).
The kind (`:def` or `:defp`) must be given, the
function name, its arguments and the compilation options.
## Examples
@@ -124,11 +120,11 @@ defmodule EEx do
"3"
"""
defmacro function_from_string(kind, name, template, args \\ [], options \\ []) do
defmacro function_from_string(kind, name, source, args \\ [], options \\ []) do
quote bind_quoted: binding() do
info = Keyword.merge([file: __ENV__.file, line: __ENV__.line], options)
args = Enum.map(args, fn arg -> {arg, [line: info[:line]], nil} end)
compiled = EEx.compile_string(template, info)
compiled = EEx.compile_string(source, info)
case kind do
:def -> def unquote(name)(unquote_splicing(args)), do: unquote(compiled)
@@ -140,11 +136,8 @@ defmodule EEx do
@doc """
Generates a function definition from the file contents.
The first argument is the kind of the generated function (`:def` or `:defp`).
The `name` argument is the name that the generated function will have.
`file` is the path to the EEx template file. `args` is a list of arguments
that the generated function will accept. They will be available inside the EEx
template. `options` is a list of EEx compilation options (see the module documentation).
The kind (`:def` or `:defp`) must be given, the
function name, its arguments and the compilation options.
This function is useful in case you have templates but
you want to precompile inside a module for speed.
@@ -167,7 +160,7 @@ defmodule EEx do
"""
defmacro function_from_file(kind, name, file, args \\ [], options \\ []) do
quote bind_quoted: binding() do
info = Keyword.merge([file: IO.chardata_to_string(file), line: 1], options)
info = Keyword.merge(options, file: file, line: 1)
args = Enum.map(args, fn arg -> {arg, [line: 1], nil} end)
compiled = EEx.compile_file(file, info)
@@ -181,25 +174,8 @@ defmodule EEx do
end
@doc """
Gets a string `source` and generates a quoted expression
Gets a string `source` and generate a quoted expression
that can be evaluated by Elixir or compiled to a function.
This is useful if you want to compile a EEx template into code and inject
that code somewhere or evaluate it at runtime.
The generated quoted code will use variables defined in the template that
will be taken from the context where the code is evaluated. If you
have a template such as `<%= a + b %>`, then the returned quoted code
will use the `a` and `b` variables in the context where it's evaluated. See
examples below.
## Examples
iex> quoted = EEx.compile_string("<%= a + b %>")
iex> {result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
iex> result
"3"
"""
@spec compile_string(String.t(), keyword) :: Macro.t()
def compile_string(source, options \\ []) when is_binary(source) and is_list(options) do
@@ -207,34 +183,12 @@ defmodule EEx do
end
@doc """
Gets a `filename` and generates a quoted expression
Gets a `filename` and generate a quoted expression
that can be evaluated by Elixir or compiled to a function.
This is useful if you want to compile a EEx template into code and inject
that code somewhere or evaluate it at runtime.
The generated quoted code will use variables defined in the template that
will be taken from the context where the code is evaluated. If you
have a template such as `<%= a + b %>`, then the returned quoted code
will use the `a` and `b` variables in the context where it's evaluated. See
examples below.
## Examples
# sample.eex
<%= a + b %>
# In code:
quoted = EEx.compile_file("sample.eex")
{result, _bindings} = Code.eval_quoted(quoted, a: 1, b: 2)
result
#=> "3"
"""
@spec compile_file(Path.t(), keyword) :: Macro.t()
def compile_file(filename, options \\ []) when is_list(options) do
filename = IO.chardata_to_string(filename)
options = Keyword.merge([file: filename, line: 1], options)
@spec compile_file(String.t(), keyword) :: Macro.t()
def compile_file(filename, options \\ []) when is_binary(filename) and is_list(options) do
options = Keyword.merge(options, file: filename, line: 1)
compile_string(File.read!(filename), options)
end
@@ -247,7 +201,7 @@ defmodule EEx do
"foo baz"
"""
@spec eval_string(String.t(), keyword, keyword) :: String.t()
@spec eval_string(String.t(), keyword, keyword) :: any
def eval_string(source, bindings \\ [], options \\ [])
when is_binary(source) and is_list(bindings) and is_list(options) do
compiled = compile_string(source, options)
@@ -262,16 +216,15 @@ defmodule EEx do
# sample.eex
foo <%= bar %>
# IEx
# iex
EEx.eval_file("sample.eex", bar: "baz")
#=> "foo baz"
"""
@spec eval_file(Path.t(), keyword, keyword) :: String.t()
@spec eval_file(String.t(), keyword, keyword) :: any
def eval_file(filename, bindings \\ [], options \\ [])
when is_list(bindings) and is_list(options) do
filename = IO.chardata_to_string(filename)
options = Keyword.put_new(options, :file, filename)
when is_binary(filename) and is_list(bindings) and is_list(options) do
options = Keyword.put(options, :file, filename)
compiled = compile_file(filename, options)
do_eval(compiled, bindings, options)
end
+2 -17
View File
@@ -41,16 +41,8 @@ defmodule EEx.Compiler do
# Generates the buffers by handling each expression from the tokenizer.
# It returns Macro.t/0 or it raises.
defp generate_buffer([{:text, line, column, chars} | rest], buffer, scope, state) do
buffer =
if function_exported?(state.engine, :handle_text, 3) do
meta = [line: line, column: column]
state.engine.handle_text(buffer, meta, IO.chardata_to_string(chars))
else
# TODO: Remove this branch on Elixir v2.0
state.engine.handle_text(buffer, IO.chardata_to_string(chars))
end
defp generate_buffer([{:text, _line, _column, chars} | rest], buffer, scope, state) do
buffer = state.engine.handle_text(buffer, IO.chardata_to_string(chars))
generate_buffer(rest, buffer, scope, state)
end
@@ -67,13 +59,6 @@ defmodule EEx.Compiler do
scope,
state
) do
if mark != '=' do
message =
"the contents of this expression won't be output unless the EEx block starts with \"<%=\""
:elixir_errors.erl_warn(start_line, state.file, message)
end
{contents, line, rest} = look_ahead_middle(rest, start_line, chars)
{contents, rest} =
+13 -15
View File
@@ -4,8 +4,10 @@ defmodule EEx.Engine do
An engine needs to implement all callbacks below.
This module also ships with a default engine implementation
you can delegate to. See `EEx.SmartEngine` as an example.
An engine may also `use EEx.Engine` to get the default behaviour
but this is not advised. In such cases, if any of the callbacks
are overridden, they must call `super()` to delegate to the
underlying `EEx.Engine`.
"""
@type state :: term
@@ -29,8 +31,7 @@ defmodule EEx.Engine do
It must return the updated state.
"""
@callback handle_text(state, [line: pos_integer, column: pos_integer], text :: String.t()) ::
state
@callback handle_text(state, text :: String.t()) :: state
@doc """
Called for the dynamic/code parts of a template.
@@ -68,7 +69,6 @@ defmodule EEx.Engine do
@callback handle_end(state) :: Macro.t()
@doc false
@deprecated "Use explicit delegation to EEx.Engine instead"
defmacro __using__(_) do
quote do
@behaviour EEx.Engine
@@ -90,7 +90,7 @@ defmodule EEx.Engine do
end
def handle_text(state, text) do
EEx.Engine.handle_text(state, [], text)
EEx.Engine.handle_text(state, text)
end
def handle_expr(state, marker, expr) do
@@ -147,7 +147,7 @@ defmodule EEx.Engine do
end
end
@doc "Default implementation for `c:init/1`."
@doc false
def init(_opts) do
%{
binary: [],
@@ -156,18 +156,18 @@ defmodule EEx.Engine do
}
end
@doc "Default implementation for `c:handle_begin/1`."
@doc false
def handle_begin(state) do
check_state!(state)
%{state | binary: [], dynamic: []}
end
@doc "Default implementation for `c:handle_end/1`."
@doc false
def handle_end(quoted) do
handle_body(quoted)
end
@doc "Default implementation for `c:handle_body/1`."
@doc false
def handle_body(state) do
check_state!(state)
%{binary: binary, dynamic: dynamic} = state
@@ -176,16 +176,14 @@ defmodule EEx.Engine do
{:__block__, [], Enum.reverse(dynamic)}
end
@doc "Default implementation for `c:handle_text/3`."
def handle_text(state, _meta, text) do
check_state!(state)
@doc false
def handle_text(state, text) do
%{binary: binary} = state
%{state | binary: [text | binary]}
end
@doc "Default implementation for `c:handle_expr/3`."
@doc false
def handle_expr(state, "=", ast) do
check_state!(state)
%{binary: binary, dynamic: dynamic, vars_count: vars_count} = state
var = Macro.var(:"arg#{vars_count}", __MODULE__)
+3 -19
View File
@@ -33,26 +33,10 @@ defmodule EEx.SmartEngine do
"""
@behaviour EEx.Engine
use EEx.Engine
@impl true
defdelegate init(opts), to: EEx.Engine
@impl true
defdelegate handle_body(state), to: EEx.Engine
@impl true
defdelegate handle_begin(state), to: EEx.Engine
@impl true
defdelegate handle_end(state), to: EEx.Engine
@impl true
defdelegate handle_text(state, meta, text), to: EEx.Engine
@impl true
def handle_expr(state, marker, expr) do
def handle_expr(buffer, mark, expr) do
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
EEx.Engine.handle_expr(state, marker, expr)
super(buffer, mark, expr)
end
end
-9
View File
@@ -43,15 +43,6 @@ defmodule EEx.SmartEngineTest do
assert_received :found
end
test "error with unused \"do\" block without \"<%=\" modifier" do
stderr =
ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert_eval("", "<% if true do %>I'm invisible!<% end %>", assigns: %{})
end)
assert stderr =~ "the contents of this expression won't be output"
end
defp assert_eval(expected, actual, binding \\ []) do
result = EEx.eval_string(actual, binding, file: __ENV__.file, engine: EEx.SmartEngine)
assert result == expected
+24 -34
View File
@@ -87,11 +87,7 @@ defmodule EExTest do
expected = "123\n456\n789"
assert_eval(expected, string, [], trim: true)
string = "<%= 123 %> \n\n456\n\n <%= 789 %>"
expected = "123\n456\n789"
assert_eval(expected, string, [], trim: true)
string = "<%= 123 %> \n \n456\n \n <%= 789 %>"
string = "<%= 123 %> \n <%= 456 %> \n <%= 789 %>"
expected = "123\n456\n789"
assert_eval(expected, string, [], trim: true)
@@ -260,7 +256,7 @@ defmodule EExTest do
assert_raise EEx.SyntaxError,
"nofile:1:18: unexpected middle of expression <% else %>",
fn ->
EEx.compile_string("<%= if true %>foo<% else %>bar<% end %>")
EEx.compile_string("<% if true %> foo<% else %>bar<% end %>")
end
end
@@ -271,16 +267,16 @@ defmodule EExTest do
end
test "when start expression is found without an end expression" do
msg = "nofile:2:18: unexpected end of string, expected a closing '<% end %>'"
msg = "nofile:2:17: unexpected end of string, expected a closing '<% end %>'"
assert_raise EEx.SyntaxError, msg, fn ->
EEx.compile_string("foo\n<%= if true do %>")
EEx.compile_string("foo\n<% if true do %>")
end
end
test "when nested end expression is found without a start expression" do
assert_raise EEx.SyntaxError, "nofile:1:31: unexpected end of expression <% end %>", fn ->
EEx.compile_string("foo <%= if true do %><% end %><% end %>")
assert_raise EEx.SyntaxError, "nofile:1:30: unexpected end of expression <% end %>", fn ->
EEx.compile_string("foo <% if true do %><% end %><% end %>")
end
end
@@ -528,7 +524,7 @@ defmodule EExTest do
<% "a" in y -> %>
Good
<% true -> %>
<%= if true do %>true<% else %>false<% end %>
<% if true do %>true<% else %>false<% end %>
Bad
<% end %>
"""
@@ -573,6 +569,18 @@ defmodule EExTest do
end
describe "buffers" do
test "unused buffers are kept out" do
string = """
<%= 123 %>
<% if true do %>
<%= 456 %>
<% end %>
<%= 789 %>
"""
assert_eval("123\n\n789\n", string)
end
test "inside comprehensions" do
string = """
<%= for _name <- packages || [] do %>
@@ -610,24 +618,6 @@ defmodule EExTest do
assert EExTest.Compiled.__info__(:attributes)[:external_resource] ==
[Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex")]
end
test "supports t:Path.t() paths" do
filename = to_charlist(Path.join(__DIR__, "fixtures/eex_template_with_bindings.eex"))
result = EEx.eval_file(filename, bar: 1)
assert_normalized_newline_equal("foo 1\n", result)
end
assert_raise EEx.SyntaxError, "my_file.eex:1:12: missing token '%>'", fn ->
EEx.compile_string("foo <%= bar", file: "my_file.eex")
end
test "supports overriding file and line through options" do
filename = Path.join(__DIR__, "fixtures/eex_template_with_syntax_error.eex")
assert_raise EEx.SyntaxError, "my_file.eex:11:1: missing token '%>'", fn ->
EEx.eval_file(filename, _bindings = [], file: "my_file.eex", line: 10)
end
end
end
describe "precompiled" do
@@ -673,8 +663,8 @@ defmodule EExTest do
buffer <> ":END"
end
def handle_text(buffer, meta, text) do
buffer <> ":TEXT-#{meta[:line]}-#{meta[:column]}(#{String.trim(text)})"
def handle_text(buffer, text) do
buffer <> ":TEXT(#{String.trim(text)})"
end
def handle_expr(buffer, "/", expr) do
@@ -692,16 +682,16 @@ defmodule EExTest do
describe "custom engines" do
test "text" do
assert_eval("BODY(INIT:TEXT-1-1(foo))", "foo", [], engine: TestEngine)
assert_eval("BODY(INIT:TEXT(foo))", "foo", [], engine: TestEngine)
end
test "custom marker" do
assert_eval("BODY(INIT:TEXT-1-1(foo):DIV(:bar))", "foo <%/ :bar %>", [], engine: TestEngine)
assert_eval("BODY(INIT:TEXT(foo):DIV(:bar))", "foo <%/ :bar %>", [], engine: TestEngine)
end
test "begin/end" do
assert_eval(
~s[BODY(INIT:TEXT-1-1(foo):EQUAL(if do\n "BEGIN:TEXT-1-17(this):END"\nelse\n "BEGIN:TEXT-1-31(that):END"\nend))],
~s[BODY(INIT:TEXT(foo):EQUAL(if do\n "BEGIN:TEXT(this):END"\nelse\n "BEGIN:TEXT(that):END"\nend))],
"foo <%= if do %>this<% else %>that<% end %>",
[],
engine: TestEngine
@@ -1 +0,0 @@
foo <%= bar
+35 -36
View File
@@ -7,7 +7,7 @@ defmodule Access do
`Access` supports keyword lists (`Keyword`) and maps (`Map`) out
of the box. Keywords supports only atoms keys, keys for maps can
be of any type. Both return `nil` if the key does not exist:
be of any type. Both returns `nil` if the key does not exist:
iex> keywords = [a: 1, b: 2]
iex> keywords[:a]
@@ -47,11 +47,12 @@ defmodule Access do
> `map[key]`, if your map is made of predefined atom keys,
> you should prefer to access those atom keys with `map.key`
> instead of `map[key]`, as `map.key` will raise if the key
> is missing (which is not supposed to happen if the keys are
> predefined). Similarly, since structs are maps and structs
> have predefined keys, they only allow the `struct.key`
> syntax and they do not allow the `struct[key]` access syntax.
> See the `Map` module for more information.
> is missing. This is important because, if a map has a predefined
> set of keys and a key is missing, it is most likely a bug
> in your software or a typo on the key name. For this reason,
> because structs are predefined in nature, they only allow
> the `struct.key` syntax and they do not allow the `struct[key]`
> access syntax. See the `Map` module for more information.
## Nested data structures
@@ -99,15 +100,16 @@ defmodule Access do
@type key :: any
@type value :: any
@type get_fun(data) ::
(:get, data, (term -> term) -> new_data :: container)
@type get_fun(data, get_value) ::
(:get, data, (term -> term) ->
{get_value, new_data :: container})
@type get_and_update_fun(data, current_value) ::
@type get_and_update_fun(data, get_value) ::
(:get_and_update, data, (term -> term) ->
{current_value, new_data :: container} | :pop)
{get_value, new_data :: container} | :pop)
@type access_fun(data, current_value) ::
get_fun(data) | get_and_update_fun(data, current_value)
@type access_fun(data, get_value) ::
get_fun(data, get_value) | get_and_update_fun(data, get_value)
@doc """
Invoked in order to access the value stored under `key` in the given term `term`.
@@ -133,16 +135,16 @@ defmodule Access do
The implementation of this callback should invoke `fun` with the value under
`key` in the passed structure `data`, or with `nil` if `key` is not present in it.
This function must return either `{current_value, new_value}` or `:pop`.
This function must return either `{get_value, update_value}` or `:pop`.
If the passed function returns `{current_value, new_value}`,
the return value of this callback should be `{current_value, new_data}`, where:
If the passed function returns `{get_value, update_value}`,
the return value of this callback should be `{get_value, new_data}`, where:
* `current_value` is the retrieved value (which can be operated on before being returned)
* `get_value` is the retrieved value (which can be operated on before being returned)
* `new_value` is the new value to be stored under `key`
* `update_value` is the new value to be stored under `key`
* `new_data` is `data` after updating the value of `key` with `new_value`.
* `new_data` is `data` after updating the value of `key` with `update_value`.
If the passed function returns `:pop`, the return value of this callback
must be `{value, new_data}` where `value` is the value under `key`
@@ -151,9 +153,8 @@ defmodule Access do
See the implementations of `Map.get_and_update/3` or `Keyword.get_and_update/3`
for more examples.
"""
@callback get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_data :: data}
when current_value: value, data: container | any_container
@callback get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
when get_value: var, data: container | any_container
@doc """
Invoked to "pop" the value under `key` out of the given data structure.
@@ -320,9 +321,9 @@ defmodule Access do
a struct that implements the `Access` behaviour).
The `fun` argument receives the value of `key` (or `nil` if `key` is not
present in `container`) and must return a two-element tuple `{current_value, new_value}`:
the "get" value `current_value` (the retrieved value, which can be operated on before
being returned) and the new value to be stored under `key` (`new_value`).
present in `container`) and must return a two-element tuple `{get_value, update_value}`:
the "get" value `get_value` (the retrieved value, which can be operated on before
being returned) and the new value to be stored under `key` (`update_value`).
`fun` may also return `:pop`, which means the current value
should be removed from the container and returned.
@@ -337,9 +338,8 @@ defmodule Access do
{1, [a: 2]}
"""
@spec get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_data :: data}
when current_value: var, data: container
@spec get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
when get_value: var, data: container
def get_and_update(container, key, fun)
def get_and_update(%module{} = container, key, fun) do
@@ -422,7 +422,7 @@ defmodule Access do
The returned function uses the default value if the key does not exist.
This can be used to specify defaults and safely traverse missing keys:
iex> get_in(%{}, [Access.key(:user, %{}), Access.key(:name, "meg")])
iex> get_in(%{}, [Access.key(:user, %{name: "meg"}), Access.key(:name)])
"meg"
Such is also useful when using update functions, allowing us to introduce
@@ -452,7 +452,7 @@ defmodule Access do
** (BadMapError) expected a map, got: []
"""
@spec key(key, term) :: access_fun(data :: struct | map, current_value :: term)
@spec key(key, term) :: access_fun(data :: struct | map, get_value :: term)
def key(key, default \\ nil) do
fn
:get, data, next ->
@@ -496,7 +496,7 @@ defmodule Access do
** (RuntimeError) Access.key!/1 expected a map/struct, got: []
"""
@spec key!(key) :: access_fun(data :: struct | map, current_value :: term)
@spec key!(key) :: access_fun(data :: struct | map, get_value :: term)
def key!(key) do
fn
:get, %{} = data, next ->
@@ -544,7 +544,7 @@ defmodule Access do
** (RuntimeError) Access.elem/1 expected a tuple, got: %{}
"""
@spec elem(non_neg_integer) :: access_fun(data :: tuple, current_value :: term)
@spec elem(non_neg_integer) :: access_fun(data :: tuple, get_value :: term)
def elem(index) when is_integer(index) and index >= 0 do
pos = index + 1
@@ -598,7 +598,7 @@ defmodule Access do
** (RuntimeError) Access.all/0 expected a list, got: %{}
"""
@spec all() :: access_fun(data :: list, current_value :: list)
@spec all() :: access_fun(data :: list, get_value :: list)
def all() do
&all/3
end
@@ -673,7 +673,7 @@ defmodule Access do
** (RuntimeError) Access.at/1 expected a list, got: %{}
"""
@spec at(integer) :: access_fun(data :: list, current_value :: term)
@spec at(integer) :: access_fun(data :: list, get_value :: term)
def at(index) when is_integer(index) do
fn op, data, next -> at(op, data, index, next) end
end
@@ -727,8 +727,7 @@ defmodule Access do
** (Enum.OutOfBoundsError) out of bounds error
"""
@doc since: "1.11.0"
@spec at!(integer) :: access_fun(data :: list, current_value :: term)
@spec at!(integer) :: access_fun(data :: list, get_value :: term)
def at!(index) when is_integer(index) do
fn op, data, next -> at!(op, data, index, next) end
end
@@ -795,7 +794,7 @@ defmodule Access do
"""
@doc since: "1.6.0"
@spec filter((term -> boolean)) :: access_fun(data :: list, current_value :: list)
@spec filter((term -> boolean)) :: access_fun(data :: list, get_value :: list)
def filter(func) when is_function(func) do
fn op, data, next -> filter(op, data, func, next) end
end
+1 -1
View File
@@ -241,7 +241,7 @@ defmodule Agent do
and the start function will return `{:error, :timeout}`.
If the `:debug` option is present, the corresponding function in the
[`:sys` module](`:sys`) will be invoked.
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
If the `:spawn_opt` option is present, its value will be passed as options
to the underlying process as in `Process.spawn/4`.
+26 -23
View File
@@ -163,7 +163,7 @@ defmodule Application do
In the sections above, we have configured an application in the
`application/0` section of the `mix.exs` file. Ultimately, Mix will use
this configuration to create an [*application resource
file*](https://erlang.org/doc/man/application.html), which is a file called
file*](http://erlang.org/doc/man/app.html), which is a file called
`APP_NAME.app`. For example, the application resource file of the OTP
application `ex_unit` is called `ex_unit.app`.
@@ -267,10 +267,11 @@ defmodule Application do
## Further information
For further details on applications please check the documentation of the
[`:application` Erlang module](`:application`), and the
[Applications](https://erlang.org/doc/design_principles/applications.html)
[`application`](http://www.erlang.org/doc/man/application.html) Erlang module,
and the
[Applications](http://www.erlang.org/doc/design_principles/applications.html)
section of the [OTP Design Principles User's
Guide](https://erlang.org/doc/design_principles/users_guide.html).
Guide](http://erlang.org/doc/design_principles/users_guide.html).
"""
@doc """
@@ -498,8 +499,7 @@ defmodule Application do
Giving a path is useful to let Elixir know that only certain paths
in a large configuration are compile time dependent.
"""
# TODO: Warn on v1.14 if get_env/fetch_env/fetch_env! is used at
# compile time instead of compile_env
# TODO: Warn on v1.14 if get_env/fetch_env/fetch_env! is used at compile time instead of compile_env
@doc since: "1.10.0"
@spec compile_env(app, key | list, value) :: value
defmacro compile_env(app, key_or_path, default \\ nil) when is_atom(app) do
@@ -507,22 +507,11 @@ defmodule Application do
raise "Application.compile_env/3 cannot be called inside functions, only in the module body"
end
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
quote do
Application.__compile_env__(unquote(app), unquote(key_or_path), unquote(default), __ENV__)
end
end
defp expand_key_or_path({:__aliases__, _, _} = alias, env),
do: Macro.expand(alias, %{env | function: {:__info__, 1}})
defp expand_key_or_path(list, env) when is_list(list),
do: Enum.map(list, &expand_key_or_path(&1, env))
defp expand_key_or_path(other, _env),
do: other
@doc false
def __compile_env__(app, key_or_path, default, env) do
case fetch_compile_env(app, key_or_path, env) do
@@ -544,8 +533,6 @@ defmodule Application do
raise "Application.compile_env!/2 cannot be called inside functions, only in the module body"
end
key_or_path = expand_key_or_path(key_or_path, __CALLER__)
quote do
Application.__compile_env__!(unquote(app), unquote(key_or_path), __ENV__)
end
@@ -707,6 +694,9 @@ defmodule Application do
:application.set_env(app, key, value, opts)
end
# TODO: Remove this once we support Erlang/OTP 22+ exclusively.
@compile {:no_warn_undefined, {:application, :set_env, 2}}
@doc """
Puts the environment for multiple apps at the same time.
@@ -715,14 +705,28 @@ defmodule Application do
* have the same application listed more than once
* have the same key inside the same application listed more than once
If those conditions are not met, it will raise.
If those conditions are not met, the behaviour is undefined
(on Erlang/OTP 21 and earlier) or will raise (on Erlang/OTP 22
and later).
It receives the same options as `put_env/4`. Returns `:ok`.
"""
@doc since: "1.9.0"
@spec put_all_env([{app, [{key, value}]}], timeout: timeout, persistent: boolean) :: :ok
def put_all_env(config, opts \\ []) when is_list(config) and is_list(opts) do
:application.set_env(config, opts)
# TODO: Remove function exported? check when we require Erlang/OTP 22+
if function_exported?(:application, :set_env, 2) do
:application.set_env(config, opts)
else
for app_keyword <- config,
{app, keyword} = app_keyword,
key_value <- keyword,
{key, value} = key_value do
:application.set_env(app, key, value, opts)
end
:ok
end
end
@doc """
@@ -739,7 +743,6 @@ defmodule Application do
defp maybe_warn_on_app_env_key(_app, key) when is_atom(key),
do: :ok
# TODO: Remove this deprecation warning on 2.0+ and allow list lookups as in compile_env.
defp maybe_warn_on_app_env_key(app, key) do
message = "passing non-atom as application env key is deprecated, got: #{inspect(key)}"
IO.warn_once({Application, :key, app, key}, message, _stacktrace_drop_levels = 2)
@@ -884,7 +887,7 @@ defmodule Application do
#=> "bar-123"
For more information on code paths, check the `Code` module in
Elixir and also Erlang's [`:code` module](`:code`).
Elixir and also Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html).
"""
@spec app_dir(app) :: String.t()
def app_dir(app) when is_atom(app) do
+1 -3
View File
@@ -4,7 +4,7 @@ defmodule Behaviour do
This module is deprecated. Instead of `defcallback/1` and
`defmacrocallback/1`, the `@callback` and `@macrocallback`
module attributes can be used respectively. See the
module attributes can be used (respectively). See the
documentation for `Module` for more information on these
attributes.
@@ -17,7 +17,6 @@ defmodule Behaviour do
@doc """
Defines a function callback according to the given type specification.
"""
@deprecated "Use the @callback module attribute instead"
defmacro defcallback(spec) do
do_defcallback(:def, split_spec(spec, quote(do: term)))
end
@@ -25,7 +24,6 @@ defmodule Behaviour do
@doc """
Defines a macro callback according to the given type specification.
"""
@deprecated "Use the @macrocallback module attribute instead"
defmacro defmacrocallback(spec) do
do_defcallback(:defmacro, split_spec(spec, quote(do: Macro.t())))
end
+16 -2
View File
@@ -191,8 +191,22 @@ defmodule Bitwise do
:erlang.bxor(left, right)
end
@doc false
def unquote(:^^^)(left, right) do
@doc """
Bitwise XOR operator.
Calculates the bitwise XOR of its arguments.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> 9 ^^^ 3
10
"""
@doc guard: true
@spec integer ^^^ integer :: integer
def left ^^^ right do
:erlang.bxor(left, right)
end
+1 -4
View File
@@ -453,7 +453,7 @@ defmodule Calendar do
b | Abbreviated month name | Jan
B | Full month name | January
c | Preferred date+time representation | 2018-10-17 12:34:56
d | Day of the month | 01, 31
d | Day of the month | 01, 12
f | Microseconds *(does not support width and padding modifiers)* | 000000, 999999, 0123
H | Hour using a 24-hour clock | 00, 23
I | Hour using a 12-hour clock | 01, 12
@@ -485,9 +485,6 @@ defmodule Calendar do
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%a, %B %d %Y")
"Mon, August 26 2019"
iex> Calendar.strftime(~U[2020-04-02 13:52:06.0Z], "%B %-d, %Y")
"April 2, 2020"
iex> Calendar.strftime(~U[2019-08-26 13:52:06.0Z], "%c")
"2019-08-26 13:52:06"
+14 -57
View File
@@ -83,8 +83,8 @@ defmodule Date do
366
iex> Enum.member?(range, ~D[2001-02-01])
true
iex> Enum.take(range, 3)
[~D[2001-01-01], ~D[2001-01-02], ~D[2001-01-03]]
iex> Enum.reduce(range, 0, fn _date, acc -> acc - 1 end)
-366
"""
@doc since: "1.5.0"
@@ -92,61 +92,19 @@ defmodule Date do
def range(%{calendar: calendar} = first, %{calendar: calendar} = last) do
{first_days, _} = to_iso_days(first)
{last_days, _} = to_iso_days(last)
# TODO: Deprecate inferring a range with a step of -1 on Elixir v1.16
step = if first_days <= last_days, do: 1, else: -1
range(first, first_days, last, last_days, calendar, step)
end
def range(%{calendar: _, year: _, month: _, day: _}, %{calendar: _, year: _, month: _, day: _}) do
raise ArgumentError, "both dates must have matching calendars"
end
@doc """
Returns a range of dates with a step.
## Examples
iex> range = Date.range(~D[2001-01-01], ~D[2002-01-01], 2)
iex> range
#DateRange<~D[2001-01-01], ~D[2002-01-01], 2>
iex> Enum.count(range)
183
iex> Enum.member?(range, ~D[2001-01-03])
true
iex> Enum.take(range, 3)
[~D[2001-01-01], ~D[2001-01-03], ~D[2001-01-05]]
"""
@doc since: "1.12.0"
@spec range(Calendar.date(), Calendar.date(), step :: pos_integer | neg_integer) ::
Date.Range.t()
def range(%{calendar: calendar} = first, %{calendar: calendar} = last, step)
when is_integer(step) and step != 0 do
{first_days, _} = to_iso_days(first)
{last_days, _} = to_iso_days(last)
range(first, first_days, last, last_days, calendar, step)
end
def range(
%{calendar: _, year: _, month: _, day: _} = first,
%{calendar: _, year: _, month: _, day: _} = last,
step
) do
raise ArgumentError,
"both dates must have matching calendar and the step must be a " <>
"non-zero integer, got: #{inspect(first)}, #{inspect(last)}, #{step}"
end
defp range(first, first_days, last, last_days, calendar, step) do
%Date.Range{
first: %Date{calendar: calendar, year: first.year, month: first.month, day: first.day},
last: %Date{calendar: calendar, year: last.year, month: last.month, day: last.day},
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
last_in_iso_days: last_days
}
end
def range(%{calendar: _, year: _, month: _, day: _}, %{calendar: _, year: _, month: _, day: _}) do
raise ArgumentError, "both dates must have matching calendars"
end
@doc """
Returns the current date in UTC.
@@ -315,7 +273,7 @@ defmodule Date do
@doc """
Parses the extended "Dates" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
The year parsed by this function is limited to four digits.
@@ -340,7 +298,7 @@ defmodule Date do
@doc """
Parses the extended "Dates" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Raises if the format is invalid.
@@ -365,7 +323,7 @@ defmodule Date do
@doc """
Converts the given `date` to
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
By default, `Date.to_iso8601/2` returns dates formatted in the "extended"
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
@@ -542,8 +500,8 @@ defmodule Date do
end
end
def compare(%{calendar: calendar1} = date1, %{calendar: calendar2} = date2) do
if Calendar.compatible_calendars?(calendar1, calendar2) do
def compare(date1, date2) do
if Calendar.compatible_calendars?(date1.calendar, date2.calendar) do
case {to_iso_days(date1), to_iso_days(date2)} do
{first, second} when first > second -> :gt
{first, second} when first < second -> :lt
@@ -701,12 +659,11 @@ defmodule Date do
end
end
@doc false
def to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
defp to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
{Calendar.ISO.date_to_iso_days(year, month, day), {0, 86_400_000_000}}
end
def to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
defp to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
calendar.naive_datetime_to_iso_days(year, month, day, 0, 0, 0, {0, 0})
end
+63 -157
View File
@@ -2,13 +2,12 @@ defmodule Date.Range do
@moduledoc """
Returns an inclusive range between dates.
Ranges must be created with the `Date.range/2` or `Date.range/3` function.
Ranges must be created with the `Date.range/2` function.
The following fields are public:
* `:first` - the initial date on the range
* `:last` - the last date on the range
* `:step` - (since v1.12.0) the step
The remaining fields are private and should not be accessed.
"""
@@ -17,132 +16,98 @@ defmodule Date.Range do
first: Date.t(),
last: Date.t(),
first_in_iso_days: iso_days(),
last_in_iso_days: iso_days(),
step: pos_integer | neg_integer
last_in_iso_days: iso_days()
}
@typep iso_days() :: Calendar.iso_days()
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days, :step]
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days]
defimpl Enumerable do
def member?(
%Date.Range{
first: %{calendar: calendar},
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
} = range,
%Date{calendar: calendar} = date
) do
{days, _} = Date.to_iso_days(date)
def member?(%{first: %{calendar: calendar}} = range, %Date{calendar: calendar} = date) do
%{
first: first,
last: last,
first_in_iso_days: first_in_iso_days,
last_in_iso_days: last_in_iso_days
} = range
cond do
empty?(range) ->
{:ok, false}
%{year: first_year, month: first_month, day: first_day} = first
%{year: last_year, month: last_month, day: last_day} = last
%{year: year, month: month, day: day} = date
first = {first_year, first_month, first_day}
last = {last_year, last_month, last_day}
date = {year, month, day}
first_days <= last_days ->
{:ok, first_days <= days and days <= last_days and rem(days - first_days, step) == 0}
true ->
{:ok, last_days <= days and days <= first_days and rem(days - first_days, step) == 0}
if first_in_iso_days <= last_in_iso_days do
{:ok, date >= first and date <= last}
else
{:ok, date >= last and date <= first}
end
end
def member?(%Date.Range{step: _}, _) do
def member?(_, _) do
{:ok, false}
end
# TODO: Remove me on v2.0
def member?(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range,
date
) do
step = if first_days <= last_days, do: 1, else: -1
member?(Map.put(date_range, :step, step), date)
def count(%{first_in_iso_days: first, last_in_iso_days: last}) do
{:ok, abs(first - last) + 1}
end
def count(range) do
{:ok, size(range)}
def slice(range) do
%{
first_in_iso_days: first,
last_in_iso_days: last,
first: %{calendar: calendar}
} = range
if first <= last do
{:ok, last - first + 1, &slice_asc(first + &1, &2, calendar)}
else
{:ok, first - last + 1, &slice_desc(first - &1, &2, calendar)}
end
end
def slice(
%Date.Range{
first_in_iso_days: first,
first: %{calendar: calendar},
step: step
} = range
) do
{:ok, size(range), &slice(first + &1 * step, step, &2, calendar)}
defp slice_asc(current, 1, calendar), do: [date_from_iso_days(current, calendar)]
defp slice_asc(current, remaining, calendar) do
[date_from_iso_days(current, calendar) | slice_asc(current + 1, remaining - 1, calendar)]
end
# TODO: Remove me on v2.0
def slice(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range
) do
step = if first_days <= last_days, do: 1, else: -1
slice(Map.put(date_range, :step, step))
defp slice_desc(current, 1, calendar), do: [date_from_iso_days(current, calendar)]
defp slice_desc(current, remaining, calendar) do
[date_from_iso_days(current, calendar) | slice_desc(current - 1, remaining - 1, calendar)]
end
defp slice(current, _step, 1, calendar) do
[date_from_iso_days(current, calendar)]
def reduce(range, acc, fun) do
%{
first_in_iso_days: first_in_iso_days,
last_in_iso_days: last_in_iso_days,
first: %{calendar: calendar}
} = range
up? = first_in_iso_days <= last_in_iso_days
reduce(first_in_iso_days, last_in_iso_days, acc, fun, calendar, up?)
end
defp slice(current, step, remaining, calendar) do
[
date_from_iso_days(current, calendar)
| slice(current + step, step, remaining - 1, calendar)
]
end
def reduce(
%Date.Range{
first_in_iso_days: first_days,
last_in_iso_days: last_days,
first: %{calendar: calendar},
step: step
},
acc,
fun
) do
reduce(first_days, last_days, acc, fun, step, calendar)
end
# TODO: Remove me on v2.0
def reduce(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range,
acc,
fun
) do
step = if first_days <= last_days, do: 1, else: -1
reduce(Map.put(date_range, :step, step), acc, fun)
end
defp reduce(_first_days, _last_days, {:halt, acc}, _fun, _step, _calendar) do
defp reduce(_x, _y, {:halt, acc}, _fun, _calendar, _up?) do
{:halted, acc}
end
defp reduce(first_days, last_days, {:suspend, acc}, fun, step, calendar) do
{:suspended, acc, &reduce(first_days, last_days, &1, fun, step, calendar)}
defp reduce(x, y, {:suspend, acc}, fun, calendar, up?) do
{:suspended, acc, &reduce(x, y, &1, fun, calendar, up?)}
end
defp reduce(first_days, last_days, {:cont, acc}, fun, step, calendar)
when step > 0 and first_days <= last_days
when step < 0 and first_days >= last_days do
reduce(
first_days + step,
last_days,
fun.(date_from_iso_days(first_days, calendar), acc),
fun,
step,
calendar
)
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = true) when x <= y do
reduce(x + 1, y, fun.(date_from_iso_days(x, calendar), acc), fun, calendar, up?)
end
defp reduce(_, _, {:cont, acc}, _fun, _step, _calendar) do
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = false) when x >= y do
reduce(x - 1, y, fun.(date_from_iso_days(x, calendar), acc), fun, calendar, up?)
end
defp reduce(_, _, {:cont, acc}, _fun, _calendar, _up) do
{:done, acc}
end
@@ -157,70 +122,11 @@ defmodule Date.Range do
%Date{year: year, month: month, day: day, calendar: calendar}
end
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
when step > 0 and first_days > last_days,
do: 0
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step})
when step < 0 and first_days < last_days,
do: 0
defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step}),
do: abs(div(last_days - first_days, step)) + 1
# TODO: Remove me on v2.0
defp size(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range
) do
step = if first_days <= last_days, do: 1, else: -1
size(Map.put(date_range, :step, step))
end
defp empty?(%Date.Range{
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
})
when step > 0 and first_days > last_days,
do: true
defp empty?(%Date.Range{
first_in_iso_days: first_days,
last_in_iso_days: last_days,
step: step
})
when step < 0 and first_days < last_days,
do: true
defp empty?(%Date.Range{step: _}), do: false
# TODO: Remove me on v2.0
defp empty?(
%{__struct__: Date.Range, first_in_iso_days: first_days, last_in_iso_days: last_days} =
date_range
) do
step = if first_days <= last_days, do: 1, else: -1
empty?(Map.put(date_range, :step, step))
end
end
defimpl Inspect do
import Kernel, except: [inspect: 2]
def inspect(%Date.Range{first: first, last: last, step: 1}, _) do
def inspect(%Date.Range{first: first, last: last}, _) do
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ">"
end
def inspect(%Date.Range{first: first, last: last, step: step}, _) do
"#DateRange<" <> inspect(first) <> ", " <> inspect(last) <> ", #{step}>"
end
# TODO: Remove me on v2.0
def inspect(%{__struct__: Date.Range, first: first, last: last} = date_range, opts) do
step = if first <= last, do: 1, else: -1
inspect(Map.put(date_range, :step, step), opts)
end
end
end
+22 -75
View File
@@ -109,9 +109,8 @@ defmodule DateTime do
{:ok, ~U[2016-05-24 13:26:08.003Z]}
When the datetime is ambiguous - for instance during changing from summer
to winter time - the two possible valid datetimes are returned in a tuple.
The first datetime is also the one which comes first chronologically, while
the second one comes last.
to winter time - the two possible valid datetimes are returned. First the one
that happens first, then the one that happens after.
iex> {:ambiguous, first_dt, second_dt} = DateTime.new(~D[2018-10-28], ~T[02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> first_dt
@@ -140,7 +139,7 @@ defmodule DateTime do
@doc since: "1.11.0"
@spec new(Date.t(), Time.t(), Calendar.time_zone(), Calendar.time_zone_database()) ::
{:ok, t}
| {:ambiguous, first_datetime :: t, second_datetime :: t}
| {:ambiguous, t, t}
| {:gap, t, t}
| {:error,
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
@@ -241,7 +240,9 @@ defmodule DateTime do
{:error, reason} ->
raise ArgumentError,
"cannot build datetime with #{inspect(date)} and #{inspect(time)}, reason: #{inspect(reason)}"
"cannot build datetime with #{inspect(date)} and #{inspect(time)}, reason: #{
inspect(reason)
}"
end
end
@@ -365,9 +366,8 @@ defmodule DateTime do
{:ok, ~U[2016-05-24 13:26:08.003Z]}
When the datetime is ambiguous - for instance during changing from summer
to winter time - the two possible valid datetimes are returned in a tuple.
The first datetime is also the one which comes first chronologically, while
the second one comes last.
to winter time - the two possible valid datetimes are returned. First the one
that happens first, then the one that happens after.
iex> {:ambiguous, first_dt, second_dt} = DateTime.from_naive(~N[2018-10-28 02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> first_dt
@@ -418,7 +418,7 @@ defmodule DateTime do
Calendar.time_zone_database()
) ::
{:ok, t}
| {:ambiguous, first_datetime :: t, second_datetime :: t}
| {:ambiguous, t, t}
| {:gap, t, t}
| {:error,
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
@@ -892,14 +892,13 @@ defmodule DateTime do
@doc """
Converts the given datetime to
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601) format.
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601) format.
By default, `DateTime.to_iso8601/2` returns datetimes formatted in the "extended"
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
Only supports converting datetimes which are in the ISO calendar,
attempting to convert datetimes from other calendars will raise.
You can also optionally specify an offset for the formatted string.
WARNING: the ISO 8601 datetime format does not contain the time zone nor
its abbreviation, which means information is lost when converting to such
@@ -931,31 +930,11 @@ defmodule DateTime do
iex> DateTime.to_iso8601(dt, :basic)
"20000229T230007-0400"
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
iex> DateTime.to_iso8601(dt, :extended, 3600)
"2000-03-01T04:00:07+01:00"
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "AMT",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: -14400, std_offset: 0, time_zone: "America/Manaus"}
iex> DateTime.to_iso8601(dt, :extended, 0)
"2000-03-01T03:00:07+00:00"
iex> dt = %DateTime{year: 2000, month: 3, day: 01, zone_abbr: "UTC",
...> hour: 03, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"}
iex> DateTime.to_iso8601(dt, :extended, 0)
"2000-03-01T03:00:07Z"
iex> {:ok, dt, offset} = DateTime.from_iso8601("2000-03-01T03:00:07Z")
iex> "2000-03-01T03:00:07Z" = DateTime.to_iso8601(dt, :extended, offset)
"""
@spec to_iso8601(Calendar.datetime(), :basic | :extended, nil | integer()) :: String.t()
def to_iso8601(datetime, format \\ :extended, offset \\ nil)
@spec to_iso8601(Calendar.datetime(), :extended | :basic) :: String.t()
def to_iso8601(datetime, format \\ :extended)
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, nil)
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format)
when format in [:extended, :basic] do
%{
year: year,
@@ -970,56 +949,21 @@ defmodule DateTime do
std_offset: std_offset
} = datetime
datetime_to_string(year, month, day, hour, minute, second, microsecond, format) <>
Calendar.ISO.date_to_string(year, month, day, format) <>
"T" <>
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format) <>
Calendar.ISO.offset_to_string(utc_offset, std_offset, time_zone, format)
end
def to_iso8601(
%{calendar: Calendar.ISO, microsecond: {_, precision}, time_zone: "Etc/UTC"} = datetime,
format,
0
)
when format in [:extended, :basic] do
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, 0)
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
"Z"
end
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, offset)
when format in [:extended, :basic] do
{_, precision} = datetime.microsecond
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, offset)
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
Calendar.ISO.offset_to_string(offset, 0, nil, format)
end
def to_iso8601(%{calendar: _} = datetime, format, offset) when format in [:extended, :basic] do
def to_iso8601(%{calendar: _} = datetime, format) when format in [:extended, :basic] do
datetime
|> convert!(Calendar.ISO)
|> to_iso8601(format, offset)
end
defp shift_by_offset(%{calendar: calendar} = datetime, offset) do
total_offset = datetime.utc_offset + datetime.std_offset
datetime
|> to_iso_days()
# Subtract total original offset in order to get UTC and add the new offset
|> Calendar.ISO.add_day_fraction_to_iso_days(offset - total_offset, 86400)
|> calendar.naive_datetime_from_iso_days()
end
defp datetime_to_string(year, month, day, hour, minute, second, microsecond, format) do
Calendar.ISO.date_to_string(year, month, day, format) <>
"T" <>
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
|> to_iso8601(format)
end
@doc """
Parses the extended "Date and time of day" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Since ISO 8601 does not include the proper time zone, the given
string will be converted to UTC and its offset in seconds will be
@@ -1029,6 +973,9 @@ defmodule DateTime do
As specified in the standard, the separator "T" may be omitted if
desired as there is no ambiguity within this function.
The year parsed by this function is limited to four digits and,
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
hour of the next day, this notation is not supported by Elixir.
Note leap seconds are not supported by the built-in Calendar.ISO.
## Examples
+142 -353
View File
@@ -1,127 +1,22 @@
defmodule Calendar.ISO do
@moduledoc """
The default calendar implementation, a Gregorian calendar following ISO 8601.
A calendar implementation that follows to ISO 8601.
This calendar implements a proleptic Gregorian calendar and
This calendar implements the proleptic Gregorian calendar and
is therefore compatible with the calendar used in most countries
today. The proleptic means the Gregorian rules for leap years are
applied for all time, consequently the dates give different results
before the year 1583 from when the Gregorian calendar was adopted.
## ISO 8601 compliance
The ISO 8601 specification is feature-rich, but allows applications
to selectively implement most parts of it. The choices Elixir makes
are catalogued below.
### Features
The standard library supports a minimal set of possible ISO 8601 features.
Specifically, the parser only supports calendar dates and does not support
ordinal and week formats.
By default Elixir only parses extended-formatted date/times. You can opt-in
to parse basic-formatted date/times.
`NaiveDateTime.to_iso8601/2` and `DateTime.to_iso8601/2` allow you to produce
either basic or extended formatted strings, and `Calendar.strftime/2` allows
you to format datetimes however else you desire.
Elixir does not support reduced accuracy formats (for example, a date without
the day component) nor decimal precisions in the lowest component (such as
`10:01:25,5`). No functions exist to parse ISO 8601 durations or time intervals.
#### Examples
Elixir expects the extended format by default when parsing:
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("20150123T235007")
{:error, :invalid_format}
Parsing can be restricted to basic if desired:
iex> Calendar.ISO.parse_naive_datetime("20150123T235007Z", :basic)
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("20150123T235007Z", :extended)
{:error, :invalid_format}
Only calendar dates are supported in parsing; ordinal and week dates are not.
iex> Calendar.ISO.parse_date("2015-04-15")
{:ok, {2015, 4, 15}}
iex> Calendar.ISO.parse_date("2015-105")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015-W16")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015-W016-3")
{:error, :invalid_format}
Years, months, days, hours, minutes, and seconds must be fully specified:
iex> Calendar.ISO.parse_date("2015-04-15")
{:ok, {2015, 4, 15}}
iex> Calendar.ISO.parse_date("2015-04")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:07.0123456")
{:ok, {23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_time("23:50:07")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("23:50")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23")
{:error, :invalid_format}
### Extensions
The parser and formatter adopt one ISO 8601 extension: extended year notation.
This allows dates to be prefixed with a `+` or `-` sign, extending the range of
expressible years from the default (`0000..9999`) to `-9999..9999`. Elixir still
restricts years in this format to four digits.
#### Examples
iex> Calendar.ISO.parse_date("-2015-01-23")
{:ok, {-2015, 1, 23}}
iex> Calendar.ISO.parse_date("+2015-01-23")
{:ok, {2015, 1, 23}}
iex> Calendar.ISO.parse_naive_datetime("-2015-01-23 23:50:07")
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("+2015-01-23 23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23 23:50:07Z")
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("+2015-01-23 23:50:07Z")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
### Additions
ISO 8601 does not allow a whitespace instead of `T` as a separator
between date and times, both when parsing and formatting.
This is a common enough representation, Elixir allows it during parsing.
The formatting of dates in `NaiveDateTime.to_iso8601/1` and `DateTime.to_iso8601/1`
do produce specification-compliant string representations using the `T` separator.
#### Examples
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07.0123456Z")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}, 0}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.0123456Z")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}, 0}
Given this is the default calendar used by Elixir, it has one
difference compared to the ISO8601 specification in that it allows
a whitespace instead of `T` as a seperator between date and times
both when parsing and formatting. Strict formatting can be done
by using the `to_iso8601` found in `NaiveDateTime` and `DateTime`.
Note that while ISO 8601 allows times and datetimes to specify
24:00:00 as the zero hour of the next day, this notation is not
supported by Elixir.
"""
@behaviour Calendar
@@ -183,10 +78,7 @@ defmodule Calendar.ISO do
@microseconds_per_second 1_000_000
@parts_per_day @seconds_per_day * @microseconds_per_second
@datetime_seps [?\s, ?T]
@ext_date_sep ?-
@ext_time_sep ?:
@sep [?\s, ?T]
@days_per_nonleap_year 365
@days_per_leap_year 366
@@ -195,11 +87,10 @@ defmodule Calendar.ISO do
# on ~D[0001-01-01] which is 366 days later.
@iso_epoch 366
[match_basic_date, match_ext_date, guard_date, read_date] =
[match_date, guard_date, read_date] =
quote do
[
<<y1, y2, y3, y4, m1, m2, d1, d2>>,
<<y1, y2, y3, y4, @ext_date_sep, m1, m2, @ext_date_sep, d1, d2>>,
<<y1, y2, y3, y4, ?-, m1, m2, ?-, d1, d2>>,
y1 >= ?0 and y1 <= ?9 and y2 >= ?0 and y2 <= ?9 and y3 >= ?0 and y3 <= ?9 and y4 >= ?0 and
y4 <= ?9 and m1 >= ?0 and m1 <= ?9 and m2 >= ?0 and m2 <= ?9 and d1 >= ?0 and d1 <= ?9 and
d2 >= ?0 and d2 <= ?9,
@@ -211,11 +102,10 @@ defmodule Calendar.ISO do
]
end
[match_basic_time, match_ext_time, guard_time, read_time] =
[match_time, guard_time, read_time] =
quote do
[
<<h1, h2, i1, i2, s1, s2>>,
<<h1, h2, @ext_time_sep, i1, i2, @ext_time_sep, s1, s2>>,
<<h1, h2, ?:, i1, i2, ?:, s1, s2>>,
h1 >= ?0 and h1 <= ?9 and h2 >= ?0 and h2 <= ?9 and i1 >= ?0 and i1 <= ?9 and i2 >= ?0 and
i2 <= ?9 and s1 >= ?0 and s1 <= ?9 and s2 >= ?0 and s2 <= ?9,
{
@@ -244,69 +134,49 @@ defmodule Calendar.ISO do
defguardp is_std_offset(offset) when is_integer(offset)
@doc """
Parses a time `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
Parses a time string.
## Examples
iex> Calendar.ISO.parse_time("23:50:07")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("23:50:07Z")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("T23:50:07Z")
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("23:50:07,0123456")
{:ok, {23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_time("23:50:07.0123456")
{:ok, {23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_time("23:50:07.123Z")
{:ok, {23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_time("2015:01:23 23-50-07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:07A")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:07.")
{:error, :invalid_format}
iex> Calendar.ISO.parse_time("23:50:61")
{:error, :invalid_time}
"""
@doc since: "1.10.0"
@impl true
def parse_time("T" <> string) when is_binary(string),
do: do_parse_time(string)
def parse_time(string) when is_binary(string),
do: parse_time(string, :extended)
do: do_parse_time(string)
@doc """
Parses a time `string` according to a given `format`.
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_time("235007", :basic)
{:ok, {23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_time("235007", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
def parse_time("T" <> string, format) when is_binary(string),
do: do_parse_time(string, format)
def parse_time(string, format) when is_binary(string),
do: do_parse_time(string, format)
defp do_parse_time(<<unquote(match_basic_time), rest::binary>>, :basic)
when unquote(guard_time) do
{hour, minute, second} = unquote(read_time)
parse_formatted_time(hour, minute, second, rest)
end
defp do_parse_time(<<unquote(match_ext_time), rest::binary>>, :extended)
when unquote(guard_time) do
{hour, minute, second} = unquote(read_time)
parse_formatted_time(hour, minute, second, rest)
end
defp do_parse_time(_, _) do
{:error, :invalid_format}
end
defp parse_formatted_time(hour, minute, second, rest) do
with {microsecond, rest} <- parse_microsecond(rest),
defp do_parse_time(string) do
with <<unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_time),
{microsecond, rest} <- parse_microsecond(rest),
{_offset, ""} <- parse_offset(rest) do
{hour, minute, second} = unquote(read_time)
if valid_time?(hour, minute, second, microsecond) do
{:ok, {hour, minute, second, microsecond}}
else
@@ -318,16 +188,14 @@ defmodule Calendar.ISO do
end
@doc """
Parses a date `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
Parses a date string.
## Examples
iex> Calendar.ISO.parse_date("2015-01-23")
{:ok, {2015, 1, 23}}
iex> Calendar.ISO.parse_date("-2015-01-23")
{:ok, {-2015, 1, 23}}
iex> Calendar.ISO.parse_date("2015:01:23")
{:error, :invalid_format}
iex> Calendar.ISO.parse_date("2015-01-32")
@@ -336,142 +204,91 @@ defmodule Calendar.ISO do
"""
@doc since: "1.10.0"
@impl true
def parse_date("-" <> string) when is_binary(string),
do: parse_date(string, -1)
def parse_date(string) when is_binary(string),
do: parse_date(string, :extended)
do: parse_date(string, 1)
@doc """
Parses a date `string` according to a given `format`.
defp parse_date(string, multiplier) do
with unquote(match_date) <- string, true <- unquote(guard_date) do
{year, month, day} = unquote(read_date)
year = multiplier * year
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_date("20150123", :basic)
{:ok, {2015, 1, 23}}
iex> Calendar.ISO.parse_date("20150123", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
def parse_date("-" <> string, format) when is_binary(string),
do: do_parse_date(string, -1, format)
def parse_date("+" <> string, format) when is_binary(string),
do: do_parse_date(string, 1, format)
def parse_date(string, format) when is_binary(string),
do: do_parse_date(string, 1, format)
defp do_parse_date(unquote(match_basic_date), multiplier, :basic) when unquote(guard_date) do
{year, month, day} = unquote(read_date)
parse_formatted_date(year, month, day, multiplier)
end
defp do_parse_date(unquote(match_ext_date), multiplier, :extended) when unquote(guard_date) do
{year, month, day} = unquote(read_date)
parse_formatted_date(year, month, day, multiplier)
end
defp do_parse_date(_, _, _) do
{:error, :invalid_format}
end
defp parse_formatted_date(year, month, day, multiplier) do
year = multiplier * year
if valid_date?(year, month, day) do
{:ok, {year, month, day}}
if valid_date?(year, month, day) do
{:ok, {year, month, day}}
else
{:error, :invalid_date}
end
else
{:error, :invalid_date}
_ ->
{:error, :invalid_format}
end
end
@doc """
Parses a naive datetime `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
Parses a naive datetime string.
## Examples
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07Z")
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07-02:30")
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07Z")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 1}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07,0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07.0123456")
{:ok, {2015, 1, 23, 23, 50, 7, {12345, 6}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123Z")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23P23:50:07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015:01:23 23-50-07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:07A")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23 23:50:61")
{:error, :invalid_time}
iex> Calendar.ISO.parse_naive_datetime("2015-01-32 23:50:07")
{:error, :invalid_date}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123+02:30")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123+00:00")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-02:30")
{:ok, {2015, 1, 23, 23, 50, 7, {123000, 3}}}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-00:00")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-00:60")
{:error, :invalid_format}
iex> Calendar.ISO.parse_naive_datetime("2015-01-23T23:50:07.123-24:00")
{:error, :invalid_format}
"""
@doc since: "1.10.0"
@impl true
def parse_naive_datetime("-" <> string) when is_binary(string),
do: parse_naive_datetime(string, -1)
def parse_naive_datetime(string) when is_binary(string),
do: parse_naive_datetime(string, :extended)
do: parse_naive_datetime(string, 1)
@doc """
Parses a naive datetime `string` according to a given `format`.
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_naive_datetime("20150123 235007", :basic)
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}}
iex> Calendar.ISO.parse_naive_datetime("20150123 235007", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
def parse_naive_datetime("-" <> string, format) when is_binary(string),
do: do_parse_naive_datetime(string, -1, format)
def parse_naive_datetime("+" <> string, format) when is_binary(string),
do: do_parse_naive_datetime(string, 1, format)
def parse_naive_datetime(string, format) when is_binary(string),
do: do_parse_naive_datetime(string, 1, format)
defp do_parse_naive_datetime(
<<unquote(match_basic_date), datetime_sep, unquote(match_basic_time), rest::binary>>,
multiplier,
:basic
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_naive_datetime(
<<unquote(match_ext_date), datetime_sep, unquote(match_ext_time), rest::binary>>,
multiplier,
:extended
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_naive_datetime(_, _, _) do
{:error, :invalid_format}
end
defp parse_formatted_naive_datetime(year, month, day, hour, minute, second, rest, multiplier) do
year = multiplier * year
with {microsecond, rest} <- parse_microsecond(rest),
defp parse_naive_datetime(string, multiplier) do
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
{microsecond, rest} <- parse_microsecond(rest),
{_offset, ""} <- parse_offset(rest) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
year = multiplier * year
cond do
not valid_date?(year, month, day) ->
{:error, :invalid_date}
@@ -488,85 +305,54 @@ defmodule Calendar.ISO do
end
@doc """
Parses a UTC datetime `string` in the `:extended` format.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
Parses a UTC datetime string.
## Examples
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07Z")
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07Z")
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07+02:30")
{:ok, {2015, 1, 23, 21, 20, 7, {0, 0}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.123+02:30")
{:ok, {2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:07")
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07,123+02:30")
{:ok, {2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23T23:50:07Z")
{:ok, {-2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("-2015-01-23T23:50:07,123+02:30")
{:ok, {-2015, 1, 23, 21, 20, 7, {123000, 3}}, 9000}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23P23:50:07")
{:error, :invalid_format}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07")
{:error, :missing_offset}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23 23:50:61")
{:error, :invalid_time}
iex> Calendar.ISO.parse_utc_datetime("2015-01-32 23:50:07")
{:error, :invalid_date}
iex> Calendar.ISO.parse_utc_datetime("2015-01-23T23:50:07.123-00:00")
{:error, :invalid_format}
"""
@doc since: "1.10.0"
@impl true
def parse_utc_datetime("-" <> string) when is_binary(string),
do: parse_utc_datetime(string, -1)
def parse_utc_datetime(string) when is_binary(string),
do: parse_utc_datetime(string, :extended)
do: parse_utc_datetime(string, 1)
@doc """
Parses a UTC datetime `string` according to a given `format`.
The `format` can either be `:basic` or `:extended`.
For more information on supported strings, see how this
module implements [ISO 8601](#module-iso-8601-compliance).
## Examples
iex> Calendar.ISO.parse_utc_datetime("20150123 235007Z", :basic)
{:ok, {2015, 1, 23, 23, 50, 7, {0, 0}}, 0}
iex> Calendar.ISO.parse_utc_datetime("20150123 235007Z", :extended)
{:error, :invalid_format}
"""
@doc since: "1.12.0"
def parse_utc_datetime("-" <> string, format) when is_binary(string),
do: do_parse_utc_datetime(string, -1, format)
def parse_utc_datetime("+" <> string, format) when is_binary(string),
do: do_parse_utc_datetime(string, 1, format)
def parse_utc_datetime(string, format) when is_binary(string),
do: do_parse_utc_datetime(string, 1, format)
defp do_parse_utc_datetime(
<<unquote(match_basic_date), datetime_sep, unquote(match_basic_time), rest::binary>>,
multiplier,
:basic
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_utc_datetime(
<<unquote(match_ext_date), datetime_sep, unquote(match_ext_time), rest::binary>>,
multiplier,
:extended
)
when unquote(guard_date) and datetime_sep in @datetime_seps and unquote(guard_time) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier)
end
defp do_parse_utc_datetime(_, _, _) do
{:error, :invalid_format}
end
defp parse_formatted_utc_datetime(year, month, day, hour, minute, second, rest, multiplier) do
year = multiplier * year
with {microsecond, rest} <- parse_microsecond(rest),
defp parse_utc_datetime(string, multiplier) do
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
{microsecond, rest} <- parse_microsecond(rest),
{offset, ""} <- parse_offset(rest) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
year = multiplier * year
cond do
not valid_date?(year, month, day) ->
{:error, :invalid_date}
@@ -596,7 +382,8 @@ defmodule Calendar.ISO do
{:ok, {year, month, day, hour, minute, second, microsecond}, offset}
end
else
_ -> {:error, :invalid_format}
_ ->
{:error, :invalid_format}
end
end
@@ -1288,7 +1075,9 @@ defmodule Calendar.ISO do
@doc """
Determines if the date given is valid according to the proleptic Gregorian calendar.
Leap seconds are not supported by the built-in Calendar.ISO.
Note that while ISO 8601 allows times to specify 24:00:00 as the
zero hour of the next day, this notation is not supported by Elixir.
Leap seconds are not supported as well by the built-in Calendar.ISO.
## Examples
+6 -3
View File
@@ -598,7 +598,7 @@ defmodule NaiveDateTime do
@doc """
Parses the extended "Date and time of day" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Time zone offset may be included in the string but they will be
simply discarded as such information is not included in naive date
@@ -607,6 +607,9 @@ defmodule NaiveDateTime do
As specified in the standard, the separator "T" may be omitted if
desired as there is no ambiguity within this function.
The year parsed by this function is limited to four digits and,
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
hour of the next day, this notation is not supported by Elixir.
Note leap seconds are not supported by the built-in Calendar.ISO.
## Examples
@@ -673,7 +676,7 @@ defmodule NaiveDateTime do
@doc """
Parses the extended "Date and time of day" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Raises if the format is invalid.
@@ -701,7 +704,7 @@ defmodule NaiveDateTime do
@doc """
Converts the given naive datetime to
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
By default, `NaiveDateTime.to_iso8601/2` returns naive datetimes formatted in the "extended"
format, for human readability. It also supports the "basic" format through passing the `:basic` option.
+9 -3
View File
@@ -211,7 +211,7 @@ defmodule Time do
@doc """
Parses the extended "Local time" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Time zone offset may be included in the string but they will be
simply discarded as such information is not included in times.
@@ -219,6 +219,12 @@ defmodule Time do
As specified in the standard, the separator "T" may be omitted if
desired as there is no ambiguity within this function.
Time representations with reduced accuracy are not supported.
Note that while ISO 8601 allows times to specify 24:00:00 as the
zero hour of the next day, this notation is not supported by Elixir.
Leap seconds are not supported as well by the built-in Calendar.ISO.
## Examples
iex> Time.from_iso8601("23:50:07")
@@ -257,7 +263,7 @@ defmodule Time do
@doc """
Parses the extended "Local time" format described by
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Raises if the format is invalid.
@@ -284,7 +290,7 @@ defmodule Time do
@doc """
Converts the given time to
[ISO 8601:2019](https://en.wikipedia.org/wiki/ISO_8601).
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
By default, `Time.to_iso8601/2` returns times formatted in the "extended"
format, for human readability. It also supports the "basic" format through
+102 -449
View File
@@ -2,7 +2,7 @@ defmodule Code do
@moduledoc ~S"""
Utilities for managing code compilation, code evaluation, and code loading.
This module complements Erlang's [`:code` module](`:code`)
This module complements Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html)
to add behaviour which is specific to Elixir. Almost all of the functions in this module
have global side effects on the behaviour of Elixir.
@@ -30,56 +30,6 @@ defmodule Code do
file, without tracking. `eval_file/2` should be used when you are interested in
the result of evaluating the file rather than the modules it defines.
## Code loading on the Erlang VM
Erlang has two modes to load code: interactive and embedded.
By default, the Erlang VM runs in interactive mode, where modules
are loaded as needed. In embedded mode the opposite happens, as all
modules need to be loaded upfront or explicitly.
You can use `ensure_loaded/1` (as well as `ensure_loaded?/1` and
`ensure_loaded!/1`) to check if a module is loaded before using it and
act.
## `ensure_compiled/1` and `ensure_compiled!/1`
Elixir also includes `ensure_compiled/1` and `ensure_compiled!/1`
functions that are a superset of `ensure_loaded/1`.
Since Elixir's compilation happens in parallel, in some situations
you may need to use a module that was not yet compiled, therefore
it can't even be loaded.
When invoked, `ensure_compiled/1` and `ensure_compiled!/1` halt the
compilation of the caller until the module becomes available. Note
the distinction between `ensure_compiled/1` and `ensure_compiled!/1`
is important: if you are using `ensure_compiled!/1`, you are
indicating to the compiler that you can only continue if said module
is available.
If you are using `Code.ensure_compiled/1`, you are implying you may
continue without the module and therefore Elixir may return
`{:error, :unavailable}` for cases where the module is not yet available
(but may be available later on).
For those reasons, developers must typically use `Code.ensure_compiled!/1`.
In particular, do not do this:
case Code.ensure_compiled(module) do
{:module, _} -> module
{:error, _} -> raise ...
end
Finally, note you only need `ensure_compiled!/1` to check for modules
being defined within the same project. It does not apply to modules from
dependencies as dependencies are always compiled upfront.
In most cases, `ensure_loaded/1` is enough. `ensure_compiled!/1`
must be used in rare cases, usually involving macros that need to
invoke a module for callback information. The use of `ensure_compiled/1`
is even less likely.
## Compilation tracers
Elixir supports compilation tracers, which allows modules to observe constructs
@@ -206,259 +156,6 @@ defmodule Code do
required_files()
end
@doc """
Receives a string and returns the cursor context.
This function receives a string with incomplete Elixir code,
representing a cursor position, and based on the string, it
provides contextual information about said position. The
return of this function can then be used to provide tips,
suggestions, and autocompletion functionality.
This function provides a best-effort detection and may not be
accurate under certain circumstances. See the "Limitations"
section below.
Consider adding a catch-all clause when handling the return
type of this function as new cursor information may be added
in future releases.
## Examples
iex> Code.cursor_context("")
:expr
iex> Code.cursor_context("hello_wor")
{:local_or_var, 'hello_wor'}
## Return values
* `{:alias, charlist}` - the context is an alias, potentially
a nested one, such as `Hello.Wor` or `HelloWor`
* `{:dot, inside_dot, charlist}` - the context is a dot
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
itself. If a var is given, this may either be a remote call or a map
field access. Examples are `Hello.wor`, `:hello.wor`, `hello.wor`,
`Hello.nested.wor`, `hello.nested.wor`, and `@hello.world`
* `{:dot_arity, inside_dot, charlist}` - the context is a dot arity
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
itself. If a var is given, it must be a remote arity. Examples are
`Hello.world/`, `:hello.world/`, `hello.world/2`, and `@hello.world/2`
* `{:dot_call, inside_dot, charlist}` - the context is a dot
call. This means parentheses or space have been added after the expression.
where `inside_dot` is either a `{:var, charlist}`, `{:alias, charlist}`,
`{:module_attribute, charlist}`, `{:unquoted_atom, charlist}` or a `dot`
itself. If a var is given, it must be a remote call. Examples are
`Hello.world(`, `:hello.world(`, `Hello.world `, `hello.world(`, `hello.world `,
and `@hello.world(`
* `:expr` - may be any expression. Autocompletion may suggest an alias,
local or var
* `{:local_or_var, charlist}` - the context is a variable or a local
(import or local) call, such as `hello_wor`
* `{:local_arity, charlist}` - the context is a local (import or local)
call, such as `hello_world/`
* `{:local_call, charlist}` - the context is a local (import or local)
call, such as `hello_world(` and `hello_world `
* `{:module_attribute, charlist}` - the context is a module attribute, such
as `@hello_wor`
* `:none` - no context possible
* `:unquoted_atom` - the context is an unquoted atom. This can be either
previous atoms or all available `:erlang` modules
## Limitations
* There is no context for operators
* The current algorithm only considers the last line of the input
* Context does not yet track strings, sigils, etc.
* Arguments of functions calls are not currently recognized
"""
@doc since: "1.12.0"
@spec cursor_context(List.Chars.t(), keyword()) ::
{:alias, charlist}
| {:dot, inside_dot, charlist}
| {:dot_arity, inside_dot, charlist}
| {:dot_call, inside_dot, charlist}
| :expr
| {:local_or_var, charlist}
| {:local_arity, charlist}
| {:local_call, charlist}
| {:module_attribute, charlist}
| :none
| {:unquoted_atom, charlist}
when inside_dot:
{:alias, charlist}
| {:dot, inside_dot, charlist}
| {:module_attribute, charlist}
| {:unquoted_atom, charlist}
| {:var, charlist}
def cursor_context(string, opts \\ [])
def cursor_context(binary, opts) when is_binary(binary) and is_list(opts) do
binary =
case :binary.matches(binary, "\n") do
[] ->
binary
matches ->
{position, _} = List.last(matches)
binary_part(binary, position + 1, byte_size(binary) - position - 1)
end
do_cursor_context(String.to_charlist(binary), opts)
end
def cursor_context(charlist, opts) when is_list(charlist) and is_list(opts) do
chunked = Enum.chunk_by(charlist, &(&1 == ?\n))
case List.last(chunked, []) do
[?\n | _] -> do_cursor_context([], opts)
rest -> do_cursor_context(rest, opts)
end
end
def cursor_context(other, opts) do
cursor_context(to_charlist(other), opts)
end
@operators '\\<>+-*/:=|&~^@%'
@non_closing_punctuation '.,([{;'
@closing_punctuation ')]}'
@space '\t\s'
@closing_identifier '?!'
@operators_and_non_closing_puctuation @operators ++ @non_closing_punctuation
@non_identifier @closing_identifier ++
@operators ++ @non_closing_punctuation ++ @closing_punctuation ++ @space
defp do_cursor_context(list, _opts) do
reverse = Enum.reverse(list)
case strip_spaces(reverse, 0) do
# It is empty
{[], _} ->
:expr
{[?: | _], 0} ->
{:unquoted_atom, ''}
{[?@ | _], 0} ->
{:module_attribute, ''}
{[?. | rest], _} ->
dot(rest, '')
# It is a local or remote call with parens
{[?( | rest], _} ->
call_to_cursor_context(rest)
# A local arity definition
{[?/ | rest], _} ->
case identifier_to_cursor_context(rest) do
{:local_or_var, acc} -> {:local_arity, acc}
{:dot, base, acc} -> {:dot_arity, base, acc}
_ -> :none
end
# Starting a new expression
{[h | _], _} when h in @operators_and_non_closing_puctuation ->
:expr
# It is a local or remote call without parens
{rest, spaces} when spaces > 0 ->
call_to_cursor_context(rest)
# It is an identifier
_ ->
identifier_to_cursor_context(reverse)
end
end
defp strip_spaces([h | rest], count) when h in @space, do: strip_spaces(rest, count + 1)
defp strip_spaces(rest, count), do: {rest, count}
defp call_to_cursor_context(reverse) do
case identifier_to_cursor_context(reverse) do
{:local_or_var, acc} -> {:local_call, acc}
{:dot, base, acc} -> {:dot_call, base, acc}
_ -> :none
end
end
defp identifier_to_cursor_context(reverse) do
case identifier(reverse) do
# Parse :: first to avoid ambiguity with atoms
{:alias, false, '::' ++ _, _} -> :none
{kind, _, '::' ++ _, acc} -> alias_or_local_or_var(kind, acc)
# Now handle atoms, any other atom is unexpected
{_kind, _, ':' ++ _, acc} -> {:unquoted_atom, acc}
{:atom, _, _, _} -> :none
# Parse .. first to avoid ambiguity with dots
{:alias, false, _, _} -> :none
{kind, _, '..' ++ _, acc} -> alias_or_local_or_var(kind, acc)
# Module attributes
{:alias, _, '@' ++ _, _} -> :none
{:identifier, _, '@' ++ _, acc} -> {:module_attribute, acc}
# Everything else
{:alias, _, '.' ++ rest, acc} -> nested_alias(rest, acc)
{:identifier, _, '.' ++ rest, acc} -> dot(rest, acc)
{kind, _, _, acc} -> alias_or_local_or_var(kind, acc)
:none -> :none
end
end
defp nested_alias(rest, acc) do
case identifier_to_cursor_context(rest) do
{:alias, prev} -> {:alias, prev ++ '.' ++ acc}
_ -> :none
end
end
defp dot(rest, acc) do
case identifier_to_cursor_context(rest) do
{:local_or_var, prev} -> {:dot, {:var, prev}, acc}
{:unquoted_atom, _} = prev -> {:dot, prev, acc}
{:alias, _} = prev -> {:dot, prev, acc}
{:dot, _, _} = prev -> {:dot, prev, acc}
{:module_attribute, _} = prev -> {:dot, prev, acc}
_ -> :none
end
end
defp alias_or_local_or_var(:alias, acc), do: {:alias, acc}
defp alias_or_local_or_var(:identifier, acc), do: {:local_or_var, acc}
defp alias_or_local_or_var(_, _), do: :none
defp identifier([?? | rest]), do: check_identifier(rest, [??])
defp identifier([?! | rest]), do: check_identifier(rest, [?!])
defp identifier(rest), do: check_identifier(rest, [])
defp check_identifier([h | _], _acc) when h in @non_identifier, do: :none
defp check_identifier(rest, acc), do: rest_identifier(rest, acc)
defp rest_identifier([h | rest], acc) when h not in @non_identifier do
rest_identifier(rest, [h | acc])
end
defp rest_identifier(rest, acc) do
case String.Tokenizer.tokenize(acc) do
{kind, _, [], _, ascii_only?, _} -> {kind, ascii_only?, rest, acc}
_ -> :none
end
end
@doc """
Removes files from the required files list.
@@ -609,33 +306,21 @@ defmodule Code do
## Examples
iex> {result, binding} = Code.eval_string("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
iex> result
3
iex> Enum.sort(binding)
[a: 1, b: 2]
iex> Code.eval_string("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
{3, [a: 1, b: 2]}
iex> {result, binding} = Code.eval_string("c = a + b", [a: 1, b: 2], __ENV__)
iex> result
3
iex> Enum.sort(binding)
[a: 1, b: 2, c: 3]
iex> Code.eval_string("c = a + b", [a: 1, b: 2], __ENV__)
{3, [a: 1, b: 2, c: 3]}
iex> {result, binding} = Code.eval_string("a = a + b", [a: 1, b: 2])
iex> result
3
iex> Enum.sort(binding)
[a: 3, b: 2]
iex> Code.eval_string("a = a + b", [a: 1, b: 2])
{3, [a: 3, b: 2]}
For convenience, you can pass `__ENV__/0` as the `opts` argument and
all imports, requires and aliases defined in the current environment
will be automatically carried over:
iex> {result, binding} = Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
iex> result
3
iex> Enum.sort(binding)
[a: 1, b: 2]
iex> Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
{3, [a: 1, b: 2]}
"""
@spec eval_string(List.Chars.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
@@ -673,8 +358,9 @@ defmodule Code do
* `:line_length` - the line length to aim for when formatting
the document. Defaults to 98. Note this value is used as
guideline but there are situations where it is not enforced.
See the "Line length" section below for more information
reference but it is not enforced by the formatter as sometimes
user intervention is required. See "Running the formatter"
section
* `:locals_without_parens` - a keyword list of name and arity
pairs that should be kept without parens whenever possible.
@@ -682,12 +368,17 @@ defmodule Code do
that name. The formatter already includes a list of functions
and this option augments this list.
* `:rename_deprecated_at` - rename all known deprecated functions
at the given version to their non-deprecated equivalent. It
expects a valid `Version` which is usually the minimum Elixir
version supported by the project.
* `:force_do_end_blocks` (since v1.9.0) - when `true`, converts all
inline usages of `do: ...`, `else: ...` and friends into `do/end`
blocks. Defaults to `false`. Note that this option is convergent:
once you set it to `true`, **all keywords** will be converted.
If you set it to `false` later on, `do/end` blocks won't be
converted back to keywords.
once you set it to `true`, all keywords will be converted. If you
set it to `false` later on, `do/end` blocks won't be converted
back to keywords.
## Design principles
@@ -695,6 +386,8 @@ defmodule Code do
First, the formatter never changes the semantics of the code by
default. This means the input AST and the output AST are equivalent.
Optional behaviour, such as `:rename_deprecated_at`, is allowed to
break this guarantee.
The second principle is to provide as little configuration as possible.
This eases the formatter adoption by removing contention points while
@@ -720,8 +413,29 @@ defmodule Code do
do not recommend to run the formatter blindly in an existing codebase.
Instead you should format and sanity check each formatted file.
For example, the formatter may break a long function definition over
multiple clauses:
Let's see some examples. The code below:
"this is a very long string ... #{inspect(some_value)}"
may be formatted as:
"this is a very long string ... #{
inspect(some_value)
}"
This happens because the only place the formatter can introduce a
new line without changing the code semantics is in the interpolation.
In those scenarios, we recommend developers to directly adjust the
code. Here we can use the binary concatenation operator `<>/2`:
"this is a very long string " <>
"... #{inspect(some_value)}"
The string concatenation makes the code fit on a single line and also
gives more options to the formatter.
A similar example is when the formatter breaks a function definition
over multiple clauses:
def my_function(
%User{name: name, age: age, ...},
@@ -786,40 +500,6 @@ defmodule Code do
we describe in the next sections the cases where the formatter keeps the
user encoding and how to control multiline expressions.
## Line length
Another point about the formatter is that the `:line_length` configuration
is a guideline. In many cases, it is not possible for the formatter to break
your code apart, which means it will go over the line length. For example,
if you have a long string:
"this is a very long string that will go over the line length"
The formatter doesn't know how to break it apart without changing the
code underlying syntax representation, so it is up to you to step in:
"this is a very long string " <>
"that will go over the line length"
The string concatenation makes the code fit on a single line and also
gives more options to the formatter.
This may also appear in do/end blocks, where the `do` keyword (or `->`)
may go over the line lenth because there is no opportunity for the
formatter to introduce a line break in a readable way. For example,
if you do:
case very_long_expression() do
And only the `do` keyword is above the line length, Elixir **will not**
emit this:
case very_long_expression()
do
So it prefers to not touch the line at all and leave `do` above the
line limit.
## Keeping user's formatting
The formatter respects the input format in some cases. Those are
@@ -966,10 +646,6 @@ defmodule Code do
are considered equivalent (the nesting is discarded alongside most of
user formatting). In such cases, the code formatter will always format to
the latter.
## Newlines
The formatter converts all newlines in code from `\r\n` to `\n`.
"""
@doc since: "1.6.0"
@spec format_string!(binary, keyword) :: iodata
@@ -1006,21 +682,15 @@ defmodule Code do
## Examples
iex> contents = quote(do: var!(a) + var!(b))
iex> {result, binding} = Code.eval_quoted(contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
iex> result
3
iex> Enum.sort(binding)
[a: 1, b: 2]
iex> Code.eval_quoted(contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
{3, [a: 1, b: 2]}
For convenience, you can pass `__ENV__/0` as the `opts` argument and
all options will be automatically extracted from the current environment:
iex> contents = quote(do: var!(a) + var!(b))
iex> {result, binding} = Code.eval_quoted(contents, [a: 1, b: 2], __ENV__)
iex> result
3
iex> Enum.sort(binding)
[a: 1, b: 2]
iex> Code.eval_quoted(contents, [a: 1, b: 2], __ENV__)
{3, [a: 1, b: 2]}
"""
@spec eval_quoted(Macro.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
@@ -1512,7 +1182,39 @@ defmodule Code do
If it succeeds in loading the module, it returns `{:module, module}`.
If not, returns `{:error, reason}` with the error reason.
See the module documentation for more information on code loading.
## Code loading on the Erlang VM
Erlang has two modes to load code: interactive and embedded.
By default, the Erlang VM runs in interactive mode, where modules
are loaded as needed. In embedded mode the opposite happens, as all
modules need to be loaded upfront or explicitly.
Therefore, this function is used to check if a module is loaded
before using it and allows one to react accordingly. For example, the `URI`
module uses this function to check if a specific parser exists for a given
URI scheme.
## `ensure_compiled/1`
Elixir also contains an `ensure_compiled/1` function that is a
superset of `ensure_loaded/1`.
Since Elixir's compilation happens in parallel, in some situations
you may need to use a module that was not yet compiled, therefore
it can't even be loaded.
When invoked, `ensure_compiled/1` halts the compilation of the caller
until the module given to `ensure_compiled/1` becomes available or
all files for the current project have been compiled. If compilation
finishes and the module is not available, an error tuple is returned.
`ensure_compiled/1` does not apply to dependencies, as dependencies
must be compiled upfront.
In most cases, `ensure_loaded/1` is enough. `ensure_compiled/1`
must be used in rare cases, usually involving macros that need to
invoke a module for callback information.
## Examples
@@ -1547,62 +1249,14 @@ defmodule Code do
match?({:module, ^module}, ensure_loaded(module))
end
@doc """
Same as `ensure_loaded/1` but raises if the module cannot be loaded.
"""
@doc since: "1.12.0"
@spec ensure_loaded!(module) :: module
def ensure_loaded!(module) do
case ensure_loaded(module) do
{:module, module} ->
module
{:error, reason} ->
raise ArgumentError,
"could not load module #{inspect(module)} due to reason #{inspect(reason)}"
end
end
@doc """
Similar to `ensure_compiled!/1` but indicates you can continue without said module.
While `ensure_compiled!/1` indicates to the Elixir compiler you can
only continue when said module is available, this function indicates
you may continue compilation without said module.
If it succeeds in loading the module, it returns `{:module, module}`.
If not, returns `{:error, reason}` with the error reason.
If the module being checked is currently in a compiler deadlock,
this function returns `{:error, :unavailable}`. Unavailable doesn't
necessarily mean the module doesn't exist, just that it is not currently
available, but it (or may not) become available in the future.
Therefore, if you can only continue if the module is available, use
`ensure_compiled!/1` instead. In particular, do not do this:
case Code.ensure_compiled(module) do
{:module, _} -> module
{:error, _} -> raise ...
end
See the module documentation for more information on code loading.
"""
@spec ensure_compiled(module) ::
{:module, module}
| {:error, :embedded | :badfile | :nofile | :on_load_failure | :unavailable}
def ensure_compiled(module) when is_atom(module) do
ensure_compiled(module, :soft)
end
@doc """
Ensures the given module is compiled and loaded.
If the module is already loaded, it works as no-op. If the module was
not compiled yet, `ensure_compiled!/1` halts the compilation of the caller
until the module given to `ensure_compiled!/1` becomes available or
not compiled yet, `ensure_compiled/1` halts the compilation of the caller
until the module given to `ensure_compiled/1` becomes available or
all files for the current project have been compiled. If compilation
finishes and the module is not available or is in a deadlock, an error
is raised.
finishes and the module is not available, an error tuple is returned.
Given this function halts compilation, use it carefully. In particular,
avoid using it to guess which modules are in the system. Overuse of this
@@ -1610,26 +1264,25 @@ defmodule Code do
if the other is compiled. This returns a specific unavailable error code,
where we cannot successfully verify a module is available or not.
See the module documentation for more information on code loading.
If it succeeds in loading the module, it returns `{:module, module}`.
If not, returns `{:error, reason}` with the error reason.
If the module being checked is currently in a compiler deadlock,
this function returns `{:error, :unavailable}`. Unavailable doesn't
necessarily mean the module doesn't exist, just that it is not currently
available, but it (or may not) become available in the future.
Check `ensure_loaded/1` for more information on module loading
and when to use `ensure_loaded/1` or `ensure_compiled/1`.
"""
@doc since: "1.12.0"
@spec ensure_compiled!(module) :: module
def ensure_compiled!(module) do
case ensure_compiled(module, :hard) do
{:module, module} ->
module
{:error, reason} ->
raise ArgumentError,
"could not load module #{inspect(module)} due to reason #{inspect(reason)}"
end
end
defp ensure_compiled(module, mode) do
@spec ensure_compiled(module) ::
{:module, module}
| {:error, :embedded | :badfile | :nofile | :on_load_failure | :unavailable}
def ensure_compiled(module) when is_atom(module) do
case :code.ensure_loaded(module) do
{:error, :nofile} = error ->
if can_await_module_compilation?() do
case Kernel.ErrorHandler.ensure_compiled(module, :module, mode) do
case Kernel.ErrorHandler.ensure_compiled(module, :module, :soft) do
:found -> {:module, module}
:deadlock -> {:error, :unavailable}
:not_found -> {:error, :nofile}
@@ -1673,7 +1326,7 @@ defmodule Code do
file.
It returns the term stored in the documentation chunk in the format defined by
[EEP 48](https://erlang.org/eep/eeps/eep-0048.html) or `{:error, reason}` if
[EEP 48](http://erlang.org/eep/eeps/eep-0048.html) or `{:error, reason}` if
the chunk is not available.
## Examples
@@ -1776,7 +1429,7 @@ defmodule Code do
@doc ~S"""
Deprecated function to retrieve old documentation format.
Elixir v1.7 adopts [EEP 48](https://erlang.org/eep/eeps/eep-0048.html)
Elixir v1.7 adopts [EEP 48](http://erlang.org/eep/eeps/eep-0048.html)
which is a new documentation format meant to be shared across all
BEAM languages. The old format, used by `Code.get_docs/2`, is no
longer available, and therefore this function always returns `nil`.
+112 -119
View File
@@ -12,11 +12,8 @@ defmodule Code.Formatter do
@empty empty()
@ampersand_prec Code.Identifier.unary_op(:&) |> elem(1)
# Operators that are composed of multiple binary operators
@multi_binary_operators [:"..//"]
# Operators that do not have space between operands
@no_space_binary_operators [:.., :"//"]
@no_space_binary_operators [:..]
# Operators that do not have newline between operands (as well as => and keywords)
@no_newline_binary_operators [:\\, :in]
@@ -250,6 +247,18 @@ defmodule Code.Formatter do
defp state(comments, opts) do
force_do_end_blocks = Keyword.get(opts, :force_do_end_blocks, false)
rename_deprecated_at =
if version = opts[:rename_deprecated_at] do
case Version.parse(version) do
{:ok, parsed} ->
parsed
:error ->
raise ArgumentError,
"invalid version #{inspect(version)} given to :rename_deprecated_at"
end
end
locals_without_parens =
Keyword.get(opts, :locals_without_parens, []) ++ @locals_without_parens
@@ -257,7 +266,7 @@ defmodule Code.Formatter do
force_do_end_blocks: force_do_end_blocks,
locals_without_parens: locals_without_parens,
operand_nesting: 2,
skip_eol: false,
rename_deprecated_at: rename_deprecated_at,
comments: comments
}
end
@@ -323,7 +332,7 @@ defmodule Code.Formatter do
{next_eol, comments, doc}
end
# Special AST nodes from compiler feedback
# Special AST nodes from compiler feedback.
defp quoted_to_algebra({{:special, :clause_args}, _meta, [args]}, _context, state) do
{doc, state} = clause_args_to_algebra(args, state)
@@ -394,17 +403,10 @@ defmodule Code.Formatter do
end
# foo[bar]
defp quoted_to_algebra({{:., _, [Access, :get]}, meta, [target, arg]}, _context, state) do
defp quoted_to_algebra({{:., _, [Access, :get]}, meta, [target | args]}, _context, state) do
{target_doc, state} = remote_target_to_algebra(target, state)
{access_doc, state} =
if keyword?(arg) do
list_to_algebra(meta, arg, state)
else
list_to_algebra(meta, [arg], state)
end
{concat(target_doc, access_doc), state}
{call_doc, state} = list_to_algebra(meta, args, state)
{concat(target_doc, call_doc), state}
end
# %Foo{}
@@ -519,17 +521,19 @@ defmodule Code.Formatter do
# not(left in right)
# left not in right
defp quoted_to_algebra({:not, meta, [{:in, _, [left, right]}]}, context, state) do
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
end
defp quoted_to_algebra({:not, meta, [{:in, _, [left, right]} = arg]}, context, state) do
%{rename_deprecated_at: since} = state
# 1..2//3
defp quoted_to_algebra({:"..//", meta, [left, middle, right]}, context, state) do
quoted_to_algebra({:"//", meta, [{:.., meta, [left, middle]}, right]}, context, state)
# TODO: Remove metadata and always rewrite to left not in right in Elixir v2.0.
if meta[:operator] == :"not in" || (since && Version.match?(since, "~> 1.5")) do
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
else
unary_op_to_algebra(:not, meta, arg, context, state)
end
end
defp quoted_to_algebra({:fn, meta, [_ | _] = clauses}, _context, state) do
anon_fun_to_algebra(clauses, line(meta), closing_line(meta), state, eol?(meta, state))
anon_fun_to_algebra(clauses, line(meta), closing_line(meta), state, eol?(meta))
end
defp quoted_to_algebra({fun, meta, args}, context, state) when is_atom(fun) and is_list(args) do
@@ -561,10 +565,6 @@ defmodule Code.Formatter do
if keyword_key?(left_arg) do
{left, state} =
case left_arg do
# TODO: Remove this clause in v1.16 when we no longer quote operator :..//
{:__block__, _, [:"..//"]} ->
{string(~S{"..//":}), state}
{:__block__, _, [atom]} when is_atom(atom) ->
key =
case Code.Identifier.classify(atom) do
@@ -711,7 +711,7 @@ defmodule Code.Formatter do
{operands, max_line} =
unwrap_right(right_arg, op, meta, right_context, [{{:root, left_context}, left_arg}])
fun = fn
operand_to_algebra = fn
{{:root, context}, arg}, _args, state ->
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :left, 2)
{{doc, @empty, 1}, state}
@@ -723,7 +723,14 @@ defmodule Code.Formatter do
end
{doc, state} =
operand_to_algebra_with_comments(operands, meta, min_line, max_line, context, state, fun)
operand_to_algebra_with_comments(
operands,
meta,
min_line,
max_line,
state,
operand_to_algebra
)
if keyword?(right_arg) and context in [:parens_arg, :no_parens_arg] do
{wrap_in_parens(doc), state}
@@ -742,7 +749,7 @@ defmodule Code.Formatter do
{pipes, min_line} =
unwrap_pipes(left_arg, meta, left_context, [{{op, right_context}, right_arg}])
fun = fn
operand_to_algebra = fn
{{:root, context}, arg}, _args, state ->
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :left, 2)
{{doc, @empty, 1}, state}
@@ -754,7 +761,7 @@ defmodule Code.Formatter do
{{concat(op_string, doc), @empty, 1}, state}
end
operand_to_algebra_with_comments(pipes, meta, min_line, max_line, context, state, fun)
operand_to_algebra_with_comments(pipes, meta, min_line, max_line, state, operand_to_algebra)
end
defp binary_op_to_algebra(op, op_string, meta, left_arg, right_arg, context, state, nesting) do
@@ -778,7 +785,7 @@ defmodule Code.Formatter do
concat(concat(group(left), op_string), group(right))
true ->
eol? = eol?(meta, state)
eol? = eol?(meta)
next_break_fits? =
op in @next_break_fits_operators and next_break_fits?(right_arg, state) and not eol?
@@ -882,45 +889,11 @@ defmodule Code.Formatter do
{Enum.reverse(acc), line(meta)}
end
defp operand_to_algebra_with_comments(operands, meta, min_line, max_line, context, state, fun) do
# If we are in a no_parens_one_arg expression, we actually cannot
# extract comments from the first operand, because it would rewrite:
#
# @spec function(x) ::
# # Comment
# any
# when x: any
#
# to:
#
# @spec # Comment
# function(x) ::
# any
# when x: any
#
# Instead we get:
#
# @spec function(x) ::
# any
# # Comment
# when x: any
#
# Which may look counter-intuitive but it actually makes sense,
# as the closest possible location for the comment is the when
# operator.
{operands, acc, state} =
if context == :no_parens_one_arg do
[operand | operands] = operands
{doc_triplet, state} = fun.(operand, :unused, state)
{operands, [doc_triplet], state}
else
{operands, [], state}
end
defp operand_to_algebra_with_comments(operands, meta, min_line, max_line, state, fun) do
{docs, comments?, state} =
quoted_to_algebra_with_comments(operands, acc, min_line, max_line, state, fun)
quoted_to_algebra_with_comments(operands, [], min_line, max_line, state, fun)
if comments? or eol?(meta, state) do
if comments? or eol?(meta) do
{docs |> Enum.reduce(&line(&2, &1)) |> force_unfit(), state}
else
{docs |> Enum.reduce(&glue(&2, &1)), state}
@@ -985,7 +958,7 @@ defmodule Code.Formatter do
)
when is_atom(fun) and is_integer(arity) do
{target_doc, state} = remote_target_to_algebra(target, state)
fun = Code.Identifier.inspect_as_function(fun)
fun = remote_fun_to_algebra(target, fun, arity, state)
{target_doc |> nest(1) |> concat(string(".#{fun}/#{arity}")), state}
end
@@ -1030,7 +1003,7 @@ defmodule Code.Formatter do
defp remote_to_algebra({{:., _, [target, fun]}, meta, args}, context, state)
when is_atom(fun) do
{target_doc, state} = remote_target_to_algebra(target, state)
fun = Code.Identifier.inspect_as_function(fun)
fun = remote_fun_to_algebra(target, fun, length(args), state)
remote_doc = target_doc |> concat(".") |> concat(string(fun))
if args == [] and not remote_target_is_a_module?(target) and not meta?(meta, :closing) do
@@ -1066,6 +1039,38 @@ defmodule Code.Formatter do
end
end
defp remote_fun_to_algebra(target, fun, arity, state) do
%{rename_deprecated_at: since} = state
atom_target =
case since && target do
{:__aliases__, _, [alias | _] = aliases} when is_atom(alias) ->
Module.concat(aliases)
{:__block__, _, [atom]} when is_atom(atom) ->
atom
_ ->
nil
end
with {fun, requirement} <- deprecated(atom_target, fun, arity),
true <- Version.match?(since, requirement) do
fun
else
_ -> Code.Identifier.inspect_as_function(fun)
end
end
# We can only rename functions in the same module because
# introducing a new module may be wrong due to aliases.
defp deprecated(Enum, :partition, 2), do: {"split_with", "~> 1.4"}
defp deprecated(Code, :unload_files, 2), do: {"unrequire_files", "~> 1.7"}
defp deprecated(Code, :loaded_files, 2), do: {"required_files", "~> 1.7"}
defp deprecated(Kernel.ParallelCompiler, :files, 2), do: {"compile", "~> 1.6"}
defp deprecated(Kernel.ParallelCompiler, :files_to_path, 2), do: {"compile_to_path", "~> 1.6"}
defp deprecated(_, _, _), do: :error
defp remote_target_to_algebra({:fn, _, [_ | _]} = quoted, state) do
# This change is not semantically required but for beautification.
{doc, state} = quoted_to_algebra(quoted, :no_parens_arg, state)
@@ -1156,7 +1161,7 @@ defmodule Code.Formatter do
end
args = if keyword?, do: left ++ right, else: left ++ [right]
many_eol? = match?([_, _ | _], args) and eol?(meta, state)
many_eol? = match?([_, _ | _], args) and eol?(meta)
no_generators? = no_generators?(args)
to_algebra_fun = &quoted_to_algebra(&1, context, &2)
@@ -1346,8 +1351,9 @@ defmodule Code.Formatter do
end
defp list_interpolation_to_algebra([entry | entries], escape, state, acc, last) do
{{:., _, [Kernel, :to_string]}, _meta, [quoted]} = entry
{doc, state} = interpolation_to_string(quoted, state)
{{:., _, [Kernel, :to_string]}, meta, [quoted]} = entry
{doc, state} = block_to_algebra(quoted, line(meta), closing_line(meta), state)
doc = surround("\#{", doc, "}")
list_interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
end
@@ -1362,8 +1368,9 @@ defmodule Code.Formatter do
end
defp interpolation_to_algebra([entry | entries], escape, state, acc, last) do
{:"::", _, [{{:., _, [Kernel, :to_string]}, _meta, [quoted]}, {:binary, _, _}]} = entry
{doc, state} = interpolation_to_string(quoted, state)
{:"::", _, [{{:., _, [Kernel, :to_string]}, meta, [quoted]}, {:binary, _, _}]} = entry
{doc, state} = block_to_algebra(quoted, line(meta), closing_line(meta), state)
doc = surround("\#{", doc, "}")
interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
end
@@ -1371,23 +1378,6 @@ defmodule Code.Formatter do
{concat(acc, last), state}
end
defp interpolation_to_string(quoted, %{skip_eol: skip_eol} = state) do
{doc, state} = block_to_algebra(quoted, @max_line, @min_line, %{state | skip_eol: true})
doc = interpolation_to_string(surround("\#{", doc, "}"))
{doc, %{state | skip_eol: skip_eol}}
end
defp interpolation_to_string(doc) do
[head | tail] =
doc
|> format_to_string()
|> String.split("\n")
Enum.reduce(tail, string(head), fn line, acc ->
concat([acc, line(), string(line)])
end)
end
## Sigils
defp maybe_sigil_to_algebra(fun, meta, args, state) do
@@ -1426,7 +1416,7 @@ defmodule Code.Formatter do
defp bitstring_to_algebra(meta, args, state) do
last = length(args) - 1
join = if eol?(meta, state), do: :line, else: :flex_break
join = if eol?(meta), do: :line, else: :flex_break
to_algebra_fun = &bitstring_segment_to_algebra(&1, &2, last)
{args_doc, join, state} =
@@ -1494,7 +1484,7 @@ defmodule Code.Formatter do
## Literals
defp list_to_algebra(meta, args, state) do
join = if eol?(meta, state), do: :line, else: :break
join = if eol?(meta), do: :line, else: :break
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{args_doc, _join, state} =
@@ -1504,7 +1494,7 @@ defmodule Code.Formatter do
end
defp map_to_algebra(meta, name_doc, [{:|, _, [left, right]}], state) do
join = if eol?(meta, state), do: :line, else: :break
join = if eol?(meta), do: :line, else: :break
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{left_doc, state} = fun.(left, state)
@@ -1521,7 +1511,7 @@ defmodule Code.Formatter do
end
defp map_to_algebra(meta, name_doc, args, state) do
join = if eol?(meta, state), do: :line, else: :break
join = if eol?(meta), do: :line, else: :break
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{args_doc, _join, state} =
@@ -1532,7 +1522,7 @@ defmodule Code.Formatter do
end
defp tuple_to_algebra(meta, args, join, state) do
join = if eol?(meta, state), do: :line, else: join
join = if eol?(meta), do: :line, else: join
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{args_doc, join, state} =
@@ -1549,11 +1539,6 @@ defmodule Code.Formatter do
Atom.to_string(atom)
end
# TODO: Remove this clause in v1.16 when we no longer quote operator :..//
defp atom_to_algebra(:"..//") do
string(":\"..//\"")
end
defp atom_to_algebra(atom) do
string = Atom.to_string(atom)
@@ -1714,7 +1699,7 @@ defmodule Code.Formatter do
|> glue(body_doc)
|> nest(2)
|> glue("end")
|> maybe_force_clauses(clauses, state)
|> maybe_force_clauses(clauses)
|> group()
{doc, state}
@@ -1748,7 +1733,7 @@ defmodule Code.Formatter do
|> glue(body_doc)
|> nest(2)
|> glue("end")
|> maybe_force_clauses(clauses, state)
|> maybe_force_clauses(clauses)
|> group()
{doc, state}
@@ -1776,7 +1761,7 @@ defmodule Code.Formatter do
"(() -> "
|> concat(nest(body_doc, :cursor))
|> concat(")")
|> maybe_force_clauses(clauses, state)
|> maybe_force_clauses(clauses)
|> group()
{doc, state}
@@ -1797,7 +1782,7 @@ defmodule Code.Formatter do
|> group()
|> concat(break() |> concat(body_doc) |> nest(2))
|> wrap_in_parens()
|> maybe_force_clauses(clauses, state)
|> maybe_force_clauses(clauses)
|> group()
{doc, state}
@@ -1816,8 +1801,8 @@ defmodule Code.Formatter do
## Clauses
defp maybe_force_clauses(doc, clauses, state) do
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta, state) end) do
defp maybe_force_clauses(doc, clauses) do
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta) end) do
force_unfit(doc)
else
doc
@@ -1840,7 +1825,7 @@ defmodule Code.Formatter do
{doc_acc, state_acc}
end)
{clauses_doc |> maybe_force_clauses([clause | clauses], state) |> group(), state}
{clauses_doc |> maybe_force_clauses([clause | clauses]) |> group(), state}
end
defp clauses_to_algebra(other, min_line, max_line, state) do
@@ -2056,6 +2041,8 @@ defmodule Code.Formatter do
{wrap_in_parens_if_operator(doc, ast), state}
end
# TODO: We can remove this workaround once we remove
# ?rearrange_uop from the parser on v2.0.
defp wrap_in_parens_if_operator(doc, {:__block__, _, [expr]}) do
wrap_in_parens_if_operator(doc, expr)
end
@@ -2111,16 +2098,21 @@ defmodule Code.Formatter do
defp binary_operator?(quoted) do
case quoted do
{op, _, [_, _, _]} when op in @multi_binary_operators -> true
{op, _, [_, _]} when is_atom(op) -> Code.Identifier.binary_op(op) != :error
_ -> false
{op, _, [_, _]} when is_atom(op) ->
Code.Identifier.binary_op(op) != :error
_ ->
false
end
end
defp unary_operator?(quoted) do
case quoted do
{op, _, [_]} when is_atom(op) -> Code.Identifier.unary_op(op) != :error
_ -> false
{op, _, [_]} when is_atom(op) ->
Code.Identifier.unary_op(op) != :error
_ ->
false
end
end
@@ -2181,8 +2173,8 @@ defmodule Code.Formatter do
false
end
defp eol_or_comments?(meta, %{comments: comments} = state) do
eol?(meta, state) or
defp eol_or_comments?(meta, %{comments: comments}) do
eol?(meta) or
(
min_line = line(meta)
max_line = closing_line(meta)
@@ -2269,8 +2261,9 @@ defmodule Code.Formatter do
defp keyword_key?(_),
do: false
defp eol?(_meta, %{skip_eol: true}), do: false
defp eol?(meta, _state), do: Keyword.get(meta, :newlines, 0) > 0
defp eol?(meta) do
Keyword.get(meta, :newlines, 0) > 0
end
defp meta?(meta, key) do
is_list(meta[key])
+13 -15
View File
@@ -37,14 +37,13 @@ defmodule Code.Identifier do
op in [:"::"] -> {:right, 60}
op in [:|] -> {:right, 70}
op in [:=] -> {:right, 100}
op in [:||, :|||, :or] -> {:left, 120}
op in [:&&, :&&&, :and] -> {:left, 130}
op in [:==, :!=, :=~, :===, :!==] -> {:left, 140}
op in [:<, :<=, :>=, :>] -> {:left, 150}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 160}
op in [:in] -> {:left, 170}
op in [:^^^] -> {:left, 180}
op in [:"//"] -> {:right, 190}
op in [:||, :|||, :or] -> {:left, 130}
op in [:&&, :&&&, :and] -> {:left, 140}
op in [:==, :!=, :=~, :===, :!==] -> {:left, 150}
op in [:<, :<=, :>=, :>] -> {:left, 160}
op in [:|>, :<<<, :>>>, :<~, :~>, :<<~, :~>>, :<~>, :<|>] -> {:left, 170}
op in [:in] -> {:left, 180}
op in [:^^^] -> {:left, 190}
op in [:++, :--, :.., :<>, :+++, :---] -> {:right, 200}
op in [:+, :-] -> {:left, 210}
op in [:*, :/] -> {:left, 220}
@@ -69,11 +68,10 @@ defmodule Code.Identifier do
the ambiguity between the atom and the keyword identifier
* `:not_callable` - an atom that cannot be used as a function call after the
`.` operator. Those are typically AST nodes that are special forms (such as
`:%{}` and `:<<>>>`) as well as nodes that are ambiguous in calls (such as
`:..` and `:...`). This category also includes atoms like `:Foo`, since
they are valid identifiers but they need quotes to be used in function
calls (`Foo."Bar"`)
`.` operator (for example, `:<<>>` is not callable because `Foo.<<>>` is a
syntax error); this category includes atoms like `:Foo`, since they are
valid identifiers but they need quotes to be used in function calls
(`Foo."Bar"`)
* `:other` - any other atom (these are usually escaped when inspected, like
`:"foo and bar"`)
@@ -83,10 +81,10 @@ defmodule Code.Identifier do
charlist = Atom.to_charlist(atom)
cond do
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :"..//", :->] ->
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :->] ->
:not_callable
atom in [:"::", :"//"] ->
atom in [:"::"] ->
:not_atomable
unary_op(atom) != :error or binary_op(atom) != :error ->
+27 -52
View File
@@ -27,8 +27,8 @@ defprotocol Collectable do
## Examples
To show how to manually use the `Collectable` protocol, let's play with a
simplified implementation for `MapSet`.
To show how to manually use the `Collectable` protocol, let's play with its
implementation for `MapSet`.
iex> {initial_acc, collector_fun} = Collectable.into(MapSet.new())
iex> updated_acc = Enum.reduce([1, 2, 3], initial_acc, fn elem, acc ->
@@ -38,33 +38,21 @@ defprotocol Collectable do
#MapSet<[1, 2, 3]>
To show how the protocol can be implemented, we can again look at the
simplified implementation for `MapSet`. In this implementation "collecting" elements
implementation for `MapSet`. In this implementation "collecting" elements
simply means inserting them in the set through `MapSet.put/2`.
defimpl Collectable, for: MapSet do
def into(map_set) do
def into(original) do
collector_fun = fn
map_set_acc, {:cont, elem} ->
MapSet.put(map_set_acc, elem)
map_set_acc, :done ->
map_set_acc
_map_set_acc, :halt ->
:ok
set, {:cont, elem} -> MapSet.put(set, elem)
set, :done -> set
_set, :halt -> :ok
end
initial_acc = map_set
{initial_acc, collector_fun}
{original, collector_fun}
end
end
So now we can call `Enum.into/2`:
iex> Enum.into([1, 2, 3], MapSet.new())
#MapSet<[1, 2, 3]>
"""
@type command :: {:cont, term} | :done | :halt
@@ -72,11 +60,8 @@ defprotocol Collectable do
@doc """
Returns an initial accumulator and a "collector" function.
Receives a `collectable` which can be used as the initial accumulator that will
be passed to the function.
The collector function receives a term and a command and injects the term into
the collectable accumulator on every `{:cont, term}` command.
The returned function receives a term and a command and injects the term into
the collectable on every `{:cont, term}` command.
`:done` is passed as a command when no further values will be injected. This
is useful when there's a need to close resources or normalizing values. A
@@ -88,13 +73,13 @@ defprotocol Collectable do
For examples on how to use the `Collectable` protocol and `into/1` see the
module documentation.
"""
@spec into(t) :: {initial_acc :: term, collector :: (term, command -> t | term)}
@spec into(t) :: {term, (term, command -> t | term)}
def into(collectable)
end
defimpl Collectable, for: List do
def into(list) do
if list != [] do
def into(original) do
if original != [] do
IO.warn(
"the Collectable protocol is deprecated for non-empty lists. The behaviour of " <>
"things like Enum.into/2 or \"for\" comprehensions with an :into option is incorrect " <>
@@ -105,14 +90,9 @@ defimpl Collectable, for: List do
end
fun = fn
list_acc, {:cont, elem} ->
[elem | list_acc]
list_acc, :done ->
list ++ :lists.reverse(list_acc)
_list_acc, :halt ->
:ok
list, {:cont, x} -> [x | list]
list, :done -> original ++ :lists.reverse(list)
_, :halt -> :ok
end
{[], fun}
@@ -120,7 +100,7 @@ defimpl Collectable, for: List do
end
defimpl Collectable, for: BitString do
def into(binary) when is_binary(binary) do
def into(original) when is_binary(original) do
fun = fn
acc, {:cont, x} when is_binary(x) and is_list(acc) ->
[acc | x]
@@ -137,14 +117,14 @@ defimpl Collectable, for: BitString do
acc, :done ->
IO.iodata_to_binary(acc)
__acc, :halt ->
_, :halt ->
:ok
end
{[binary], fun}
{[original], fun}
end
def into(bitstring) do
def into(original) when is_bitstring(original) do
fun = fn
acc, {:cont, x} when is_bitstring(x) ->
<<acc::bitstring, x::bitstring>>
@@ -152,27 +132,22 @@ defimpl Collectable, for: BitString do
acc, :done ->
acc
_acc, :halt ->
_, :halt ->
:ok
end
{bitstring, fun}
{original, fun}
end
end
defimpl Collectable, for: Map do
def into(map) do
def into(original) do
fun = fn
map_acc, {:cont, {key, value}} ->
Map.put(map_acc, key, value)
map_acc, :done ->
map_acc
_map_acc, :halt ->
:ok
map, {:cont, {k, v}} -> Map.put(map, k, v)
map, :done -> map
_, :halt -> :ok
end
{map, fun}
{original, fun}
end
end
+3 -6
View File
@@ -16,8 +16,7 @@ defmodule Config do
import_config "#{config_env()}.exs"
`import Config` will import the functions `config/2`, `config/3`
`config_env/0`, `config_target/0`, and `import_config/1`
to help you manage your configuration.
and `import_config/1` to help you manage your configuration.
`config/2` and `config/3` are used to define key-value configuration
for a given application. Once Mix starts, it will automatically
@@ -36,10 +35,8 @@ defmodule Config do
**Important:** if you are writing a library to be used by other developers,
it is generally recommended to avoid the application environment, as the
application environment is effectively a global storage. Also note that
the `config/config.exs` of a library is not evaluated when the library is
used as a dependency, as configuration is always meant to configure the
current project. For more information, read our [library guidelines](library-guidelines.md).
application environment is effectively a global storage. For more information,
read our [library guidelines](library-guidelines.md).
## Migrating from `use Mix.Config`
+11 -43
View File
@@ -11,45 +11,11 @@ defmodule Config.Provider do
the file system. For more information on runtime configuration,
see `mix release`.
## Multiple config files
## Sample config provider
One common use of config providers is to specify multiple
configuration files in a release. Elixir ships with one provider,
called `Config.Reader`, which is capable of handling Elixir's
built-in config files.
For example, imagine you want to list some basic configuration
on Mix's built-in `config/runtime.exs` file, but you also want
some additional configuration files. To do so, you can do this
in your `mix.exs`:
releases: [
demo: [
config_providers: [
{Config.Reader, {:system, "RELEASE_ROOT", "/extra_config.exs"}}
]
]
]
You can place this `extra_config.exs` file in your release in
multiple ways:
1. If it is available on the host when assembling the release,
you can place it on "rel/overlays/extra_config.exs" and it
will be automatically copied to the release root
2. If it is available on the target during deployment, you can
simply copy it to the release root as a step in your deployment
Now once the system boots, it will load both `config/runtime.exs`
and `extra_config.exs` early in the boot process.
## Custom config provider
You can also implement custom config providers, similar to how
`Config.Reader` works. For example, imagine you need to load
some configuration from a JSON file and load that into the system.
Said configuration provider would look like:
For example, imagine you need to load some configuration from
a JSON file and load that into the system. Said configuration
provider would look like:
defmodule JSONConfigProvider do
@behaviour Config.Provider
@@ -73,17 +39,19 @@ defmodule Config.Provider do
end
end
Then, when specifying your release, you can specify the provider in
Then when specifying your release, you can specify the provider in
the release configuration:
releases: [
demo: [
config_providers: [
{JSONConfigProvider, "/etc/config.json"}
]
# ...,
config_providers: [{JSONConfigProvider, "/etc/config.json"}]
]
]
Now once the system boots, it will invoke the provider early in
the boot process, save the merged configuration to the disk, and
reboot the system with the new values in place.
"""
@type config :: keyword
@@ -125,7 +93,7 @@ defmodule Config.Provider do
Loads configuration (typically during system boot).
It receives the current `config` and the `state` returned by
`c:init/1`. Then, you typically read the extra configuration
`c:init/1`. Then you typically read the extra configuration
from an external source and merge it into the received `config`.
Merging should be done with `Config.Reader.merge/2`, as it
performs deep merge. It should return the updated config.
+1 -1
View File
@@ -16,7 +16,7 @@ defmodule Config.Reader do
Or if you want to read a custom path inside the release:
config_providers: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
config_provider: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
You can also pass a keyword list of options to the reader,
where the `:path` is a required key:
+3 -3
View File
@@ -147,10 +147,10 @@ defmodule DynamicSupervisor do
extra_arguments: [term()]
}
@typedoc "Options given to `start_link` functions"
@typedoc "Option values used by the `start*` functions"
@type option :: GenServer.option()
@typedoc "Options given to `start_link` and `init/1` functions"
@typedoc "Options given to `start_link/1` and `init/1`"
@type init_option ::
{:strategy, strategy()}
| {:max_restarts, non_neg_integer()}
@@ -419,7 +419,7 @@ defmodule DynamicSupervisor do
@doc since: "1.6.0"
@spec which_children(Supervisor.supervisor()) :: [
# module() | :dynamic here because :supervisor.modules() is not exported
{:undefined, pid | :restarting, :worker | :supervisor, [module()] | :dynamic}
{:undefined, pid | :restarting, :worker | :supervisor, module() | :dynamic}
]
def which_children(supervisor) do
call(supervisor, :which_children)
+148 -658
View File
File diff suppressed because it is too large Load Diff
+18 -72
View File
@@ -252,8 +252,8 @@ defmodule Exception do
defp rewrite_arg(arg) do
Macro.prewalk(arg, fn
{:%{}, meta, [__struct__: Range, first: first, last: last, step: step]} ->
{:"..//", meta, [first, last, step]}
{:%{}, meta, [__struct__: Range, first: first, last: last]} ->
{:.., meta, [first, last]}
other ->
other
@@ -709,7 +709,7 @@ defmodule ArgumentError do
@impl true
def blame(
exception,
%{message: "argument error"} = exception,
[{:erlang, :apply, [module, function, args], _} | _] = stacktrace
) do
message =
@@ -783,7 +783,12 @@ defmodule ArithmeticError do
end
defmodule SystemLimitError do
defexception message: "a system limit has been reached"
defexception []
@impl true
def message(_) do
"a system limit has been reached"
end
end
defmodule SyntaxError do
@@ -987,23 +992,11 @@ defmodule UndefinedFunctionError do
@doc false
def hint_for_loaded_module(module, function, arity, exports) do
cond do
macro_exported?(module, function, arity) ->
". However there is a macro with the same name and arity. " <>
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
message = otp_obsolete(module, function, arity) ->
", #{message}"
true ->
IO.iodata_to_binary(did_you_mean(module, function, exports))
end
end
defp otp_obsolete(module, function, arity) do
case :otp_internal.obsolete(module, function, arity) do
{:removed, [_ | _] = string} -> string
_ -> nil
if macro_exported?(module, function, arity) do
". However there is a macro with the same name and arity. " <>
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
else
IO.iodata_to_binary(did_you_mean(module, function, exports))
end
end
@@ -1255,10 +1248,6 @@ defmodule KeyError do
end
@impl true
def blame(exception = %{message: message}, stacktrace) when is_binary(message) do
{exception, stacktrace}
end
def blame(exception = %{term: nil}, stacktrace) do
message = message(exception.key, exception.term)
{%{exception | message: message}, stacktrace}
@@ -1416,32 +1405,16 @@ defmodule ErlangError do
end
@doc false
def normalize(:badarg, stacktrace) do
case error_info(:badarg, stacktrace) do
{:ok, args} ->
message = "errors were found at the given arguments:\n\n#{args}"
%ArgumentError{message: message}
:error ->
%ArgumentError{}
end
def normalize(:badarg, _stacktrace) do
%ArgumentError{}
end
def normalize(:badarith, _stacktrace) do
%ArithmeticError{}
end
def normalize(:system_limit, stacktrace) do
case error_info(:system_limit, stacktrace) do
{:ok, args} ->
message =
"a system limit has been reached due to errors at the given arguments:\n\n#{args}"
%SystemLimitError{message: message}
:error ->
%SystemLimitError{}
end
def normalize(:system_limit, _stacktrace) do
%SystemLimitError{}
end
def normalize(:cond_clause, _stacktrace) do
@@ -1531,31 +1504,4 @@ defmodule ErlangError do
defp from_stacktrace(_) do
{nil, nil, nil}
end
@doc false
def error_info(erl_exception, stacktrace) do
with [{module, _, args_or_arity, opts} | _] <- stacktrace,
%{} = error_info <- opts[:error_info] do
module = Map.get(error_info, :module, module)
function = Map.get(error_info, :function, :format_error)
arity = if is_integer(args_or_arity), do: args_or_arity, else: length(args_or_arity)
extra = apply(module, function, [erl_exception, stacktrace])
args_errors = Map.take(extra, Enum.to_list(1..arity//1))
if map_size(args_errors) > 0 do
{:ok, IO.iodata_to_binary(Enum.map(args_errors, &arg_error/1))}
else
:error
end
else
_ -> :error
end
end
defp arg_error({n, message}), do: " * #{nth(n)} argument: #{message}\n"
defp nth(1), do: "1st"
defp nth(2), do: "2nd"
defp nth(3), do: "3rd"
defp nth(n), do: "#{n}th"
end
+8 -17
View File
@@ -764,16 +764,15 @@ defmodule File do
end
@doc """
Copies the contents of `source_file` to `destination_file` preserving its modes.
Copies the contents in `source_file` to `destination_file` preserving its modes.
`source_file` must be a file or a symbolic link to one. `destination_file` must
be a path to a non-existent file. If either is a directory, `{:error, :eisdir}`
will be returned.
`source_file` and `destination_file` must be a file or a symbolic link to one,
or in the case of destination, a path to a non-existent file. If either one of
them is a directory, `{:error, :eisdir}` will be returned.
The `callback` function is invoked if the `destination_file` already exists.
The function receives arguments for `source_file` and `destination_file`;
it should return `true` if the existing file should be overwritten, `false` if
otherwise. The default callback returns `true`.
If a file already exists in the destination, it invokes a
callback which should return `true` if the existing file
should be overwritten, `false` otherwise. The callback defaults to return `true`.
The function returns `:ok` in case of success. Otherwise, it returns
`{:error, reason}`.
@@ -1542,12 +1541,6 @@ defmodule File do
executes the given function and then reverts back
to the previous path regardless of whether there is an exception.
The current working directory is temporarily set for the BEAM globally. This
can lead to race conditions if multiple processes are changing the current
working directory concurrently. To run an external command in a given
directory without changing the global current working directory, use the
`:cd` option of `System.cmd/3` and `Port.open/2`.
Raises an error if retrieving or changing the current
directory fails.
"""
@@ -1614,9 +1607,7 @@ defmodule File do
which means it can be used both for read and write.
The `line_or_bytes` argument configures how the file is read when
streaming, by `:line` (default) or by a given number of bytes. When
using the `:line` option, CRLF line breaks (`"\r\n"`) are normalized
to LF (`"\n"`).
streaming, by `:line` (default) or by a given number of bytes.
Operating the stream can fail on open for the same reasons as
`File.open!/2`. Note that the file is automatically opened each time streaming
+40 -96
View File
@@ -46,46 +46,6 @@ defmodule Float do
@precision_range 0..15
@type precision_range :: 0..15
@doc """
Computes `base` raised to power of `exponent`.
`base` must be a float and `exponent` can be any number.
However, if a negative base and a fractional exponent
are given, it raises `ArithmeticError`.
It always returns a float. See `Integer.pow/2` for
exponentiation that returns integers.
## Examples
iex> Float.pow(2.0, 0)
1.0
iex> Float.pow(2.0, 1)
2.0
iex> Float.pow(2.0, 10)
1024.0
iex> Float.pow(2.0, -1)
0.5
iex> Float.pow(2.0, -3)
0.125
iex> Float.pow(3.0, 1.5)
5.196152422706632
iex> Float.pow(-2.0, 3)
-8.0
iex> Float.pow(-2.0, 4)
16.0
iex> Float.pow(-1.0, 0.5)
** (ArithmeticError) bad argument in arithmetic expression
"""
@doc since: "1.12.0"
@spec pow(float, number) :: float
def pow(base, exponent) when is_float(base) and is_number(exponent),
do: :math.pow(base, exponent)
@doc """
Parses a binary into a float.
@@ -308,11 +268,11 @@ defmodule Float do
raise ArgumentError, invalid_precision_message(precision)
end
defp round(0.0 = num, _precision, _rounding), do: num
defp round(0.0, _precision, _rounding), do: 0.0
defp round(float, precision, rounding) do
<<sign::1, exp::11, significant::52-bitstring>> = <<float::float>>
{num, count} = decompose(significant, 1)
{num, count, _} = decompose(significant, 1)
count = count - exp + 1023
cond do
@@ -364,22 +324,6 @@ defmodule Float do
end
end
defp decompose(significant, initial) do
decompose(significant, 1, 0, initial)
end
defp decompose(<<1::1, bits::bitstring>>, count, last_count, acc) do
decompose(bits, count + 1, count, (acc <<< (count - last_count)) + 1)
end
defp decompose(<<0::1, bits::bitstring>>, count, last_count, acc) do
decompose(bits, count + 1, last_count, acc)
end
defp decompose(<<>>, _count, last_count, acc) do
{acc, last_count}
end
defp scale_up(num, boundary, exp) when num >= boundary, do: {num, exp}
defp scale_up(num, boundary, exp), do: scale_up(num <<< 1, boundary, exp - 1)
@@ -459,55 +403,55 @@ defmodule Float do
def ratio(0.0), do: {0, 1}
def ratio(float) when is_float(float) do
<<sign::1, exp::11, mantissa::52>> = <<float::float>>
case <<float::float>> do
<<sign::1, 0::11, significant::52-bitstring>> ->
{num, _, den} = decompose(significant, 0)
{sign(sign, num), shift_left(den, 1022)}
{num, den_exp} =
if exp != 0 do
# Floats are expressed like this:
# (2**52 + mantissa) * 2**(-52 + exp - 1023)
#
# We compute the root factors of the mantissa so we have this:
# (2**52 + mantissa * 2**count) * 2**(-52 + exp - 1023)
{mantissa, count} = root_factors(mantissa, 0)
<<sign::1, exp::11, significant::52-bitstring>> ->
{num, _, den} = decompose(significant, 1)
num = sign(sign, num)
# Now we can move the count around so we have this:
# (2**(52-count) + mantissa) * 2**(count + -52 + exp - 1023)
if mantissa == 0 do
{1, exp - 1023}
else
num = (1 <<< (52 - count)) + mantissa
den_exp = count - 52 + exp - 1023
{num, den_exp}
case exp - 1023 do
exp when exp > 0 ->
{den, exp} = shift_right(den, exp)
{shift_left(num, exp), den}
exp when exp < 0 ->
{num, shift_left(den, -exp)}
0 ->
{num, den}
end
else
# Subnormals are expressed like this:
# (mantissa) * 2**(-52 + 1 - 1023)
#
# So we compute it to this:
# (mantissa * 2**(count)) * 2**(-52 + 1 - 1023)
#
# Which becomes:
# mantissa * 2**(count-1074)
root_factors(mantissa, -1074)
end
if den_exp > 0 do
{sign(sign, num <<< den_exp), 1}
else
{sign(sign, num), 1 <<< -den_exp}
end
end
defp root_factors(mantissa, count) when mantissa != 0 and (mantissa &&& 1) == 0,
do: root_factors(mantissa >>> 1, count + 1)
defp decompose(significant, initial) do
decompose(significant, 1, 0, 2, 1, initial)
end
defp root_factors(mantissa, count),
do: {mantissa, count}
defp decompose(<<1::1, bits::bitstring>>, count, last_count, power, _last_power, acc) do
decompose(bits, count + 1, count, power <<< 1, power, shift_left(acc, count - last_count) + 1)
end
@compile {:inline, sign: 2}
defp decompose(<<0::1, bits::bitstring>>, count, last_count, power, last_power, acc) do
decompose(bits, count + 1, last_count, power <<< 1, last_power, acc)
end
defp decompose(<<>>, _count, last_count, _power, last_power, acc) do
{acc, last_count, last_power}
end
@compile {:inline, sign: 2, shift_left: 2}
defp sign(0, num), do: num
defp sign(1, num), do: -num
defp shift_left(num, times), do: num <<< times
defp shift_right(num, 0), do: {num, 0}
defp shift_right(1, times), do: {1, times}
defp shift_right(num, times), do: shift_right(num >>> 1, times - 1)
@doc """
Returns a charlist which corresponds to the text representation
of the given float.
+2 -2
View File
@@ -7,7 +7,7 @@ defmodule GenEvent do
If you are interested in implementing an event manager, please read the
"Alternatives" section below. If you have to implement an event handler to
integrate with an existing system, such as Elixir's Logger, please use
[`:gen_event`](`:gen_event`) instead.
[`:gen_event`](https://erlang.org/doc/man/gen_event.html) instead.
## Alternatives
@@ -38,7 +38,7 @@ defmodule GenEvent do
If your use case requires exactly what GenEvent provided, or you have to
integrate with an existing `:gen_event`-based system, you can still use the
[`:gen_event`](`:gen_event`) Erlang module.
[`:gen_event`](http://erlang.org/doc/man/gen_event.html) Erlang module.
"""
@moduledoc deprecated: "Use Erlang/OTP's :gen_event module instead"
+33 -20
View File
@@ -61,7 +61,7 @@ defmodule GenServer do
Every time you do a `GenServer.call/3`, the client will send a message
that must be handled by the `c:handle_call/3` callback in the GenServer.
A `cast/2` message must be handled by `c:handle_cast/2`. There are 8 possible
A `cast/2` message must be handled by `c:handle_cast/2`. There are 7 possible
callbacks to be implemented when you use a `GenServer`. The only required
callback is `c:init/1`.
@@ -163,12 +163,12 @@ defmodule GenServer do
using `Process.register/2`.
* `{:global, term}` - the GenServer is registered globally with the given
term using the functions in the [`:global` module](`:global`).
term using the functions in the [`:global` module](http://www.erlang.org/doc/man/global.html).
* `{:via, module, term}` - the GenServer is registered with the given
mechanism and name. The `:via` option expects a module that exports
`register_name/2`, `unregister_name/1`, `whereis_name/1` and `send/2`.
One such example is the [`:global` module](`:global`) which uses these functions
One such example is the [`:global` module](http://www.erlang.org/doc/man/global.html) which uses these functions
for keeping the list of names of processes and their associated PIDs
that are available globally for a network of Elixir nodes. Elixir also
ships with a local, decentralized and scalable registry called `Registry`
@@ -242,8 +242,7 @@ defmodule GenServer do
end
defp schedule_work do
# We schedule the work to happen in 2 hours (written in milliseconds).
# Alternatively, one might write :timer.hours(2)
# In 2 hours
Process.send_after(self(), :work, 2 * 60 * 60 * 1000)
end
end
@@ -312,14 +311,14 @@ defmodule GenServer do
## Debugging with the :sys module
GenServers, as [special processes](https://erlang.org/doc/design_principles/spec_proc.html),
can be debugged using the [`:sys` module](`:sys`).
GenServers, as [special processes](http://erlang.org/doc/design_principles/spec_proc.html),
can be debugged using the [`:sys` module](http://www.erlang.org/doc/man/sys.html).
Through various hooks, this module allows developers to introspect the state of
the process and trace system events that happen during its execution, such as
received messages, sent replies and state changes.
Let's explore the basic functions from the
[`:sys` module](`:sys`) used for debugging:
[`:sys` module](http://www.erlang.org/doc/man/sys.html) used for debugging:
* `:sys.get_state/2` - allows retrieval of the state of the process.
In the case of a GenServer process, it will be the callback module state,
@@ -401,8 +400,8 @@ defmodule GenServer do
in Erlang can also provide extra insight.
* [GenServer - Elixir's Getting Started Guide](https://elixir-lang.org/getting-started/mix-otp/genserver.html)
* [`:gen_server` module documentation](`:gen_server`)
* [gen_server Behaviour - OTP Design Principles](https://erlang.org/doc/design_principles/gen_server_concepts.html)
* [`:gen_server` module documentation](http://www.erlang.org/doc/man/gen_server.html)
* [gen_server Behaviour - OTP Design Principles](http://www.erlang.org/doc/design_principles/gen_server_concepts.html)
* [Clients and Servers - Learn You Some Erlang for Great Good!](http://learnyousomeerlang.com/clients-and-servers)
"""
@@ -425,7 +424,7 @@ defmodule GenServer do
`c:handle_call/3` for more information on hibernation.
Returning `{:ok, state, {:continue, continue}}` is similar to
`{:ok, state}` except that immediately after entering the loop,
`{:ok, state}` except that immediately after entering the loop
the `c:handle_continue/2` callback will be invoked with the value
`continue` as first argument.
@@ -472,8 +471,8 @@ defmodule GenServer do
Returning `{:reply, reply, new_state, :hibernate}` is similar to
`{:reply, reply, new_state}` except the process is hibernated and will
continue the loop once a message is in its message queue. However, if a message is
already in the message queue, the process will continue the loop immediately. Hibernating a
continue the loop once a message is in its message queue. If a message is
already in the message queue this will be immediately. Hibernating a
`GenServer` causes garbage collection and leaves a continuous heap that
minimises the memory used by the process.
@@ -507,7 +506,7 @@ defmodule GenServer do
occurs as with a `:reply` tuple.
Returning `{:stop, reason, reply, new_state}` stops the loop and `c:terminate/2`
is called with reason `reason` and state `new_state`. Then, the `reply` is sent
is called with reason `reason` and state `new_state`. Then the `reply` is sent
as the response to call and the process exits with reason `reason`.
Returning `{:stop, reason, new_state}` is similar to
@@ -585,6 +584,8 @@ defmodule GenServer do
This callback is optional. If one is not implemented, the server will fail
if a continue instruction is used.
This callback is only supported on Erlang/OTP 21+.
"""
@callback handle_continue(continue :: term, state :: term) ::
{:noreply, new_state}
@@ -598,13 +599,15 @@ defmodule GenServer do
`reason` is exit reason and `state` is the current state of the `GenServer`.
The return value is ignored.
`c:terminate/2` is called if the `GenServer` traps exits (using `Process.flag/2`)
*and* the parent process sends an exit signal, or a callback (except `c:init/1`)
does one of the following:
`c:terminate/2` is called if a callback (except `c:init/1`) does one of the
following:
* returns a `:stop` tuple
* raises (via `Kernel.raise/2`) or exits (via `Kernel.exit/1`)
* raises
* calls `Kernel.exit/1`
* returns an invalid value
* the `GenServer` traps exits (using `Process.flag/2`) *and* the parent
process sends an exit signal
If part of a supervision tree, a `GenServer` will receive an exit
signal when the tree is shutting down. The exit signal is based on
@@ -711,7 +714,7 @@ defmodule GenServer do
{:debug, debug}
| {:name, name}
| {:timeout, timeout}
| {:spawn_opt, [Process.spawn_opt()]}
| {:spawn_opt, Process.spawn_opt()}
| {:hibernate_after, timeout}
@typedoc "Debug options supported by the `start*` functions"
@@ -890,7 +893,7 @@ defmodule GenServer do
milliseconds initializing or it will be terminated and the start function
will return `{:error, :timeout}`
* `:debug` - if present, the corresponding function in the [`:sys` module](`:sys`) is invoked
* `:debug` - if present, the corresponding function in the [`:sys` module](http://www.erlang.org/doc/man/sys.html) is invoked
* `:spawn_opt` - if present, its value is passed as options to the
underlying process as in `Process.spawn/4`
@@ -1036,8 +1039,18 @@ defmodule GenServer do
is unknown whether the destination `server` successfully
handled the message.
`c:handle_cast/2` will be called on the server to handle
the request. In case the `server` is on a node which is
not yet connected to the caller one, the semantics differ
depending on the used Erlang/OTP version.
`server` can be any of the values described in the "Name registration"
section of the documentation for this module.
Before Erlang/OTP 21, the call is going to block until a
connection happens. This was done to guarantee ordering.
Starting with Erlang/OTP 21, both Erlang and Elixir do
not block the call.
"""
@spec cast(server, term) :: :ok
def cast(server, request)
+8 -9
View File
@@ -26,8 +26,8 @@ defprotocol Inspect do
defimpl Inspect, for: MapSet do
import Inspect.Algebra
def inspect(map_set, opts) do
concat(["#MapSet<", to_doc(MapSet.to_list(map_set), opts), ">"])
def inspect(dict, opts) do
concat(["#MapSet<", to_doc(MapSet.to_list(dict), opts), ">"])
end
end
@@ -454,17 +454,16 @@ defimpl Inspect, for: Any do
end
def inspect(map, name, opts) do
map = Map.to_list(map) ++ [:...]
# Use the :limit option and an extra element to force
# `container_doc/6` to append "...".
opts = %{opts | limit: min(opts.limit, map_size(map))}
map = Map.to_list(map) ++ ["..."]
open = color("#" <> name <> "<", :map, opts)
sep = color(",", :map, opts)
close = color(">", :map, opts)
fun = fn
{key, value}, opts -> Inspect.List.keyword({key, value}, opts)
:..., _opts -> "..."
end
container_doc(open, map, close, opts, fun, separator: sep, break: :strict)
container_doc(open, map, close, opts, &Inspect.List.keyword/2, separator: sep, break: :strict)
end
end
+2 -2
View File
@@ -894,12 +894,12 @@ defmodule Inspect.Algebra do
format(width, 0, [{0, :flat, doc}])
end
# Type representing the document mode to be rendered:
# Type representing the document mode to be rendered
#
# * flat - represents a document with breaks as flats (a break may fit, as it may break)
# * break - represents a document with breaks as breaks (a break always fits, since it breaks)
#
# The following modes are exclusive to fitting:
# The following modes are exclusive to fitting
#
# * flat_no_break - represents a document with breaks as flat not allowed to enter in break mode
# * break_no_flat - represents a document with breaks as breaks not allowed to enter in flat mode
+42 -113
View File
@@ -64,55 +64,6 @@ defmodule Integer do
"""
defguard is_even(integer) when is_integer(integer) and (integer &&& 1) == 0
@doc """
Computes `base` raised to power of `exponent`.
Both `base` and `exponent` must be integers.
The exponent must be zero or positive.
See `Float.pow/2` for exponentiation of negative
exponents as well as floats.
## Examples
iex> Integer.pow(2, 0)
1
iex> Integer.pow(2, 1)
2
iex> Integer.pow(2, 10)
1024
iex> Integer.pow(2, 11)
2048
iex> Integer.pow(2, 64)
0x10000000000000000
iex> Integer.pow(3, 4)
81
iex> Integer.pow(4, 3)
64
iex> Integer.pow(-2, 3)
-8
iex> Integer.pow(-2, 4)
16
iex> Integer.pow(2, -2)
** (ArithmeticError) bad argument in arithmetic expression
"""
@doc since: "1.12.0"
@spec pow(integer, non_neg_integer) :: integer
def pow(base, exponent) when is_integer(base) and is_integer(exponent) do
if exponent < 0, do: :erlang.error(:badarith, [base, exponent])
guarded_pow(base, exponent)
end
# https://en.wikipedia.org/wiki/Exponentiation_by_squaring
defp guarded_pow(_, 0), do: 1
defp guarded_pow(b, 1), do: b
defp guarded_pow(b, e) when (e &&& 1) == 0, do: guarded_pow(b * b, e >>> 1)
defp guarded_pow(b, e), do: b * guarded_pow(b * b, e >>> 1)
@doc """
Computes the modulo remainder of an integer division.
@@ -275,7 +226,7 @@ defmodule Integer do
** (ArgumentError) invalid base 38
"""
@spec parse(binary, 2..36) :: {integer, remainder_of_binary :: binary} | :error
@spec parse(binary, 2..36) :: {integer, binary} | :error
def parse(binary, base \\ 10)
def parse(_binary, base) when base not in 2..36 do
@@ -318,12 +269,12 @@ defmodule Integer do
defp count_digits_nosign(<<_::bits>>, _, count), do: count
# TODO: Remove Integer.to_string/1 once the minimum supported version is
# Erlang/OTP 22, since it is covered by the now BIF Integer.to_string/2.
# Please reapply commit 2622fd6b0aa419a983a899a1fbdb5deefba3d85d.
@doc """
Returns a binary which corresponds to the text representation
of `integer` in the given `base`.
`base` can be an integer between 2 and 36. If no `base` is given,
it defaults to `10`.
of `integer`.
Inlined by the compiler.
@@ -341,6 +292,22 @@ defmodule Integer do
iex> Integer.to_string(0123)
"123"
"""
@spec to_string(integer) :: String.t()
def to_string(integer) do
:erlang.integer_to_binary(integer)
end
@doc """
Returns a binary which corresponds to the text representation
of `integer` in the given `base`.
`base` can be an integer between 2 and 36.
Inlined by the compiler.
## Examples
iex> Integer.to_string(100, 16)
"64"
@@ -352,16 +319,15 @@ defmodule Integer do
"""
@spec to_string(integer, 2..36) :: String.t()
def to_string(integer, base \\ 10) do
def to_string(integer, base) do
:erlang.integer_to_binary(integer, base)
end
# TODO: Remove Integer.to_charlist/1 once the minimum supported version is
# Erlang/OTP 22, since it is covered by the now BIF Integer.to_charlist/2.
# Please reapply commit 2622fd6b0aa419a983a899a1fbdb5deefba3d85d.
@doc """
Returns a charlist which corresponds to the text representation
of `integer` in the given `base`.
`base` can be an integer between 2 and 36. If no `base` is given,
it defaults to `10`.
Returns a charlist which corresponds to the text representation of the given `integer`.
Inlined by the compiler.
@@ -379,6 +345,21 @@ defmodule Integer do
iex> Integer.to_charlist(0123)
'123'
"""
@spec to_charlist(integer) :: charlist
def to_charlist(integer) do
:erlang.integer_to_list(integer)
end
@doc """
Returns a charlist which corresponds to the text representation of `integer` in the given `base`.
`base` can be an integer between 2 and 36.
Inlined by the compiler.
## Examples
iex> Integer.to_charlist(100, 16)
'64'
@@ -390,7 +371,7 @@ defmodule Integer do
"""
@spec to_charlist(integer, 2..36) :: charlist
def to_charlist(integer, base \\ 10) do
def to_charlist(integer, base) do
:erlang.integer_to_list(integer, base)
end
@@ -433,58 +414,6 @@ defmodule Integer do
defp gcd_positive(integer1, 0), do: integer1
defp gcd_positive(integer1, integer2), do: gcd_positive(integer2, rem(integer1, integer2))
@doc """
Returns the extended greatest common divisor of the two given integers.
It uses the Extended Euclidean algorithm to return a three-element tuple with the `gcd`
and the coefficients `m` and `n` of Bézout's identity such that:
gcd(a, b) = m*a + n*b
By convention, `extended_gcd(0, 0)` returns `{0, 0, 0}`.
## Examples
iex> Integer.extended_gcd(240, 46)
{2, -9, 47}
iex> Integer.extended_gcd(46, 240)
{2, 47, -9}
iex> Integer.extended_gcd(-46, 240)
{2, -47, -9}
iex> Integer.extended_gcd(-46, -240)
{2, -47, 9}
iex> Integer.extended_gcd(14, 21)
{7, -1, 1}
iex> Integer.extended_gcd(10, 0)
{10, 1, 0}
iex> Integer.extended_gcd(0, 10)
{10, 0, 1}
iex> Integer.extended_gcd(0, 0)
{0, 0, 0}
"""
@doc since: "1.12.0"
@spec extended_gcd(integer, integer) :: {non_neg_integer, integer, integer}
def extended_gcd(0, 0), do: {0, 0, 0}
def extended_gcd(0, n), do: {n, 0, 1}
def extended_gcd(n, 0), do: {n, 1, 0}
def extended_gcd(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
extended_gcd(integer2, integer1, 0, 1, 1, 0)
end
defp extended_gcd(r1, r0, s1, s0, t1, t0) do
div = div(r0, r1)
case r0 - div * r1 do
0 when r1 > 0 -> {r1, s1, t1}
0 when r1 < 0 -> {-r1, -s1, -t1}
r2 -> extended_gcd(r2, r1, s0 - div * s1, s1, t0 - div * t1, t1)
end
end
@doc false
@deprecated "Use Integer.to_charlist/1 instead"
def to_char_list(integer), do: Integer.to_charlist(integer)
+9 -35
View File
@@ -34,7 +34,7 @@ defmodule IO do
IO data is a data type that can be used as a more efficient alternative to binaries
in certain situations.
A term of type **IO data** is a binary or a list containing bytes (integers within the `0..255` range)
A term of type **IO data** is a binary or a list containing bytes (integers in `0..255`)
or nested IO data. The type is recursive. Let's see an example of one of
the possible IO data representing the binary `"hello"`:
@@ -99,8 +99,8 @@ defmodule IO do
Erlang and Elixir also have the idea of `t:chardata/0`. Chardata is very
similar to IO data: the only difference is that integers in IO data represent
bytes while integers in chardata represent Unicode code points. Bytes
(`t:byte/0`) are integers within the `0..255` range, while Unicode code points
(`t:char/0`) are integers within the `0..0x10FFFF` range. The `IO` module provides
(`t:byte/0`) are integers in the `0..255` range, while Unicode code points
(`t:char/0`) are integers in the range `0..0x10FFFF`. The `IO` module provides
the `chardata_to_string/1` function for chardata as the "counter-part" of the
`iodata_to_binary/1` function for IO data.
@@ -108,8 +108,8 @@ defmodule IO do
argument error. For example, let's try to put a code point that is not
representable with one byte, like `?π`, inside IO data:
IO.iodata_to_binary(["The symbol for pi is: ", ?π])
#=> ** (ArgumentError) argument error
iex> IO.iodata_to_binary(["The symbol for pi is: ", ?π])
** (ArgumentError) argument error
If we use chardata instead, it will work as expected:
@@ -508,17 +508,6 @@ defmodule IO do
:io.get_line(map_dev(device), to_chardata(prompt))
end
@doc """
Returns a line-based `IO.Stream` on `:stdio`.
This is equivalent to:
IO.stream(:stdio, :line)
"""
@doc since: "1.12.0"
def stream, do: stream(:stdio, :line)
@doc """
Converts the IO `device` into an `IO.Stream`.
@@ -535,9 +524,6 @@ defmodule IO do
Note that an IO stream has side effects and every time
you go over the stream you may get different results.
`stream/1` has been introduced in Elixir v1.12.0,
while `stream/2` has been available since v1.0.0.
## Examples
Here is an example on how we mimic an echo server
@@ -547,23 +533,12 @@ defmodule IO do
"""
@spec stream(device, :line | pos_integer) :: Enumerable.t()
def stream(device \\ :stdio, line_or_codepoints)
def stream(device, line_or_codepoints)
when line_or_codepoints == :line
when is_integer(line_or_codepoints) and line_or_codepoints > 0 do
IO.Stream.__build__(map_dev(device), false, line_or_codepoints)
end
@doc """
Returns a raw, line-based `IO.Stream` on `:stdio`. The operation is Unicode unsafe.
This is equivalent to:
IO.binstream(:stdio, :line)
"""
@doc since: "1.12.0"
def binstream, do: binstream(:stdio, :line)
@doc """
Converts the IO `device` into an `IO.Stream`. The operation is Unicode unsafe.
@@ -572,7 +547,8 @@ defmodule IO do
and write.
The `device` is iterated by the given number of bytes or line by line if
`:line` is given. This reads from the IO device as a raw binary.
`:line` is given.
This reads from the IO device as a raw binary.
Note that an IO stream has side effects and every time
you go over the stream you may get different results.
@@ -580,11 +556,9 @@ defmodule IO do
Finally, do not use this function on IO devices in Unicode
mode as it will return the wrong result.
`binstream/1` has been introduced in Elixir v1.12.0,
while `binstream/2` has been available since v1.0.0.
"""
@spec binstream(device, :line | pos_integer) :: Enumerable.t()
def binstream(device \\ :stdio, line_or_bytes)
def binstream(device, line_or_bytes)
when line_or_bytes == :line
when is_integer(line_or_bytes) and line_or_bytes > 0 do
IO.Stream.__build__(map_dev(device), true, line_or_bytes)
+1 -1
View File
@@ -242,7 +242,7 @@ defmodule IO.ANSI do
performed. If you don't want this behaviour, use `format_fragment/2`.
An optional boolean parameter can be passed to enable or disable
emitting actual ANSI codes. When `false`, no ANSI codes will be emitted.
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
By default checks if ANSI is enabled using the `enabled?/0` function.
## Examples
+178 -500
View File
File diff suppressed because it is too large Load Diff
+2 -3
View File
@@ -173,11 +173,10 @@ defmodule Kernel.LexicalTracker do
end
def handle_cast({:add_import, module, fas, line, warn}, state) do
to_remove = for {{:import, {^module, _, _}} = key, _} <- state.directives, do: key
directives =
state.directives
|> Map.drop(to_remove)
|> Enum.reject(&match?({{:import, {^module, _, _}}, _}, &1))
|> Map.new()
|> add_directive(module, line, warn, :import)
directives =
+20 -47
View File
@@ -90,11 +90,6 @@ defmodule Kernel.ParallelCompiler do
spawn_workers(files, :compile, options)
end
@doc """
Compiles the given files and writes resulting BEAM files into path.
See `compile/2` for more information.
"""
@doc since: "1.6.0"
def compile_to_path(files, path, options \\ []) when is_binary(path) and is_list(options) do
spawn_workers(files, {:compile, path}, options)
@@ -380,31 +375,24 @@ defmodule Kernel.ParallelCompiler do
# There is potentially a deadlock. We will release modules with
# the following order:
#
# 1. Code.ensure_compiled/1 checks without a known definition (deadlock = soft)
# 2. Code.ensure_compiled/1 checks with a known definition (deadlock = soft)
# 3. Struct/import/require/ensure_compiled! checks without a known definition (deadlock = hard)
# 4. Modules without a known definition
# 5. Code invocation (deadlock = raise)
# 1. Code.ensure_compiled/1 checks (deadlock = soft)
# 2. Struct checks (deadlock = hard)
# 3. Modules without a known definition
# 4. Code invocation (deadlock = raise)
#
# The reason for step 3 and 4 is to not treat typos as deadlocks and
# help developers handle those sooner. However, this can have false
# positives in case multiple modules are defined in the same file
# and the module we are waiting for is defined later on.
#
# Finally, note there is no difference between hard and raise, the
# In theory there is no difference between hard and raise, the
# difference is where the raise is happening, inside the compiler
# or in the caller.
cond do
deadlocked = deadlocked(waiting, :soft) || deadlocked(waiting, :hard) ->
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, state)
deadlocked =
deadlocked(waiting, :soft, false) ||
deadlocked(waiting, :soft, true) || deadlocked(waiting, :hard, false) ||
without_definition(waiting, files)
without_definition = without_definition(waiting, files) ->
spawn_workers(without_definition, spawned, waiting, files, result, warnings, state)
if deadlocked do
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, state)
else
errors = handle_deadlock(waiting, files)
{{:error, errors, warnings}, state}
true ->
errors = handle_deadlock(waiting, files)
{{:error, errors, warnings}, state}
end
end
@@ -464,21 +452,13 @@ defmodule Kernel.ParallelCompiler do
nillify_empty(
for %{pid: pid} <- files,
{_, ^pid, ref, on, _, _} <- List.wrap(List.keyfind(waiting, pid, 1)),
not defining?(on, waiting),
not Enum.any?(waiting, fn {_, _, _, _, defining, _} -> on in defining end),
do: {ref, :not_found}
)
end
defp deadlocked(waiting, type, defining?) do
nillify_empty(
for {_, _, ref, on, _, ^type} <- waiting,
defining?(on, waiting) == defining?,
do: {ref, :deadlock}
)
end
defp defining?(on, waiting) do
Enum.any?(waiting, fn {_, _, _, _, defining, _} -> on in defining end)
defp deadlocked(waiting, type) do
nillify_empty(for {_, _, ref, _, _, ^type} <- waiting, do: {ref, :deadlock})
end
defp nillify_empty([]), do: nil
@@ -623,13 +603,13 @@ defmodule Kernel.ParallelCompiler do
data = maybe_warn_long_compilation(data, state)
if state.profile != :none do
compiling = to_padded_ms(data.compiling)
waiting = to_padded_ms(data.waiting)
relative = Path.relative_to_cwd(data.file)
compiling = System.convert_time_unit(data.compiling, :native, :millisecond)
waiting = System.convert_time_unit(data.waiting, :native, :millisecond)
extra = if waiting > 0, do: " (plus #{waiting}ms waiting)", else: ""
IO.puts(
:stderr,
"[profile] #{compiling}ms compiling + #{waiting}ms waiting for #{relative}"
"[profile] #{Path.relative_to_cwd(data.file)} compiled in #{compiling}ms" <> extra
)
end
@@ -640,13 +620,6 @@ defmodule Kernel.ParallelCompiler do
end)
end
defp to_padded_ms(time) do
time
|> System.convert_time_unit(:native, :millisecond)
|> Integer.to_string()
|> String.pad_leading(6, " ")
end
defp discard_down(pid) do
receive do
{:DOWN, _, :process, ^pid, _} -> :ok
+20 -44
View File
@@ -201,7 +201,7 @@ defmodule Kernel.SpecialForms do
iex> <<"foo"::utf32>>
<<0, 0, 0, 102, 0, 0, 0, 111, 0, 0, 0, 111>>
Otherwise we get an `ArgumentError` when constructing the binary:
Otherwise we get an `ArgumentError` when construcing the binary:
rest = "oo"
<<102, rest>>
@@ -365,8 +365,8 @@ defmodule Kernel.SpecialForms do
ERL_COMPILER_OPTIONS=bin_opt_info mix compile
To learn more about specific optimizations and performance considerations,
check out the
["Constructing and matching binaries" chapter of the Erlang's Efficiency Guide](https://erlang.org/doc/efficiency_guide/binaryhandling.html).
check out
[Erlang's Efficiency Guide on handling binaries](http://www.erlang.org/doc/efficiency_guide/binaryhandling.html).
"""
defmacro unquote(:<<>>)(args), do: error!([args])
@@ -617,11 +617,9 @@ defmodule Kernel.SpecialForms do
import List, only: [flatten: 1]
import String, except: [split: 2]
Importing the same module again will erase the previous imports,
except when the `except` option is used, which is always exclusive
on a previously declared `import/2`. If there is no previous import,
then it applies to all functions and macros in the module. For
example:
Note that calling `except` is always exclusive on a previously
declared `import/2`. If there is no previous import, then it applies
to all functions and macros in the module. For example:
import List, only: [flatten: 1, keyfind: 4]
import List, except: [flatten: 1]
@@ -1416,7 +1414,7 @@ defmodule Kernel.SpecialForms do
The `IO` module provides streams, that are both `Enumerable` and
`Collectable`, here is an upcase echo server using comprehensions:
for line <- IO.stream(), into: IO.stream() do
for line <- IO.stream(:stdio, :line), into: IO.stream(:stdio, :line) do
String.upcase(line)
end
@@ -1550,16 +1548,9 @@ defmodule Kernel.SpecialForms do
...> else
...> :error ->
...> {:error, :wrong_data}
...>
...> _other_error ->
...> :unexpected_error
...> end
{:error, :wrong_data}
The `else` block works like a `case` clause: it can have multiple clauses,
and the first match will be used. Variables bound inside `with` (such as
`width` in this example) are not available in the `else` block.
If an `else` block is used and there are no matching clauses, a `WithClauseError`
exception is raised.
"""
@@ -1607,7 +1598,7 @@ defmodule Kernel.SpecialForms do
defmacro unquote(:__block__)(args), do: error!([args])
@doc """
Capture operator. Captures or creates an anonymous function.
Caputure operator. Captures or creates an anonymous function.
## Capture
@@ -1730,21 +1721,19 @@ defmodule Kernel.SpecialForms do
## Examples
case File.read(file) do
{:ok, contents} when is_binary(contents) ->
String.split(contents, "\n")
{:error, _reason} ->
Logger.warning "could not find #{file}, assuming empty..."
[]
case thing do
{:selector, i, value} when is_integer(i) ->
value
value ->
value
end
In the example above, we match the result of `File.read/1`
against each clause "head" and execute the clause "body"
corresponding to the first clause that matches.
In the example above, we match `thing` against each clause "head"
and execute the clause "body" corresponding to the first clause
that matches.
If no clause matches, an error is raised. For this reason,
it may be necessary to add a final catch-all clause (like `_`)
If no clause matches, an error is raised.
For this reason, it may be necessary to add a final catch-all clause (like `_`)
which will always match.
x = 10
@@ -1752,7 +1741,6 @@ defmodule Kernel.SpecialForms do
case x do
0 ->
"This clause won't match"
_ ->
"This clause would match any value (x = #{x})"
end
@@ -1770,7 +1758,8 @@ defmodule Kernel.SpecialForms do
value
#=> unbound variable value
Variables in the outer context cannot be overridden either:
When binding variables with the same names as variables in the outer context,
the variables in the outer context are not affected.
value = 7
@@ -1797,19 +1786,6 @@ defmodule Kernel.SpecialForms do
end
#=> "Will match"
## Using guards to match against multiple values
While it is not possible to match against multiple patterns in a single
clause, it's possible to match against multiple values by using guards:
case data do
value when value in [:one, :two] ->
"#{value} has been matched"
:three ->
"three has been matched"
end
"""
defmacro case(condition, clauses), do: error!([condition, clauses])
+8 -26
View File
@@ -643,7 +643,10 @@ defmodule Kernel.Typespec do
end
defp typespec({:__aliases__, _, _} = alias, vars, caller, state) do
typespec(expand_remote(alias, caller), vars, caller, state)
# We set a function name to avoid tracking
# aliases in typespecs as compile time dependencies.
atom = Macro.expand(alias, %{caller | function: {:typespec, 0}})
typespec(atom, vars, caller, state)
end
# Handle funs
@@ -731,7 +734,9 @@ defmodule Kernel.Typespec do
# Handle remote calls
defp typespec({{:., meta, [remote, name]}, _, args} = orig, vars, caller, state) do
remote = expand_remote(remote, caller)
# We set a function name to avoid tracking
# aliases in typespecs as compile time dependencies.
remote = Macro.expand(remote, %{caller | function: {:typespec, 0}})
cond do
not is_atom(remote) ->
@@ -902,25 +907,6 @@ defmodule Kernel.Typespec do
## Helpers
# This is a backport of Macro.expand/2 because we want to expand
# aliases but we don't them to become compile-time references.
defp expand_remote({:__aliases__, _, _} = alias, env) do
case :elixir_aliases.expand(alias, env) do
receiver when is_atom(receiver) ->
receiver
aliases ->
aliases = :lists.map(&Macro.expand_once(&1, env), aliases)
case :lists.all(&is_atom/1, aliases) do
true -> :elixir_aliases.concat(aliases)
false -> alias
end
end
end
defp expand_remote(other, env), do: Macro.expand(other, env)
defp compile_error(caller, desc) do
raise CompileError, file: caller.file, line: caller.line, description: desc
end
@@ -1022,11 +1008,7 @@ defmodule Kernel.Typespec do
:elixir_errors.erl_warn(caller.line, caller.file, warning)
{_, :used_once} ->
compile_error(
caller,
"type variable #{name} is used only once. Type variables in typespecs " <>
"must be referenced at least twice, otherwise it is equivalent to term()"
)
compile_error(caller, "type variable #{name} is unused")
_ ->
:ok
+2 -11
View File
@@ -108,16 +108,7 @@ defmodule Kernel.Utils do
:lists.foreach(foreach, enforce_keys)
struct = :maps.put(:__struct__, module, :maps.from_list(fields))
case enforce_keys -- :maps.keys(struct) do
[] ->
{struct, enforce_keys, Module.get_attribute(module, :derive)}
error_keys ->
raise ArgumentError,
"@enforce_keys required keys (#{inspect(error_keys)}) that are not defined in defstruct: " <>
"#{inspect(fields)}"
end
{struct, enforce_keys, Module.get_attribute(module, :derive)}
end
defp warn_on_duplicate_struct_key([]) do
@@ -267,7 +258,7 @@ defmodule Kernel.Utils do
{new_var, acc}
%{} ->
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc) + 1))
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc)))
new_var = Macro.var(generated, Elixir)
{new_var, Map.put(acc, pair, {new_var, var})}
end
+13 -22
View File
@@ -287,8 +287,8 @@ defmodule Keyword do
{nil, [a: 1]}
"""
@spec get_and_update(t, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_keywords :: t}
@spec get_and_update(t, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, value}
when current_value: value
def get_and_update(keywords, key, fun)
when is_list(keywords) and is_atom(key),
@@ -351,8 +351,8 @@ defmodule Keyword do
{1, []}
"""
@spec get_and_update!(t, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_keywords :: t}
@spec get_and_update!(t, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, t}
when current_value: value
def get_and_update!(keywords, key, fun) do
get_and_update!(keywords, key, fun, [])
@@ -732,16 +732,10 @@ defmodule Keyword do
iex> Keyword.equal?([a: 1, b: 2, a: 3], [b: 2, a: 3, a: 1])
true
Comparison between values is done with `===/3`,
which means integers are not equivalent to floats:
iex> Keyword.equal?([a: 1.0], [a: 1])
false
"""
@spec equal?(t, t) :: boolean
def equal?(left, right) when is_list(left) and is_list(right) do
:lists.sort(left) === :lists.sort(right)
:lists.sort(left) == :lists.sort(right)
end
@doc """
@@ -899,11 +893,11 @@ defmodule Keyword do
[{key, fun.(value)} | delete(keywords, key)]
end
defp update!([{_, _} = pair | keywords], key, fun, original) do
[pair | update!(keywords, key, fun, original)]
defp update!([{_, _} = e | keywords], key, fun, original) do
[e | update!(keywords, key, fun, original)]
end
defp update!([], key, _fun, original) do
defp update!([], key, _fun, original) when is_atom(key) do
raise(KeyError, key: key, term: original)
end
@@ -929,21 +923,18 @@ defmodule Keyword do
[a: 1, b: 11]
"""
@spec update(t, key, default :: value, (existing_value :: value -> new_value :: value)) :: t
@spec update(t, key, default :: value, (existing_value :: value -> updated_value :: value)) :: t
def update(keywords, key, default, fun)
when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
update_guarded(keywords, key, default, fun)
end
defp update_guarded([{key, value} | keywords], key, _default, fun) do
def update([{key, value} | keywords], key, _default, fun) do
[{key, fun.(value)} | delete(keywords, key)]
end
defp update_guarded([{_, _} = pair | keywords], key, default, fun) do
[pair | update_guarded(keywords, key, default, fun)]
def update([{_, _} = e | keywords], key, default, fun) do
[e | update(keywords, key, default, fun)]
end
defp update_guarded([], key, default, _fun) do
def update([], key, default, _fun) when is_atom(key) do
[{key, default}]
end
+16 -33
View File
@@ -15,13 +15,6 @@ defmodule List do
iex> [1, true, 2, false, 3, true] -- [true, false]
[1, 2, 3, true]
An element can be prepended to a list using `|`:
iex> new = 0
iex> list = [1, 2, 3]
iex> [new | list]
[0, 1, 2, 3]
Lists in Elixir are effectively linked lists, which means
they are internally represented in pairs containing the
head and the tail of a list:
@@ -262,18 +255,13 @@ defmodule List do
end
@doc """
Returns the first element in `list` or `default` if `list` is empty.
`first/2` has been introduced in Elixir v1.12.0, while `first/1` has been available since v1.0.0.
Returns the first element in `list` or `nil` if `list` is empty.
## Examples
iex> List.first([])
nil
iex> List.first([], 1)
1
iex> List.first([1])
1
@@ -281,25 +269,19 @@ defmodule List do
1
"""
@spec first([], any) :: any
@spec first([elem, ...], any) :: elem when elem: var
def first(list, default \\ nil)
def first([], default), do: default
def first([head | _], _default), do: head
@spec first([]) :: nil
@spec first([elem, ...]) :: elem when elem: var
def first([]), do: nil
def first([head | _]), do: head
@doc """
Returns the last element in `list` or `default` if `list` is empty.
`last/2` has been introduced in Elixir v1.12.0, while `last/1` has been available since v1.0.0.
Returns the last element in `list` or `nil` if `list` is empty.
## Examples
iex> List.last([])
nil
iex> List.last([], 1)
1
iex> List.last([1])
1
@@ -307,13 +289,11 @@ defmodule List do
3
"""
@spec last([], any) :: any
@spec last([elem, ...], any) :: elem when elem: var
@compile {:inline, last: 2}
def last(list, default \\ nil)
def last([], default), do: default
def last([head], _default), do: head
def last([_ | tail], default), do: last(tail, default)
@spec last([]) :: nil
@spec last([elem, ...]) :: elem when elem: var
def last([]), do: nil
def last([head]), do: head
def last([_ | tail]), do: last(tail)
@doc """
Receives a list of tuples and returns the first tuple
@@ -831,6 +811,9 @@ defmodule List do
iex> List.to_existing_atom('🌢 Elixir')
:"🌢 Elixir"
iex> List.to_existing_atom('this_atom_will_never_exist')
** (ArgumentError) argument error
"""
@spec to_existing_atom(charlist) :: atom
def to_existing_atom(charlist) do
@@ -916,7 +899,7 @@ defmodule List do
Note that this function expects a list of integers representing
Unicode code points. If you have a list of bytes, you must instead use
the [`:binary` module](`:binary`).
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
## Examples
@@ -971,7 +954,7 @@ defmodule List do
Note that this function expects a list of integers representing
Unicode code points. If you have a list of bytes, you must instead use
the [`:binary` module](`:binary`).
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
## Examples
+31 -47
View File
@@ -270,7 +270,7 @@ defmodule Macro do
def pipe(expr, {:fn, _, _}, _integer) do
raise ArgumentError,
"cannot pipe #{to_string(expr)} into an anonymous function without" <>
" calling the function; use Kernel.then/2 instead or" <>
" calling the function; use something like (fn ... end).() or" <>
" define the anonymous function as a regular private function"
end
@@ -341,7 +341,7 @@ defmodule Macro do
variables using `Macro.var/2`.
Note the arguments are not unique. If you later on want
to access the same variables, you can invoke this function
to access this same varibles, you can invoke this function
with the same inputs. Use `generate_unique_arguments/2` to
generate a unique arguments that can't be overridden.
@@ -391,8 +391,8 @@ defmodule Macro do
by the atoms `var` and `context`.
Note this variable is not unique. If you later on want
to access this same variable, you can invoke `var/2`
again with the same arguments. Use `unique_var/2` to
to access this same varible, you can invoke `var/2`
again with the same argument. Use `unique_var/2` to
generate a unique variable that can't be overridden.
## Examples
@@ -478,14 +478,10 @@ defmodule Macro do
end
defp do_traverse_args(args, acc, pre, post) when is_list(args) do
:lists.mapfoldl(
fn x, acc ->
{x, acc} = pre.(x, acc)
do_traverse(x, acc, pre, post)
end,
acc,
args
)
Enum.map_reduce(args, acc, fn x, acc ->
{x, acc} = pre.(x, acc)
do_traverse(x, acc, pre, post)
end)
end
@doc """
@@ -545,15 +541,10 @@ defmodule Macro do
iex> Macro.decompose_call(quote(do: 42))
:error
iex> Macro.decompose_call(quote(do: {:foo, [], []}))
:error
"""
@spec decompose_call(t()) :: {atom, [t()]} | {t(), atom, [t()]} | :error
def decompose_call(ast)
def decompose_call({:{}, _, args}) when is_list(args), do: :error
def decompose_call({{:., _, [remote, function]}, _, args})
when is_tuple(remote) or is_atom(remote),
do: {remote, function, args}
@@ -640,15 +631,13 @@ defmodule Macro do
expanding structs defined under the module being compiled.
It will raise `CompileError` if the struct is not available.
From Elixir v1.12, calling this function also adds an export
dependency on the given struct.
"""
@doc since: "1.8.0"
@spec struct!(module, Macro.Env.t()) :: %{__struct__: module} when module: module()
def struct!(module, env) when is_atom(module) do
if module == env.module do
Module.get_attribute(module, :__struct__)
end || :elixir_map.load_struct([line: env.line], module, [], [], env)
Module.get_attribute(module, :struct)
end || :elixir_map.load_struct([line: env.line], module, [], env)
end
@doc """
@@ -725,8 +714,8 @@ defmodule Macro do
and return a version with it unescaped.
"""
@spec unescape_string(String.t()) :: String.t()
def unescape_string(string) do
:elixir_interpolation.unescape_string(string)
def unescape_string(chars) do
:elixir_interpolation.unescape_chars(chars)
end
@doc ~S"""
@@ -741,9 +730,8 @@ defmodule Macro do
representing the code point of the character it wants to unescape.
Here is the default mapping function implemented by Elixir:
def unescape_map(:newline), do: true
def unescape_map(:unicode), do: true
def unescape_map(:hex), do: true
def unescape_map(unicode), do: true
def unescape_map(hex), do: true
def unescape_map(?0), do: ?0
def unescape_map(?a), do: ?\a
def unescape_map(?b), do: ?\b
@@ -760,9 +748,9 @@ defmodule Macro do
If the `unescape_map/1` function returns `false`, the char is
not escaped and the backslash is kept in the string.
Newlines, Unicode, and hexadecimals code points will be escaped if
the map returns `true` respectively for `:newline`, `:unicode`, and
`:hex`.
Hexadecimals and Unicode code points will be escaped if the map
function returns `true` for `?x`. Unicode code points if the map
function returns `true` for `?u`.
## Examples
@@ -772,23 +760,25 @@ defmodule Macro do
"""
@spec unescape_string(String.t(), (non_neg_integer -> non_neg_integer | false)) :: String.t()
def unescape_string(string, map) do
:elixir_interpolation.unescape_string(string, map)
def unescape_string(chars, map) do
:elixir_interpolation.unescape_chars(chars, map)
end
@doc false
@deprecated "Traverse over the arguments using Enum.map/2 instead"
def unescape_tokens(tokens) do
for token <- tokens do
if is_binary(token), do: unescape_string(token), else: token
case :elixir_interpolation.unescape_tokens(tokens) do
{:ok, unescaped_tokens} -> unescaped_tokens
{:error, reason} -> raise ArgumentError, to_string(reason)
end
end
@doc false
@deprecated "Traverse over the arguments using Enum.map/2 instead"
def unescape_tokens(tokens, map) do
for token <- tokens do
if is_binary(token), do: unescape_string(token, map), else: token
case :elixir_interpolation.unescape_tokens(tokens, map) do
{:ok, unescaped_tokens} -> unescaped_tokens
{:error, reason} -> raise ArgumentError, to_string(reason)
end
end
@@ -969,7 +959,7 @@ defmodule Macro do
def to_string({target, _, args} = ast, fun) when is_list(args) do
with :error <- unary_call(ast, fun),
:error <- op_call(ast, fun),
:error <- binary_call(ast, fun),
:error <- sigil_call(ast, fun) do
{list, last} = split_last(args)
@@ -1087,6 +1077,8 @@ defmodule Macro do
"\#{" <> to_string(arg, fun) <> "}"
binary when is_binary(binary) ->
binary = inspect_no_limit(binary)
binary = binary_part(binary, 1, byte_size(binary) - 2)
escape_sigil(binary, left)
end)
@@ -1141,14 +1133,7 @@ defmodule Macro do
:error
end
defp op_call({:"..//", _, [left, middle, right]} = ast, fun) do
left = op_to_string(left, fun, :.., :left)
middle = op_to_string(middle, fun, :.., :right)
right = op_to_string(right, fun, :"//", :right)
{:ok, fun.(ast, left <> ".." <> middle <> "//" <> right)}
end
defp op_call({op, _, [left, right]} = ast, fun) when is_atom(op) do
defp binary_call({op, _, [left, right]} = ast, fun) when is_atom(op) do
case Identifier.binary_op(op) do
{_, _} ->
left = op_to_string(left, fun, op, :left)
@@ -1161,7 +1146,7 @@ defmodule Macro do
end
end
defp op_call(_, _) do
defp binary_call(_, _) do
:error
end
@@ -1679,8 +1664,7 @@ defmodule Macro do
end
defp do_underscore(<<h, t, rest::binary>>, _)
when h >= ?A and h <= ?Z and not (t >= ?A and t <= ?Z) and not (t >= ?0 and t <= ?9) and
t != ?. and t != ?_ do
when h >= ?A and h <= ?Z and not (t >= ?A and t <= ?Z) and t != ?. and t != ?_ do
<<?_, to_lower_char(h), t>> <> do_underscore(rest, t)
end
+1 -4
View File
@@ -97,7 +97,7 @@ defmodule Macro.Env do
line: line,
macro_aliases: macro_aliases,
macros: macros,
module: module,
module: atom,
prematch_vars: prematch_vars,
unused_vars: unused_vars,
requires: requires,
@@ -105,9 +105,7 @@ defmodule Macro.Env do
vars: vars
}
# Define the __struct__ callbacks by hand for bootstrap reasons.
# TODO: Remove :vars field on v2.0
@doc false
def __struct__ do
%{
__struct__: __MODULE__,
@@ -132,7 +130,6 @@ defmodule Macro.Env do
}
end
@doc false
def __struct__(kv) do
Enum.reduce(kv, __struct__(), fn {k, v}, acc -> :maps.update(k, v, acc) end)
end
+28 -34
View File
@@ -19,13 +19,8 @@ defmodule Map do
in a map literal, the last one prevails.
When the key in a key-value pair is an atom, the `key: value` shorthand syntax
can be used (as in many other special forms):
iex> %{a: 1, b: 2}
%{a: 1, b: 2}
If you want to mix the shorthand syntax with `=>`, the shorthand syntax must come
at the end:
can be used (as in many other special forms), provided key-value pairs are put at
the end:
iex> %{"hello" => "world", a: 1, b: 2}
%{:a => 1, :b => 2, "hello" => "world"}
@@ -50,11 +45,11 @@ defmodule Map do
map.foo
#=> "bar"
map.non_existing_key
** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
#=> ** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
> Note: do not add parens when accessing fields, such as in `data.key()`.
> If parenthesis are used, Elixir will expect `data` to be an atom representing
> a module and attempt to call the *function* `key/0` in it.
> If parenthesis are used, Elixir will consider it to be a function call
> on `data`, which would be expected to be an atom.
The two syntaxes for accessing keys reveal the dual nature of maps. The `map[key]`
syntax is used for dynamically created maps that may have any key, of any type.
@@ -216,10 +211,20 @@ defmodule Map do
@spec new(Enumerable.t(), (term -> {key, value})) :: map
def new(enumerable, transform) when is_function(transform, 1) do
enumerable
|> Enum.map(transform)
|> Enum.to_list()
|> new_transform(transform, [])
end
defp new_transform([], _fun, acc) do
acc
|> :lists.reverse()
|> :maps.from_list()
end
defp new_transform([element | rest], fun, acc) do
new_transform(rest, fun, [fun.(element) | acc])
end
@doc """
Returns whether the given `key` exists in the given `map`.
@@ -609,7 +614,7 @@ defmodule Map do
%{a: 1, b: 11}
"""
@spec update(map, key, default :: value, (existing_value :: value -> new_value :: value)) ::
@spec update(map, key, default :: value, (existing_value :: value -> updated_value :: value)) ::
map
def update(map, key, default, fun) when is_function(fun, 1) do
case map do
@@ -627,8 +632,8 @@ defmodule Map do
@doc """
Removes the value associated with `key` in `map` and returns the value and the updated map.
If `key` is present in `map`, it returns `{value, updated_map}` where `value` is the value of
the key and `updated_map` is the result of removing `key` from `map`. If `key`
If `key` is present in `map`, it returns `{value, new_map}` where `value` is the value of
the key and `new_map` is the result of removing `key` from `map`. If `key`
is not present in `map`, `{default, map}` is returned.
## Examples
@@ -641,7 +646,7 @@ defmodule Map do
{3, %{a: 1}}
"""
@spec pop(map, key, default) :: {value, updated_map :: map} | {default, map} when default: value
@spec pop(map, key, value) :: {value, new_map :: map}
def pop(map, key, default \\ nil) do
case :maps.take(key, map) do
{_, _} = tuple -> tuple
@@ -650,8 +655,8 @@ defmodule Map do
end
@doc """
Removes the value associated with `key` in `map` and returns the value
and the updated map, or it raises if `key` is not present.
Returns and removes the value associated with `key` in `map` or raises
if `key` is not present.
Behaves the same as `pop/3` but raises if `key` is not present in `map`.
@@ -666,7 +671,7 @@ defmodule Map do
"""
@doc since: "1.10.0"
@spec pop!(map, key) :: {value, updated_map :: map}
@spec pop!(map, key) :: {value, map}
def pop!(map, key) do
case :maps.take(key, map) do
{_, _} = tuple -> tuple
@@ -807,7 +812,7 @@ defmodule Map do
** (KeyError) key :b not found in: %{a: 1}
"""
@spec update!(map, key, (existing_value :: value -> new_value :: value)) :: map
@spec update!(map, key, (existing_value :: value -> updated_value :: value)) :: map
def update!(map, key, fun) when is_function(fun, 1) do
value = fetch!(map, key)
put(map, key, fun.(value))
@@ -836,7 +841,7 @@ defmodule Map do
iex> Map.get_and_update(%{a: 1}, :b, fn current_value ->
...> {current_value, "new value!"}
...> end)
{nil, %{a: 1, b: "new value!"}}
{nil, %{b: "new value!", a: 1}}
iex> Map.get_and_update(%{a: 1}, :a, fn _ -> :pop end)
{1, %{}}
@@ -845,8 +850,8 @@ defmodule Map do
{nil, %{a: 1}}
"""
@spec get_and_update(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
{current_value, new_map :: map}
@spec get_and_update(map, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, map}
when current_value: value
def get_and_update(map, key, fun) when is_function(fun, 1) do
current = get(map, key)
@@ -887,7 +892,7 @@ defmodule Map do
{1, %{}}
"""
@spec get_and_update!(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
@spec get_and_update!(map, key, (value -> {current_value, new_value :: value} | :pop)) ::
{current_value, map}
when current_value: value
def get_and_update!(map, key, fun) when is_function(fun, 1) do
@@ -940,11 +945,6 @@ defmodule Map do
Two maps are considered to be equal if they contain
the same keys and those keys contain the same values.
Note this function exists for completeness so the `Map`
and `Keyword` modules provide similar APIs. In practice,
developers often compare maps using `==/2` or `===/2`
directly.
## Examples
iex> Map.equal?(%{a: 1, b: 2}, %{b: 2, a: 1})
@@ -952,12 +952,6 @@ defmodule Map do
iex> Map.equal?(%{a: 1, b: 2}, %{b: 1, a: 2})
false
Comparison between keys and values is done with `===/3`,
which means integers are not equivalent to floats:
iex> Map.equal?(%{a: 1.0}, %{a: 1})
false
"""
@spec equal?(map, map) :: boolean
def equal?(map1, map2)
+3 -7
View File
@@ -57,7 +57,7 @@ defmodule MapSet do
@opaque t(value) :: %__MODULE__{map: %{optional(value) => []}}
@type t :: t(term)
# TODO: Remove version key on Elixir v2.0
# TODO: Remove version key on v2.0
defstruct map: %{}, version: 2
@doc """
@@ -223,9 +223,7 @@ defmodule MapSet do
@doc """
Checks if two sets are equal.
The comparison between elements is done using `===/2`,
which a set with `1` is not equivalent to a set with
`1.0`.
The comparison between elements must be done using `===/2`.
## Examples
@@ -233,13 +231,11 @@ defmodule MapSet do
true
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([3, 4]))
false
iex> MapSet.equal?(MapSet.new([1]), MapSet.new([1.0]))
false
"""
@spec equal?(t, t) :: boolean
def equal?(%MapSet{map: map1, version: version}, %MapSet{map: map2, version: version}) do
map1 === map2
Map.equal?(map1, map2)
end
# Elixir v1.5 changed the map representation, so on
+26 -213
View File
@@ -235,7 +235,7 @@ defmodule Module do
end
For the list of supported warnings, see
[`:dialyzer` module](`:dialyzer`).
[`:dialyzer` module](http://www.erlang.org/doc/man/dialyzer.html).
Multiple uses of `@dialyzer` will accumulate instead of overriding
previous ones.
@@ -253,7 +253,7 @@ defmodule Module do
[`mix compile.elixir`](https://hexdocs.pm/mix/Mix.Tasks.Compile.Elixir.html).
If the external resource does not exist, the module still has
a dependency on it, causing the module to be recompiled as soon
a dependency on it, causing the module be recompiled as soon
as the file is added.
### `@file`
@@ -305,9 +305,9 @@ defmodule Module do
Accepts the function name (as an atom) of a function in the current module or
`{function_name, 0}` tuple where `function_name` is the name of a function in
the current module. The function must have an arity of 0 (no arguments). If
the function does not return `:ok`, the loading of the module will be aborted.
For example:
the current module. The function must be public and have an arity of 0 (no
arguments). If the function does not return `:ok`, the loading of the module
will be aborted. For example:
defmodule MyModule do
@on_load :load_check
@@ -335,16 +335,6 @@ defmodule Module do
@vsn "1.0"
end
### Struct attributes
* `@derive` - derives an implementation for the given protocol for the
struct defined in the current module
* `@enforce_keys` - ensures the given keys are always set when building
the struct defined in the current module
See `Kernel.defstruct/1` for more information on building and using structs.
### Typespec attributes
The following attributes are part of typespecs and are also built-in in
@@ -553,103 +543,6 @@ defmodule Module do
@callback __info__(:md5) :: binary()
@callback __info__(:module) :: module()
@doc """
Returns information about module attributes used by Elixir.
See the "Module attributes" section in the module documentation for more
information on each attribute.
## Examples
iex> map = Module.reserved_attributes()
iex> Map.has_key?(map, :moduledoc)
true
iex> Map.has_key?(map, :doc)
true
"""
@doc since: "1.12.0"
def reserved_attributes() do
%{
after_compile: %{
doc: "A hook that will be invoked right after the current module is compiled."
},
before_compile: %{
doc: "A hook that will be invoked before the module is compiled."
},
behaviour: %{
doc: "Specifies that the current module implements a given behaviour."
},
on_definition: %{
doc:
"A hook that will be invoked when each function or macro in the current module is defined."
},
impl: %{
doc: "Declares an implementation of a callback function or macro."
},
compile: %{
doc: "Defines options for module compilation."
},
deprecated: %{
doc: "Provides the deprecation reason for a function."
},
moduledoc: %{
doc: "Provides documentation for the current module."
},
doc: %{
doc: "Provides documentation for a function/macro/callback."
},
typedoc: %{
doc: "Provides documentation for a type."
},
dialyzer: %{
doc: "Defines Dialyzer warnings to request or suppress."
},
external_resource: %{
doc: "Specifies an external resource for the current module."
},
file: %{
doc:
"Changes the filename used in stacktraces for the function or macro that follows the attribute."
},
on_load: %{
doc: "A hook that will be invoked whenever the module is loaded."
},
vsn: %{
doc: "Specify the module version."
},
type: %{
doc: "Defines a type to be used in `@spec`."
},
typep: %{
doc: "Defines a private type to be used in `@spec`."
},
opaque: %{
doc: "Defines an opaque type to be used in `@spec`."
},
spec: %{
doc: "Provides a specification for a function."
},
callback: %{
doc: "Provides a specification for a behaviour callback."
},
macrocallback: %{
doc: "Provides a specification for a macro behaviour callback."
},
optional_callbacks: %{
doc: "Specifies which behaviour callbacks and macro behaviour callbacks are optional."
},
derive: %{
doc:
"Derives an implementation for the given protocol for the struct defined in the current module."
},
enforce_keys: %{
doc:
"Ensures the given keys are always set when building the struct defined in the current module."
}
}
end
@doc """
Checks if a module is open.
@@ -831,6 +724,9 @@ defmodule Module do
## Examples
iex> Module.safe_concat([Module, Unknown])
** (ArgumentError) argument error
iex> Module.safe_concat([List, Chars])
List.Chars
@@ -849,6 +745,9 @@ defmodule Module do
## Examples
iex> Module.safe_concat(Module, Unknown)
** (ArgumentError) argument error
iex> Module.safe_concat(List, Chars)
List.Chars
@@ -1162,62 +1061,6 @@ defmodule Module do
:ets.select(set, [{{{:def, :"$1"}, kind, :_, :_, :_, :_}, [], [:"$1"]}])
end
@doc """
Returns the definition for the given name-arity pair.
It returns a tuple with the `version`, the `kind`,
the definition `metadata`, and a list with each clause.
Each clause is a four-element tuple with metadata,
the arguments, the guards, and the clause AST.
The clauses are returned in the Elixir AST but a subset
that has already been expanded and normalized. This makes
it useful for analyzing code but it cannot be reinjected
into the module as it will have lost some of its original
context. Given this AST representation is mostly internal,
it is versioned and it may change at any time. Therefore,
**use this API with caution**.
"""
@spec get_definition(module, definition) ::
{:v1, def_kind, meta :: keyword,
[{meta :: keyword, arguments :: [Macro.t()], guards :: [Macro.t()], Macro.t()}]}
@doc since: "1.12.0"
def get_definition(module, {name, arity})
when is_atom(module) and is_atom(name) and is_integer(arity) do
assert_not_compiled!(__ENV__.function, module, "")
{set, bag} = data_tables_for(module)
case :ets.lookup(set, {:def, {name, arity}}) do
[{_key, kind, meta, _, _, _}] ->
{:v1, kind, meta, bag_lookup_element(bag, {:clauses, {name, arity}}, 2)}
[] ->
nil
end
end
@doc """
Deletes a definition from a module.
It returns true if the definition exists and it was removed,
otherwise it returns false.
"""
@doc since: "1.12.0"
@spec delete_definition(module, definition) :: boolean()
def delete_definition(module, {name, arity})
when is_atom(module) and is_atom(name) and is_integer(arity) do
assert_not_readonly!(__ENV__.function, module)
case :elixir_def.take_definition(module, {name, arity}) do
false ->
false
_ ->
:elixir_locals.yank({name, arity}, module)
true
end
end
@doc """
Makes the given functions in `module` overridable.
@@ -1608,8 +1451,7 @@ defmodule Module do
redefining @doc attribute previously set at line #{current_line}.
Please remove the duplicate docs. If instead you want to override a \
previously defined @doc, attach the @doc attribute to a function head \
(the function signature not followed by any do-block). For example:
previously defined @doc, attach the @doc attribute to a function head:
@doc """
new docs
@@ -2131,13 +1973,20 @@ defmodule Module do
end
defp preprocess_attribute(:impl, value) do
if is_boolean(value) or (is_atom(value) and value != nil) do
value
else
raise ArgumentError,
"@impl is a built-in module attribute that marks the next definition " <>
"as a callback implementation. It should be a module or a boolean, " <>
"got: #{inspect(value)}"
case value do
_ when is_boolean(value) ->
value
module when is_atom(module) and module != nil ->
# Attempt to compile behaviour but ignore failure (will warn later)
_ = Code.ensure_compiled(module)
value
_ ->
raise ArgumentError,
"@impl is a built-in module attribute that marks the next definition " <>
"as a callback implementation. It should be a module or a boolean, " <>
"got: #{inspect(value)}"
end
end
@@ -2185,46 +2034,10 @@ defmodule Module do
end
end
defp preprocess_attribute(:dialyzer, value) do
# From https://github.com/erlang/otp/blob/master/lib/stdlib/src/erl_lint.erl
:lists.foreach(
fn attr ->
if not valid_dialyzer_attribute?(attr) do
raise ArgumentError, "invalid value for @dialyzer attribute: #{inspect(attr)}"
end
end,
List.wrap(value)
)
value
end
defp preprocess_attribute(_key, value) do
value
end
defp valid_dialyzer_attribute?({key, fun_arities}) when is_atom(key) do
(key == :nowarn_function or valid_dialyzer_attribute?(key)) and
:lists.all(
fn
{fun, arity} when is_atom(fun) and is_integer(arity) -> true
_ -> false
end,
List.wrap(fun_arities)
)
end
defp valid_dialyzer_attribute?(attr) do
:lists.member(
attr,
[:no_return, :no_unused, :no_improper_lists, :no_fun_app] ++
[:no_match, :no_opaque, :no_fail_call, :no_contracts] ++
[:no_behaviours, :no_undefined_callbacks, :unmatched_returns] ++
[:error_handling, :race_conditions, :no_missing_calls] ++
[:specdiffs, :overspecs, :underspecs, :unknown, :no_underspecs]
)
end
defp preprocess_doc_meta([], _module, _line, map), do: map
defp preprocess_doc_meta([{key, _} | tail], module, line, map)
+42 -38
View File
@@ -4,7 +4,6 @@ defmodule Module.ParallelChecker do
@type cache() :: {pid(), :ets.tid()}
@type warning() :: term()
@type kind() :: :def | :defmacro
@type mode() :: :elixir | :erlang
@doc """
Receives pairs of module maps and BEAM binaries. In parallel it verifies
@@ -68,17 +67,17 @@ defmodule Module.ParallelChecker do
or if the function does not exist return `{:error, :function}`.
"""
@spec fetch_export(cache(), module(), atom(), arity()) ::
{:ok, mode(), kind(), binary() | nil} | {:error, :function | :module}
{:ok, kind(), binary() | nil} | {:error, :function | :module}
def fetch_export({_server, ets}, module, fun, arity) do
case :ets.lookup(ets, {:cached, module}) do
[{_key, false}] ->
{:error, :module}
[{_key, mode}] ->
case :ets.lookup(ets, {:export, module, {fun, arity}}) do
[{_key, kind, reason}] -> {:ok, mode, kind, reason}
[{_key, true}] ->
case :ets.lookup(ets, {:export, {module, fun, arity}}) do
[{_key, kind, reason}] -> {:ok, kind, reason}
[] -> {:error, :function}
end
[{_key, false}] ->
{:error, :module}
end
end
@@ -90,9 +89,10 @@ defmodule Module.ParallelChecker do
def all_exports({_server, ets}, module) do
# This is only called after we get a deprecation notice
# so we can assume it's a cached module
ets
|> :ets.match({{:export, module, :"$1"}, :_, :_})
|> Enum.flat_map(& &1)
[{_key, exports}] = :ets.lookup(ets, {:all_exports, module})
exports
|> Enum.map(fn {function, _kind} -> function end)
|> Enum.sort()
end
@@ -336,31 +336,28 @@ defmodule Module.ParallelChecker do
definitions_to_exports(map.definitions)
deprecated = Map.new(map.deprecated)
cache_info(ets, map.module, exports, deprecated, :elixir)
cache_info(ets, map.module, exports, deprecated)
end
defp cache_from_info(ets, module) do
if Code.ensure_loaded?(module) do
{mode, exports} = info_exports(module)
exports = info_exports(module)
deprecated = info_deprecated(module)
cache_info(ets, module, exports, deprecated, mode)
cache_info(ets, module, exports, deprecated)
else
:ets.insert(ets, {{:cached, module}, false})
end
end
defp info_exports(module) do
map =
Map.new(
[{{:__info__, 1}, :def}] ++
behaviour_exports(module) ++
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
Enum.map(module.__info__(:functions), &{&1, :def})
)
{:elixir, map}
Map.new(
[{{:__info__, 1}, :def}] ++
behaviour_exports(module) ++
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
Enum.map(module.__info__(:functions), &{&1, :def})
)
rescue
_ -> {:erlang, Map.new(Enum.map(module.module_info(:exports), &{&1, :def}))}
_ -> Map.new(Enum.map(module.module_info(:exports), &{&1, :def}))
end
defp info_deprecated(module) do
@@ -369,32 +366,39 @@ defmodule Module.ParallelChecker do
_ -> %{}
end
defp cache_info(ets, module, exports, deprecated, mode) do
Enum.each(exports, fn {{fun, arity}, kind} ->
reason = Map.get(deprecated, {fun, arity})
:ets.insert(ets, {{:export, module, {fun, arity}}, kind, reason})
{{fun, arity}, kind}
end)
defp cache_info(ets, module, exports, deprecated) do
exports =
Enum.map(exports, fn {{fun, arity}, kind} ->
reason = Map.get(deprecated, {fun, arity})
:ets.insert(ets, {{:export, {module, fun, arity}}, kind, reason})
:ets.insert(ets, {{:cached, module}, mode})
{{fun, arity}, kind}
end)
:ets.insert(ets, {{:all_exports, module}, exports})
:ets.insert(ets, {{:cached, module}, true})
end
defp cache_chunk(ets, module, exports) do
Enum.each(exports, fn {{fun, arity}, %{kind: kind, deprecated_reason: reason}} ->
:ets.insert(ets, {{:export, module, {fun, arity}}, kind, reason})
exports =
Enum.map(exports, fn {{fun, arity}, %{kind: kind, deprecated_reason: reason}} ->
:ets.insert(ets, {{:export, {module, fun, arity}}, kind, reason})
{{fun, arity}, kind}
end)
{{fun, arity}, kind}
end)
:ets.insert(ets, {{:export, module, {:__info__, 1}}, :def, nil})
:ets.insert(ets, {{:cached, module}, :elixir})
:ets.insert(ets, {{:export, {module, :__info__, 1}}, :def, nil})
exports = [{{:__info__, 1}, :def} | exports]
:ets.insert(ets, {{:all_exports, module}, exports})
:ets.insert(ets, {{:cached, module}, true})
end
defp behaviour_exports(%{is_behaviour: true}), do: [{{:behaviour_info, 1}, :def}]
defp behaviour_exports(%{is_behaviour: false}), do: []
defp behaviour_exports(module) when is_atom(module) do
if function_exported?(module, :behaviour_info, 1) do
if {:behaviour_info, 1} in module.module_info(:functions) do
[{{:behaviour_info, 1}, :def}]
else
[]
+109 -36
View File
@@ -60,7 +60,7 @@ defmodule Module.Types do
head_stack = Unify.push_expr_stack(def_expr, stack)
with {:ok, _types, context} <- Pattern.of_head(args, guards, head_stack, context),
{:ok, _type, context} <- Expr.of_expr(body, :dynamic, stack, context) do
{:ok, _type, context} <- Expr.of_expr(body, stack, context) do
context.warnings
else
{:error, {type, error, context}} ->
@@ -92,7 +92,7 @@ defmodule Module.Types do
traces: %{},
# Counter to give type variables unique names
counter: 0,
# Track if a variable was inferred from a type guard function such is_tuple/1
# Track if a variable was infered from a type guard function such is_tuple/1
# or a guard function that fails such as elem/2, possible values are:
# `:guarded` when `is_tuple(x)`
# `:guarded` when `is_tuple and elem(x, 0)`
@@ -115,21 +115,104 @@ defmodule Module.Types do
# When false do not add a trace when a type variable is refined,
# useful when merging contexts where the variables already have traces
trace: true,
# There are two factors that control how we track guards.
#
# * consider_type_guards?: if type guards should be considered.
# This applies only at the root and root-based "and" and "or" nodes.
#
# * keep_guarded? - if a guarded clause should remain as guarded
# even on failure. Used on the right side of and.
#
type_guards: {_consider_type_guards? = true, _keep_guarded? = false},
# Track if we are in a context where type guard functions should
# affect inference
type_guards_enabled?: true,
# Context used to determine if unification is bi-directional, :expr
# is directional, :pattern is bi-directional
context: nil
}
end
## VARIABLE LIFTING
@doc """
Lifts type variables to their infered types from the context.
"""
def lift_types(types, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
{types, _context} = Enum.map_reduce(types, context, &do_lift_type/2)
types
end
@doc """
Lifts a single type to its infered type from the context.
"""
def lift_type(type, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
{type, _context} = do_lift_type(type, context)
type
end
# Lift type variable to its infered (hopefully concrete) types from the context
defp do_lift_type({:var, var}, context) do
case Map.fetch(context.lifted_types, var) do
{:ok, lifted_var} ->
{{:var, lifted_var}, context}
:error ->
case Map.fetch(context.types, var) do
{:ok, :unbound} ->
new_lifted_var(var, context)
{:ok, type} ->
# Remove visited types to avoid infinite loops
# then restore after we are done recursing on vars
types = context.types
context = %{context | types: Map.delete(context.types, var)}
{type, context} = do_lift_type(type, context)
{type, %{context | types: types}}
:error ->
new_lifted_var(var, context)
end
end
end
defp do_lift_type({:tuple, n, types}, context) do
{types, context} = Enum.map_reduce(types, context, &do_lift_type/2)
{{:tuple, n, types}, context}
end
defp do_lift_type({:map, pairs}, context) do
{pairs, context} =
Enum.map_reduce(pairs, context, fn {kind, key, value}, context ->
{key, context} = do_lift_type(key, context)
{value, context} = do_lift_type(value, context)
{{kind, key, value}, context}
end)
{{:map, pairs}, context}
end
defp do_lift_type({:list, type}, context) do
{type, context} = do_lift_type(type, context)
{{:list, type}, context}
end
defp do_lift_type(other, context) do
{other, context}
end
defp new_lifted_var(original_var, context) do
types = Map.put(context.lifted_types, original_var, context.lifted_counter)
counter = context.lifted_counter + 1
type = {:var, context.lifted_counter}
context = %{context | lifted_types: types, lifted_counter: counter}
{type, context}
end
## ERROR TO WARNING
# Collect relevant information from context and traces to report error
@@ -139,7 +222,8 @@ defmodule Module.Types do
location = {context.file, line, {context.module, fun, arity}}
traces = type_traces(stack, context)
{left, right, traces} = lift_all_types(left, right, traces, context)
traces = tag_traces(traces, context)
error = {:unable_unify, left, right, {location, stack.last_expr, traces}}
{Module.Types, error, location}
end
@@ -150,13 +234,14 @@ defmodule Module.Types do
# in the stack since we get related variables anyway?
stack =
stack.unify_stack
|> Enum.uniq()
|> Enum.flat_map(&[&1 | related_variables(&1, context.types)])
|> Enum.uniq()
Enum.flat_map(stack, fn var_index ->
with %{^var_index => traces} <- context.traces,
%{^var_index => expr_var} <- context.types_to_vars do
Enum.map(traces, &tag_trace(expr_var, &1, context))
Enum.map(traces, &{expr_var, &1})
else
_other -> []
end
@@ -174,26 +259,15 @@ defmodule Module.Types do
end
# Tag if trace is for a concrete type or type variable
defp tag_trace(var, {type, expr, location}, context) do
with {:var, var_index} <- type,
%{^var_index => expr_var} <- context.types_to_vars do
{:var, var, expr_var, expr, location}
else
_ -> {:type, var, type, expr, location}
end
end
defp lift_all_types(left, right, traces, context) do
all_types = [left, right] ++ for({:type, _, type, _, _} <- traces, do: type)
[left, right | all_types] = Unify.lift_types(all_types, context)
{traces, []} =
Enum.map_reduce(traces, all_types, fn
{:type, var, _, expr, location}, [type | acc] -> {{:type, var, type, expr, location}, acc}
other, acc -> {other, acc}
end)
{left, right, traces}
defp tag_traces(traces, context) do
Enum.flat_map(traces, fn {var, {type, expr, location}} ->
with {:var, var_index} <- type,
%{^var_index => expr_var} <- context.types_to_vars do
[{var, {:var, expr_var, expr, location}}]
else
_ -> [{var, {:type, type, expr, location}}]
end
end)
end
## FORMAT WARNINGS
@@ -269,10 +343,9 @@ defmodule Module.Types do
defp format_traces(traces, simplify?) do
traces
|> Enum.uniq()
|> Enum.reverse()
|> Enum.map_reduce([], fn
{:type, var, type, expr, location}, hints ->
{var, {:type, type, expr, location}}, hints ->
{hint, hints} = format_type_hint(type, expr, hints)
trace = [
@@ -290,7 +363,7 @@ defmodule Module.Types do
{trace, hints}
{:var, var1, var2, expr, location}, hints ->
{var1, {:var, var2, expr, location}}, hints ->
trace = [
"where \"",
Macro.to_string(var1),
+86 -112
View File
@@ -4,57 +4,58 @@ defmodule Module.Types.Expr do
alias Module.Types.{Of, Pattern}
import Module.Types.{Helpers, Unify}
def of_expr(expr, expected, %{context: stack_context} = stack, context)
when stack_context != :expr do
of_expr(expr, expected, %{stack | context: :expr}, context)
def of_expr(expr, %{context: stack_context} = stack, context) when stack_context != :expr do
of_expr(expr, %{stack | context: :expr}, context)
end
# :atom
def of_expr(atom, _expected, _stack, context) when is_atom(atom) do
def of_expr(atom, _stack, context) when is_atom(atom) do
{:ok, {:atom, atom}, context}
end
# 12
def of_expr(literal, _expected, _stack, context) when is_integer(literal) do
def of_expr(literal, _stack, context) when is_integer(literal) do
{:ok, :integer, context}
end
# 1.2
def of_expr(literal, _expected, _stack, context) when is_float(literal) do
def of_expr(literal, _stack, context) when is_float(literal) do
{:ok, :float, context}
end
# "..."
def of_expr(literal, _expected, _stack, context) when is_binary(literal) do
def of_expr(literal, _stack, context) when is_binary(literal) do
{:ok, :binary, context}
end
# #PID<...>
def of_expr(literal, _expected, _stack, context) when is_pid(literal) do
def of_expr(literal, _stack, context) when is_pid(literal) do
{:ok, :dynamic, context}
end
# <<...>>>
def of_expr({:<<>>, _meta, args}, _expected, stack, context) do
case Of.binary(args, stack, context, &of_expr/4) do
def of_expr({:<<>>, _meta, args}, stack, context) do
result = Of.binary(args, stack, context, &of_expr/3)
case result do
{:ok, context} -> {:ok, :binary, context}
{:error, reason} -> {:error, reason}
end
end
# left | []
def of_expr({:|, _meta, [left_expr, []]} = expr, _expected, stack, context) do
def of_expr({:|, _meta, [left_expr, []]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
of_expr(left_expr, :dynamic, stack, context)
of_expr(left_expr, stack, context)
end
# left | right
def of_expr({:|, _meta, [left_expr, right_expr]} = expr, _expected, stack, context) do
def of_expr({:|, _meta, [left_expr, right_expr]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case of_expr(left_expr, :dynamic, stack, context) do
case of_expr(left_expr, stack, context) do
{:ok, left, context} ->
case of_expr(right_expr, :dynamic, stack, context) do
case of_expr(right_expr, stack, context) do
{:ok, {:list, right}, context} ->
{:ok, to_union([left, right], context), context}
@@ -71,23 +72,22 @@ defmodule Module.Types.Expr do
end
# []
def of_expr([], _expected, _stack, context) do
def of_expr([], _stack, context) do
{:ok, {:list, :dynamic}, context}
end
# [expr, ...]
def of_expr(exprs, _expected, stack, context) when is_list(exprs) do
def of_expr(exprs, stack, context) when is_list(exprs) do
stack = push_expr_stack(exprs, stack)
case map_reduce_ok(exprs, context, &of_expr(&1, :dynamic, stack, &2)) do
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
{:ok, types, context} -> {:ok, {:list, to_union(types, context)}, context}
{:error, reason} -> {:error, reason}
end
end
# __CALLER__
def of_expr({:__CALLER__, _meta, var_context}, _expected, _stack, context)
when is_atom(var_context) do
def of_expr({:__CALLER__, _meta, var_context}, _stack, context) when is_atom(var_context) do
struct_pair = {:required, {:atom, :__struct__}, {:atom, Macro.Env}}
pairs =
@@ -99,8 +99,7 @@ defmodule Module.Types.Expr do
end
# __STACKTRACE__
def of_expr({:__STACKTRACE__, _meta, var_context}, _expected, _stack, context)
when is_atom(var_context) do
def of_expr({:__STACKTRACE__, _meta, var_context}, _stack, context) when is_atom(var_context) do
file = {:tuple, 2, [{:atom, :file}, {:list, :integer}]}
line = {:tuple, 2, [{:atom, :line}, :integer]}
file_line = {:list, {:union, [file, line]}}
@@ -109,45 +108,41 @@ defmodule Module.Types.Expr do
end
# var
def of_expr(var, _expected, _stack, context) when is_var(var) do
def of_expr(var, _stack, context) when is_var(var) do
{:ok, get_var!(var, context), context}
end
# {left, right}
def of_expr({left, right}, expected, stack, context) do
of_expr({:{}, [], [left, right]}, expected, stack, context)
def of_expr({left, right}, stack, context) do
of_expr({:{}, [], [left, right]}, stack, context)
end
# {...}
def of_expr({:{}, _meta, exprs} = expr, _expected, stack, context) do
def of_expr({:{}, _meta, exprs} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case map_reduce_ok(exprs, context, &of_expr(&1, :dynamic, stack, &2)) do
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
{:ok, types, context} -> {:ok, {:tuple, length(types), types}, context}
{:error, reason} -> {:error, reason}
end
end
# left = right
def of_expr({:=, _meta, [left_expr, right_expr]} = expr, _expected, stack, context) do
# TODO: We might want to bring the expected type forward in case the type of this
# pattern is not useful. For example: 1 = _ = expr
def of_expr({:=, _meta, [left_expr, right_expr]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, left_type, context} <-
Pattern.of_pattern(left_expr, stack, context),
{:ok, right_type, context} <- of_expr(right_expr, left_type, stack, context),
do: unify(right_type, left_type, stack, context)
{:ok, right_type, context} <- of_expr(right_expr, stack, context),
do: unify(right_type, left_type, %{stack | context: :pattern}, context)
end
# %{map | ...}
def of_expr({:%{}, _, [{:|, _, [map, args]}]} = expr, _expected, stack, context) do
def of_expr({:%{}, _, [{:|, _, [map, args]}]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
map_type = {:map, [{:optional, :dynamic, :dynamic}]}
with {:ok, map_type, context} <- of_expr(map, map_type, stack, context),
{:ok, {:map, arg_pairs}, context} <- Of.closed_map(args, stack, context, &of_expr/4),
with {:ok, map_type, context} <- of_expr(map, stack, context),
{:ok, {:map, arg_pairs}, context} <- Of.closed_map(args, stack, context, &of_expr/3),
dynamic_value_pairs =
Enum.map(arg_pairs, fn {:required, key, _value} -> {:required, key, :dynamic} end),
args_type = {:map, dynamic_value_pairs ++ [{:optional, :dynamic, :dynamic}]},
@@ -165,68 +160,55 @@ defmodule Module.Types.Expr do
end
# %Struct{map | ...}
def of_expr(
{:%, meta, [module, {:%{}, _, [{:|, _, [_, _]}]} = update]} = expr,
_expected,
stack,
context
) do
def of_expr({:%, meta, [module, {:%{}, _, [{:|, _, [_, _]}]} = update]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
map_type = {:map, [{:optional, :dynamic, :dynamic}]}
with {:ok, struct, context} <- Of.struct(module, meta, context),
{:ok, update, context} <- of_expr(update, map_type, stack, context) do
{:ok, update, context} <- of_expr(update, stack, context) do
unify(update, struct, stack, context)
end
end
# %{...}
def of_expr({:%{}, _meta, args} = expr, _expected, stack, context) do
def of_expr({:%{}, _meta, args} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
Of.closed_map(args, stack, context, &of_expr/4)
Of.closed_map(args, stack, context, &of_expr/3)
end
# %Struct{...}
def of_expr({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, _expected, stack, context) do
def of_expr({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, struct, context} <- Of.struct(module, meta1, context),
{:ok, map, context} <- Of.open_map(args, stack, context, &of_expr/4) do
{:ok, map, context} <- Of.open_map(args, stack, context, &of_expr/3) do
unify(map, struct, stack, context)
end
end
# ()
def of_expr({:__block__, _meta, []}, _expected, _stack, context) do
def of_expr({:__block__, _meta, []}, _stack, context) do
{:ok, {:atom, nil}, context}
end
# (expr; expr)
def of_expr({:__block__, _meta, exprs}, expected, stack, context) do
expected_types = List.duplicate(:dynamic, length(exprs) - 1) ++ [expected]
result =
map_reduce_ok(Enum.zip(exprs, expected_types), context, fn {expr, expected}, context ->
of_expr(expr, expected, stack, context)
end)
case result do
def of_expr({:__block__, _meta, exprs}, stack, context) do
case map_reduce_ok(exprs, context, &of_expr(&1, stack, &2)) do
{:ok, expr_types, context} -> {:ok, Enum.at(expr_types, -1), context}
{:error, reason} -> {:error, reason}
end
end
# case expr do pat -> expr end
def of_expr({:case, _meta, [case_expr, [{:do, clauses}]]} = expr, _expected, stack, context) do
def of_expr({:case, _meta, [case_expr, [{:do, clauses}]]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, _expr_type, context} <- of_expr(case_expr, :dynamic, stack, context),
with {:ok, _expr_type, context} <- of_expr(case_expr, stack, context),
{:ok, context} <- of_clauses(clauses, stack, context),
do: {:ok, :dynamic, context}
end
# fn pat -> expr end
def of_expr({:fn, _meta, clauses} = expr, _expected, stack, context) do
def of_expr({:fn, _meta, clauses} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case of_clauses(clauses, stack, context) do
@@ -239,7 +221,7 @@ defmodule Module.Types.Expr do
@try_clause_blocks [:catch, :else, :after]
# try do expr end
def of_expr({:try, _meta, [blocks]} = expr, _expected, stack, context) do
def of_expr({:try, _meta, [blocks]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
{result, context} =
@@ -249,20 +231,20 @@ defmodule Module.Types.Expr do
{:->, _, [[{:in, _, [var, _exceptions]}], body]}, context = acc ->
{_type, context} = new_pattern_var(var, context)
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
with {:ok, context} <- of_expr_context(body, stack, context) do
{:ok, keep_warnings(acc, context)}
end
{:->, _, [[var], body]}, context = acc ->
{_type, context} = new_pattern_var(var, context)
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
with {:ok, context} <- of_expr_context(body, stack, context) do
{:ok, keep_warnings(acc, context)}
end
end)
{block, body}, context = acc when block in @try_blocks ->
with {:ok, context} <- of_expr_context(body, :dynamic, stack, context) do
with {:ok, context} <- of_expr_context(body, stack, context) do
{:ok, keep_warnings(acc, context)}
end
@@ -277,7 +259,7 @@ defmodule Module.Types.Expr do
end
# receive do pat -> expr end
def of_expr({:receive, _meta, [blocks]} = expr, _expected, stack, context) do
def of_expr({:receive, _meta, [blocks]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
{result, context} =
@@ -289,8 +271,8 @@ defmodule Module.Types.Expr do
of_clauses(clauses, stack, context)
{:after, [{:->, _meta, [head, body]}]}, context = acc ->
with {:ok, _type, context} <- of_expr(head, :dynamic, stack, context),
{:ok, _type, context} <- of_expr(body, :dynamic, stack, context),
with {:ok, _type, context} <- of_expr(head, stack, context),
{:ok, _type, context} <- of_expr(body, stack, context),
do: {:ok, keep_warnings(acc, context)}
end)
@@ -301,7 +283,7 @@ defmodule Module.Types.Expr do
end
# for pat <- expr do expr end
def of_expr({:for, _meta, args} = expr, _expected, stack, context) do
def of_expr({:for, _meta, args} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
{clauses, [[{:do, block} | opts]]} = Enum.split(args, -1)
@@ -312,7 +294,7 @@ defmodule Module.Types.Expr do
{:ok, :dynamic, context}
end
else
with {:ok, _type, context} <- of_expr(block, :dynamic, stack, context) do
with {:ok, _type, context} <- of_expr(block, stack, context) do
{:ok, :dynamic, context}
end
end
@@ -320,7 +302,7 @@ defmodule Module.Types.Expr do
end
# with pat <- expr do expr end
def of_expr({:with, _meta, clauses} = expr, _expected, stack, context) do
def of_expr({:with, _meta, clauses} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case reduce_ok(clauses, context, &with_clause(&1, stack, &2)) do
@@ -329,14 +311,13 @@ defmodule Module.Types.Expr do
end
end
# fun.(args)
def of_expr({{:., _meta1, [fun]}, _meta2, args} = expr, _expected, stack, context) do
# TODO: Use expected type to infer intersection return type
# fun.(arg)
def of_expr({{:., _meta1, [fun]}, _meta2, args} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case of_expr(fun, :dynamic, stack, context) do
case of_expr(fun, stack, context) do
{:ok, _fun_type, context} ->
case map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
@@ -347,12 +328,12 @@ defmodule Module.Types.Expr do
end
# expr.key_or_fun
def of_expr({{:., _meta1, [expr1, key_or_fun]}, meta2, []} = expr2, _expected, stack, context)
def of_expr({{:., _meta1, [expr1, key_or_fun]}, meta2, []} = expr2, stack, context)
when not is_atom(expr1) do
stack = push_expr_stack(expr2, stack)
if Keyword.get(meta2, :no_parens, false) do
with {:ok, expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
with {:ok, expr_type, context} <- of_expr(expr1, stack, context),
{value_var, context} = add_var(context),
pair_type = {:required, {:atom, key_or_fun}, value_var},
optional_type = {:optional, :dynamic, :dynamic},
@@ -360,23 +341,20 @@ defmodule Module.Types.Expr do
{:ok, _map_type, context} <- unify(map_field_type, expr_type, stack, context),
do: {:ok, value_var, context}
else
# TODO: Use expected type to infer intersection return type
with {:ok, expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
with {:ok, expr_type, context} <- of_expr(expr1, stack, context),
{:ok, _map_type, context} <- unify(expr_type, :atom, stack, context),
do: {:ok, :dynamic, context}
end
end
# expr.fun(arg)
def of_expr({{:., meta1, [expr1, fun]}, _meta2, args} = expr2, _expected, stack, context) do
# TODO: Use expected type to infer intersection return type
def of_expr({{:., meta1, [expr1, fun]}, _meta2, args} = expr2, stack, context) do
context = Of.remote(expr1, fun, length(args), meta1, context)
stack = push_expr_stack(expr2, stack)
with {:ok, _expr_type, context} <- of_expr(expr1, :dynamic, stack, context),
{:ok, _fun_type, context} <- of_expr(fun, :dynamic, stack, context) do
case map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
with {:ok, _expr_type, context} <- of_expr(expr1, stack, context),
{:ok, _fun_type, context} <- of_expr(fun, stack, context) do
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
@@ -384,12 +362,7 @@ defmodule Module.Types.Expr do
end
# &Foo.bar/1
def of_expr(
{:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]},
_expected,
_stack,
context
)
def of_expr({:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]}, _stack, context)
when is_atom(module) and is_atom(fun) do
context = Of.remote(module, fun, arity, meta, context)
{:ok, :dynamic, context}
@@ -397,19 +370,16 @@ defmodule Module.Types.Expr do
# &foo/1
# & &1
def of_expr({:&, _meta, _arg}, _expected, _stack, context) do
def of_expr({:&, _meta, _arg}, _stack, context) do
# TODO: Function type
{:ok, :dynamic, context}
end
# fun(arg)
def of_expr({fun, _meta, args} = expr, _expected, stack, context)
when is_atom(fun) and is_list(args) do
# TODO: Use expected type to infer intersection return type
def of_expr({fun, _meta, args} = expr, stack, context) when is_atom(fun) and is_list(args) do
stack = push_expr_stack(expr, stack)
case map_reduce_ok(args, context, &of_expr(&1, :dynamic, stack, &2)) do
case map_reduce_ok(args, context, &of_expr(&1, stack, &2)) do
{:ok, _arg_types, context} -> {:ok, :dynamic, context}
{:error, reason} -> {:error, reason}
end
@@ -418,15 +388,17 @@ defmodule Module.Types.Expr do
defp for_clause({:<-, _, [left, expr]}, stack, context) do
{pattern, guards} = extract_head([left])
with {:ok, _pattern_type, context} <- Pattern.of_head([pattern], guards, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
# TODO: Check that of_guard/3 returns a boolean
{:ok, _guard_type, context} <- Pattern.of_guard(guards_to_or(guards), stack, context),
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
do: {:ok, context}
end
defp for_clause({:<<>>, _, [{:<-, _, [pattern, expr]}]}, stack, context) do
# TODO: the compiler guarantees pattern is a binary but we need to check expr is a binary
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
do: {:ok, context}
end
@@ -435,15 +407,15 @@ defmodule Module.Types.Expr do
end
defp for_clause(expr, stack, context) do
of_expr_context(expr, :dynamic, stack, context)
of_expr_context(expr, stack, context)
end
defp for_option({:into, expr}, stack, context) do
of_expr_context(expr, :dynamic, stack, context)
of_expr_context(expr, stack, context)
end
defp for_option({:reduce, expr}, stack, context) do
of_expr_context(expr, :dynamic, stack, context)
of_expr_context(expr, stack, context)
end
defp for_option({:uniq, _}, _stack, context) do
@@ -453,8 +425,10 @@ defmodule Module.Types.Expr do
defp with_clause({:<-, _, [left, expr]}, stack, context) do
{pattern, guards} = extract_head([left])
with {:ok, _pattern_type, context} <- Pattern.of_head([pattern], guards, stack, context),
{:ok, _expr_type, context} <- of_expr(expr, :dynamic, stack, context),
with {:ok, _pattern_type, context} <- Pattern.of_pattern(pattern, stack, context),
# TODO: Check that of_guard/3 returns a boolean
{:ok, _guard_type, context} <- Pattern.of_guard(guards_to_or(guards), stack, context),
{:ok, _expr_type, context} <- of_expr(expr, stack, context),
do: {:ok, context}
end
@@ -463,11 +437,11 @@ defmodule Module.Types.Expr do
end
defp with_clause(expr, stack, context) do
of_expr_context(expr, :dynamic, stack, context)
of_expr_context(expr, stack, context)
end
defp with_option({:do, body}, stack, context) do
of_expr_context(body, :dynamic, stack, context)
of_expr_context(body, stack, context)
end
defp with_option({:else, clauses}, stack, context) do
@@ -479,7 +453,7 @@ defmodule Module.Types.Expr do
{patterns, guards} = extract_head(head)
with {:ok, _, context} <- Pattern.of_head(patterns, guards, stack, context),
{:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context),
{:ok, _expr_type, context} <- of_expr(body, stack, context),
do: {:ok, keep_warnings(acc, context)}
end)
end
@@ -507,8 +481,8 @@ defmodule Module.Types.Expr do
[other]
end
defp of_expr_context(expr, expected, stack, context) do
case of_expr(expr, expected, stack, context) do
defp of_expr_context(expr, stack, context) do
case of_expr(expr, stack, context) do
{:ok, _type, context} -> {:ok, context}
{:error, reason} -> {:error, reason}
end
+26 -72
View File
@@ -11,44 +11,14 @@ defmodule Module.Types.Of do
import Module.Types.Helpers
import Module.Types.Unify
# There are important assumptions on how we work with maps.
#
# First, the keys in the map must be ordered by subtyping.
#
# Second, optional keys must be a superset of the required
# keys, i.e. %{required(atom) => integer, optional(:foo) => :bar}
# is forbidden.
#
# Third, in order to preserve co/contra-variance, a supertype
# must satisfy its subtypes. I.e. %{foo: :bar, atom() => :baz}
# is forbidden, it must be %{foo: :bar, atom() => :baz | :bar}.
#
# Once we support user declared maps, we need to validate these
# assumptions.
@doc """
Handles open maps (with dynamic => dynamic).
"""
def open_map(args, stack, context, of_fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, of_fun) do
# If we match on a map such as %{"foo" => "bar"}, we cannot
# assert that %{binary() => binary()}, since we are matching
# only a single binary of infinite possible values. Therefore,
# the correct would be to match it to %{binary() => binary() | var}.
#
# We can skip this in two cases:
#
# 1. If the key is a singleton, then we know that it has no
# other value than the current one
#
# 2. If the value is a variable, then there is no benefit in
# creating another variable, so we can skip it
#
# For now, we skip generating the var itself and introduce
# :dynamic instead.
def open_map(args, stack, context, fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, fun) do
pairs =
for {key, value} <- pairs, not has_unbound_var?(key, context) do
if singleton?(key, context) or match?({:var, _}, value) do
if singleton?(key, context) do
{key, value}
else
{key, to_union([value, :dynamic], context)}
@@ -63,16 +33,16 @@ defmodule Module.Types.Of do
@doc """
Handles closed maps (without dynamic => dynamic).
"""
def closed_map(args, stack, context, of_fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, of_fun) do
def closed_map(args, stack, context, fun) do
with {:ok, pairs, context} <- map_pairs(args, stack, context, fun) do
{:ok, {:map, closed_to_unions(pairs, context)}, context}
end
end
defp map_pairs(pairs, stack, context, of_fun) do
defp map_pairs(pairs, stack, context, fun) do
map_reduce_ok(pairs, context, fn {key, value}, context ->
with {:ok, key_type, context} <- of_fun.(key, :dynamic, stack, context),
{:ok, value_type, context} <- of_fun.(value, :dynamic, stack, context),
with {:ok, key_type, context} <- fun.(key, stack, context),
{:ok, value_type, context} <- fun.(value, stack, context),
do: {:ok, {key_type, value_type}, context}
end)
end
@@ -143,39 +113,39 @@ defmodule Module.Types.Of do
In the stack, we add nodes such as <<expr>>, <<..., expr>>, etc,
based on the position of the expression within the binary.
"""
def binary([], _stack, context, _of_fun) do
def binary([], _stack, context, _fun) do
{:ok, context}
end
def binary([head], stack, context, of_fun) do
def binary([head], stack, context, fun) do
head_stack = push_expr_stack({:<<>>, get_meta(head), [head]}, stack)
binary_segment(head, head_stack, context, of_fun)
binary_segment(head, head_stack, context, fun)
end
def binary([head | tail], stack, context, of_fun) do
def binary([head | tail], stack, context, fun) do
head_stack = push_expr_stack({:<<>>, get_meta(head), [head, @suffix]}, stack)
case binary_segment(head, head_stack, context, of_fun) do
{:ok, context} -> binary_many(tail, stack, context, of_fun)
case binary_segment(head, head_stack, context, fun) do
{:ok, context} -> binary_many(tail, stack, context, fun)
{:error, reason} -> {:error, reason}
end
end
defp binary_many([last], stack, context, of_fun) do
defp binary_many([last], stack, context, fun) do
last_stack = push_expr_stack({:<<>>, get_meta(last), [@prefix, last]}, stack)
binary_segment(last, last_stack, context, of_fun)
binary_segment(last, last_stack, context, fun)
end
defp binary_many([head | tail], stack, context, of_fun) do
defp binary_many([head | tail], stack, context, fun) do
head_stack = push_expr_stack({:<<>>, get_meta(head), [@prefix, head, @suffix]}, stack)
case binary_segment(head, head_stack, context, of_fun) do
{:ok, context} -> binary_many(tail, stack, context, of_fun)
case binary_segment(head, head_stack, context, fun) do
{:ok, context} -> binary_many(tail, stack, context, fun)
{:error, reason} -> {:error, reason}
end
end
defp binary_segment({:"::", _meta, [expr, specifiers]}, stack, context, of_fun) do
defp binary_segment({:"::", _meta, [expr, specifiers]}, stack, context, fun) do
expected_type =
collect_binary_specifier(specifiers, &binary_type(stack.context, &1)) || :integer
@@ -192,7 +162,7 @@ defmodule Module.Types.Of do
{:ok, context}
true ->
with {:ok, type, context} <- of_fun.(expr, expected_type, stack, context),
with {:ok, type, context} <- fun.(expr, stack, context),
{:ok, _type, context} <- unify(type, expected_type, stack, context),
do: {:ok, context}
end
@@ -249,12 +219,12 @@ defmodule Module.Types.Of do
defp check_export(module, fun, arity, meta, context) do
case ParallelChecker.fetch_export(context.cache, module, fun, arity) do
{:ok, mode, :def, reason} ->
check_deprecated(mode, module, fun, arity, reason, meta, context)
{:ok, :def, reason} ->
check_deprecated(module, fun, arity, reason, meta, context)
{:ok, mode, :defmacro, reason} ->
{:ok, :defmacro, reason} ->
context = warn(meta, context, {:unrequired_module, module, fun, arity})
check_deprecated(mode, module, fun, arity, reason, meta, context)
check_deprecated(module, fun, arity, reason, meta, context)
{:error, :module} ->
if warn_undefined?(module, fun, arity, context) do
@@ -273,7 +243,7 @@ defmodule Module.Types.Of do
end
end
defp check_deprecated(:elixir, module, fun, arity, reason, meta, context) do
defp check_deprecated(module, fun, arity, reason, meta, context) do
if reason do
warn(meta, context, {:deprecated, module, fun, arity, reason})
else
@@ -281,22 +251,6 @@ defmodule Module.Types.Of do
end
end
defp check_deprecated(:erlang, module, fun, arity, _reason, meta, context) do
case :otp_internal.obsolete(module, fun, arity) do
{:deprecated, string} when is_list(string) ->
reason = string |> List.to_string() |> String.capitalize()
warn(meta, context, {:deprecated, module, fun, arity, reason})
{:deprecated, string, removal} when is_list(string) and is_list(removal) ->
reason = string |> List.to_string() |> String.capitalize()
reason = "It will be removed in #{removal}. #{reason}"
warn(meta, context, {:deprecated, module, fun, arity, reason})
_ ->
context
end
end
# The protocol code dispatches to unknown modules, so we ignore them here.
#
# try do
+135 -100
View File
@@ -57,9 +57,8 @@ defmodule Module.Types.Pattern do
)
when is_atom(var_context) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> of_pattern(arg, stack, context) end
with {:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, expected_fun) do
with {:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, &of_pattern/3) do
{:ok, {:map, [{:required, {:atom, :__struct__}, :atom} | pairs]}, context}
end
end
@@ -68,11 +67,10 @@ defmodule Module.Types.Pattern do
def of_pattern({:%, _meta1, [var, {:%{}, _meta2, args}]} = expr, stack, context)
when not is_atom(var) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> of_pattern(arg, stack, context) end
with {:ok, var_type, context} = of_pattern(var, stack, context),
{:ok, _, context} <- unify(var_type, :atom, stack, context),
{:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, expected_fun) do
{:ok, {:map, pairs}, context} <- Of.open_map(args, stack, context, &of_pattern/3) do
{:ok, {:map, [{:required, {:atom, :__struct__}, var_type} | pairs]}, context}
end
end
@@ -81,6 +79,10 @@ defmodule Module.Types.Pattern do
of_shared(expr, stack, context, &of_pattern/3)
end
def unify_kinds(:required, _), do: :required
def unify_kinds(_, :required), do: :required
def unify_kinds(:optional, :optional), do: :optional
## GUARDS
# TODO: Some guards can be changed to intersection types or higher order types
@@ -184,29 +186,23 @@ defmodule Module.Types.Pattern do
def of_guard({{:., _, [:erlang, :andalso]}, _, [left, right]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
fresh_context = fresh_context(context)
with {:ok, left_type, context} <- of_guard(left, stack, context),
with {:ok, left_type, left_context} <- of_guard(left, stack, fresh_context),
{:ok, right_type, right_context} <- of_guard(right, stack, fresh_context),
{:ok, context} <- merge_context_and(context, stack, left_context, right_context),
{:ok, _, context} <- unify(left_type, @boolean, stack, context),
{:ok, right_type, context} <- of_guard(right, keep_guarded(stack), context),
do: {:ok, to_union([@boolean, right_type], context), context}
{:ok, _, context} <- unify(right_type, @boolean, stack, context),
do: {:ok, @boolean, context}
end
def of_guard({{:., _, [:erlang, :orelse]}, _, [left, right]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
left_indexes = collect_var_indexes_from_expr(left, context)
right_indexes = collect_var_indexes_from_expr(right, context)
fresh_context = fresh_context(context)
with {:ok, left_type, left_context} <- of_guard(left, stack, context),
{:ok, _right_type, right_context} <- of_guard(right, stack, context),
context =
merge_context_or(
left_indexes,
right_indexes,
context,
stack,
left_context,
right_context
),
with {:ok, left_type, left_context} <- of_guard(left, stack, fresh_context),
{:ok, _right_type, right_context} <- of_guard(right, stack, fresh_context),
{:ok, context} <- merge_context_or(context, stack, left_context, right_context),
{:ok, _, context} <- unify(left_type, @boolean, stack, context),
do: {:ok, @boolean, context}
end
@@ -230,13 +226,12 @@ defmodule Module.Types.Pattern do
stack = push_expr_stack(expr, stack)
{param_types, return_type} = guard_signature(guard, length(args))
type_guard? = type_guard?(guard)
{consider_type_guards?, keep_guarded?} = stack.type_guards
# Only check type guards in the context of and/or/not,
# a type guard in the context of is_tuple(x) > :foo
# should not affect the inference of x
if not type_guard? or consider_type_guards? do
arg_stack = %{stack | type_guards: {false, keep_guarded?}}
if not type_guard? or stack.type_guards_enabled? do
arg_stack = %{stack | type_guards_enabled?: type_guard?}
with {:ok, arg_types, context} <-
map_reduce_ok(args, context, &of_guard(&1, arg_stack, &2)),
@@ -244,7 +239,9 @@ defmodule Module.Types.Pattern do
{arg_types, guard_sources} =
case arg_types do
[{:var, index} | rest_arg_types] when type_guard? ->
guard_sources = Map.put_new(context.guard_sources, index, :guarded)
guard_sources =
Map.update(context.guard_sources, index, [:guarded], &[:guarded | &1])
{rest_arg_types, guard_sources}
_ ->
@@ -254,7 +251,7 @@ defmodule Module.Types.Pattern do
guard_sources =
Enum.reduce(arg_types, guard_sources, fn
{:var, index}, guard_sources ->
Map.update(guard_sources, index, :fail, &guarded_if_keep_guarded(&1, keep_guarded?))
Map.update(guard_sources, index, [:fail], &[:fail | &1])
_, guard_sources ->
guard_sources
@@ -274,26 +271,18 @@ defmodule Module.Types.Pattern do
# var
def of_guard(var, _stack, context) when is_var(var) do
{:ok, get_var!(var, context), context}
type = Map.fetch!(context.vars, var_name(var))
{:ok, type, context}
end
def of_guard(expr, stack, context) do
of_shared(expr, stack, context, &of_guard/3)
end
defp collect_var_indexes_from_expr(expr, context) do
{_, vars} =
Macro.prewalk(expr, %{}, fn
var, acc when is_var(var) ->
var_name = var_name(var)
%{^var_name => type} = context.vars
{var, collect_var_indexes(type, context, acc)}
other, acc ->
{other, acc}
end)
Map.keys(vars)
defp fresh_context(context) do
types = Map.new(context.types, fn {var, _} -> {var, :unbound} end)
traces = Map.new(context.traces, fn {var, _} -> {var, []} end)
%{context | types: types, traces: traces}
end
defp unify_call(args, params, stack, context) do
@@ -305,76 +294,126 @@ defmodule Module.Types.Pattern do
end)
end
defp merge_context_or(left_indexes, right_indexes, context, stack, left, right) do
left_different = filter_different_indexes(left_indexes, left, right)
right_different = filter_different_indexes(right_indexes, left, right)
case {left_different, right_different} do
{[index], [index]} -> merge_context_or_equal(index, stack, left, right)
{_, _} -> merge_context_or_diff(left_different, context, left)
defp merge_context_and(context, stack, left, right) do
with {:ok, context} <- unify_new_types(context, stack, left),
{:ok, context} <- unify_new_types(context, stack, right) do
guard_sources = and_guard_sources(left.guard_sources, right.guard_sources)
guard_sources = merge_guard_sources([context.guard_sources, guard_sources])
{:ok, %{context | guard_sources: guard_sources}}
end
end
defp filter_different_indexes(indexes, left, right) do
Enum.filter(indexes, fn index ->
%{^index => left_type} = left.types
%{^index => right_type} = right.types
left_type != right_type
end)
end
defp unify_new_types(context, stack, new_context) do
context = merge_traces(context, new_context)
defp merge_context_or_equal(index, stack, left, right) do
%{^index => left_type} = left.types
%{^index => right_type} = right.types
reduce_ok(Map.to_list(new_context.types), context, fn
{_index, :unbound}, context ->
{:ok, context}
cond do
left_type == :unbound ->
refine_var!(index, right_type, stack, left)
{index, new_type}, context ->
case unify({:var, index}, new_type, %{stack | trace: false}, context) do
{:ok, _, context} ->
{:ok, context}
right_type == :unbound ->
left
true ->
# Only include right side if left side is from type guard such as is_list(x),
# do not refine in case of length(x)
if left.guard_sources[index] == :fail do
guard_sources = Map.put(left.guard_sources, index, :fail)
left = %{left | guard_sources: guard_sources}
refine_var!(index, left_type, stack, left)
else
guard_sources = merge_guard_sources([left.guard_sources, right.guard_sources])
left = %{left | guard_sources: guard_sources}
refine_var!(index, to_union([left_type, right_type], left), stack, left)
{:error, reason} ->
{:error, reason}
end
end
end
# If the variable failed, we can keep them from the left side as is.
# If they didn't fail, then we need to restore them to their original value.
defp merge_context_or_diff(indexes, old_context, new_context) do
Enum.reduce(indexes, new_context, fn index, context ->
if new_context.guard_sources[index] == :fail do
context
else
restore_var!(index, new_context, old_context)
end
end)
end
defp merge_guard_sources(sources) do
Enum.reduce(sources, fn left, right ->
Map.merge(left, right, fn
_index, :guarded, :guarded -> :guarded
_index, _, _ -> :fail
end)
Map.merge(left, right, fn _index, left, right -> join_guard_source(left, right) end)
end)
end
defp guarded_if_keep_guarded(:guarded, true), do: :guarded
defp guarded_if_keep_guarded(_, _), do: :fail
defp join_guard_source(left, right) do
sources = left ++ right
defp keep_guarded(%{type_guards: {consider?, _}} = stack),
do: %{stack | type_guards: {consider?, true}}
cond do
:fail in sources -> [:fail]
:guarded in sources -> [:guarded]
true -> []
end
end
defp and_guard_sources(left, right) do
Map.merge(left, right, fn _index, left, right ->
# When the failing guard function wont fail due to type check function before it,
# for example: is_list(x) and length(x)
if :guarded in left and :fail in right do
[:guarded]
else
join_guard_source(left, right)
end
end)
end
defp merge_traces(context, new_context) do
traces =
:maps.fold(
fn index, new_traces, traces ->
:maps.update_with(index, &(new_traces ++ &1), new_traces, traces)
end,
context.traces,
new_context.traces
)
%{context | traces: traces}
end
defp merge_context_or(context, stack, left, right) do
context =
case {Map.to_list(left.types), Map.to_list(right.types)} do
{[{index, :unbound}], [{index, type}]} ->
refine_var(index, type, stack, context)
{[{index, type}], [{index, :unbound}]} ->
refine_var(index, type, stack, context)
{[{index, left_type}], [{index, right_type}]} ->
# Only include right side if left side is from type guard such as is_list(x),
# do not refine in case of length(x)
left_guard_sources = Map.get(left.guard_sources, index, [])
if :fail in left_guard_sources do
guard_sources = Map.put(context.guard_sources, index, [:fail])
context = %{context | guard_sources: guard_sources}
refine_var(index, left_type, stack, context)
else
guard_sources =
merge_guard_sources([
context.guard_sources,
left.guard_sources,
right.guard_sources
])
context = %{context | guard_sources: guard_sources}
refine_var(index, to_union([left_type, right_type], context), stack, context)
end
{left_types, _right_types} ->
Enum.reduce(left_types, context, fn {index, left_type}, context ->
left_guard_sources = Map.get(left.guard_sources, index, [])
if :fail in left_guard_sources do
guard_sources =
merge_guard_sources([
context.guard_sources,
left.guard_sources,
right.guard_sources
])
context = %{context | guard_sources: guard_sources}
refine_var(index, left_type, stack, context)
else
context
end
end)
end
{:ok, context}
end
defp guard_signature(name, arity) do
Map.fetch!(@guard_functions, {name, arity})
@@ -408,9 +447,7 @@ defmodule Module.Types.Pattern do
# <<...>>>
defp of_shared({:<<>>, _meta, args}, stack, context, fun) do
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
case Of.binary(args, stack, context, expected_fun) do
case Of.binary(args, stack, context, fun) do
{:ok, context} -> {:ok, :binary, context}
{:error, reason} -> {:error, reason}
end
@@ -504,18 +541,16 @@ defmodule Module.Types.Pattern do
# %{...}
defp of_shared({:%{}, _meta, args} = expr, stack, context, fun) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
Of.open_map(args, stack, context, expected_fun)
Of.open_map(args, stack, context, fun)
end
# %Struct{...}
defp of_shared({:%, meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context, fun)
when is_atom(module) do
stack = push_expr_stack(expr, stack)
expected_fun = fn arg, _expected, stack, context -> fun.(arg, stack, context) end
with {:ok, struct, context} <- Of.struct(module, meta1, context),
{:ok, map, context} <- Of.open_map(args, stack, context, expected_fun) do
{:ok, map, context} <- Of.open_map(args, stack, context, fun) do
unify(map, struct, stack, context)
end
end
+25 -178
View File
@@ -28,8 +28,6 @@ defmodule Module.Types.Unify do
# * subtype? (subtypes only)
# * has_unbound_var? (composite only)
# * recursive_type? (composite only)
# * collect_vars (composite only)
# * lift_types (composite only)
#
@doc """
@@ -61,11 +59,23 @@ defmodule Module.Types.Unify do
end
defp do_unify(type, {:var, var}, stack, context) do
unify_var(var, type, stack, context, _var_source = false)
case context.types do
%{^var => {:var, var_type}} ->
do_unify(type, {:var, var_type}, stack, context)
%{} ->
unify_var(var, type, stack, context, _var_source = false)
end
end
defp do_unify({:var, var}, type, stack, context) do
unify_var(var, type, stack, context, _var_source = true)
case context.types do
%{^var => {:var, var_type}} ->
do_unify({:var, var_type}, type, stack, context)
%{} ->
unify_var(var, type, stack, context, _var_source = true)
end
end
defp do_unify({:tuple, n, sources}, {:tuple, n, targets}, stack, context) do
@@ -99,22 +109,14 @@ defmodule Module.Types.Unify do
{:ok, target, context}
end
defp do_unify({:union, types}, target, stack, context) do
unify_result =
map_reduce_ok(types, context, fn type, context ->
unify(type, target, stack, context)
end)
case unify_result do
{:ok, types, context} -> {:ok, to_union(types, context), context}
{:error, context} -> {:error, context}
end
end
defp do_unify(source, target, stack, context) do
cond do
# TODO: This condition exists to handle unions with unbound vars.
match?({:union, _}, target) and has_unbound_var?(target, context) ->
# This condition exists to handle unions with unbound vars.
# TODO: handle unions properly. Note we can easily unify
# "union < type" even if union has vars as the vars must be
# type
(match?({:union, _}, source) and has_unbound_var?(source, context)) or
(match?({:union, _}, target) and has_unbound_var?(target, context)) ->
{:ok, source, context}
subtype?(source, target, context) ->
@@ -132,7 +134,7 @@ defmodule Module.Types.Unify do
defp unify_var(var, type, stack, context, var_source?) do
case context.types do
%{^var => :unbound} ->
context = refine_var!(var, type, stack, context)
context = refine_var(var, type, stack, context)
stack = push_unify_stack(var, stack)
if recursive_type?(type, [], context) do
@@ -145,31 +147,6 @@ defmodule Module.Types.Unify do
{:ok, {:var, var}, context}
end
%{^var => {:var, new_var} = var_type} ->
unify_result =
if var_source? do
unify(var_type, type, stack, context)
else
unify(type, var_type, stack, context)
end
case unify_result do
{:ok, type, context} ->
{:ok, type, context}
{:error, {type, reason, %{traces: error_traces} = error_context}} ->
old_var_traces = Map.get(context.traces, new_var, [])
new_var_traces = Map.get(error_traces, new_var, [])
add_var_traces = Enum.drop(new_var_traces, -length(old_var_traces))
error_traces =
error_traces
|> Map.update(var, add_var_traces, &(add_var_traces ++ &1))
|> Map.put(new_var, old_var_traces)
{:error, {type, reason, %{error_context | traces: error_traces}}}
end
%{^var => var_type} ->
# Only add trace if the variable wasn't already "expanded"
context =
@@ -193,7 +170,7 @@ defmodule Module.Types.Unify do
{:ok, {:var, var}, context}
{:ok, res_type, context} ->
context = refine_var!(var, res_type, stack, context)
context = refine_var(var, res_type, stack, context)
{:ok, {:var, var}, context}
{:error, reason} ->
@@ -429,21 +406,10 @@ defmodule Module.Types.Unify do
%{stack | unify_stack: [var | stack.unify_stack]}
end
@doc """
Restores the variable information from the old context into new context.
"""
def restore_var!(var, new_context, old_context) do
%{^var => type} = old_context.types
%{^var => trace} = old_context.traces
types = Map.put(new_context.types, var, type)
traces = Map.put(new_context.traces, var, trace)
%{new_context | types: types, traces: traces}
end
@doc """
Set the type for a variable and add trace.
"""
def refine_var!(var, type, stack, context) do
def refine_var(var, type, stack, context) do
types = Map.put(context.types, var, type)
context = %{context | types: types}
trace_var(var, type, stack, context)
@@ -509,41 +475,6 @@ defmodule Module.Types.Unify do
false
end
@doc """
Collects all type vars recursively.
"""
def collect_var_indexes(type, context, acc \\ %{})
def collect_var_indexes({:var, var}, context, acc) do
case acc do
%{^var => _} ->
acc
%{} ->
case context.types do
%{^var => :unbound} -> Map.put(acc, var, true)
%{^var => type} -> collect_var_indexes(type, context, Map.put(acc, var, true))
end
end
end
def collect_var_indexes({:tuple, _, args}, context, acc),
do: Enum.reduce(args, acc, &collect_var_indexes(&1, context, &2))
def collect_var_indexes({:union, args}, context, acc),
do: Enum.reduce(args, acc, &collect_var_indexes(&1, context, &2))
def collect_var_indexes({:list, arg}, context, acc),
do: collect_var_indexes(arg, context, acc)
def collect_var_indexes({:map, pairs}, context, acc) do
Enum.reduce(pairs, acc, fn {_, key, value}, acc ->
collect_var_indexes(value, context, collect_var_indexes(key, context, acc))
end)
end
def collect_var_indexes(_type, _context, acc), do: acc
@doc """
Checks if the type has a type var.
"""
@@ -625,7 +556,7 @@ defmodule Module.Types.Unify do
def subtype?({:tuple, n, left_types}, {:tuple, n, right_types}, context) do
left_types
|> Enum.zip(right_types)
|> Enum.all?(fn {left, right} -> subtype?(left, right, context) end)
|> Enum.any?(fn {left, right} -> subtype?(left, right, context) end)
end
def subtype?({:map, left_pairs}, {:map, right_pairs}, context) do
@@ -704,90 +635,6 @@ defmodule Module.Types.Unify do
[]
end
## Type lifting
@doc """
Lifts type variables to their inferred types from the context.
"""
def lift_types(types, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
{types, _context} = Enum.map_reduce(types, context, &lift_type/2)
types
end
# Lift type variable to its inferred (hopefully concrete) types from the context
defp lift_type({:var, var}, context) do
case context.lifted_types do
%{^var => lifted_var} ->
{{:var, lifted_var}, context}
%{} ->
case context.types do
%{^var => :unbound} ->
new_lifted_var(var, context)
%{^var => type} ->
if recursive_type?(type, [], context) do
new_lifted_var(var, context)
else
# Remove visited types to avoid infinite loops
# then restore after we are done recursing on vars
types = context.types
context = put_in(context.types[var], :unbound)
{type, context} = lift_type(type, context)
{type, %{context | types: types}}
end
%{} ->
new_lifted_var(var, context)
end
end
end
defp lift_type({:union, types}, context) do
{types, context} = Enum.map_reduce(types, context, &lift_type/2)
{{:union, types}, context}
end
defp lift_type({:tuple, n, types}, context) do
{types, context} = Enum.map_reduce(types, context, &lift_type/2)
{{:tuple, n, types}, context}
end
defp lift_type({:map, pairs}, context) do
{pairs, context} =
Enum.map_reduce(pairs, context, fn {kind, key, value}, context ->
{key, context} = lift_type(key, context)
{value, context} = lift_type(value, context)
{{kind, key, value}, context}
end)
{{:map, pairs}, context}
end
defp lift_type({:list, type}, context) do
{type, context} = lift_type(type, context)
{{:list, type}, context}
end
defp lift_type(other, context) do
{other, context}
end
defp new_lifted_var(original_var, context) do
types = Map.put(context.lifted_types, original_var, context.lifted_counter)
counter = context.lifted_counter + 1
type = {:var, context.lifted_counter}
context = %{context | lifted_types: types, lifted_counter: counter}
{type, context}
end
@doc """
Formats types.
@@ -831,7 +678,7 @@ defmodule Module.Types.Unify do
end
def format_type({:var, index}, _simplify?) do
"var#{index + 1}"
"var#{index}"
end
def format_type(atom, _simplify?) when is_atom(atom) do
+14 -23
View File
@@ -428,32 +428,34 @@ defmodule OptionParser do
end
end
# Handles -a, -abc, -abc=something, -n2
# Handles -a, -abc, -abc=something
defp next_with_config(["-" <> option | rest] = argv, config) do
%{allow_nonexistent_atoms?: allow_nonexistent_atoms?} = config
{option, value} = split_option(option)
original = "-" <> option
letters = String.graphemes(option)
cond do
is_nil(value) and starts_with_number?(option) ->
is_nil(value) and negative_number?(original) ->
{:error, argv}
String.contains?(option, ["-", "_"]) ->
{:undefined, original, value, rest}
String.length(option) == 1 ->
tl(letters) == [] ->
# We have a regular one-letter alias here
tagged = tag_oneletter_alias(option, config)
next_tagged(tagged, value, original, rest, config)
true ->
key = get_option_key(option, config.allow_nonexistent_atoms?)
key = get_option_key(option, allow_nonexistent_atoms?)
option_key = config.aliases[key]
if key && option_key do
IO.warn("multi-letter aliases are deprecated, got: #{inspect(key)}")
next_tagged({:default, option_key}, value, original, rest, config)
else
next_with_config(expand_multiletter_alias(option, value) ++ rest, config)
next_with_config(expand_multiletter_alias(letters, value) ++ rest, config)
end
end
end
@@ -705,25 +707,13 @@ defmodule OptionParser do
end
end
defp expand_multiletter_alias(options, value) do
{options, maybe_integer} =
options
|> String.to_charlist()
|> Enum.split_while(&(&1 not in ?0..?9))
defp expand_multiletter_alias(letters, value) do
{last, expanded} =
options
|> List.to_string()
|> String.graphemes()
letters
|> Enum.map(&("-" <> &1))
|> List.pop_at(-1)
expanded ++
[
last <>
if(maybe_integer != [], do: "=#{maybe_integer}", else: "") <>
if(value, do: "=#{value}", else: "")
]
expanded ++ [last <> if(value, do: "=" <> value, else: "")]
end
defp normalize_tag(:negated, option, value, switches) do
@@ -764,7 +754,7 @@ defmodule OptionParser do
defp value_in_tail?(["-" | _]), do: true
defp value_in_tail?(["- " <> _ | _]), do: true
defp value_in_tail?(["-" <> arg | _]), do: starts_with_number?(arg)
defp value_in_tail?(["-" <> arg | _]), do: negative_number?("-" <> arg)
defp value_in_tail?([]), do: false
defp value_in_tail?(_), do: true
@@ -796,8 +786,9 @@ defmodule OptionParser do
end
end
defp starts_with_number?(<<char, _::binary>>) when char in ?0..?9, do: true
defp starts_with_number?(_), do: false
defp negative_number?(arg) do
match?({_, ""}, Float.parse(arg))
end
defp format_errors([_ | _] = errors, opts) do
types = opts[:switches] || opts[:strict]
+16 -23
View File
@@ -66,7 +66,7 @@ defmodule Path do
case type(path) do
:relative ->
absname_join([relative_to, path])
absname_join(relative_to, path)
:absolute ->
absname_join([path])
@@ -80,11 +80,11 @@ defmodule Path do
# Absolute path on current drive
defp absname_vr(["/" | rest], [volume | _], _relative), do: absname_join([volume | rest])
# Relative to current directory on current drive
# Relative to current directory on current drive.
defp absname_vr([<<x, ?:>> | rest], [<<x, _::binary>> | _], relative),
do: absname(absname_join(rest), relative)
# Relative to current directory on another drive
# Relative to current directory on another drive.
defp absname_vr([<<x, ?:>> | name], _, _relative) do
cwd =
case :file.get_cwd([x, ?:]) do
@@ -97,25 +97,25 @@ defmodule Path do
@slash [?/, ?\\]
defp absname_join([]), do: ""
defp absname_join(list), do: absname_join(list, major_os_type())
# Joins a list
defp absname_join([name1, name2 | rest]), do: absname_join([absname_join(name1, name2) | rest])
defp absname_join([name1, name2 | rest], os_type) do
joined = do_absname_join(IO.chardata_to_string(name1), relative(name2), [], os_type)
absname_join([joined | rest], os_type)
end
defp absname_join([name]),
do: do_absname_join(IO.chardata_to_string(name), <<>>, [], major_os_type())
defp absname_join([name], os_type) do
do_absname_join(IO.chardata_to_string(name), <<>>, [], os_type)
end
# Joins two paths
defp absname_join(left, right),
do: do_absname_join(IO.chardata_to_string(left), relative(right), [], major_os_type())
defp do_absname_join(<<uc_letter, ?:, rest::binary>>, relativename, [], :win32)
when uc_letter in ?A..?Z,
do: do_absname_join(rest, relativename, [?:, uc_letter + ?a - ?A], :win32)
when uc_letter in ?A..?Z do
do_absname_join(rest, relativename, [?:, uc_letter + ?a - ?A], :win32)
end
defp do_absname_join(<<c1, c2, rest::binary>>, relativename, [], :win32)
when c1 in @slash and c2 in @slash,
do: do_absname_join(rest, relativename, '//', :win32)
when c1 in @slash and c2 in @slash do
do_absname_join(rest, relativename, '//', :win32)
end
defp do_absname_join(<<?\\, rest::binary>>, relativename, result, :win32),
do: do_absname_join(<<?/, rest::binary>>, relativename, result, :win32)
@@ -313,9 +313,6 @@ defmodule Path do
iex> Path.relative_to("/usr/local/foo", "/etc")
"/usr/local/foo"
iex> Path.relative_to("/usr/local/foo", "/usr/local/foo")
"."
"""
@spec relative_to(t, t) :: binary
def relative_to(path, from) do
@@ -323,10 +320,6 @@ defmodule Path do
relative_to(split(path), split(from), path)
end
defp relative_to(path, path, _original) do
"."
end
defp relative_to([h | t1], [h | t2], original) do
relative_to(t1, t2, original)
end
+1 -1
View File
@@ -448,7 +448,7 @@ defmodule Process do
If the process is already dead when calling `Process.monitor/1`, a
`:DOWN` message is delivered immediately.
See ["The need for monitoring"](https://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
See [the need for monitoring](https://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
for an example. See `:erlang.monitor/2` for more information.
Inlined by the compiler.
+54 -59
View File
@@ -4,11 +4,11 @@ defmodule Protocol do
A protocol specifies an API that should be defined by its
implementations. A protocol is defined with `Kernel.defprotocol/2`
and its implementations with `Kernel.defimpl/3`.
and its implementations with `Kernel.defimpl/2`.
## A real case
## Examples
In Elixir, we have two nouns for checking how many items there
In Elixir, we have two verbs for checking how many items there
are in a data structure: `length` and `size`. `length` means the
information must be computed. For example, `length(list)` needs to
traverse the whole list to calculate its length. On the other hand,
@@ -53,7 +53,7 @@ defmodule Protocol do
It is possible to implement protocols for all Elixir types:
* Structs (see the "Protocols and Structs" section below)
* Structs (see below)
* `Tuple`
* `Atom`
* `List`
@@ -65,7 +65,7 @@ defmodule Protocol do
* `Map`
* `Port`
* `Reference`
* `Any` (see the "Fallback to `Any`" section below)
* `Any` (see below)
## Protocols and Structs
@@ -79,7 +79,7 @@ defmodule Protocol do
end
When implementing a protocol for a struct, the `:for` option can
be omitted if the `defimpl/3` call is inside the module that defines
be omitted if the `defimpl` call is inside the module that defines
the struct:
defmodule User do
@@ -134,13 +134,13 @@ defmodule Protocol do
def reverse(term), do: Enum.reverse(term)
end
Inside `defimpl/3`, you can use `@protocol` to access the protocol
Inside `defimpl/2`, you can use `@protocol` to access the protocol
being implemented and `@for` to access the module it is being
defined for.
## Types
Defining a protocol automatically defines a zero-arity type named `t`, which
Defining a protocol automatically defines a type named `t`, which
can be used as follows:
@spec print_size(Size.t()) :: :ok
@@ -177,7 +177,7 @@ defmodule Protocol do
* `impl_for/1` - returns the module that implements the protocol for the given argument,
`nil` otherwise
* `impl_for!/1` - same as above but raises `Protocol.UndefinedError` if an implementation is
* `impl_for!/1` - same as above but raises an error if an implementation is
not found
For example, for the `Enumerable` protocol we have:
@@ -191,17 +191,15 @@ defmodule Protocol do
iex> Enumerable.impl_for(42)
nil
In addition, every protocol implementation module contains the `__impl__/1`
function. The function takes one of the following atoms:
In addition, every protocol implementation module contains the `__impl__/1` function. The
function takes one of the following atoms:
* `:for` - returns the module responsible for the data structure of the
protocol implementation
* `:for` - returns the module responsible for the data structure of the protocol implementation
* `:protocol` - returns the protocol module for which this implementation
is provided
* `:protocol` - returns the protocol module for which this implementation is provided
For example, the module implementing the `Enumerable` protocol for lists is
`Enumerable.List`. Therefore, we can invoke `__impl__/1` on this module:
For example, the module implementing the `Enumerable` protocol for lists is `Enumerable.List`.
Therefore, we can invoke `__impl__/1` on this module:
iex(1)> Enumerable.List.__impl__(:for)
List
@@ -211,17 +209,22 @@ defmodule Protocol do
## Consolidation
In order to speed up protocol dispatching, whenever all protocol implementations
are known up-front, typically after all Elixir code in a project is compiled,
Elixir provides a feature called *protocol consolidation*. Consolidation directly
links protocols to their implementations in a way that invoking a function from a
In order to cope with code loading in development, protocols in
Elixir provide a slow implementation of protocol dispatching specific
to development.
In order to speed up dispatching in production environments, where
all implementations are known up-front, Elixir provides a feature
called *protocol consolidation*. Consolidation directly links protocols
to their implementations in a way that invoking a function from a
consolidated protocol is equivalent to invoking two remote functions.
Protocol consolidation is applied by default to all Mix projects during compilation.
This may be an issue during test. For instance, if you want to implement a protocol
during test, the implementation will have no effect, as the protocol has already been
consolidated. One possible solution is to include compilation directories that are
specific to your test environment in your mix.exs:
Protocol consolidation is applied by default to all Mix projects during
compilation. This may be an issue during test. For instance, if you want
to implement a protocol during test, the implementation will have no
effect, as the protocol has already been consolidated. One possible
solution is to include compilation directories that are specific to your
test environment in your mix.exs:
def project do
...
@@ -308,11 +311,9 @@ defmodule Protocol do
end
defp assert_protocol!(module, extra) do
try do
Code.ensure_compiled!(module)
rescue
e in ArgumentError ->
raise ArgumentError, e.message <> extra
case Code.ensure_compiled(module) do
{:module, ^module} -> :ok
_ -> raise ArgumentError, "#{inspect(module)} is not available" <> extra
end
try do
@@ -339,11 +340,9 @@ defmodule Protocol do
defp assert_impl!(protocol, base, extra) do
impl = Module.concat(protocol, base)
try do
Code.ensure_compiled!(impl)
rescue
e in ArgumentError ->
raise ArgumentError, e.message <> extra
case Code.ensure_compiled(impl) do
{:module, ^impl} -> :ok
_ -> raise ArgumentError, "#{inspect(impl)} is not available" <> extra
end
try do
@@ -398,19 +397,17 @@ defmodule Protocol do
end
Derivable.ok(%ImplStruct{})
#=> {:ok, %ImplStruct{a: 0, b: 0}, %ImplStruct{a: 0, b: 0}, []}
{:ok, %ImplStruct{a: 0, b: 0}, %ImplStruct{a: 0, b: 0}, []}
Explicit derivations can now be called via `__deriving__/3`:
Explicit derivations can now be called via `__deriving__`:
# Explicitly derived via `__deriving__/3`
# Explicitly derived via `__deriving__`
Derivable.ok(%ImplStruct{a: 1, b: 1})
#=> {:ok, %ImplStruct{a: 1, b: 1}, %ImplStruct{a: 0, b: 0}, []}
# Explicitly derived by API via `__deriving__/3`
# Explicitly derived by API via `__deriving__`
require Protocol
Protocol.derive(Derivable, ImplStruct, :oops)
Derivable.ok(%ImplStruct{a: 1, b: 1})
#=> {:ok, %ImplStruct{a: 1, b: 1}, %ImplStruct{a: 0, b: 0}, :oops}
"""
defmacro derive(protocol, module, options \\ []) do
@@ -442,7 +439,7 @@ defmodule Protocol do
@spec extract_protocols([charlist | String.t()]) :: [atom]
def extract_protocols(paths) do
extract_matching_by_attribute(paths, 'Elixir.', fn module, attributes ->
case attributes[:__protocol__] do
case attributes[:protocol] do
[fallback_to_any: _] -> module
_ -> nil
end
@@ -473,7 +470,7 @@ defmodule Protocol do
prefix = Atom.to_charlist(protocol) ++ '.'
extract_matching_by_attribute(paths, prefix, fn _mod, attributes ->
case attributes[:__impl__] do
case attributes[:protocol_impl] do
[protocol: ^protocol, for: for] -> for
_ -> nil
end
@@ -564,7 +561,7 @@ defmodule Protocol do
chunks = :lists.filter(fn {_name, value} -> value != :missing_chunk end, chunks)
chunks = :lists.map(fn {name, value} -> {List.to_string(name), value} end, chunks)
case attributes[:__protocol__] do
case attributes[:protocol] do
[fallback_to_any: any] ->
{:ok, {any, definitions}, specs, {info, chunks}}
@@ -684,16 +681,14 @@ defmodule Protocol do
# We don't allow function definition inside protocols
import Kernel,
except: [
def: 1,
def: 2,
defp: 1,
defp: 2,
defguard: 1,
defguardp: 1,
defmacrop: 1,
defmacrop: 2,
defmacro: 1,
defmacro: 2,
defmacrop: 1,
defmacrop: 2
defp: 1,
defp: 2,
def: 1,
def: 2
]
# Import the new dsl that holds the new def
@@ -800,8 +795,8 @@ defmodule Protocol do
# Store information as an attribute so it
# can be read without loading the module.
Module.register_attribute(__MODULE__, :__protocol__, persist: true)
@__protocol__ [fallback_to_any: !!@fallback_to_any]
Module.register_attribute(__MODULE__, :protocol, persist: true)
@protocol [fallback_to_any: !!@fallback_to_any]
@doc false
@spec __protocol__(:module) :: __MODULE__
@@ -860,8 +855,8 @@ defmodule Protocol do
unquote(block)
Module.register_attribute(__MODULE__, :__impl__, persist: true)
@__impl__ [protocol: @protocol, for: @for]
Module.register_attribute(__MODULE__, :protocol_impl, persist: true)
@protocol_impl [protocol: @protocol, for: @for]
unquote(impl)
end
@@ -902,8 +897,8 @@ defmodule Protocol do
else
quoted =
quote do
Module.register_attribute(__MODULE__, :__impl__, persist: true)
@__impl__ [protocol: unquote(protocol), for: unquote(for)]
Module.register_attribute(__MODULE__, :protocol_impl, persist: true)
@protocol_impl [protocol: unquote(protocol), for: unquote(for)]
@doc false
@spec __impl__(:target) :: unquote(impl)
+59 -256
View File
@@ -1,75 +1,23 @@
defmodule Range do
@moduledoc """
Ranges represent a sequence of zero, one or many, ascending
or descending integers with a common difference called step.
Ranges represent a sequence of one or many, ascending
or descending, consecutive integers.
Ranges are always inclusive and they may have custom steps.
The most common form of creating and matching on ranges is
via the [`first..last`](`../2`) and [`first..last//step`](`..///3`)
notations, auto-imported from `Kernel`:
Ranges can be either increasing (`first <= last`) or
decreasing (`first > last`). Ranges are also always
inclusive.
iex> Enum.to_list(1..3)
[1, 2, 3]
iex> Enum.to_list(1..3//2)
[1, 3]
iex> Enum.to_list(3..1//-1)
[3, 2, 1]
A range is represented internally as a struct. However,
the most common form of creating and matching on ranges
is via the `../2` macro, auto-imported from `Kernel`:
Ranges may also have a single element:
iex> Enum.to_list(1..1)
[1]
iex> Enum.to_list(1..1//2)
[1]
Or even no elements at all:
iex> Enum.to_list(10..0//1)
[]
iex> Enum.to_list(0..10//-1)
[]
When defining a range without a step, the step will be
defined based on the first and last position of the
range, If `first >= last`, it will be an increasing range
with a step of 1. Otherwise, it is a decreasing range.
Note however implicitly decreasing ranges are deprecated.
Therefore, if you need a decreasing range from `3` to `1`,
prefer to write `3..1//-1` instead.
## Definition
An increasing range `first..last//step` is a range from
`first` to `last` increasing by `step` where all values
`v` must be `first <= v and v <= last`. Therefore, a range
`10..0//1` is an empty range because there is no value `v`
that is `10 <= v and v <= 0`.
Similarly, a decreasing range `first..last//-step` is a range
from `first` to `last` decreasing by `step` where all values
`v` must be `first >= v and v >= last`. Therefore, a range
`0..10//-1` is an empty range because there is no value `v`
that is `0 >= v and v >= 10`.
## Representation
Internally, ranges are represented as structs:
iex> range = 1..9//2
1..9//2
iex> first..last//step = range
iex> range = 1..3
1..3
iex> first..last = range
iex> first
1
iex> last
9
iex> step
2
iex> range.step
2
You can access the range fields (`first`, `last`, and `step`)
directly but you should not modify nor create ranges by hand.
Instead use the proper operators or `new/2` and `new/3`.
3
A range implements the `Enumerable` protocol, which means
functions in the `Enum` module can be used to work with
@@ -92,37 +40,23 @@ defmodule Range do
not materialize the whole list of integers.
"""
@enforce_keys [:first, :last, :step]
defstruct first: nil, last: nil, step: nil
defstruct first: nil, last: nil
@type limit :: integer
@type step :: pos_integer | neg_integer
@type t :: %__MODULE__{first: limit, last: limit, step: step}
@type t(first, last) :: %__MODULE__{first: first, last: last, step: step}
@type t :: %__MODULE__{first: integer, last: integer}
@type t(first, last) :: %__MODULE__{first: first, last: last}
@doc """
Creates a new range.
If `first` is less than `last`, the range will be increasing from
`first` to `last`. If `first` is equal to `last`, the range will contain
one element, which is the number itself.
If `first` is greater than `last`, the range will be decreasing from `first`
to `last`, albeit this behaviour is deprecated. Therefore, it is advised to
explicitly list the step with `new/3`.
## Examples
iex> Range.new(-100, 100)
-100..100
"""
@spec new(limit, limit) :: t
@spec new(integer, integer) :: t
def new(first, last) when is_integer(first) and is_integer(last) do
# TODO: Deprecate inferring a range with a step of -1 on Elixir v1.17
step = if first <= last, do: 1, else: -1
%Range{first: first, last: last, step: step}
%Range{first: first, last: last}
end
def new(first, last) do
@@ -131,66 +65,6 @@ defmodule Range do
"got: #{inspect(first)}..#{inspect(last)}"
end
@doc """
Creates a new range with `step`.
## Examples
iex> Range.new(-100, 100, 2)
-100..100//2
"""
@doc since: "1.12.0"
@spec new(limit, limit, step) :: t
def new(first, last, step)
when is_integer(first) and is_integer(last) and is_integer(step) and step != 0 do
%Range{first: first, last: last, step: step}
end
def new(first, last, step) do
raise ArgumentError,
"ranges (first..last//step) expect both sides to be integers and the step to be a " <>
"non-zero integer, got: #{inspect(first)}..#{inspect(last)}//#{inspect(step)}"
end
@doc """
Returns the size of `range`.
## Examples
iex> Range.size(1..10)
10
iex> Range.size(1..10//2)
5
iex> Range.size(1..10//3)
4
iex> Range.size(1..10//-1)
0
iex> Range.size(10..1)
10
iex> Range.size(10..1//-1)
10
iex> Range.size(10..1//-2)
5
iex> Range.size(10..1//-3)
4
iex> Range.size(10..1//1)
0
"""
@doc since: "1.12.0"
def size(range)
def size(first..last//step) when step > 0 and first > last, do: 0
def size(first..last//step) when step < 0 and first < last, do: 0
def size(first..last//step), do: abs(div(last - first, step)) + 1
# TODO: Remove me on v2.0
def size(%{__struct__: Range, first: first, last: last} = range) do
step = if first <= last, do: 1, else: -1
size(Map.put(range, :step, step))
end
@doc """
Checks if two ranges are disjoint.
@@ -205,161 +79,90 @@ defmodule Range do
iex> Range.disjoint?(1..5, 2..7)
false
Steps are also considered when computing the ranges to be disjoint:
iex> Range.disjoint?(1..10//2, 2..10//2)
true
# First element in common in all below is 29
iex> Range.disjoint?(2..100//3, 9..100//5)
false
iex> Range.disjoint?(101..2//-3, 99..9//-5)
false
iex> Range.disjoint?(1..100//14, 8..100//21)
false
iex> Range.disjoint?(57..-1//-14, 8..100//21)
false
iex> Range.disjoint?(1..100//14, 51..8//-21)
false
# If 29 is out of range
iex> Range.disjoint?(1..28//14, 8..28//21)
true
iex> Range.disjoint?(2..28//3, 9..28//5)
true
"""
@doc since: "1.8.0"
@spec disjoint?(t, t) :: boolean
def disjoint?(first1..last1//step1 = range1, first2..last2//step2 = range2) do
if size(range1) == 0 or size(range2) == 0 do
true
else
{first1, last1, step1} = normalize(first1, last1, step1)
{first2, last2, step2} = normalize(first2, last2, step2)
cond do
last2 < first1 or last1 < first2 ->
true
abs(step1) == 1 and abs(step2) == 1 ->
false
true ->
# We need to find the first intersection of two arithmetic
# progressions and see if they belong within the ranges
# https://math.stackexchange.com/questions/1656120/formula-to-find-the-first-intersection-of-two-arithmetic-progressions
{gcd, u, v} = Integer.extended_gcd(-step1, step2)
c = first1 - first2 + step2 - step1
t1 = -c / step1 * u
t2 = -c / step2 * v
t = max(floor(t1) + 1, floor(t2) + 1)
x = div(c * u + t * step2, gcd) - 1
y = div(c * v + t * step1, gcd) - 1
x < 0 or first1 + x * step1 > last1 or
y < 0 or first2 + y * step2 > last2
end
end
def disjoint?(first1..last1 = _range1, first2..last2 = _range2) do
{first1, last1} = normalize(first1, last1)
{first2, last2} = normalize(first2, last2)
last2 < first1 or last1 < first2
end
@compile inline: [normalize: 3]
defp normalize(first, last, step) when first > last, do: {last, first, -step}
defp normalize(first, last, step), do: {first, last, step}
@compile inline: [normalize: 2]
defp normalize(first, last) when first > last, do: {last, first}
defp normalize(first, last), do: {first, last}
@doc false
@deprecated "Pattern match on first..last//step instead"
@deprecated "Pattern match on first..last instead"
def range?(term)
def range?(first..last) when is_integer(first) and is_integer(last), do: true
def range?(_), do: false
end
defimpl Enumerable, for: Range do
def reduce(first..last//step, acc, fun) do
reduce(first, last, acc, fun, step)
def reduce(first..last, acc, fun) do
reduce(first, last, acc, fun, _up? = last >= first)
end
# TODO: Remove me on v2.0
def reduce(%{__struct__: Range, first: first, last: last} = range, acc, fun) do
step = if first <= last, do: 1, else: -1
reduce(Map.put(range, :step, step), acc, fun)
end
defp reduce(_first, _last, {:halt, acc}, _fun, _step) do
defp reduce(_first, _last, {:halt, acc}, _fun, _up?) do
{:halted, acc}
end
defp reduce(first, last, {:suspend, acc}, fun, step) do
{:suspended, acc, &reduce(first, last, &1, fun, step)}
defp reduce(first, last, {:suspend, acc}, fun, up?) do
{:suspended, acc, &reduce(first, last, &1, fun, up?)}
end
defp reduce(first, last, {:cont, acc}, fun, step)
when step > 0 and first <= last
when step < 0 and first >= last do
reduce(first + step, last, fun.(first, acc), fun, step)
defp reduce(first, last, {:cont, acc}, fun, _up? = true) when first <= last do
reduce(first + 1, last, fun.(first, acc), fun, _up? = true)
end
defp reduce(first, last, {:cont, acc}, fun, _up? = false) when first >= last do
reduce(first - 1, last, fun.(first, acc), fun, _up? = false)
end
defp reduce(_, _, {:cont, acc}, _fun, _up) do
{:done, acc}
end
def member?(first..last//step = range, value) when is_integer(value) do
cond do
Range.size(range) == 0 ->
{:ok, false}
first <= last ->
{:ok, first <= value and value <= last and rem(value - first, step) == 0}
true ->
{:ok, last <= value and value <= first and rem(value - first, step) == 0}
def member?(first..last, value) when is_integer(value) do
if first <= last do
{:ok, first <= value and value <= last}
else
{:ok, last <= value and value <= first}
end
end
# TODO: Remove me on v2.0
def member?(%{__struct__: Range, first: first, last: last} = range, value)
when is_integer(value) do
step = if first <= last, do: 1, else: -1
member?(Map.put(range, :step, step), value)
end
def member?(_, _value) do
def member?(_.._, _value) do
{:ok, false}
end
def count(range) do
{:ok, Range.size(range)}
def count(first..last) do
if first <= last do
{:ok, last - first + 1}
else
{:ok, first - last + 1}
end
end
def slice(first.._//step = range) do
{:ok, Range.size(range), &slice(first + &1 * step, step, &2)}
def slice(first..last) do
if first <= last do
{:ok, last - first + 1, &slice_asc(first + &1, &2)}
else
{:ok, first - last + 1, &slice_desc(first - &1, &2)}
end
end
# TODO: Remove me on v2.0
def slice(%{__struct__: Range, first: first, last: last} = range) do
step = if first <= last, do: 1, else: -1
slice(Map.put(range, :step, step))
end
defp slice_asc(current, 1), do: [current]
defp slice_asc(current, remaining), do: [current | slice_asc(current + 1, remaining - 1)]
defp slice(current, _step, 1), do: [current]
defp slice(current, step, remaining), do: [current | slice(current + step, step, remaining - 1)]
defp slice_desc(current, 1), do: [current]
defp slice_desc(current, remaining), do: [current | slice_desc(current - 1, remaining - 1)]
end
defimpl Inspect, for: Range do
import Inspect.Algebra
import Kernel, except: [inspect: 2]
def inspect(first..last//1, opts) do
def inspect(first..last, opts) do
concat([to_doc(first, opts), "..", to_doc(last, opts)])
end
def inspect(first..last//step, opts) do
concat([to_doc(first, opts), "..", to_doc(last, opts), "//", to_doc(step, opts)])
end
# TODO: Remove me on v2.0
def inspect(%{__struct__: Range, first: first, last: last} = range, opts) do
step = if first <= last, do: 1, else: -1
inspect(Map.put(range, :step, step), opts)
end
end
+20 -26
View File
@@ -4,7 +4,7 @@ defmodule Regex do
Regex is based on PCRE (Perl Compatible Regular Expressions) and
built on top of Erlang's `:re` module. More information can be found
in the [`:re` module documentation](`:re`).
in the [`:re` module documentation](http://www.erlang.org/doc/man/re.html).
Regular expressions in Elixir can be created using the sigils
`~r` (see `Kernel.sigil_r/2`) or `~R` (see `Kernel.sigil_R/2`):
@@ -107,6 +107,7 @@ defmodule Regex do
* alnum - Letters and digits
* alpha - Letters
* ascii - Character codes 0-127
* blank - Space or tab only
* cntrl - Control characters
* digit - Decimal digits (same as \\d)
@@ -119,11 +120,6 @@ defmodule Regex do
* word - "Word" characters (same as \w)
* xdigit - Hexadecimal digits
There is another character class, `ascii`, that erroneously matches
Latin-1 characters instead of the 0-127 range specified by POSIX. This
cannot be fixed without altering the behaviour of other classes, so we
recommend matching the range with `[\\0-\x7f]` instead.
Note the behaviour of those classes may change according to the Unicode
and other modifiers:
@@ -300,8 +296,6 @@ defmodule Regex do
Defaults to `:binary`.
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
to see the possible capture values.
* `:offset` - (since v1.12.0) specifies the starting offset to match in the given string.
Defaults to zero.
## Examples
@@ -321,9 +315,8 @@ defmodule Regex do
def run(%Regex{} = regex, string, options) when is_binary(string) do
return = Keyword.get(options, :return, :binary)
captures = Keyword.get(options, :capture, :all)
offset = Keyword.get(options, :offset, 0)
case safe_run(regex, string, [{:capture, captures, return}, {:offset, offset}]) do
case safe_run(regex, string, [{:capture, captures, return}]) do
:nomatch -> nil
:match -> []
{:match, results} -> results
@@ -432,8 +425,6 @@ defmodule Regex do
Defaults to `:binary`.
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
to see the possible capture values.
* `:offset` - (since v1.12.0) specifies the starting offset to match in the given string.
Defaults to zero.
## Examples
@@ -459,8 +450,7 @@ defmodule Regex do
def scan(%Regex{} = regex, string, options) when is_binary(string) do
return = Keyword.get(options, :return, :binary)
captures = Keyword.get(options, :capture, :all)
offset = Keyword.get(options, :offset, 0)
options = [{:capture, captures, return}, :global, {:offset, offset}]
options = [{:capture, captures, return}, :global]
case safe_run(regex, string, options) do
:match -> []
@@ -653,8 +643,20 @@ defmodule Regex do
"""
@spec replace(t, String.t(), String.t() | (... -> String.t()), [term]) :: String.t()
def replace(%Regex{} = regex, string, replacement, options \\ [])
when is_binary(string) and is_list(options) do
def replace(regex, string, replacement, options \\ [])
def replace(regex, string, replacement, options)
when is_binary(string) and is_binary(replacement) and is_list(options) do
do_replace(regex, string, precompile_replacement(replacement), options)
end
def replace(regex, string, replacement, options)
when is_binary(string) and is_function(replacement) and is_list(options) do
{:arity, arity} = Function.info(replacement, :arity)
do_replace(regex, string, {replacement, arity}, options)
end
defp do_replace(%Regex{} = regex, string, replacement, options) do
opts = if Keyword.get(options, :global) != false, do: [:global], else: []
opts = [{:capture, :all, :index} | opts]
@@ -663,20 +665,13 @@ defmodule Regex do
string
{:match, [mlist | t]} when is_list(mlist) ->
apply_list(string, precompile_replacement(replacement), [mlist | t])
|> IO.iodata_to_binary()
apply_list(string, replacement, [mlist | t]) |> IO.iodata_to_binary()
{:match, slist} ->
apply_list(string, precompile_replacement(replacement), [slist])
|> IO.iodata_to_binary()
apply_list(string, replacement, [slist]) |> IO.iodata_to_binary()
end
end
defp precompile_replacement(replacement) when is_function(replacement) do
{:arity, arity} = Function.info(replacement, :arity)
{replacement, arity}
end
defp precompile_replacement(""), do: []
defp precompile_replacement(<<?\\, ?g, ?{, rest::binary>>) when byte_size(rest) > 0 do
@@ -828,7 +823,6 @@ defmodule Regex do
@doc false
# Unescape map function used by Macro.unescape_string.
def unescape_map(:newline), do: true
def unescape_map(?f), do: ?\f
def unescape_map(?n), do: ?\n
def unescape_map(?r), do: ?\r
+16 -82
View File
@@ -354,20 +354,13 @@ defmodule Registry do
"expected :listeners to be a list of named processes, got: #{inspect(listeners)}"
end
compressed = Keyword.get(options, :compressed, false)
unless is_boolean(compressed) do
raise ArgumentError,
"expected :compressed to be a boolean, got: #{inspect(compressed)}"
end
# The @info format must be kept in sync with Registry.Partition optimization.
entries = [
{@all_info, {keys, partitions, nil, nil, listeners}},
{@key_info, {keys, partitions, nil}} | meta
]
Registry.Supervisor.start_link(keys, name, partitions, listeners, entries, compressed)
Registry.Supervisor.start_link(keys, name, partitions, listeners, entries)
end
@doc false
@@ -731,64 +724,6 @@ defmodule Registry do
acc
end
@doc """
Reads the values for the given `key` for `pid` in `registry`.
For unique registries, it is either an empty list or a list
with a single element. For duplicate registries, it is a list
with zero, one, or multiple elements.
## Examples
In the example below we register the current process and look it up
both from itself and other processes:
iex> Registry.start_link(keys: :unique, name: Registry.UniqueLookupTest)
iex> Registry.values(Registry.UniqueLookupTest, "hello", self())
[]
iex> {:ok, _} = Registry.register(Registry.UniqueLookupTest, "hello", :world)
iex> Registry.values(Registry.UniqueLookupTest, "hello", self())
[:world]
iex> Task.async(fn -> Registry.values(Registry.UniqueLookupTest, "hello", self()) end) |> Task.await()
[]
iex> parent = self()
iex> Task.async(fn -> Registry.values(Registry.UniqueLookupTest, "hello", parent) end) |> Task.await()
[:world]
The same applies to duplicate registries:
iex> Registry.start_link(keys: :duplicate, name: Registry.DuplicateLookupTest)
iex> Registry.values(Registry.DuplicateLookupTest, "hello", self())
[]
iex> {:ok, _} = Registry.register(Registry.DuplicateLookupTest, "hello", :world)
iex> Registry.values(Registry.DuplicateLookupTest, "hello", self())
[:world]
iex> {:ok, _} = Registry.register(Registry.DuplicateLookupTest, "hello", :another)
iex> Enum.sort(Registry.values(Registry.DuplicateLookupTest, "hello", self()))
[:another, :world]
"""
@doc since: "1.12.0"
@spec values(registry, key, pid) :: [value]
def values(registry, key, pid) when is_atom(registry) do
case key_info!(registry) do
{:unique, partitions, key_ets} ->
key_ets = key_ets || key_ets!(registry, key, partitions)
case safe_lookup_second(key_ets, key) do
{^pid, value} ->
[value]
_ ->
[]
end
{:duplicate, partitions, key_ets} ->
key_ets = key_ets || key_ets!(registry, pid, partitions)
for {^pid, value} <- safe_lookup_second(key_ets, key), do: value
end
end
@doc """
Unregisters all entries for the given `key` associated to the current
process in `registry`.
@@ -1404,12 +1339,12 @@ defmodule Registry.Supervisor do
@moduledoc false
use Supervisor
def start_link(kind, registry, partitions, listeners, entries, compressed) do
arg = {kind, registry, partitions, listeners, entries, compressed}
def start_link(kind, registry, partitions, listeners, entries) do
arg = {kind, registry, partitions, listeners, entries}
Supervisor.start_link(__MODULE__, arg, name: registry)
end
def init({kind, registry, partitions, listeners, entries, compressed}) do
def init({kind, registry, partitions, listeners, entries}) do
^registry = :ets.new(registry, [:set, :public, :named_table, read_concurrency: true])
true = :ets.insert(registry, entries)
@@ -1417,7 +1352,7 @@ defmodule Registry.Supervisor do
for i <- 0..(partitions - 1) do
key_partition = Registry.Partition.key_name(registry, i)
pid_partition = Registry.Partition.pid_name(registry, i)
arg = {kind, registry, i, partitions, key_partition, pid_partition, listeners, compressed}
arg = {kind, registry, i, partitions, key_partition, pid_partition, listeners}
%{
id: pid_partition,
@@ -1477,9 +1412,9 @@ defmodule Registry.Partition do
## Callbacks
def init({kind, registry, i, partitions, key_partition, pid_partition, listeners, compressed}) do
def init({kind, registry, i, partitions, key_partition, pid_partition, listeners}) do
Process.flag(:trap_exit, true)
key_ets = init_key_ets(kind, key_partition, compressed)
key_ets = init_key_ets(kind, key_partition)
pid_ets = init_pid_ets(kind, pid_partition)
# If we have only one partition, we do an optimization which
@@ -1500,18 +1435,17 @@ defmodule Registry.Partition do
# The key partition is a set for unique keys,
# duplicate bag for duplicate ones.
defp init_key_ets(:unique, key_partition, compressed) do
opts = [:set, :public, read_concurrency: true, write_concurrency: true]
:ets.new(key_partition, compression_opt(opts, compressed))
defp init_key_ets(:unique, key_partition) do
:ets.new(key_partition, [:set, :public, read_concurrency: true, write_concurrency: true])
end
defp init_key_ets(:duplicate, key_partition, compressed) do
opts = [:duplicate_bag, :public, read_concurrency: true, write_concurrency: true]
:ets.new(key_partition, compression_opt(opts, compressed))
end
defp compression_opt(opts, compressed) do
if compressed, do: [:compressed] ++ opts, else: opts
defp init_key_ets(:duplicate, key_partition) do
:ets.new(key_partition, [
:duplicate_bag,
:public,
read_concurrency: true,
write_concurrency: true
])
end
# A process can always have multiple keys, so the
+41 -151
View File
@@ -1105,9 +1105,9 @@ defmodule Stream do
end
@doc """
Zips two enumerables together, lazily.
Zips two collections together, lazily.
The zipping finishes as soon as either enumerable completes.
The zipping finishes as soon as any enumerable completes.
## Examples
@@ -1118,9 +1118,7 @@ defmodule Stream do
"""
@spec zip(Enumerable.t(), Enumerable.t()) :: Enumerable.t()
def zip(enumerable1, enumerable2) do
zip_with(enumerable1, enumerable2, fn left, right -> {left, right} end)
end
def zip(left, right), do: zip([left, right])
@doc """
Zips corresponding elements from a finite collection of enumerables
@@ -1139,175 +1137,69 @@ defmodule Stream do
@doc since: "1.4.0"
@spec zip(enumerables) :: Enumerable.t() when enumerables: [Enumerable.t()] | Enumerable.t()
def zip(enumerables) do
zip_with(enumerables, &List.to_tuple(&1))
&prepare_zip(enumerables, &1, &2)
end
@doc """
Lazily zips corresponding elements from two enumerables into a new one, transforming them with
the `zip_fun` function as it goes.
The `zip_fun` will be called with the first element from `enumerable1` and the first
element from `enumerable2`, then with the second element from each, and so on until
either one of the enumerables completes.
## Examples
iex> concat = Stream.concat(1..3, 4..6)
iex> Stream.zip_with(concat, concat, fn a, b -> a + b end) |> Enum.to_list()
[2, 4, 6, 8, 10, 12]
"""
@doc since: "1.12.0"
@spec zip_with(Enumerable.t(), Enumerable.t(), (term, term -> term)) :: Enumerable.t()
def zip_with(enumerable1, enumerable2, zip_fun)
when is_list(enumerable1) and is_list(enumerable2) and is_function(zip_fun, 2) do
&zip_pair(enumerable1, enumerable2, &1, &2, zip_fun)
end
def zip_with(enumerable1, enumerable2, zip_fun) when is_function(zip_fun, 2) do
zip_with([enumerable1, enumerable2], fn [left, right] -> zip_fun.(left, right) end)
end
defp zip_pair(_list1, _list2, {:halt, acc}, _fun, _zip_fun) do
{:halted, acc}
end
defp zip_pair(list1, list2, {:suspend, acc}, fun, zip_fun) do
{:suspended, acc, &zip_pair(list1, list2, &1, fun, zip_fun)}
end
defp zip_pair([], _list2, {:cont, acc}, _fun, _zip_fun), do: {:done, acc}
defp zip_pair(_list1, [], {:cont, acc}, _fun, _zip_fun), do: {:done, acc}
defp zip_pair([head1 | tail1], [head2 | tail2], {:cont, acc}, fun, zip_fun) do
zip_pair(tail1, tail2, fun.(zip_fun.(head1, head2), acc), fun, zip_fun)
end
@doc """
Lazily zips corresponding elements from a finite collection of enumerables into a new
enumerable, transforming them with the `zip_fun` function as it goes.
The first element from each of the enums in `enumerables` will be put into a list which is then passed to
the 1-arity `zip_fun` function. Then, the second elements from each of the enums are put into a list and passed to
`zip_fun`, and so on until any one of the enums in `enumerables` completes.
Returns a new enumerable with the results of calling `zip_fun`.
## Examples
iex> concat = Stream.concat(1..3, 4..6)
iex> Stream.zip_with([concat, concat], fn [a, b] -> a + b end) |> Enum.to_list()
[2, 4, 6, 8, 10, 12]
iex> concat = Stream.concat(1..3, 4..6)
iex> Stream.zip_with([concat, concat, 1..3], fn [a, b, c] -> a + b + c end) |> Enum.to_list()
[3, 6, 9]
"""
@doc since: "1.12.0"
@spec zip_with(enumerables, (Enumerable.t() -> term)) :: Enumerable.t()
when enumerables: [Enumerable.t()] | Enumerable.t()
def zip_with(enumerables, zip_fun) when is_function(zip_fun, 1) do
if is_list(enumerables) and :lists.all(&is_list/1, enumerables) do
&zip_list(enumerables, &1, &2, zip_fun)
else
&zip_enum(enumerables, &1, &2, zip_fun)
end
end
defp zip_list(_enumerables, {:halt, acc}, _fun, _zip_fun) do
{:halted, acc}
end
defp zip_list(enumerables, {:suspend, acc}, fun, zip_fun) do
{:suspended, acc, &zip_list(enumerables, &1, fun, zip_fun)}
end
defp zip_list(enumerables, {:cont, acc}, fun, zip_fun) do
case zip_list_heads_tails(enumerables, [], []) do
{heads, tails} -> zip_list(tails, fun.(zip_fun.(heads), acc), fun, zip_fun)
:error -> {:done, acc}
end
end
defp zip_list_heads_tails([[head | tail] | rest], heads, tails) do
zip_list_heads_tails(rest, [head | heads], [tail | tails])
end
defp zip_list_heads_tails([[] | _rest], _heads, _tails) do
:error
end
defp zip_list_heads_tails([], heads, tails) do
{:lists.reverse(heads), :lists.reverse(tails)}
end
defp zip_enum(enumerables, acc, fun, zip_fun) do
step = fn x, acc ->
{:suspend, :lists.reverse([x | acc])}
end
defp prepare_zip(enumerables, acc, fun) do
step = &do_zip_step(&1, &2)
enum_funs =
Enum.map(enumerables, fn enum ->
{&Enumerable.reduce(enum, &1, step), [], :cont}
end)
do_zip_enum(enum_funs, acc, fun, zip_fun)
do_zip(enum_funs, acc, fun)
end
# This implementation of do_zip_enum/4 works for any number of streams to zip
defp do_zip_enum(zips, {:halt, acc}, _fun, _zip_fun) do
# This implementation of do_zip/3 works for any number of
# streams to zip, even if right now zip/2 only zips two streams.
defp do_zip(zips, {:halt, acc}, _fun) do
do_zip_close(zips)
{:halted, acc}
end
defp do_zip_enum(zips, {:suspend, acc}, fun, zip_fun) do
{:suspended, acc, &do_zip_enum(zips, &1, fun, zip_fun)}
defp do_zip(zips, {:suspend, acc}, fun) do
{:suspended, acc, &do_zip(zips, &1, fun)}
end
defp do_zip_enum([], {:cont, acc}, _callback, _zip_fun) do
defp do_zip([], {:cont, acc}, _callback) do
{:done, acc}
end
defp do_zip_enum(zips, {:cont, acc}, callback, zip_fun) do
defp do_zip(zips, {:cont, acc}, callback) do
try do
do_zip_next(zips, acc, callback, [], [], zip_fun)
do_zip_next_tuple(zips, acc, callback, [], [])
catch
kind, reason ->
do_zip_close(zips)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:next, buffer, acc} ->
do_zip_enum(buffer, acc, callback, zip_fun)
do_zip(buffer, acc, callback)
{:done, _acc} = other ->
other
end
end
# do_zip_next/6 computes the next tuple formed by
# do_zip_next_tuple/5 computes the next tuple formed by
# the next element of each zipped stream.
defp do_zip_next(
[{_, [], :halt} | zips],
acc,
_callback,
_yielded_elems,
buffer,
_zip_fun
) do
defp do_zip_next_tuple([{_, [], :halt} | zips], acc, _callback, _yielded_elems, buffer) do
do_zip_close(:lists.reverse(buffer, zips))
{:done, acc}
end
defp do_zip_next([{fun, [], :cont} | zips], acc, callback, yielded_elems, buffer, zip_fun) do
defp do_zip_next_tuple([{fun, [], :cont} | zips], acc, callback, yielded_elems, buffer) do
case fun.({:cont, []}) do
{:suspended, [elem | next_acc], fun} ->
next_buffer = [{fun, next_acc, :cont} | buffer]
do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun)
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
{_, [elem | next_acc]} ->
next_buffer = [{fun, next_acc, :halt} | buffer]
do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun)
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
{_, []} ->
# The current zipped stream terminated, so we close all the streams
@@ -1317,30 +1209,28 @@ defmodule Stream do
end
end
defp do_zip_next(
[{fun, zip_acc, zip_op} | zips],
acc,
callback,
yielded_elems,
buffer,
zip_fun
) do
defp do_zip_next_tuple([{fun, zip_acc, zip_op} | zips], acc, callback, yielded_elems, buffer) do
[elem | rest] = zip_acc
next_buffer = [{fun, rest, zip_op} | buffer]
do_zip_next(zips, acc, callback, [elem | yielded_elems], next_buffer, zip_fun)
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
end
defp do_zip_next([] = _zips, acc, callback, yielded_elems, buffer, zip_fun) do
defp do_zip_next_tuple([] = _zips, acc, callback, yielded_elems, buffer) do
# "yielded_elems" is a reversed list of results for the current iteration of
# zipping. That is to say, the nth element from each of the enums being zipped.
# It needs to be reversed and passed to the zipping function so it can do it's thing.
{:next, :lists.reverse(buffer), callback.(zip_fun.(:lists.reverse(yielded_elems)), acc)}
# zipping: it needs to be reversed and converted to a tuple to have the next
# tuple in the list resulting from zipping.
zipped = List.to_tuple(:lists.reverse(yielded_elems))
{:next, :lists.reverse(buffer), callback.(zipped, acc)}
end
defp do_zip_close(zips) do
:lists.foreach(fn {fun, _, _} -> fun.({:halt, []}) end, zips)
end
defp do_zip_step(x, acc) do
{:suspend, :lists.reverse([x | acc])}
end
## Sources
@doc """
@@ -1358,7 +1248,7 @@ defmodule Stream do
def cycle(enumerable)
def cycle([]) do
raise ArgumentError, "cannot cycle over an empty enumerable"
raise ArgumentError, "cannot cycle over empty enumerable"
end
def cycle(enumerable) when is_list(enumerable) do
@@ -1407,7 +1297,7 @@ defmodule Stream do
fn acc ->
case reduce.(acc) do
{state, []} when state in [:done, :halted] ->
raise ArgumentError, "cannot cycle over an empty enumerable"
raise ArgumentError, "cannot cycle over empty enumerable"
other ->
other
@@ -1443,9 +1333,9 @@ defmodule Stream do
## Examples
# Although not necessary, let's seed the random algorithm
iex> :rand.seed(:exsss, {1, 2, 3})
iex> :rand.seed(:exrop, {1, 2, 3})
iex> Stream.repeatedly(&:rand.uniform/0) |> Enum.take(3)
[0.5455598952593053, 0.6039309974353404, 0.6684893034823949]
[0.7498295129076106, 0.06161655489244533, 0.7924073127680873]
"""
@spec repeatedly((() -> element)) :: Enumerable.t()
@@ -1694,7 +1584,7 @@ defmodule Stream do
end
defimpl Enumerable, for: Stream do
@compile :inline_list_funcs
@compile :inline_list_funs
def count(_lazy), do: {:error, __MODULE__}
@@ -1731,7 +1621,7 @@ defimpl Enumerable, for: Stream do
defp do_done({reason, [acc | _]}, nil), do: {reason, acc}
defp do_done({reason, [acc | t]}, {done, fun}) do
[h | _] = :lists.reverse(t)
[h | _] = Enum.reverse(t)
case done.([acc, h], fun) do
{:cont, [acc | _]} -> {reason, acc}
@@ -1745,7 +1635,7 @@ defimpl Inspect, for: Stream do
import Inspect.Algebra
def inspect(%{enum: enum, funs: funs}, opts) do
inner = [enum: enum, funs: :lists.reverse(funs)]
inner = [enum: enum, funs: Enum.reverse(funs)]
concat(["#Stream<", to_doc(inner, opts), ">"])
end
end
+89 -134
View File
@@ -69,7 +69,7 @@ defmodule String do
## Code points and grapheme cluster
The functions in this module act according to the Unicode
Standard, version 13.0.0.
Standard, version 12.1.0.
As per the standard, a code point is a single Unicode Character,
which may be represented by one or more bytes.
@@ -133,7 +133,7 @@ defmodule String do
* `Kernel.bit_size/1` and `Kernel.byte_size/1` - size related functions
* `Kernel.is_bitstring/1` and `Kernel.is_binary/1` - type-check function
* Plus a number of functions for working with binaries (bytes)
in the [`:binary` module](`:binary`)
in the [`:binary` module](http://www.erlang.org/doc/man/binary.html)
There are many situations where using the `String` module can
be avoided in favor of binary functions or pattern matching.
@@ -286,7 +286,7 @@ defmodule String do
@typedoc "Pattern used in functions like `replace/4` and `split/3`"
@type pattern :: t | [t] | :binary.cp()
@conditional_mappings [:greek, :turkic]
@conditional_mappings [:greek]
@doc """
Checks if a string contains only printable characters up to `character_limit`.
@@ -470,11 +470,11 @@ defmodule String do
@spec split(t, pattern | Regex.t(), keyword) :: [t]
def split(string, pattern, options \\ [])
def split(string, %Regex{} = pattern, options) when is_binary(string) and is_list(options) do
def split(string, %Regex{} = pattern, options) when is_binary(string) do
Regex.split(pattern, string, options)
end
def split(string, "", options) when is_binary(string) and is_list(options) do
def split(string, "", options) when is_binary(string) do
parts = Keyword.get(options, :parts, :infinity)
index = parts_to_index(parts)
trim = Keyword.get(options, :trim, false)
@@ -486,7 +486,7 @@ defmodule String do
end
end
def split(string, pattern, options) when is_binary(string) and is_list(options) do
def split(string, pattern, options) when is_binary(string) do
parts = Keyword.get(options, :parts, :infinity)
trim = Keyword.get(options, :trim, false)
@@ -559,7 +559,7 @@ defmodule String do
@spec splitter(t, pattern, keyword) :: Enumerable.t()
def splitter(string, pattern, options \\ [])
def splitter(string, "", options) when is_binary(string) and is_list(options) do
def splitter(string, "", options) do
if Keyword.get(options, :trim, false) do
Stream.unfold(string, &next_grapheme/1)
else
@@ -567,7 +567,7 @@ defmodule String do
end
end
def splitter(string, pattern, options) when is_binary(string) and is_list(options) do
def splitter(string, pattern, options) do
pattern = maybe_compile_pattern(pattern)
trim = Keyword.get(options, :trim, false)
Stream.unfold(string, &do_splitter(&1, pattern, trim))
@@ -626,13 +626,11 @@ defmodule String do
@spec split_at(t, integer) :: {t, t}
def split_at(string, position)
def split_at(string, position)
when is_binary(string) and is_integer(position) and position >= 0 do
def split_at(string, position) when is_integer(position) and position >= 0 do
do_split_at(string, position)
end
def split_at(string, position)
when is_binary(string) and is_integer(position) and position < 0 do
def split_at(string, position) when is_integer(position) and position < 0 do
position = length(string) + position
case position >= 0 do
@@ -647,7 +645,7 @@ defmodule String do
end
@doc ~S"""
Returns `true` if `string1` is canonically equivalent to `string2`.
Returns `true` if `string1` is canonically equivalent to 'string2'.
It performs Normalization Form Canonical Decomposition (NFD) on the
strings before comparing them. This function is equivalent to:
@@ -674,7 +672,7 @@ defmodule String do
"""
@spec equivalent?(t, t) :: boolean
def equivalent?(string1, string2) when is_binary(string1) and is_binary(string2) do
def equivalent?(string1, string2) do
normalize(string1, :nfd) == normalize(string2, :nfd)
end
@@ -730,28 +728,28 @@ defmodule String do
"""
def normalize(string, form)
def normalize(string, :nfd) when is_binary(string) do
def normalize(string, :nfd) do
case :unicode.characters_to_nfd_binary(string) do
string when is_binary(string) -> string
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfd)
end
end
def normalize(string, :nfc) when is_binary(string) do
def normalize(string, :nfc) do
case :unicode.characters_to_nfc_binary(string) do
string when is_binary(string) -> string
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfc)
end
end
def normalize(string, :nfkd) when is_binary(string) do
def normalize(string, :nfkd) do
case :unicode.characters_to_nfkd_binary(string) do
string when is_binary(string) -> string
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfkd)
end
end
def normalize(string, :nfkc) when is_binary(string) do
def normalize(string, :nfkc) do
case :unicode.characters_to_nfkc_binary(string) do
string when is_binary(string) -> string
{:error, good, <<head, rest::binary>>} -> good <> <<head>> <> normalize(rest, :nfkc)
@@ -761,10 +759,10 @@ defmodule String do
@doc """
Converts all characters in the given string to uppercase according to `mode`.
`mode` may be `:default`, `:ascii`, `:greek` or `:turkic`. The `:default` mode considers
`mode` may be `:default`, `:ascii` or `:greek`. The `:default` mode considers
all non-conditional transformations outlined in the Unicode standard. `:ascii`
uppercases only the letters a to z. `:greek` includes the context sensitive
mappings found in Greek. `:turkic` properly handles the letter i with the dotless variant.
mappings found in Greek.
## Examples
@@ -784,16 +782,8 @@ defmodule String do
iex> String.upcase("olá", :ascii)
"OLá"
And `:turkic` properly handles the letter i with the dotless variant:
iex> String.upcase("ıi")
"II"
iex> String.upcase("ıi", :turkic)
"Iİ"
"""
@spec upcase(t, :default | :ascii | :greek | :turkic) :: t
@spec upcase(t, :default | :ascii | :greek) :: t
def upcase(string, mode \\ :default)
def upcase("", _mode) do
@@ -808,7 +798,7 @@ defmodule String do
IO.iodata_to_binary(upcase_ascii(string))
end
def upcase(string, mode) when is_binary(string) and mode in @conditional_mappings do
def upcase(string, mode) when mode in @conditional_mappings do
String.Casing.upcase(string, [], mode)
end
@@ -821,10 +811,10 @@ defmodule String do
@doc """
Converts all characters in the given string to lowercase according to `mode`.
`mode` may be `:default`, `:ascii`, `:greek` or `:turkic`. The `:default` mode considers
`mode` may be `:default`, `:ascii` or `:greek`. The `:default` mode considers
all non-conditional transformations outlined in the Unicode standard. `:ascii`
lowercases only the letters A to Z. `:greek` includes the context sensitive
mappings found in Greek. `:turkic` properly handles the letter i with the dotless variant.
mappings found in Greek.
## Examples
@@ -844,7 +834,7 @@ defmodule String do
iex> String.downcase("OLÁ", :ascii)
"olÁ"
The `:greek` mode properly handles the context sensitive sigma in Greek:
And `:greek` properly handles the context sensitive sigma in Greek:
iex> String.downcase("ΣΣ")
"σσ"
@@ -852,16 +842,8 @@ defmodule String do
iex> String.downcase("ΣΣ", :greek)
"σς"
And `:turkic` properly handles the letter i with the dotless variant:
iex> String.downcase("Iİ")
"ii̇"
iex> String.downcase("Iİ", :turkic)
"ıi"
"""
@spec downcase(t, :default | :ascii | :greek | :turkic) :: t
@spec downcase(t, :default | :ascii | :greek) :: t
def downcase(string, mode \\ :default)
def downcase("", _mode) do
@@ -876,7 +858,7 @@ defmodule String do
IO.iodata_to_binary(downcase_ascii(string))
end
def downcase(string, mode) when is_binary(string) and mode in @conditional_mappings do
def downcase(string, mode) when mode in @conditional_mappings do
String.Casing.downcase(string, [], mode)
end
@@ -890,10 +872,10 @@ defmodule String do
Converts the first character in the given string to
uppercase and the remainder to lowercase according to `mode`.
`mode` may be `:default`, `:ascii`, `:greek` or `:turkic`. The `:default` mode considers
`mode` may be `:default`, `:ascii` or `:greek`. The `:default` mode considers
all non-conditional transformations outlined in the Unicode standard. `:ascii`
capitalizes only the letters A to Z. `:greek` includes the context sensitive
mappings found in Greek. `:turkic` properly handles the letter i with the dotless variant.
lowercases only the letters A to Z. `:greek` includes the context sensitive
mappings found in Greek.
## Examples
@@ -907,7 +889,7 @@ defmodule String do
"Olá"
"""
@spec capitalize(t, :default | :ascii | :greek | :turkic) :: t
@spec capitalize(t, :default | :ascii | :greek) :: t
def capitalize(string, mode \\ :default)
def capitalize(<<char, rest::binary>>, :ascii) do
@@ -1181,8 +1163,7 @@ defmodule String do
"""
@spec trim_leading(t, t) :: t
def trim_leading(string, to_trim)
when is_binary(string) and is_binary(to_trim) do
def trim_leading(string, to_trim) do
replace_leading(string, to_trim, "")
end
@@ -1212,8 +1193,7 @@ defmodule String do
"""
@spec trim_trailing(t, t) :: t
def trim_trailing(string, to_trim)
when is_binary(string) and is_binary(to_trim) do
def trim_trailing(string, to_trim) do
replace_trailing(string, to_trim, "")
end
@@ -1228,7 +1208,7 @@ defmodule String do
"""
@spec trim(t) :: t
def trim(string) when is_binary(string) do
def trim(string) do
string
|> trim_leading()
|> trim_trailing()
@@ -1245,7 +1225,7 @@ defmodule String do
"""
@spec trim(t, t) :: t
def trim(string, to_trim) when is_binary(string) and is_binary(to_trim) do
def trim(string, to_trim) do
string
|> trim_leading(to_trim)
|> trim_trailing(to_trim)
@@ -1582,7 +1562,7 @@ defmodule String do
one single grapheme.
"""
@spec reverse(t) :: t
def reverse(string) when is_binary(string) do
def reverse(string) do
do_reverse(next_grapheme(string), [])
end
@@ -1595,7 +1575,7 @@ defmodule String do
@compile {:inline, duplicate: 2}
@doc """
Returns a string `subject` repeated `n` times.
Returns a string `subject` duplicated `n` times.
Inlined by the compiler.
@@ -1612,7 +1592,7 @@ defmodule String do
"""
@spec duplicate(t, non_neg_integer) :: t
def duplicate(subject, n) when is_binary(subject) and is_integer(n) and n >= 0 do
def duplicate(subject, n) do
:binary.copy(subject, n)
end
@@ -1712,12 +1692,11 @@ defmodule String do
"""
@spec valid?(t) :: boolean
def valid?(<<string::binary>>), do: valid_utf8?(string)
def valid?(_), do: false
def valid?(string)
defp valid_utf8?(<<_::utf8, rest::bits>>), do: valid_utf8?(rest)
defp valid_utf8?(<<>>), do: true
defp valid_utf8?(_), do: false
def valid?(<<_::utf8, t::binary>>), do: valid?(t)
def valid?(<<>>), do: true
def valid?(_), do: false
@doc false
@deprecated "Use String.valid?/1 instead"
@@ -1762,7 +1741,7 @@ defmodule String do
def chunk("", _), do: []
def chunk(string, trait) when is_binary(string) and trait in [:valid, :printable] do
def chunk(string, trait) when trait in [:valid, :printable] do
{cp, _} = next_codepoint(string)
pred_fn = make_chunk_pred(trait)
do_chunk(string, pred_fn.(cp), pred_fn)
@@ -1830,7 +1809,7 @@ defmodule String do
"""
@spec next_grapheme(t) :: {grapheme, t} | nil
def next_grapheme(binary) when is_binary(binary) do
def next_grapheme(binary) do
case next_grapheme_size(binary) do
{size, rest} -> {binary_part(binary, 0, size), rest}
nil -> nil
@@ -1873,7 +1852,7 @@ defmodule String do
"""
@spec first(t) :: grapheme | nil
def first(string) when is_binary(string) do
def first(string) do
case next_grapheme(string) do
{char, _} -> char
nil -> nil
@@ -1894,7 +1873,7 @@ defmodule String do
"""
@spec last(t) :: grapheme | nil
def last(string) when is_binary(string) do
def last(string) do
do_last(next_grapheme(string), nil)
end
@@ -1943,11 +1922,11 @@ defmodule String do
"""
@spec at(t, integer) :: grapheme | nil
def at(string, position) when is_binary(string) and is_integer(position) and position >= 0 do
def at(string, position) when is_integer(position) and position >= 0 do
do_at(string, position)
end
def at(string, position) when is_binary(string) and is_integer(position) and position < 0 do
def at(string, position) when is_integer(position) and position < 0 do
position = length(string) + position
case position >= 0 do
@@ -2005,9 +1984,7 @@ defmodule String do
""
end
def slice(string, start, length)
when is_binary(string) and is_integer(start) and is_integer(length) and start >= 0 and
length >= 0 do
def slice(string, start, length) when start >= 0 and length >= 0 do
case String.Unicode.split_at(string, start) do
{_, nil} ->
""
@@ -2018,9 +1995,7 @@ defmodule String do
end
end
def slice(string, start, length)
when is_binary(string) and is_integer(start) and is_integer(length) and start < 0 and
length >= 0 do
def slice(string, start, length) when start < 0 and length >= 0 do
start = length(string) + start
case start >= 0 do
@@ -2052,24 +2027,19 @@ defmodule String do
iex> String.slice("elixir", 1..10)
"lixir"
iex> String.slice("elixir", 10..3)
""
iex> String.slice("elixir", -4..-1)
"ixir"
iex> String.slice("elixir", 2..-1)
"ixir"
iex> String.slice("elixir", -4..6)
"ixir"
For ranges where `start > stop`, you need to explicit
mark them as increasing:
iex> String.slice("elixir", 2..-1//1)
"ixir"
iex> String.slice("elixir", 1..-2//1)
"lixi"
If values are out of bounds, it returns an empty string:
iex> String.slice("elixir", 10..3)
iex> String.slice("elixir", -1..-4)
""
iex> String.slice("elixir", -10..-7)
@@ -2083,37 +2053,22 @@ defmodule String do
"""
@spec slice(t, Range.t()) :: t
def slice(string, first..last//step = range) when is_binary(string) do
# TODO: Deprecate negative steps on Elixir v1.16
# TODO: There are two features we can add to slicing ranges:
# 1. We can allow the step to be any positive number
# 2. We can allow slice and reverse at the same time. However, we can't
# implement so right now. First we will have to raise if a decreasing
# range is given on Elixir v2.0.
if step == 1 or (step == -1 and first > last) do
slice_range(string, first, last)
else
raise ArgumentError,
"String.slice/2 does not accept ranges with custom steps, got: #{inspect(range)}"
end
end
# TODO: Remove me on v2.0
def slice(string, %{__struct__: Range, first: first, last: last} = range) do
step = if first <= last, do: 1, else: -1
slice(string, Map.put(range, :step, step))
end
def slice(string, range)
defp slice_range("", _, _), do: ""
def slice("", _.._), do: ""
defp slice_range(string, first, -1) when first >= 0 do
def slice(string, first..-1) when first >= 0 do
case String.Unicode.split_at(string, first) do
{_, nil} -> ""
{start_bytes, _} -> binary_part(string, start_bytes, byte_size(string) - start_bytes)
{_, nil} ->
""
{start_bytes, _} ->
binary_part(string, start_bytes, byte_size(string) - start_bytes)
end
end
defp slice_range(string, first, last) when first >= 0 and last >= 0 do
def slice(string, first..last) when first >= 0 and last >= 0 do
if last >= first do
slice(string, first, last - first + 1)
else
@@ -2121,8 +2076,9 @@ defmodule String do
end
end
defp slice_range(string, first, last) do
{bytes, length} = acc_bytes(next_grapheme_size(string), [], 0)
def slice(string, first..last) do
{bytes, length} = do_acc_bytes(next_grapheme_size(string), [], 0)
first = add_if_negative(first, length)
last = add_if_negative(last, length)
@@ -2132,25 +2088,21 @@ defmodule String do
last = min(last + 1, length)
bytes = Enum.drop(bytes, length - last)
first = last - first
{length_bytes, start_bytes} = split_bytes(bytes, 0, first)
binary_part(string, start_bytes, length_bytes)
{length_bytes, start_bytes} = Enum.split(bytes, first)
binary_part(string, Enum.sum(start_bytes), Enum.sum(length_bytes))
end
end
defp acc_bytes({size, rest}, bytes, length) do
acc_bytes(next_grapheme_size(rest), [size | bytes], length + 1)
end
defp acc_bytes(nil, bytes, length) do
{bytes, length}
end
defp add_if_negative(value, to_add) when value < 0, do: value + to_add
defp add_if_negative(value, _to_add), do: value
defp split_bytes(rest, acc, 0), do: {acc, Enum.sum(rest)}
defp split_bytes([], acc, _), do: {acc, 0}
defp split_bytes([head | tail], acc, count), do: split_bytes(tail, head + acc, count - 1)
defp do_acc_bytes({size, rest}, bytes, length) do
do_acc_bytes(next_grapheme_size(rest), [size | bytes], length + 1)
end
defp do_acc_bytes(nil, bytes, length) do
{bytes, length}
end
@doc """
Returns `true` if `string` starts with any of the prefixes given.
@@ -2262,7 +2214,7 @@ defmodule String do
"""
@spec match?(t, Regex.t()) :: boolean
def match?(string, regex) when is_binary(string) do
def match?(string, regex) do
Regex.match?(regex, string)
end
@@ -2329,7 +2281,7 @@ defmodule String do
strings.
In case you need to work with bytes, take a look at the
[`:binary` module](`:binary`).
[`:binary` module](http://www.erlang.org/doc/man/binary.html).
## Examples
@@ -2373,7 +2325,7 @@ defmodule String do
"""
@spec to_atom(String.t()) :: atom
def to_atom(string) when is_binary(string) do
def to_atom(string) do
:erlang.binary_to_atom(string, :utf8)
end
@@ -2390,9 +2342,12 @@ defmodule String do
iex> String.to_existing_atom("my_atom")
:my_atom
iex> String.to_existing_atom("this_atom_will_never_exist")
** (ArgumentError) argument error
"""
@spec to_existing_atom(String.t()) :: atom
def to_existing_atom(string) when is_binary(string) do
def to_existing_atom(string) do
:erlang.binary_to_existing_atom(string, :utf8)
end
@@ -2418,7 +2373,7 @@ defmodule String do
"""
@spec to_integer(String.t()) :: integer
def to_integer(string) when is_binary(string) do
def to_integer(string) do
:erlang.binary_to_integer(string)
end
@@ -2434,7 +2389,7 @@ defmodule String do
"""
@spec to_integer(String.t(), 2..36) :: integer
def to_integer(string, base) when is_binary(string) and is_integer(base) do
def to_integer(string, base) do
:erlang.binary_to_integer(string, base)
end
@@ -2460,7 +2415,7 @@ defmodule String do
"""
@spec to_float(String.t()) :: float
def to_float(string) when is_binary(string) do
def to_float(string) do
:erlang.binary_to_float(string)
end
@@ -2497,7 +2452,7 @@ defmodule String do
def bag_distance(_string, ""), do: 0.0
def bag_distance("", _string), do: 0.0
def bag_distance(string1, string2) when is_binary(string1) and is_binary(string2) do
def bag_distance(string1, string2) do
{bag1, length1} = string_to_bag(string1, %{}, 0)
{bag2, length2} = string_to_bag(string2, %{}, 0)
@@ -2563,7 +2518,7 @@ defmodule String do
def jaro_distance(_string, ""), do: 0.0
def jaro_distance("", _string), do: 0.0
def jaro_distance(string1, string2) when is_binary(string1) and is_binary(string2) do
def jaro_distance(string1, string2) do
{chars1, len1} = chars_and_length(string1)
{chars2, len2} = chars_and_length(string2)
@@ -2650,7 +2605,7 @@ defmodule String do
"""
@doc since: "1.3.0"
@spec myers_difference(t, t) :: [{:eq | :ins | :del, t}]
def myers_difference(string1, string2) when is_binary(string1) and is_binary(string2) do
def myers_difference(string1, string2) do
graphemes(string1)
|> List.myers_difference(graphemes(string2))
|> Enum.map(fn {kind, chars} -> {kind, IO.iodata_to_binary(chars)} end)
+1 -1
View File
@@ -415,7 +415,7 @@ defmodule StringIO do
defp list_to_binary(data, :unicode) when is_list(data), do: List.to_string(data)
defp list_to_binary(data, :latin1) when is_list(data), do: :erlang.list_to_binary(data)
# From https://erlang.org/doc/apps/stdlib/io_protocol.html: result can be any
# From http://erlang.org/doc/apps/stdlib/io_protocol.html: result can be any
# Erlang term, but if it is a list(), the I/O server can convert it to a binary().
defp get_until_result(data, encoding) when is_list(data), do: list_to_binary(data, encoding)
defp get_until_result(data, _), do: data
+1 -1
View File
@@ -930,7 +930,7 @@ defmodule Supervisor do
"""
@spec which_children(supervisor) :: [
# inlining module() | :dynamic here because :supervisor.modules() is not exported
{term() | :undefined, child | :restarting, :worker | :supervisor, [module()] | :dynamic}
{term() | :undefined, child | :restarting, :worker | :supervisor, module() | :dynamic}
]
def which_children(supervisor) do
call(supervisor, :which_children)
+33 -258
View File
@@ -43,7 +43,7 @@ defmodule System do
system time may not match in case of time warps although the VM works towards
aligning them. This time is not monotonic (i.e., it may decrease)
as its behaviour is configured [by the VM time warp
mode](https://erlang.org/doc/apps/erts/time_correction.html#Time_Warp_Modes);
mode](http://www.erlang.org/doc/apps/erts/time_correction.html#Time_Warp_Modes);
* `monotonic_time/0` - a monotonically increasing time provided
by the Erlang VM.
@@ -58,7 +58,7 @@ defmodule System do
For a more complete rundown on the VM support for different
times, see the [chapter on time and time
correction](https://erlang.org/doc/apps/erts/time_correction.html)
correction](http://www.erlang.org/doc/apps/erts/time_correction.html)
in the Erlang docs.
"""
@@ -81,22 +81,6 @@ defmodule System do
| :nanosecond
| pos_integer
@type signal ::
:sigabrt
| :sigalrm
| :sigchld
| :sighup
| :sigquit
| :sigstop
| :sigterm
| :sigtstp
| :sigusr1
| :sigusr2
@vm_signals [:sigquit, :sigterm, :sigusr1]
@os_signals [:sighup, :sigabrt, :sigalrm, :sigusr2, :sigchld, :sigstop, :sigtstp]
@signals @vm_signals ++ @os_signals
@base_dir :filename.join(__DIR__, "../../..")
@version_file :filename.join(@base_dir, "VERSION")
@@ -425,8 +409,8 @@ defmodule System do
The function must receive the exit status code as an argument.
If the VM terminates programmatically, via `System.stop/1`, `System.halt/1`,
or exit signals, the `at_exit/1` callbacks are not executed.
If the VM terminates programmatically, via `System.stop/1` or `System.halt/1`,
the `at_exit/1` callbacks are not executed.
"""
@spec at_exit((non_neg_integer -> any)) :: :ok
def at_exit(fun) when is_function(fun, 1) do
@@ -434,150 +418,6 @@ defmodule System do
:ok
end
defmodule SignalHandler do
@moduledoc false
@behaviour :gen_event
@impl true
def init({event, fun}) do
{:ok, {event, fun}}
end
@impl true
def handle_call(_message, state) do
{:ok, :ok, state}
end
@impl true
def handle_event(signal, {event, fun}) do
if signal == event, do: :ok = fun.()
{:ok, {event, fun}}
end
@impl true
def handle_info(_, {event, fun}) do
{:ok, {event, fun}}
end
end
@doc """
Traps the given `signal` to execute the `fun`.
**Important**: Trapping signals may have strong implications
on how a system shuts down and behave in production and
therefore it is extremely discouraged for libraries to
set their own traps. Instead, they should redirect users
to configure them themselves. The only cases where it is
acceptable for libraries to set their own traps is when
using Elixir in script mode, such as in `.exs` files and
via Mix tasks.
An optional `id` that uniquely identifies the function
can be given, otherwise a unique one is automatically
generated. If a previously registered `id` is given,
this function returns an error tuple. The `id` can be
used to remove a registered signal by calling
`untrap_signal/2`.
The given `fun` receives no arguments and it must return
`:ok`.
It returns `{:ok, id}` in case of success,
`{:error, :already_registered}` in case the id has already
been registered for the given signal, or `{:error, :not_sup}`
in case trapping exists is not supported by the current OS.
The first time a signal is trapped, it will override the
default behaviour from the operating system. If the same
signal is trapped multiple times, subsequent functions
given to `trap_signal` will execute *first*. In other
words, you can consider each function is prepended to
the signal handler.
By default, the Erlang VM register traps to the three
signals:
* `:sigstop` - gracefully shuts down the VM with `stop/0`
* `:sigquit` - halts the VM via `halt/0`
* `:sigusr1` - halts the VM via status code of 1
Therefore, if you add traps to the signals above, the
default behaviour above will be executed after all user
signals.
## Implementation notes
All signals run from a single process. Therefore, blocking the
`fun` will block subsequent traps. It is also not possible to add
or remove traps from within a trap itself.
Internally, this functionality is built on top of `:os.set_signal/2`.
When you register a trap, Elixir automatically sets it to `:handle`
and it reverts it back to `:default` once all traps are removed
(except for `:sigquit`, `:sigterm`, and `:sigusr1` which are always
handled). If you or a library call `:os.set_signal/2` directly,
it may disable Elixir traps (or Elixir may override your configuration).
"""
@doc since: "1.12.0"
@spec trap_signal(signal, (() -> :ok)) :: {:ok, reference()} | {:error, :not_sup}
@spec trap_signal(signal, id, (() -> :ok)) ::
{:ok, id} | {:error, :already_registered} | {:error, :not_sup}
when id: term()
def trap_signal(signal, id \\ make_ref(), fun)
when signal in @signals and is_function(fun, 0) do
:elixir_config.serial(fn ->
gen_id = {signal, id}
if {SignalHandler, gen_id} in signal_handlers() do
{:error, :already_registered}
else
try do
:os.set_signal(signal, :handle)
rescue
_ -> {:error, :not_sup}
else
:ok ->
:ok =
:gen_event.add_handler(:erl_signal_server, {SignalHandler, gen_id}, {signal, fun})
{:ok, id}
end
end
end)
end
@doc """
Removes a previously registered `signal` with `id`.
"""
@doc since: "1.12.0"
@spec untrap_signal(signal, id) :: :ok | {:error, :not_found} when id: term
def untrap_signal(signal, id) when signal in @signals do
:elixir_config.serial(fn ->
gen_id = {signal, id}
case :gen_event.delete_handler(:erl_signal_server, {SignalHandler, gen_id}, :delete) do
:ok ->
if not trapping?(signal) do
:os.set_signal(signal, :default)
end
:ok
{:error, :module_not_found} ->
{:error, :not_found}
end
end)
end
defp trapping?(signal) do
signal in @vm_signals or
Enum.any?(signal_handlers(), &match?({_, {^signal, _}}, &1))
end
defp signal_handlers do
:gen_event.which_handlers(:erl_signal_server)
end
@doc """
Locates an executable on the system.
@@ -597,29 +437,19 @@ defmodule System do
end
end
# TODO: Remove this once we require Erlang/OTP 24+
@compile {:no_warn_undefined, {:os, :env, 0}}
@doc """
Returns all system environment variables.
The returned value is a map containing name-value pairs.
Variable names and their values are strings.
"""
# TODO: Remove this once we require Erlang/OTP 24+
@spec get_env() :: %{optional(String.t()) => String.t()}
def get_env do
if function_exported?(:os, :env, 0) do
Map.new(:os.env(), fn {k, v} ->
{IO.chardata_to_string(k), IO.chardata_to_string(v)}
end)
else
Enum.into(:os.getenv(), %{}, fn var ->
var = IO.chardata_to_string(var)
[k, v] = String.split(var, "=", parts: 2)
{k, v}
end)
end
Enum.into(:os.getenv(), %{}, fn var ->
var = IO.chardata_to_string(var)
[k, v] = String.split(var, "=", parts: 2)
{k, v}
end)
end
@doc """
@@ -756,13 +586,23 @@ defmodule System do
@doc """
Deprecated mechanism to retrieve the last exception stacktrace.
Starting from Erlang/OTP 23, this function will always return an
empty list.
Accessing the stacktrace outside of a rescue/catch is deprecated.
If you want to support only Elixir v1.7+, you must access
`__STACKTRACE__/0` inside a rescue/catch. If you want to support
earlier Elixir versions, move `System.stacktrace/0` inside a rescue/catch.
Starting from Erlang/OTP 23, this function will always return an empty list.
Note that the Erlang VM (and therefore this function) does not
return the current stacktrace but rather the stacktrace of the
latest exception. To retrieve the stacktrace of the current process,
use `Process.info(self(), :current_stacktrace)` instead.
"""
# TODO: Remove conditional on Erlang/OTP 23+.
# TODO: Once Erlang/OTP 23 is required, remove conditional, and update @doc accordingly.
# The warning is emitted by the compiler - so a @doc annotation is enough
# Note Elixir may be compiled in an earlier Erlang version but runs on a
# newer one, so we need the check at compilation time and runtime.
@deprecated "Use __STACKTRACE__ instead"
@doc deprecated: "Use __STACKTRACE__ instead"
if function_exported?(:erlang, :get_stacktrace, 0) do
def stacktrace do
if function_exported?(:erlang, :get_stacktrace, 0) do
@@ -881,62 +721,6 @@ defmodule System do
:init.stop(String.to_charlist(status))
end
@doc ~S"""
Executes the given `command` in the OS shell.
It uses `sh` for Unix-like systems and `cmd` for Windows.
**Important**: Use this function with care. In particular, **never
pass untrusted user input to this function**, as the user would be
able to perform "command injection attacks" by executing any code
directly on the machine. Generally speaking, prefer to use `cmd/3`
over this function.
## Examples
iex> System.shell("echo hello")
{"hello\n", 0}
If you want to stream the devices to IO as they come:
iex> System.shell("echo hello", into: IO.stream())
hello
{%IO.Stream{}, 0}
## Options
It accepts the same options as `cmd/3`, except for `arg0`.
"""
@doc since: "1.12.0"
@spec shell(binary, keyword) :: {Collectable.t(), exit_status :: non_neg_integer}
def shell(command, opts \\ []) when is_binary(command) do
assert_no_null_byte!(command, "System.shell/2")
# Finding shell command logic from :os.cmd in OTP
# https://github.com/erlang/otp/blob/8deb96fb1d017307e22d2ab88968b9ef9f1b71d0/lib/kernel/src/os.erl#L184
command =
case :os.type() do
{:unix, _} ->
command =
command
|> String.replace("\"", "\\\"")
|> String.to_charlist()
'sh -c "' ++ command ++ '"'
{:win32, osname} ->
command = String.to_charlist(command)
case {System.get_env("COMSPEC"), osname} do
{nil, :windows} -> 'command.com /s /c ' ++ command
{nil, _} -> 'cmd /s /c ' ++ command
{cmd, _} -> '#{cmd} /s /c ' ++ command
end
end
do_cmd({:spawn, command}, [], opts)
end
@doc ~S"""
Executes the given `command` with `args`.
@@ -969,9 +753,7 @@ defmodule System do
iex> System.cmd("echo", ["hello"], env: [{"MIX_ENV", "test"}])
{"hello\n", 0}
If you want to stream the devices to IO as they come:
iex> System.cmd("echo", ["hello"], into: IO.stream())
iex> System.cmd("echo", ["hello"], into: IO.stream(:stdio, :line))
hello
{%IO.Stream{}, 0}
@@ -1022,7 +804,7 @@ defmodule System do
## Shell commands
If you desire to execute a trusted command inside a shell, with pipes,
redirecting and so on, please check `shell/2`.
redirecting and so on, please check `:os.cmd/1`.
"""
@spec cmd(binary, [binary], keyword) :: {Collectable.t(), exit_status :: non_neg_integer}
def cmd(command, args, opts \\ []) when is_binary(command) and is_list(args) do
@@ -1041,15 +823,11 @@ defmodule System do
:os.find_executable(cmd) || :erlang.error(:enoent, [command, args, opts])
end
do_cmd({:spawn_executable, cmd}, [args: args], opts)
end
defp do_cmd(port_init, base_opts, opts) do
{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide] ++ base_opts, "")
{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide, args: args], "")
{initial, fun} = Collectable.into(into)
try do
do_port(Port.open(port_init, opts), initial, fun)
do_cmd(Port.open({:spawn_executable, cmd}, opts), initial, fun)
catch
kind, reason ->
fun.(initial, :halt)
@@ -1059,18 +837,17 @@ defmodule System do
end
end
defp do_port(port, acc, fun) do
defp do_cmd(port, acc, fun) do
receive do
{^port, {:data, data}} ->
do_port(port, fun.(acc, {:cont, data}), fun)
do_cmd(port, fun.(acc, {:cont, data}), fun)
{^port, {:exit_status, status}} ->
{acc, status}
end
end
defp cmd_opts([{:into, any} | t], opts, _into),
do: cmd_opts(t, opts, any)
defp cmd_opts([{:into, any} | t], opts, _into), do: cmd_opts(t, opts, any)
defp cmd_opts([{:cd, bin} | t], opts, into) when is_binary(bin),
do: cmd_opts(t, [{:cd, bin} | opts], into)
@@ -1081,8 +858,7 @@ defmodule System do
defp cmd_opts([{:stderr_to_stdout, true} | t], opts, into),
do: cmd_opts(t, [:stderr_to_stdout | opts], into)
defp cmd_opts([{:stderr_to_stdout, false} | t], opts, into),
do: cmd_opts(t, opts, into)
defp cmd_opts([{:stderr_to_stdout, false} | t], opts, into), do: cmd_opts(t, opts, into)
defp cmd_opts([{:parallelism, bool} | t], opts, into) when is_boolean(bool),
do: cmd_opts(t, [{:parallelism, bool} | opts], into)
@@ -1093,8 +869,7 @@ defmodule System do
defp cmd_opts([{key, val} | _], _opts, _into),
do: raise(ArgumentError, "invalid option #{inspect(key)} with value #{inspect(val)}")
defp cmd_opts([], opts, into),
do: {into, opts}
defp cmd_opts([], opts, into), do: {into, opts}
defp validate_env(enum) do
Enum.map(enum, fn
@@ -1173,7 +948,7 @@ defmodule System do
unit before you display them to humans.
To determine how many seconds the `:native` unit represents in your current
runtime, you can call this function to convert 1 second to the `:native`
runtime, you can can call this function to convert 1 second to the `:native`
time unit: `System.convert_time_unit(1, :second, :native)`.
"""
@spec convert_time_unit(integer, time_unit | :native, time_unit | :native) :: integer
+130 -213
View File
@@ -19,7 +19,7 @@ defmodule Task do
Besides `async/1` and `await/2`, tasks can also be
started as part of a supervision tree and dynamically spawned
on remote nodes. We will explore these scenarios next.
on remote nodes. We will explore all three scenarios next.
## async and await
@@ -43,99 +43,31 @@ defmodule Task do
meant to receive the result no longer exists, there is
no purpose in completing the computation.
If this is not desired, you will want to use supervised
tasks, described next.
If this is not desired, use `Task.start/1` or consider starting
the task under a `Task.Supervisor` using `async_nolink` or
`start_child`.
## Dynamically supervised tasks
`Task.yield/2` is an alternative to `await/2` where the caller will
temporarily block, waiting until the task replies or crashes. If the
result does not arrive within the timeout, it can be called again at a
later moment. This allows checking for the result of a task multiple
times. If a reply does not arrive within the desired time,
`Task.shutdown/2` can be used to stop the task.
The `Task.Supervisor` module allows developers to dynamically
create multiple supervised tasks.
## Supervised tasks
A short example is:
{:ok, pid} = Task.Supervisor.start_link()
task =
Task.Supervisor.async(pid, fn ->
# Do something
end)
Task.await(task)
However, in the majority of cases, you want to add the task supervisor
to your supervision tree:
Supervisor.start_link([
{Task.Supervisor, name: MyApp.TaskSupervisor}
], strategy: :one_for_one)
And now you can use async/await once again passig the name of
the supervisor isntead of the pid:
Task.Supervisor.async(MyApp.TaskSupervisor, fn ->
# Do something
end)
|> Task.await()
We encourage developers to rely on supervised tasks as much as
possible. Supervised tasks enable a huge variety of patterns
which allows you explicit control on how to handle the results,
errors, and timeouts. Here is a summary:
* Use `Task.Supervisor.start_child/2` to start a fire-and-forget
task and you don't care about its results nor about if it completes
successfully
* Use `Task.Supervisor.async/2` + `Task.await/2` allows you to execute
tasks concurrently and retrieve its result. If the task fails,
the caller will also fail
* Use `Task.Supervisor.async_nolink/2` + `Task.yield/2` + `Task.shutdown/2`
allows you to execute tasks concurrently and retrieve their results
or the reason they failed within a given time frame. If the task fails,
the caller won't fail: you will receive the error reason either on
`yield` or `shutdown`
See the `Task.Supervisor` module for details on the supported operations.
### Distributed tasks
Since Elixir provides a `Task.Supervisor`, it is easy to use one
to dynamically start tasks across nodes:
# On the remote node
Task.Supervisor.start_link(name: MyApp.DistSupervisor)
# On the client
supervisor = {MyApp.DistSupervisor, :remote@local}
Task.Supervisor.async(supervisor, MyMod, :my_fun, [arg1, arg2, arg3])
Note that, when working with distributed tasks, one should use the
`Task.Supervisor.async/4` function that expects explicit module, function,
and arguments, instead of `Task.Supervisor.async/2` that works with anonymous
functions. That's because anonymous functions expect the same module version
to exist on all involved nodes. Check the `Agent` module documentation for
more information on distributed processes as the limitations described there
apply to the whole ecosystem.
## Statically supervised tasks
The `Task` module implements the `child_spec/1` function, which
allows it to be started directly under a regular `Supervisor` -
instead of a `Task.Supervisor` - by passing a tuple with a function
to run:
It is also possible to spawn a task under a supervisor. The `Task`
module implements the `child_spec/1` function, which allows it to
be started directly under a supervisor by passing a tuple with
a function to run:
Supervisor.start_link([
{Task, fn -> :some_work end}
], strategy: :one_for_one)
This is often useful when you need to execute some steps while
setting up your supervision tree. For example: to warm up caches,
log the initialization status, etc.
If you don't want to put the Task code directly under the `Supervisor`,
you can wrap the `Task` in its own module, similar to how you would
do with a `GenServer` or an `Agent`:
However, if you want to invoke a specific module, function and
arguments, or give the task process a name, you need to define
the task in its own module:
defmodule MyTask do
use Task
@@ -155,10 +87,10 @@ defmodule Task do
{MyTask, arg}
], strategy: :one_for_one)
Since these tasks are supervised and not directly linked to the caller,
they cannot be awaited on. By default, the functions `Task.start`
and `Task.start_link` are for fire-and-forget tasks, where you don't
care about the results or if it completes successfully or not.
Since these tasks are supervised and not directly linked to
the caller, they cannot be awaited on. `start_link/1`, unlike
`async/1`, returns `{:ok, pid}` (which is the result expected
by supervisors).
`use Task` defines a `child_spec/1` function, allowing the
defined module to be put under a supervision tree. The generated
@@ -184,6 +116,63 @@ defmodule Task do
preceding `use Task` will be attached to the generated `child_spec/1`
function.
## Dynamically supervised tasks
The `Task.Supervisor` module allows developers to dynamically
create multiple supervised tasks.
A short example is:
{:ok, pid} = Task.Supervisor.start_link()
task =
Task.Supervisor.async(pid, fn ->
# Do something
end)
Task.await(task)
However, in the majority of cases, you want to add the task supervisor
to your supervision tree:
Supervisor.start_link([
{Task.Supervisor, name: MyApp.TaskSupervisor}
], strategy: :one_for_one)
Now you can dynamically start supervised tasks:
Task.Supervisor.start_child(MyApp.TaskSupervisor, fn ->
# Do something
end)
Or even use the async/await pattern:
Task.Supervisor.async(MyApp.TaskSupervisor, fn ->
# Do something
end)
|> Task.await()
Finally, check `Task.Supervisor` for other supported operations.
## Distributed tasks
Since Elixir provides a `Task.Supervisor`, it is easy to use one
to dynamically start tasks across nodes:
# On the remote node
Task.Supervisor.start_link(name: MyApp.DistSupervisor)
# On the client
supervisor = {MyApp.DistSupervisor, :remote@local}
Task.Supervisor.async(supervisor, MyMod, :my_fun, [arg1, arg2, arg3])
Note that, when working with distributed tasks, one should use the `Task.Supervisor.async/4` function
that expects explicit module, function and arguments, instead of `Task.Supervisor.async/2` that
works with anonymous functions. That's because anonymous functions expect
the same module version to exist on all involved nodes. Check the `Agent` module
documentation for more information on distributed processes as the limitations
described there apply to the whole ecosystem.
## Ancestor and Caller Tracking
Whenever you start a new process, Elixir annotates the parent of that process
@@ -298,11 +287,11 @@ defmodule Task do
end
@doc """
Starts a task as part of a supervision tree with the given `fun`.
Starts a process linked to the current process.
`fun` must be a zero-arity anonymous function.
This is used to start a statically supervised task under a supervision tree.
This is often used to start the process as part of a supervision tree.
"""
@spec start_link((() -> any)) :: {:ok, pid}
def start_link(fun) when is_function(fun, 0) do
@@ -310,15 +299,12 @@ defmodule Task do
end
@doc """
Starts a task as part of a supervision tree with the given
`module`, `function`, and `args`.
This is used to start a statically supervised task under a supervision tree.
Starts a task as part of a supervision tree.
"""
@spec start_link(module, atom, [term]) :: {:ok, pid}
def start_link(module, function, args)
when is_atom(module) and is_atom(function) and is_list(args) do
mfa = {module, function, args}
def start_link(module, function_name, args)
when is_atom(module) and is_atom(function_name) and is_list(args) do
mfa = {module, function_name, args}
Task.Supervised.start_link(get_owner(self()), get_callers(self()), mfa)
end
@@ -327,14 +313,9 @@ defmodule Task do
`fun` must be a zero-arity anonymous function.
This should only used when the task is used for side-effects
(like I/O) and you have no interest on its results nor if it
completes successfully.
If the current node is shutdown, the node will terminate even
if the task was not completed. For this reason, we recommend
to use `Task.Supervisor.start_child/2` instead, which allows
you to control the shutdown time via the `:shutdown` option.
This is only used when the task is used for side-effects
(i.e. no interest in the returned result) and it should not
be linked to the current process.
"""
@spec start((() -> any)) :: {:ok, pid}
def start(fun) when is_function(fun, 0) do
@@ -344,14 +325,9 @@ defmodule Task do
@doc """
Starts a task.
This should only used when the task is used for side-effects
(like I/O) and you have no interest on its results nor if it
completes successfully.
If the current node is shutdown, the node will terminate even
if the task was not completed. For this reason, we recommend
to use `Task.Supervisor.start_child/2` instead, which allows
you to control the shutdown time via the `:shutdown` option.
This is only used when the task is used for side-effects
(i.e. no interest in the returned result) and it should not
be linked to the current process.
"""
@spec start(module, atom, [term]) :: {:ok, pid}
def start(module, function_name, args)
@@ -363,14 +339,30 @@ defmodule Task do
@doc """
Starts a task that must be awaited on.
`fun` must be a zero-arity anonymous function. This function
spawns a process that is linked to and monitored by the caller
process. A `Task` struct is returned containing the relevant
information. Developers must eventually call `Task.await/2` or
`Task.yield/2` followed by `Task.shutdown/2` on the returned task.
`fun` must be a zero-arity anonymous function.
This function spawns a process that is linked to and monitored
by the caller process. A `Task` struct is returned containing
the relevant information.
Read the `Task` module documentation for more information about the
general usage of `async/1` and `async/3`.
See also `async/3`.
"""
@spec async((() -> any)) :: t
def async(fun) when is_function(fun, 0) do
async(:erlang, :apply, [fun, []])
end
@doc """
Starts a task that must be awaited on.
A `Task` struct is returned containing the relevant information.
Developers must eventually call `Task.await/2` or `Task.yield/2`
followed by `Task.shutdown/2` on the returned task.
Read the `Task` module documentation for more information about
the general usage of async tasks.
the general usage of `async/1` and `async/3`.
## Linking
@@ -419,17 +411,11 @@ defmodule Task do
to any supervisor, you may leave dangling tasks in case
the parent dies.
"""
@spec async((() -> any)) :: t
def async(fun) when is_function(fun, 0) do
async(:erlang, :apply, [fun, []])
end
## Message format
@doc """
Starts a task that must be awaited on.
Similar to `async/1` except the function to be started is
specified by the given `module`, `function_name`, and `args`.
The reply sent by the task will be in the format `{ref, result}`,
where `ref` is the monitor reference held by the task struct
and `result` is the return value of the task function.
"""
@spec async(module, atom, [term]) :: t
def async(module, function_name, args)
@@ -470,13 +456,10 @@ defmodule Task do
at the same time. Defaults to `System.schedulers_online/0`.
* `:ordered` - whether the results should be returned in the same order
as the input stream. When the output is ordered, Elixir may need to
buffer results to emit them in the original order. Setting this option
to false disables the need to buffer at the cost of removing ordering.
This is also useful when you're using the tasks only for the side effects.
Note that regardless of what `:ordered` is set to, the tasks will
process asynchronously. If you need to process elements in order,
consider using `Enum.map/2` or `Enum.each/2` instead. Defaults to `true`.
as the input stream. This option is useful when you have large
streams and don't want to buffer results before they are delivered.
This is also useful when you're using the tasks for side effects.
Defaults to `true`.
* `:timeout` - the maximum amount of time (in milliseconds or `:infinity`)
each task is allowed to execute for. Defaults to `5000`.
@@ -604,7 +587,7 @@ defmodule Task do
end
end
@doc ~S"""
@doc """
Awaits a task reply and returns it.
In case the task process dies, the current process will exit with the same
@@ -626,86 +609,20 @@ defmodule Task do
to be able to check multiple times if a long-running task has finished
its computation, use `yield/2` instead.
## Compatibility with OTP behaviours
It is not recommended to `await` a long-running task inside an OTP
behaviour such as `GenServer`. Instead, you should match on the message
coming from a task inside your `c:GenServer.handle_info/2` callback. For
more information on the format of the message, see the documentation for
`async/1`.
## Examples
iex> task = Task.async(fn -> 1 + 1 end)
iex> Task.await(task)
2
## Compatibility with OTP behaviours
It is not recommended to `await` a long-running task inside an OTP
behaviour such as `GenServer`. Instead, you should match on the message
coming from a task inside your `c:GenServer.handle_info/2` callback.
A GenServer will receive two messages on `handle_info/2`:
* `{ref, result}` - the reply message where `ref` is the monitor
reference returned by the `task.ref` and `result` is the task
result
* `{:DOWN, ref, :process, pid, reason}` - since all tasks are also
monitored, you will also receive the `:DOWN` message delivered by
`Process.monitor/1`. If you receive the `:DOWN` message without a
a reply, it means the task crashed
Another consideration to have in mind is that tasks started by `Task.async/1`
are always linked to their callers and you may not want the GenServer to
crash if the task crashes. Therefore, it is preferable to instead use
`Task.Supervisor.async_nolink/3` inside OTP behaviours. For completeness, here
is an example of a GenServer that start tasks and handles their results:
defmodule GenServerTaskExample do
use GenServer
def start_link(opts) do
GenServer.start_link(__MODULE__, :ok, opts)
end
def init(_opts) do
# We will keep all running tasks in a map
{:ok, %{tasks: %{}}}
end
# Imagine we invoke a task from the GenServer to access a URL...
def handle_call(:some_message, _from, state) do
url = ...
task = Task.Supervisor.async_nolink(MyApp.TaskSupervisor, fn -> fetch_url(url) end)
# After we start the task, we store its reference and the url it is fetching
state = put_in(state.tasks[task.ref], url)
{:reply, :ok, state}
end
# If the task succeeds...
def handle_info({ref, result}, state) do
# The task succeed so we can cancel the monitoring and discard the DOWN message
Process.demonitor(ref, [:flush])
{url, state} = pop_in(state.tasks[ref])
IO.puts "Got #{inspect(result)} for URL #{inspect url}"
{:noreply, state}
end
# If the task fails...
def handle_info({:DOWN, ref, _, _, reason}, state) do
{url, state} = pop_in(state.tasks[ref])
IO.puts "URL #{inspect url} failed with reason #{inspect(reason)}"
{:noreply, state}
end
end
With the server defined, you will want to start the task supervisor
above and the GenServer in your supervision tree:
children = [
{Task.Supervisor, name: MyApp.TaskSupervisor},
{GenServerTaskExample, name: MyApp.GenServerTaskExample}
]
Supervisor.start_link(children, strategy: :one_for_one)
"""
@spec await(t, timeout) :: term
def await(%Task{ref: ref, owner: owner} = task, timeout \\ 5000) when is_timeout(timeout) do
+9 -8
View File
@@ -177,11 +177,6 @@ defmodule Task.Supervised do
def stream(enumerable, acc, reducer, mfa, options, spawn) do
next = &Enumerable.reduce(enumerable, &1, fn x, acc -> {:suspend, [x | acc]} end)
max_concurrency = Keyword.get(options, :max_concurrency, System.schedulers_online())
unless is_integer(max_concurrency) and max_concurrency > 0 do
raise ArgumentError, ":max_concurrency must be an integer greater than zero"
end
ordered? = Keyword.get(options, :ordered, true)
timeout = Keyword.get(options, :timeout, 5000)
on_timeout = Keyword.get(options, :on_timeout, :exit)
@@ -317,7 +312,6 @@ defmodule Task.Supervised do
stream_deliver({:cont, acc}, max + 1, spawned, delivered, waiting, next, config)
else
pair = deliver_now(result, acc, next, config)
waiting = Map.delete(waiting, position)
stream_reduce(pair, max + 1, spawned, delivered + 1, waiting, next, config)
end
@@ -398,8 +392,15 @@ defmodule Task.Supervised do
:erlang.raise(kind, reason, __STACKTRACE__)
else
pair ->
waiting = Map.delete(waiting, delivered)
stream_deliver(pair, max, spawned, delivered + 1, waiting, next, config)
stream_deliver(
pair,
max,
spawned,
delivered + 1,
Map.delete(waiting, delivered),
next,
config
)
end
%{} ->
+7 -8
View File
@@ -27,7 +27,10 @@ defmodule Task.Supervisor do
@typedoc "Option values used by `start_link`"
@type option ::
DynamicSupervisor.option()
| DynamicSupervisor.init_option()
# :permanent | :transient | :temporary here because :supervisor.restart() is not exported
| {:restart, :permanent | :transient | :temporary}
# :brutal_kill | timeout() here because :supervisor.shutdown() is not exported
| {:shutdown, :brutal_kill | timeout()}
@doc false
def child_spec(opts) when is_list(opts) do
@@ -69,7 +72,7 @@ defmodule Task.Supervisor do
described under the `Name Registration` section in the `GenServer` module
docs;
* `:max_restarts`, `:max_seconds`, and `:max_children` - as specified in
* `:max_restarts`, `:max_seconds` and `:max_children` - as specified in
`DynamicSupervisor`;
This function could also receive `:restart` and `:shutdown` as options
@@ -379,14 +382,10 @@ defmodule Task.Supervisor do
@doc """
Starts a task as a child of the given `supervisor`.
Task.Supervisor.start_child(MyTaskSupervisor, fn ->
IO.puts "I am running in a task"
end)
Note that the spawned process is not linked to the caller, but
only to the supervisor. This command is useful in case the
task needs to perform side-effects (like I/O) and you have no
interest on its results nor if it completes successfully.
task needs to perform side-effects (like I/O) and does not need
to report back to the caller.
## Options
-38
View File
@@ -128,44 +128,6 @@ defmodule Tuple do
:erlang.delete_element(index + 1, tuple)
end
@doc """
Computes a sum of tuple elements.
## Examples
iex> Tuple.sum({255, 255})
510
iex> Tuple.sum({255, 0.0})
255.0
iex> Tuple.sum({})
0
"""
@doc since: "1.12.0"
@spec sum(tuple) :: number()
def sum(tuple), do: sum(tuple, tuple_size(tuple))
defp sum(_tuple, 0), do: 0
defp sum(tuple, index), do: :erlang.element(index, tuple) + sum(tuple, index - 1)
@doc """
Computes a product of tuple elements.
## Examples
iex> Tuple.product({255, 255})
65025
iex> Tuple.product({255, 1.0})
255.0
iex> Tuple.product({})
1
"""
@doc since: "1.12.0"
@spec product(tuple) :: number()
def product(tuple), do: product(tuple, tuple_size(tuple))
defp product(_tuple, 0), do: 1
defp product(tuple, index), do: :erlang.element(index, tuple) * product(tuple, index - 1)
@doc """
Converts a tuple to a list.
+56 -126
View File
@@ -71,89 +71,55 @@ defmodule URI do
end
@doc """
Encodes `enumerable` into a query string using `encoding`.
Encodes an enumerable into a query string.
Takes an enumerable that enumerates as a list of two-element
tuples (for instance, a map or a keyword list) and returns a string
in the form of `key1=value1&key2=value2...`.
in the form of `key1=value1&key2=value2...` where keys and
values are URL encoded as per `encode_www_form/1`.
Keys and values can be any term that implements the `String.Chars`
protocol with the exception of lists, which are explicitly forbidden.
You can specify one of the following `encoding` strategies:
* `:www_form` - (default, since v1.12.0) keys and values are URL encoded as
per `encode_www_form/1`. This is the format typically used by browsers on
query strings and form data. It encodes " " as "+".
* `:rfc3986` - (since v1.12.0) the same as `:www_form` except it encodes
" " as "%20" according [RFC 3986](https://tools.ietf.org/html/rfc3986).
This is the best option if you are encoding in a non-browser situation,
since encoding spaces as "+" can be ambiguous to URI parsers. This can
inadvertently lead to spaces being interpreted as literal plus signs.
Encoding defaults to `:www_form` for backward compatibility.
## Examples
iex> query = %{"foo" => 1, "bar" => 2}
iex> URI.encode_query(query)
iex> hd = %{"foo" => 1, "bar" => 2}
iex> URI.encode_query(hd)
"bar=2&foo=1"
iex> query = %{"key" => "value with spaces"}
iex> URI.encode_query(query)
"key=value+with+spaces"
iex> query = %{"key" => "value with spaces"}
iex> URI.encode_query(query, :rfc3986)
"key=value%20with%20spaces"
iex> URI.encode_query(%{key: [:a, :list]})
** (ArgumentError) encode_query/2 values cannot be lists, got: [:a, :list]
** (ArgumentError) encode_query/1 values cannot be lists, got: [:a, :list]
"""
@spec encode_query(Enum.t(), :rfc3986 | :www_form) :: binary
def encode_query(enumerable, encoding \\ :www_form) do
Enum.map_join(enumerable, "&", &encode_kv_pair(&1, encoding))
@spec encode_query(Enum.t()) :: binary
def encode_query(enumerable) do
Enum.map_join(enumerable, "&", &encode_kv_pair/1)
end
defp encode_kv_pair({key, _}, _encoding) when is_list(key) do
raise ArgumentError, "encode_query/2 keys cannot be lists, got: #{inspect(key)}"
defp encode_kv_pair({key, _}) when is_list(key) do
raise ArgumentError, "encode_query/1 keys cannot be lists, got: #{inspect(key)}"
end
defp encode_kv_pair({_, value}, _encoding) when is_list(value) do
raise ArgumentError, "encode_query/2 values cannot be lists, got: #{inspect(value)}"
defp encode_kv_pair({_, value}) when is_list(value) do
raise ArgumentError, "encode_query/1 values cannot be lists, got: #{inspect(value)}"
end
defp encode_kv_pair({key, value}, :rfc3986) do
encode(Kernel.to_string(key), &char_unreserved?/1) <>
"=" <> encode(Kernel.to_string(value), &char_unreserved?/1)
end
defp encode_kv_pair({key, value}, :www_form) do
defp encode_kv_pair({key, value}) do
encode_www_form(Kernel.to_string(key)) <> "=" <> encode_www_form(Kernel.to_string(value))
end
@doc """
Decodes `query` into a map.
Decodes a query string into a map.
Given a query string in the form of `key1=value1&key2=value2...`, this
function inserts each key-value pair in the query string as one entry in the
given `map`. Keys and values in the resulting map will be binaries. Keys and
values will be percent-unescaped.
You can specify one of the following `encoding` options:
* `:www_form` - (default, since v1.12.0) keys and values are decoded as per
`decode_www_form/1`. This is the format typically used by browsers on
query strings and form data. It decodes "+" as " ".
* `:rfc3986` - (since v1.12.0) keys and values are decoded as per
`decode/1`. The result is the same as `:www_form` except for leaving "+"
as is in line with [RFC 3986](https://tools.ietf.org/html/rfc3986).
Encoding defaults to `:www_form` for backward compatibility.
Use `query_decoder/1` if you want to iterate over each value manually.
## Examples
@@ -164,54 +130,43 @@ defmodule URI do
iex> URI.decode_query("percent=oh+yes%21", %{"starting" => "map"})
%{"percent" => "oh yes!", "starting" => "map"}
iex> URI.decode_query("percent=oh+yes%21", %{}, :rfc3986)
%{"percent" => "oh+yes!"}
"""
@spec decode_query(binary, %{optional(binary) => binary}, :rfc3986 | :www_form) :: %{
optional(binary) => binary
}
def decode_query(query, map \\ %{}, encoding \\ :www_form)
@spec decode_query(binary, %{optional(binary) => binary}) :: %{optional(binary) => binary}
def decode_query(query, map \\ %{})
def decode_query(query, %_{} = dict, encoding) when is_binary(query) do
IO.warn(
"URI.decode_query/3 expects the second argument to be a map, other usage is deprecated"
)
decode_query_into_dict(query, dict, encoding)
def decode_query(query, %_{} = dict) when is_binary(query) do
IO.warn("URI.decode_query/2 is deprecated, please use URI.decode_query/1")
decode_query_into_dict(query, dict)
end
def decode_query(query, map, encoding) when is_binary(query) and is_map(map) do
decode_query_into_map(query, map, encoding)
def decode_query(query, map) when is_binary(query) and is_map(map) do
decode_query_into_map(query, map)
end
def decode_query(query, dict, encoding) when is_binary(query) do
IO.warn(
"URI.decode_query/3 expects the second argument to be a map, other usage is deprecated"
)
decode_query_into_dict(query, dict, encoding)
def decode_query(query, dict) when is_binary(query) do
IO.warn("URI.decode_query/2 is deprecated, please use URI.decode_query/1")
decode_query_into_dict(query, dict)
end
defp decode_query_into_map(query, map, encoding) do
case decode_next_query_pair(query, encoding) do
defp decode_query_into_map(query, map) do
case decode_next_query_pair(query) do
nil ->
map
{{key, value}, rest} ->
decode_query_into_map(rest, Map.put(map, key, value), encoding)
decode_query_into_map(rest, Map.put(map, key, value))
end
end
defp decode_query_into_dict(query, dict, encoding) do
case decode_next_query_pair(query, encoding) do
defp decode_query_into_dict(query, dict) do
case decode_next_query_pair(query) do
nil ->
dict
{{key, value}, rest} ->
# Avoid warnings about Dict being deprecated
dict_module = Dict
decode_query_into_dict(rest, dict_module.put(dict, key, value), encoding)
decode_query_into_dict(rest, dict_module.put(dict, key, value))
end
end
@@ -221,40 +176,25 @@ defmodule URI do
Key and value in each tuple will be binaries and will be percent-unescaped.
You can specify one of the following `encoding` options:
* `:www_form` - (default, since v1.12.0) keys and values are decoded as per
`decode_www_form/1`. This is the format typically used by browsers on
query strings and form data. It decodes "+" as " ".
* `:rfc3986` - (since v1.12.0) keys and values are decoded as per
`decode/1`. The result is the same as `:www_form` except for leaving "+"
as is in line with [RFC 3986](https://tools.ietf.org/html/rfc3986).
Encoding defaults to `:www_form` for backward compatibility.
## Examples
iex> URI.query_decoder("foo=1&bar=2") |> Enum.to_list()
[{"foo", "1"}, {"bar", "2"}]
iex> URI.query_decoder("food=bread%26butter&drinks=tap%20water+please") |> Enum.to_list()
[{"food", "bread&butter"}, {"drinks", "tap water please"}]
iex> URI.query_decoder("food=bread%26butter&drinks=tap%20water+please", :rfc3986) |> Enum.to_list()
[{"food", "bread&butter"}, {"drinks", "tap water+please"}]
iex> URI.query_decoder("food=bread%26butter&drinks=tap%20water") |> Enum.to_list()
[{"food", "bread&butter"}, {"drinks", "tap water"}]
"""
@spec query_decoder(binary, :rfc3986 | :www_form) :: Enumerable.t()
def query_decoder(query, encoding \\ :www_form) when is_binary(query) do
Stream.unfold(query, &decode_next_query_pair(&1, encoding))
@spec query_decoder(binary) :: Enumerable.t()
def query_decoder(query) when is_binary(query) do
Stream.unfold(query, &decode_next_query_pair/1)
end
defp decode_next_query_pair("", _encoding) do
defp decode_next_query_pair("") do
nil
end
defp decode_next_query_pair(query, encoding) do
defp decode_next_query_pair(query) do
{undecoded_next_pair, rest} =
case :binary.split(query, "&") do
[next_pair, rest] -> {next_pair, rest}
@@ -263,24 +203,13 @@ defmodule URI do
next_pair =
case :binary.split(undecoded_next_pair, "=") do
[key, value] ->
{decode_with_encoding(key, encoding), decode_with_encoding(value, encoding)}
[key] ->
{decode_with_encoding(key, encoding), ""}
[key, value] -> {decode_www_form(key), decode_www_form(value)}
[key] -> {decode_www_form(key), ""}
end
{next_pair, rest}
end
defp decode_with_encoding(string, :www_form) do
decode_www_form(string)
end
defp decode_with_encoding(string, :rfc3986) do
decode(string)
end
@doc ~s"""
Checks if `character` is a reserved one in a URI.
@@ -371,10 +300,6 @@ defmodule URI do
@doc """
Encodes `string` as "x-www-form-urlencoded".
Note "x-www-form-urlencoded" is not specified as part of
RFC 3986. However, it is a commonly used format to encode
query strings and form data by browsers.
## Example
iex> URI.encode_www_form("put: it+й")
@@ -422,10 +347,6 @@ defmodule URI do
@doc """
Decodes `string` as "x-www-form-urlencoded".
Note "x-www-form-urlencoded" is not specified as part of
RFC 3986. However, it is a commonly used format to encode
query strings and form data by browsers.
## Examples
iex> URI.decode_www_form("%3Call+in%2F")
@@ -462,13 +383,14 @@ defmodule URI do
defp hex_to_dec(_n), do: throw(:malformed_uri)
@doc """
Parses a well-formed URI into its components.
Parses a well-formed URI reference into its components.
This function can parse both absolute and relative URLs. You can check
if a URI is absolute or relative by checking if the `scheme` field is
nil or not. Furthermore, this function expects both absolute and
relative URIs to be well-formed and does not perform any validation.
See the "Examples" section below.
Note this function expects a well-formed URI and does not perform
any validation. See the "Examples" section below for examples of how
`URI.parse/1` can be used to parse a wide range of URIs.
This function uses the parsing regular expression as defined
in [RFC 3986, Appendix B](https://tools.ietf.org/html/rfc3986#appendix-B).
When a URI is given without a port, the value returned by
`URI.default_port/1` for the URI's scheme is used for the `:port` field.
@@ -721,8 +643,16 @@ defmodule URI do
defp remove_dot_segments([head | tail], acc), do: remove_dot_segments(tail, [head | acc])
defp path_to_segments(path) do
path |> String.split("/") |> Enum.reverse()
[head | tail] = String.split(path, "/")
reverse_and_discard_empty(tail, [head])
end
defp reverse_and_discard_empty([], acc), do: acc
defp reverse_and_discard_empty([head], acc), do: [head | acc]
defp reverse_and_discard_empty(["" | tail], acc), do: reverse_and_discard_empty(tail, acc)
defp reverse_and_discard_empty([head | tail], acc),
do: reverse_and_discard_empty(tail, [head | acc])
end
defimpl String.Chars, for: URI do
@@ -8,12 +8,12 @@ Elixir applies bug fixes only to the latest minor branch. Security patches are a
Elixir version | Support
:------------- | :-----------------------------
1.12 | Bug fixes and security patches
1.11 | Security patches only
1.10 | Security patches only
1.11 | Development
1.10 | Bug fixes and security patches
1.9 | Security patches only
1.8 | Security patches only
1.7 | Security patches only
1.6 | Security patches only
New releases are announced in the read-only [announcements mailing list](https://groups.google.com/group/elixir-lang-ann). All security releases [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
@@ -39,25 +39,24 @@ The only exception to the compatibility guarantees above are experimental featur
## Compatibility between Elixir and Erlang/OTP
Erlang/OTP versioning is independent from the versioning of Elixir. Erlang releases a new major version yearly. Our goal is to support the last three Erlang major versions by the time Elixir is released. The compatibility table is shown below.
Erlang/OTP versioning is independent from the versioning of Elixir. Each version of Elixir supports a specific range of Erlang/OTP versions. The compatibility table is shown below.
Elixir version | Supported Erlang/OTP versions
:------------- | :-------------------------------
1.12 | 22 - 24
1.11 | 21 - 23 (and Erlang/OTP 24 from v1.11.4)
1.10 | 21 - 22 (and Erlang/OTP 23 from v1.10.3)
1.9 | 20 - 22
1.8 | 20 - 22
1.7 | 19 - 22
1.6 | 19 - 20 (and Erlang/OTP 21 from v1.6.6)
1.5 | 18 - 20
1.4 | 18 - 19 (and Erlang/OTP 20 from v1.4.5)
1.3 | 18 - 19
1.2 | 18 - 18 (and Erlang/OTP 19 from v1.2.6)
1.1 | 17 - 18
1.0 | 17 - 17 (and Erlang/OTP 18 from v1.0.5)
1.1 | 17 - 18
1.2 | 18 - 18 (and Erlang/OTP 19 from v1.2.6)
1.3 | 18 - 19
1.4 | 18 - 19 (and Erlang/OTP 20 from v1.4.5)
1.5 | 18 - 20
1.6 | 19 - 20 (and Erlang/OTP 21 from v1.6.6)
1.7 | 19 - 22
1.8 | 20 - 22
1.9 | 20 - 22
1.10 | 21 - 22 (and Erlang/OTP 23 from v1.10.3)
1.11 | 21 - 23
Note Elixir may add compatibility to new Erlang/OTP versions on patch releases, such as support for Erlang/OTP 20 in v1.4.5. Those releases are made for convenience and typically contain the minimum changes for Elixir to run without errors, if any changes are necessary. Only the next minor release, in this case v1.5.0, effectively leverages the new features provided by the latest Erlang/OTP release.
While Elixir often adds compatibility to new Erlang/OTP versions on released branches, such as support for Erlang/OTP 20 in v1.4.5, those releases usually contain the minimum changes for Elixir to run without errors. Only the next minor release, in this case v1.5.0, does effectively leverage the new features provided by the latest Erlang/OTP release.
## Deprecations
@@ -77,10 +76,6 @@ The first column is the version the feature was hard deprecated. The second colu
Version | Deprecated feature | Replaced by (available since)
:-------| :-------------------------------------------------- | :---------------------------------------------------------------
[v1.12] | `^^^/2` | Use `bxor/2` instead (v1.0)
[v1.12] | `@foo()` to read module attributes | Remove the parenthesis (v1.0)
[v1.12] | `use EEx.Engine` | Explicitly delegate to EEx.Engine instead (v1.0)
[v1.12] | `:xref` compiler in Mix | Nothing (it always runs as part of the compiler now)
[v1.11] | `Mix.Project.compile/2` | `Mix.Task.run("compile", args)` (v1.0)
[v1.11] | `Supervisor.Spec.worker/3` and `Supervisor.Spec.supervisor/3` | The new child specs outlined in `Supervisor` (v1.5)
[v1.11] | `Supervisor.start_child/2` and `Supervisor.terminate_child/2` | `DynamicSupervisor` (v1.6)
@@ -97,7 +92,6 @@ Version | Deprecated feature | Replaced by (ava
[v1.9] | Enumerable keys in `Map.drop/2`, `Map.split/2`, and `Map.take/2` | Call `Enum.to_list/1` on the second argument before hand (v1.0)
[v1.9] | `Mix.Project.load_paths/1` | `Mix.Project.compile_path/1` (v1.0)
[v1.9] | Passing `:insert_replaced` to `String.replace/4` | Use `:binary.replace/4` (v1.0)
[v1.8] | Passing a non-empty list to `Collectable.into/1` | `Kernel.++/2` or `Keyword.merge/2` (v1.0)
[v1.8] | Passing a non-empty list to `:into` in [`for`](`Kernel.SpecialForms.for/1`) | `Kernel.++/2` or `Keyword.merge/2` (v1.0)
[v1.8] | Passing a non-empty list to `Enum.into/2` | `Kernel.++/2` or `Keyword.merge/2` (v1.0)
[v1.8] | Time units in its plural form, such as: `:seconds`, `:milliseconds`, and the like | Use the singular form, such as: `:second`, `:millisecond`, and so on (v1.4)
@@ -111,7 +105,7 @@ Version | Deprecated feature | Replaced by (ava
[v1.8] | `System.cwd/0` and `System.cwd!/0` | `File.cwd/0` and `File.cwd!/0` (v1.0)
[v1.7] | `Code.get_docs/2` | `Code.fetch_docs/1` (v1.7)
[v1.7] | `Enum.chunk/2,3,4` | `Enum.chunk_every/2` and [`Enum.chunk_every/3,4`](`Enum.chunk_every/4`) (v1.5)
[v1.7] | Calling `super/1` in`GenServer` callbacks | Implementing the behaviour explicitly without calling `super/1` (v1.0)
[v1.7] | Calling `super/1` in`GenServer` callbacks | Implenting the behaviour explicitly without calling `super/1` (v1.0)
[v1.7] | [`not left in right`](`Kernel.in/2`) | [`left not in right`](`Kernel.in/2`) (v1.5)
[v1.7] | `Registry.start_link/3` | `Registry.start_link/1` (v1.5)
[v1.7] | `Stream.chunk/2,3,4` | `Stream.chunk_every/2` and [`Stream.chunk_every/3,4`](`Stream.chunk_every/4`) (v1.5)
@@ -124,7 +118,7 @@ Version | Deprecated feature | Replaced by (ava
[v1.5] | `Atom.to_char_list/1` | `Atom.to_charlist/1` (v1.3)
[v1.5] | `Enum.filter_map/3` | `Enum.filter/2` + `Enum.map/2` or [`for`](`Kernel.SpecialForms.for/1`) comprehensions (v1.0)
[v1.5] | `Float.to_char_list/1` | `Float.to_charlist/1` (v1.3)
[v1.5] | `GenEvent` module | `Supervisor` and `GenServer` (v1.0);<br/>[`GenStage`](https://hex.pm/packages/gen_stage) (v1.3);<br/>[`:gen_event`](`:gen_event`) (Erlang/OTP 17)
[v1.5] | `GenEvent` module | `Supervisor` and `GenServer` (v1.0);<br/>[`GenStage`](https://hex.pm/packages/gen_stage) (v1.3);<br/>[`:gen_event`](http://www.erlang.org/doc/man/gen_event.html) (Erlang/OTP 17)
[v1.5] | `<%=` in middle and end expressions in `EEx` | Use `<%` (`<%=` is allowed only in start expressions) (v1.0)
[v1.5] | `:as_char_lists` value in `t:Inspect.Opts.t/0` type | `:as_charlists` value (v1.3)
[v1.5] | `:char_lists` key in `t:Inspect.Opts.t/0` type | `:charlists` key (v1.3)
@@ -181,4 +175,3 @@ Version | Deprecated feature | Replaced by (ava
[v1.9]: https://github.com/elixir-lang/elixir/blob/v1.9/CHANGELOG.md#4-hard-deprecations
[v1.10]: https://github.com/elixir-lang/elixir/blob/v1.10/CHANGELOG.md#4-hard-deprecations
[v1.11]: https://github.com/elixir-lang/elixir/blob/v1.11/CHANGELOG.md#4-hard-deprecations
[v1.12]: https://github.com/elixir-lang/elixir/blob/v1.12/CHANGELOG.md#4-hard-deprecations
+2 -18
View File
@@ -34,14 +34,6 @@ Writing code is only the first of many steps to publish a package. We strongly r
Projects are often made available to other developers [by publishing a Hex package](https://hex.pm/docs/publish). Hex also [supports private packages for organizations](https://hex.pm/pricing). If ExDoc is configured for the Mix project, publishing a package on Hex will also automatically publish the generated documentation to [HexDocs](https://hexdocs.pm).
## Dependency handling
When your library is published and used as a dependency, its [lockfile](https://hexdocs.pm/mix/Mix.Project.html#module-configuration) (usually named `mix.lock`) is _ignored by the host project_. Running `mix deps.get` in the host project attempts to get the latest possible versions of your library’s dependencies, as specified by the requirements in the `deps` section of your `mix.exs`. These versions might be greater than those stored in your `mix.lock` (and hence used in your tests / CI).
On the other hand, contributors of your library, need a deterministic build, which implies the presence of `mix.lock` in your Version Control System (VCS).
The best practice of handling `mix.lock` file therefore would be to keep it in VCS, and run two different Continuous Integration (CI) workflows: the usual deterministic one, and another one, that starts with `mix deps.unlock --all` and always compiles your library and runs tests against latest versions of dependencies. The latter one might be even run nightly or otherwise recurrently to stay notified about any possible issue in regard to dependencies updates.
## Anti-patterns
In this section we document common anti-patterns to avoid when writing libraries.
@@ -178,15 +170,7 @@ end
That's because by reading the application in the module body and storing it in a module attribute, we are effectively reading the configuration at compile-time, which may become an issue when configuring the system later.
If, for some reason, you must read the application environment at compile time, use `Application.compile_env/2`. Read [the "Compile-time environment" section of the `Application` module documentation](Application.html#module-compile-time-environment) for more information.
### Avoid defining modules that are not in your "namespace"
Even though Elixir does not formally have the concept of namespaces, a library should use its name as a "prefix" for all of its modules (except for special cases like mix tasks). For example if the library's OTP application name is `:my_lib`, then all of its modules should start with the `MyLib` prefix, for example `MyLib.User`, `MyLib.SubModule`, and `MyLib.Application`.
This is important because the Erlang VM can only load one instance of a module at a time. So if there are multiple libraries that define the same module, then they are incompatible with each other due to this limitation. By always using the library name as a prefix, it avoids module name clashes due to the unique prefix.
Furthermore, when writing a library that is an extension of another library, you should avoid defining modules inside the parent's library namespace. For example, if you are writing a package that adds authentication to [`Plug`](https://github.com/elixir-plug/plug) called `plug_auth`, its modules should be namespaced under `PlugAuth` instead of `Plug.Auth`, so it avoid conflicts with `Plug` if it were to ever define its own authentication functionality.
If, for some reason, you must read the application environment at compile time, use `Application.compile_env/2`. Read [the "Compile-time environment" section of the Application docs](Application.html#module-compile-time-environment) for more information.
### Avoid `use` when an `import` is enough
@@ -234,7 +218,7 @@ While there are situations where `use SomeModule` is necessary, `use` should be
Although the previous section could be summarized as "avoid macros", both topics are important enough to deserve their own sections.
To quote [the official guide on macros](https://elixir-lang.org/getting-started/meta/macros.html):
To quote [the official guide on Macros](https://elixir-lang.org/getting-started/meta/macros.html):
> Even though Elixir attempts its best to provide a safe environment for macros, the major responsibility of writing clean code with macros falls on developers. Macros are harder to write than ordinary Elixir functions and it's considered to be bad style to use them when they're not necessary. So write macros responsibly.
>
+54 -24
View File
@@ -6,27 +6,28 @@ This document covers operators in Elixir, how they are parsed, how they can be d
The following is a list of all operators that Elixir is capable of parsing, ordered from higher to lower precedence, alongside their associativity:
Operator | Associativity
----------------------------------------------------- | -------------
`@` | Unary
`.` | Left
`+` `-` `!` `^` `not` `~~~` | Unary
`*` `/` | Left
`+` `-` | Left
`++` `--` `+++` `---` `..` `<>` | Right
`in` `not in` | Left
`\|>` `<<<` `>>>` `<<~` `~>>` `<~` `~>` `<~>` `<\|>` | Left
`<` `>` `<=` `>=` | Left
`==` `!=` `=~` `===` `!==` | Left
`&&` `&&&` `and` | Left
`\|\|` `\|\|\|` `or` | Left
`=` | Right
`&` | Unary
`=>` (valid only inside `%{}`) | Right
`\|` | Right
`::` | Right
`when` | Right
`<-` `\\` | Left
Operator | Associativity
---------------------------------------------------------------------------------------- | -------------
`@` | Unary
`.` | Left to right
`+` `-` `!` `^` `not` `~~~` | Unary
`*` `/` | Left to right
`+` `-` | Left to right
`++` `--` `..` `<>` `+++` `---` | Right to left
`^^^` | Left to right
`in` `not in` | Left to right
`\|>` `<<<` `>>>` `<<~` `~>>` `<~` `~>` `<~>` `<\|>` | Left to right
`<` `>` `<=` `>=` | Left to right
`==` `!=` `=~` `===` `!==` | Left to right
`&&` `&&&` `and` | Left to right
`\|\|` `\|\|\|` `or` | Left to right
`=` | Right to left
`&` | Unary
`=>` (valid syntax only inside `%{}`) | Right to left
`\|` | Right to left
`::` | Right to left
`when` | Right to left
`<-` `\\` | Left to right
## General operators
@@ -154,12 +155,41 @@ The following is a table of all the operators that Elixir is capable of parsing,
* `~>`
* `<~>`
* `<|>`
* `^^^`
* `+++`
* `---`
* `~~~`
The following operators are used by the `Bitwise` module when imported: [`&&&`](`Bitwise.&&&/2`), [`<<<`](`Bitwise.<<</2`), [`>>>`](`Bitwise.>>>/2`), [`|||`](`Bitwise.|||/2`), [`~~~`](`Bitwise.~~~/1`). See the documentation for `Bitwise` for more information.
The following operators are used by the `Bitwise` module when imported: [`&&&`](`Bitwise.&&&/2`), [`^^^`](`Bitwise.^^^/2`), [`<<<`](`Bitwise.<<</2`), [`>>>`](`Bitwise.>>>/2`), [`|||`](`Bitwise.|||/2`), [`~~~`](`Bitwise.~~~/1`). See the documentation for `Bitwise` for more information.
Note the Elixir community generally discourages custom operators. They can be hard to read and even more to understand, as they don't have a descriptive name like functions do. That said, some specific cases or custom domain specific languages (DSLs) may justify these practices.
### Redefining existing operators
It is also possible replace predefined operators, such as `+`, but doing so is extremely discouraged.
The operators that Elixir uses (for example, `+`) can be defined by any module and used in place of the ones defined by Elixir, provided they're specifically not imported from `Kernel` (which is imported everywhere by default). For example:
```elixir
defmodule WrongMath do
# Let's make math wrong by changing the meaning of +:
def a + b, do: a - b
end
```
Now, we will get an error if we try to use this operator "out of the box":
```elixir
iex> import WrongMath
iex> 1 + 2
** (CompileError) iex:11: function +/2 imported from both WrongMath and Kernel, call is ambiguous
```
So, as mentioned above, we need to explicitly *not* import `+/2` from `Kernel`:
```elixir
iex> import WrongMath
iex> import Kernel, except: [+: 2]
iex> 1 + 2
-1
```
### Final note
While it's possible to define unused operators (such as `<~>`) and to "override" predefined operators (such as `+`), the Elixir community generally discourages this. Custom-defined operators can be really hard to read and even more to understand, as they don't have a descriptive name like functions do. That said, some specific cases or custom domain specific languages (DSLs) may justify these practices.
+3 -3
View File
@@ -1,12 +1,12 @@
# Patterns and Guards
Elixir provides pattern matching, which allows us to assert on the shape or extract values from data structures. Patterns are often augmented with guards, which give developers the ability to perform more complex checks, albeit limited.
Elixir provides pattern matching, which allows us to assert on the shape or extract values from data-structures. Patterns are often augmented with guards, which give developers the ability to perform more complex checks, albeit limited.
This page describes the semantics of patterns and guards, where they are all allowed, and how to extend them.
## Patterns
Patterns in Elixir are made of variables, literals, and data structure specific syntax. One of the most used constructs to perform pattern matching is the match operator ([`=`](`=/2`)):
Patterns in Elixir are made of variables, literals, and data-structure specific syntax. One of the most used constructs to perform pattern matching is the match operator ([`=`](`=/2`)):
```iex
iex> x = 1
@@ -33,7 +33,7 @@ iex> 1 = y
In other words, patterns are allowed only on the left side of `=`. The right side of `=` follows the regular evaluation semantics of the language.
Now let's cover the pattern matching rules for each construct and then for each relevant data types.
Now let's cover the pattern matching rules for each construct and then for each relevant data-types.
### Variables
+5 -13
View File
@@ -21,7 +21,7 @@ Integers (`1234`) and floats (`123.4`) in Elixir are represented as a sequence o
### Atoms
Unquoted atoms start with a colon (`:`) which must be immediately followed by a Unicode letter or an underscore. The atom may continue using a sequence of Unicode letters, numbers, underscores, and `@`. Atoms may end in `!` or `?`. See [Unicode syntax](unicode-syntax.md) for a formal specification. Valid unquoted atoms are: `:ok`, `:ISO8601`, and `:integer?`.
Unquoted atoms start with a colon (`:`) which must be immediately followed by an underscore or a Unicode letter. The atom may continue using a sequence of Unicode letters, numbers, underscores, and `@`. Atoms may end in `!` or `?`. See [Unicode Syntax](unicode-syntax.md) for a formal specification. Valid unquoted atoms are: `:ok`, `:ISO8601`, and `:integer?`.
If the colon is immediately followed by a pair of double- or single-quotes surrounding the atom name, the atom is considered quoted. In contrast with an unquoted atom, this one can be made of any Unicode character (not only letters), such as `:'🌢 Elixir'`, `:"++olá++"`, and `:"123"`.
@@ -84,13 +84,13 @@ Structs built on the map syntax by passing the struct name between `%` and `{`.
### Variables
Variables in Elixir must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The variable may continue using a sequence of Unicode letters, numbers, and underscores. Variables may end in `?` or `!`. See [Unicode syntax](unicode-syntax.md) for a formal specification.
Variables in Elixir must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The variable may continue using a sequence of Unicode letters, numbers, and underscores. Variables may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.md) for a formal specification.
[Elixir's naming conventions](naming-conventions.md) recommend variables to be in `snake_case` format.
### Non-qualified calls (local calls)
Non-qualified calls, such as `add(1, 2)`, must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The call may continue using a sequence of Unicode letters, numbers, and underscore. Calls may end in `?` or `!`. See [Unicode syntax](unicode-syntax.md) for a formal specification.
Non-qualified calls, such as `add(1, 2)`, must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The call may continue using a sequence of Unicode letters, numbers, and underscore. Calls may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.md) for a formal specification.
Parentheses for non-qualified calls are optional, except for zero-arity calls, which would then be ambiguous with variables. If parentheses are used, they must immediately follow the function name *without spaces*. For example, `add (1, 2)` is a syntax error, since `(1, 2)` is treated as an invalid block which is attempted to be given as a single argument to `add`.
@@ -102,7 +102,7 @@ As many programming languages, Elixir also support operators as non-qualified ca
### Qualified calls (remote calls)
Qualified calls, such as `Math.add(1, 2)`, must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The call may continue using a sequence of Unicode letters, numbers, and underscores. Calls may end in `?` or `!`. See [Unicode syntax](unicode-syntax.md) for a formal specification.
Qualified calls, such as `Math.add(1, 2)`, must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The call may continue using a sequence of Unicode letters, numbers, and underscores. Calls may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.md) for a formal specification.
[Elixir's naming conventions](naming-conventions.md) recommend calls to be in `snake_case` format.
@@ -425,15 +425,7 @@ end
The above is treated the same as `sum(1, 2, 3)` by the parser.
The same applies to qualified calls such as `Foo.bar(1, 2, 3)`, which is equivalent to `Foo.bar 1, 2, 3`. There are, however, some situations where parentheses are required:
* when calling anonymous functions, such as `f.(1, 2)`;
* for non-qualified calls with no arguments, such as `sum()`. Removing the parentheses for `sum` causes it to be represented as the variable `sum`;
* for dynamic qualified calls with no arguments. `data.key` means accessing a field named `key` in the map given by `data`. `mod.fun()`, with parens, means calling a function named `fun` in the module `mod`;
In practice, developers prefer to add parentheses to most of their calls. They are skipped mainly in Elixir's control-flow constructs, such as `defmodule`, `if`, `case`, etc, and in certain DSLs.
The same applies to qualified calls such as `Foo.bar(1, 2, 3)`, which is the same as `Foo.bar 1, 2, 3`. However, remember parentheses are not optional for non-qualified calls with no arguments, such as `sum()`. Removing the parentheses for `sum` causes it to be represented as the variable `sum`, which means they would be no longer equivalent.
### Keywords
+9 -9
View File
@@ -2,8 +2,8 @@
Elixir comes with a notation for declaring types and specifications. Elixir is a dynamically typed language, and as such, type specifications are never used by the compiler to optimize or modify code. Still, using type specifications is useful because:
* they provide documentation (for example, tools such as [`ExDoc`](https://hexdocs.pm/ex_doc/) show type specifications in the documentation)
* they're used by tools such as [Dialyzer](`:dialyzer`), that can analyze code with typespec to find type inconsistencies and possible bugs
* they provide documentation (for example, tools such as [ExDoc](https://github.com/elixir-lang/ex_doc) show type specifications in the documentation)
* they're used by tools such as [Dialyzer](http://www.erlang.org/doc/man/dialyzer.html), that can analyze code with typespec to find type inconsistencies and possible bugs
Type specifications (sometimes referred to as *typespecs*) are defined in different contexts using the following attributes:
@@ -36,7 +36,7 @@ In the example above, this happens:
## Types and their syntax
The syntax Elixir provides for type specifications is similar to [the one in Erlang](https://erlang.org/doc/reference_manual/typespec.html). Most of the built-in types provided in Erlang (for example, `pid()`) are expressed in the same way: `pid()` (or simply `pid`). Parameterized types (such as `list(integer)`) are supported as well and so are remote types (such as `Enum.t`). Integers and atom literals are allowed as types (for example, `1`, `:atom`, or `false`). All other types are built out of unions of predefined types. Some shorthands are allowed, such as `[...]`, `<<>>`, and `{...}`.
The syntax Elixir provides for type specifications is similar to [the one in Erlang](http://www.erlang.org/doc/reference_manual/typespec.html). Most of the built-in types provided in Erlang (for example, `pid()`) are expressed in the same way: `pid()` (or simply `pid`). Parameterized types (such as `list(integer)`) are supported as well and so are remote types (such as `Enum.t`). Integers and atom literals are allowed as types (for example, `1`, `:atom`, or `false`). All other types are built out of unions of predefined types. Some shorthands are allowed, such as `[...]`, `<<>>`, and `{...}`.
The notation to represent the union of types is the pipe `|`. For example, the typespec `type :: atom() | pid() | tuple()` creates a type `type` that can be either an `atom`, a `pid`, or a `tuple`. This is usually called a [sum type](https://en.wikipedia.org/wiki/Tagged_union) in other languages
@@ -59,12 +59,12 @@ The notation to represent the union of types is the pipe `|`. For example, the t
| non_neg_integer() # 0, 1, 2, 3, ...
| pos_integer() # 1, 2, 3, ...
## Lists
| list(type) # proper list ([]-terminated)
| nonempty_list(type) # non-empty proper list
| maybe_improper_list(content_type, termination_type) # proper or improper list
| nonempty_improper_list(content_type, termination_type) # improper list
| nonempty_maybe_improper_list(content_type, termination_type) # non-empty proper or improper list
## Lists
| list(type) # proper list ([]-terminated)
| nonempty_list(type) # non-empty proper list
| maybe_improper_list(type1, type2) # proper or improper list
| nonempty_improper_list(type1, type2) # improper list
| nonempty_maybe_improper_list(type1, type2) # non-empty proper or improper list
| Literals # Described in section "Literals"
| BuiltIn # Described in section "Built-in types"
+7 -9
View File
@@ -18,13 +18,13 @@ where `<Start>` uses the same categories as the spec but restricts them to the N
> characters derived from the Unicode General Category of uppercase letters, lowercase letters, titlecase letters, modifier letters, other letters, letter numbers, plus `Other_ID_Start`, minus `Pattern_Syntax` and `Pattern_White_Space` code points
>
> In set notation: `[\p{L}\p{Nl}\p{Other_ID_Start}-\p{Pattern_Syntax}-\p{Pattern_White_Space}]`.
> In set notation: `[\p{L}\p{Nl}\p{Other_ID_Start}-\p{Pattern_Syntax}-\p{Pattern_White_Space}]`
and `<Continue>` uses the same categories as the spec but restricts them to the NFC form (see R6):
> ID_Start characters, plus characters having the Unicode General Category of nonspacing marks, spacing combining marks, decimal number, connector punctuation, plus `Other_ID_Continue`, minus `Pattern_Syntax` and `Pattern_White_Space` code points.
>
> In set notation: `[\p{ID_Start}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Other_ID_Continue}-\p{Pattern_Syntax}-\p{Pattern_White_Space}]`.
> In set notation: `[\p{ID_Start}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Other_ID_Continue}-\p{Pattern_Syntax}-\p{Pattern_White_Space}]`
`<Ending>` is an addition specific to Elixir that includes only the code points `?` (003F) and `!` (0021).
@@ -36,19 +36,17 @@ Elixir does not allow the use of ZWJ or ZWNJ in identifiers and therefore does n
Unicode atoms in Elixir follow the identifier rule above with the following modifications:
* `<Start>` additionally includes the code point `_` (005F)
* `<Continue>` additionally includes the code point `@` (0040)
* `<Start>` includes the code point `_` (005F)
* `<Continue>` includes the code point `@` (0040)
Note atoms can also be quoted, which allows any characters, such as `:"hello elixir"`. All Elixir operators are also valid atoms (`:+`, `:@`, `:|>`, etc.). The full description of valid atoms is available in the ["Atoms" section in the syntax reference](syntax-reference.html#atoms).
> Note that all Elixir operators are also valid atoms. Therefore `:+`, `:@`, `:|>`, and others are all valid atoms. The full description of valid atoms is available in the Syntax Reference, this document covers only the rules for identifier-based atoms.
### Variables
Variables in Elixir follow the identifier rule above with the following modifications:
* `<Start>` additionally includes the code point `_` (005F)
* `<Start>` additionally excludes Lu (letter uppercase) and Lt (letter titlecase) characters
In set notation: `[\u{005F}\p{Ll}\p{Lm}\p{Lo}\p{Nl}\p{Other_ID_Start}-\p{Pattern_Syntax}-\p{Pattern_White_Space}]`.
* `<Start>` includes the code point `_` (005F)
* `<Start>` must not include Lu (letter uppercase) and Lt (letter titlecase) characters
## R3. Pattern_White_Space and Pattern_Syntax Characters
+1 -1
View File
@@ -138,7 +138,7 @@ However, keep in mind `@moduledoc false` or `@doc false` do not make a function
* Move the undocumented function to a module with `@moduledoc false`, like `MyApp.Hidden`, ensuring the function won't be accidentally exposed or imported. Remember you can use `@moduledoc false` to hide a whole module and still document each function with `@doc`. Tools will still ignore the module.
* Start the function name with one or two underscores, for example, `__add__/2`. Functions starting with underscore are automatically treated as hidden, although you can also be explicit and add `@doc false`. The compiler does not import functions with leading underscores and they hint to anyone reading the code of their intended private usage.
* Start the function name with one or two underscores, for example, `__add__/2`. Functions starting with underscore are autoamtically treated as hidden, although you can also be explicit and add `@doc false`. The compiler does not import functions with leading underscores and they hint to anyone reading the code of their intended private usage.
## Code.fetch_docs/1
+2 -12
View File
@@ -1,18 +1,8 @@
# Returns config for Elixir docs
canonical = System.fetch_env!("CANONICAL")
[
extras: Path.wildcard("lib/elixir/pages/*.md") ++ ["CHANGELOG.md"],
deps: [
eex: "https://hexdocs.pm/eex/#{canonical}",
ex_unit: "https://hexdocs.pm/ex_unit/#{canonical}",
iex: "https://hexdocs.pm/iex/#{canonical}",
logger: "https://hexdocs.pm/logger/#{canonical}",
mix: "https://hexdocs.pm/mix/#{canonical}"
],
groups_for_functions: [
Guards: &(&1[:guard] == true)
Guards: & &1[:guard] == true
],
skip_undefined_reference_warnings_on: ["lib/elixir/pages/compatibility-and-deprecations.md"],
groups_for_modules: [
@@ -63,7 +53,7 @@ canonical = System.fetch_env!("CANONICAL")
StringIO,
System
],
Calendar: [
"Calendar": [
Calendar,
Calendar.ISO,
Calendar.TimeZoneDatabase,
+3 -3
View File
@@ -111,10 +111,10 @@ preload_common_modules() ->
parse_otp_release() ->
%% Whenever we change this check, we should also change Makefile.
case string:to_integer(erlang:system_info(otp_release)) of
{Num, _} when Num >= 22 ->
{Num, _} when Num >= 21 ->
Num;
_ ->
io:format(standard_error, "ERROR! Unsupported Erlang/OTP version, expected Erlang/OTP 22+~n", []),
io:format(standard_error, "ERROR! Unsupported Erlang/OTP version, expected Erlang/OTP 21+~n", []),
erlang:halt(1)
end.
@@ -252,7 +252,7 @@ eval_forms(Tree, RawBinding, OE) ->
case Erl of
{atom, _, Atom} ->
{Atom, RawBinding, NewE};
{Atom, Binding, NewE};
_ ->
Exprs =
+8
View File
@@ -30,3 +30,11 @@
warn_on_unnecessary_quotes=true,
warnings=[]
}).
%% TODO: Remove this once we support Erlang/OTP 22+ exclusively.
%% See https://github.com/erlang/otp/pull/1972
-if(?OTP_RELEASE >= 22).
-define(NO_SPAWN_COMPILER_PROCESS, no_spawn_compiler_process).
-else.
-define(NO_SPAWN_COMPILER_PROCESS, dialyzer, no_spawn_compiler_process).
-endif.
+14 -37
View File
@@ -76,37 +76,20 @@ expand({'__aliases__', _Meta, List}, _Aliases, _E) ->
%% Ensure a module is loaded before its usage.
ensure_loaded(_Meta, 'Elixir.Kernel', _E) -> ok;
ensure_loaded(Meta, Module, E) ->
case code:ensure_loaded(Module) of
{module, Module} ->
ok;
_ ->
case wait_for_module(Module) of
found ->
ok;
Wait ->
Kind = case lists:member(Module, ?key(E, context_modules)) of
true ->
case ?key(E, module) of
Module -> circular_module;
_ -> scheduled_module
end;
false when Wait == deadlock ->
deadlock_module;
false ->
unloaded_module
end,
elixir_errors:form_error(Meta, E, ?MODULE, {Kind, Module})
end
end.
wait_for_module(Module) ->
case is_pid(erlang:get(elixir_compiler_pid)) of
true -> 'Elixir.Kernel.ErrorHandler':ensure_compiled(Module, module, hard);
false -> not_found
ensure_loaded(Meta, Ref, E) ->
try
Ref:module_info(module)
catch
error:undef ->
Kind = case lists:member(Ref, ?key(E, context_modules)) of
true ->
case ?key(E, module) of
Ref -> circular_module;
_ -> scheduled_module
end;
false -> unloaded_module
end,
elixir_errors:form_error(Meta, E, ?MODULE, {Kind, Ref})
end.
%% Receives an atom and returns the last bit as an alias.
@@ -161,12 +144,6 @@ lookup(Else, Dict, Counter) ->
format_error({unloaded_module, Module}) ->
io_lib:format("module ~ts is not loaded and could not be found", [inspect(Module)]);
format_error({deadlock_module, Module}) ->
io_lib:format("module ~ts is not loaded and could not be found. "
"This may be happening because the module you are trying to load "
"directly or indirectly depends on the current module",
[inspect(Module)]);
format_error({scheduled_module, Module}) ->
io_lib:format(
"module ~ts is not loaded but was defined. This happens when you depend on "
+1 -6
View File
@@ -1,6 +1,6 @@
-module(elixir_config).
-compile({no_auto_import, [get/1]}).
-export([new/1, delete/1, put/2, get/1, get/2, update/2, get_and_put/2, warn/2, serial/1]).
-export([new/1, delete/1, put/2, get/1, get/2, update/2, get_and_put/2, warn/2]).
-export([start_link/0, init/1, handle_call/3, handle_cast/2]).
-behaviour(gen_server).
@@ -25,9 +25,6 @@ get_and_put(Key, Value) ->
update(Key, Fun) ->
gen_server:call(?MODULE, {update, Key, Fun}).
serial(Fun) ->
gen_server:call(?MODULE, {serial, Fun}).
%% Used to guarantee warnings are emitted only once per caller.
warn(Key, [{Mod, Fun, ArgsOrArity, _} | _]) ->
EtsKey = {warn, Key, Mod, Fun, to_arity(ArgsOrArity)},
@@ -57,8 +54,6 @@ start_link() ->
init(Tab) ->
{ok, Tab}.
handle_call({serial, Fun}, _From, Tab) ->
{reply, Fun(), Tab};
handle_call({put, Key, Value}, _From, Tab) ->
ets:insert(Tab, {Key, Value}),
{reply, ok, Tab};
+12 -2
View File
@@ -59,7 +59,11 @@ fetch_definitions(File, Module) ->
error:badarg -> []
end,
fetch_definition(Entries, File, Module, Set, Bag, [], []).
{All, Private} = fetch_definition(Entries, File, Module, Set, Bag, [], []),
Unreachable = elixir_locals:warn_unused_local(File, Module, All, Private),
elixir_locals:ensure_no_undefined_local(File, Module, All),
elixir_locals:ensure_no_import_conflict(File, Module, All),
{All, Unreachable}.
fetch_definition([Tuple | T], File, Module, Set, Bag, All, Private) ->
[{_, Kind, Meta, _, Check, {MaxDefaults, _, Defaults}}] = ets:lookup(Set, {def, Tuple}),
@@ -79,7 +83,7 @@ fetch_definition([Tuple | T], File, Module, Set, Bag, All, Private) ->
fetch_definition(T, File, Module, Set, Bag, NewAll, NewPrivate)
catch
error:badarg ->
elixir_errors:form_error(Meta, File, ?MODULE, {function_head, Kind, Tuple}),
check_bodiless_function(Check, Meta, File, Module, Kind, Tuple),
fetch_definition(T, File, Module, Set, Bag, All, Private)
end;
@@ -338,6 +342,12 @@ check_valid_defaults(Meta, File, Name, Arity, Kind, 0, _, LastDefaults, true, tr
check_valid_defaults(_Meta, _File, _Name, _Arity, _Kind, 0, _StoredDefaults, _LastDefaults, _HasBody, _LastHasBody) ->
ok.
check_bodiless_function(Check, _Meta, _File, Module, _Kind, _Tuple)
when Check == false; Module == 'Elixir.Module' ->
ok;
check_bodiless_function(_Check, Meta, File, _Module, Kind, Tuple) ->
elixir_errors:form_error(Meta, File, ?MODULE, {function_head, Kind, Tuple}).
check_args_for_function_head(Meta, Args, E) ->
[begin
elixir_errors:form_error(Meta, E, ?MODULE, invalid_args_for_function_head)

Some files were not shown because too many files have changed in this diff Show More