Compare commits

..
111 Commits
Author SHA1 Message Date
José Valim c2a9c93f02 Release v1.6.2 2018-02-28 10:35:19 +01:00
José Valim d896f541f1 Allow subdirectories in .formatter.exs (#7398)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-02-28 10:24:23 +01:00
José Valim f033672fa8 Update CHANGELOG 2018-02-27 22:54:33 +01:00
José Valim 717c72e71b Ensure module is loaded before function exported check
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-02-24 12:33:01 +01:00
José Valim 9ac830f56b Include soft deprecations in the CHANGELOG 2018-02-24 11:54:34 +01:00
Unai Esteibar 0ded67dd60 Don't remove docs for previous function declaration on @impl true (#7383)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-02-23 20:32:15 +01:00
José Valim abbf173da7 Do not talk about tuple child_spec before they are introduced
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-02-21 09:27:02 +01:00
Jens Fischer fc1c303481 Fix DynamicSupervisor error report when restarting child (#7377)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-02-21 09:27:00 +01:00
José Valim 5e1e2e6a83 Inject less code when reading @attribute 2018-02-20 19:42:29 +01:00
Florian Ebeling 77d708e1bc Improve documentation for ExUnit.Callbacks (#7338)
The change refines and extends the documentation for the ExUnit
callbacks for test setup, "setup" and "setup_all".

* refer to the introductory Context section in function documentation
* name various ways to define setup code using atom naming unary
  function, list of atoms naming unary functions and block.
* clarify that multiple setup callbacks can be used
* reduce two overly similar paragraphs about return values into a
  single one

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-02-20 12:35:09 +01:00
José Valim cb0f900b1c Document defoverridable behaviour regarding compilation callbacks
Closes #7340

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-02-20 12:35:08 +01:00
jvf 815420ce0f Fix DynamicSupervisor handling of extra arguments on child restart (#7371)
Fixes #7369

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-02-20 12:34:24 +01:00
José Valim cba11d6941 Ensure -> does not pass line lengths, closes #7279 2018-02-10 20:38:46 +01:00
José Valim 966f8f74ae Bring add_doc/6 back, closes #7335 2018-02-10 11:07:30 +01:00
Michał Muskała 55241d92e2 Clarify behaviour of failing gaurds (#7334)
[ci skip]
2018-02-09 18:38:11 +01:00
José Valim 11086ac450 Include the environment in missing dep error message, closes #7331 2018-02-09 18:38:07 +01:00
José Valim 0c81857622 Consistently raise for missing do option in definitions
Closes #7333.
2018-02-09 17:07:12 +01:00
Michał Muskała bd0a2e8a12 Bring back Mix.Shell.cmd/2
It was not brought back when Mix.Shell.cmd/3 was "undeprecated" in b2e5b69.

Signed-off-by: Michał Muskała <michal@muskala.eu>
2018-02-08 23:33:21 +01:00
José Valim df50dca83a Use tags on windows/unix and allow failures on appveyor (#7320) 2018-02-08 20:24:43 +01:00
José Valim f0969f0aa0 Only cache deps opts when the root formatter is used 2018-02-08 20:24:39 +01:00
José Valim ad489d7d80 Improve the docs for mix format
Closes #7327.
2018-02-08 19:12:08 +01:00
José Valim f8afbf3321 Publicly document child_spec/1 2018-02-08 14:51:37 +01:00
Pavel Platto 3b5f10874f Add is_integer/1 guard for argument "n" in Stream.drop/2 (#7315) 2018-02-06 17:58:57 +01:00
Raphael Costa e17b2470c6 Fixed eprof to work with multiple processes (#7317) 2018-02-06 16:37:49 +01:00
Bram Verburg 8a1c5c38d0 Fix DynamicSupervisor error report 2018-02-06 16:36:41 +01:00
José Valim 00deab98bf Update CHANGELOG 2018-02-03 01:40:12 -02:00
Ross Kaffenberger 47f4721c0b Update doc for DynamicSupervisor.start_child (#7299)
The previous example made it seem like the arguments to
`def start_child` where being dropped by `spec = MyWorker`.
2018-02-01 22:14:08 -02:00
José Valim beb69232c4 Properly format warning_test.exs 2018-02-01 14:08:19 -02:00
José Valim 4b43c7ae20 Do not change code invocation semantics in guards 2018-02-01 11:38:29 -02:00
José Valim ca49a53ab2 Consistently preserve the user choice in regards to parens, closes #7286 2018-01-31 13:50:30 -02:00
Paweł Chmielowski 1ff0e1ac7a Teach Mix erlang compiler alternative spelling for -behavior declaration (#7290)
Erlang recognizes both -behaviour and -behavior, but mix was only able to
process one, which made erlang compiler complain about missing behaviour
when source file was using the other one
2018-01-31 13:41:51 -02:00
José Valim ae66125f2e Update dynamic_supervisor.ex
Closes #7291
2018-01-31 13:36:17 -02:00
José Valim 3025cc759b Improve docs for dynamic and task supervisors 2018-01-31 13:35:49 -02:00
José Valim 8f18a5f4ff Use Supervisor.init/2 instead of supervise/2 with the new child specs 2018-01-31 13:35:41 -02:00
Benjamin Tan Wei Hao 1626f9659b Use the new child specification format in DynamicSupervisor docs
Since `Supervisor.Spec.worker/3` is deprecated (#7244)
2018-01-31 13:35:36 -02:00
Frank Hunleth 9083937177 Add missing & in port zombie script (#7287)
This is needed to background the processing being run so that control
could move on to the read loop.
2018-01-31 07:26:02 -02:00
Xavier Noria da90bb741e Defines the prep_stop/1 optional callback for applications (#7289) 2018-01-31 07:25:58 -02:00
Daniel Kempkens 51d56274de Re-add missing Supervisor behaviour (#7288)
Closes #7285.
2018-01-31 07:25:55 -02:00
José Valim 79fcebb407 Fix CHANGELOG 2018-01-29 18:19:44 -02:00
José Valim 2b588dfb3e Release v1.6.1 2018-01-29 17:47:48 -02:00
José Valim 2ba98f247a Continue passing :case to setup/setup_all/test 2018-01-29 17:29:34 -02:00
José Valim bc9b5ea700 Filter missing chunks instead of adding them 2018-01-28 21:58:06 -02:00
José Valim 992c240b14 Ensure exit propagation only to non-root evaluators, closes #7255 2018-01-28 11:24:20 -02:00
José Valim 79ae08f962 Raise on invalid map syntax, closes #7268 2018-01-28 11:24:15 -02:00
José Valim 5d7ee90cba Implement child_spec/1 for DynamicSupervisor 2018-01-27 20:05:55 -02:00
José Valim 66cf08ea89 Support bitstrings in Collectable
Closes #7234
2018-01-21 13:25:25 +01:00
Guilherme Pasqualino ad62bb970d Fix example of "Catching values of any kind" (#7241)
The code snippet in the session "Catching values of any kind" of the documentation of `try` had wrong indentation, because of that the snippet was wrongly formatted.
2018-01-21 13:25:21 +01:00
José Valim 18eef65839 Format stream_test 2018-01-21 01:18:35 +01:00
Fernando Tapia Rico 0611069c74 Set proper path of common options used in manpages (#7237)
The path for the common options shared between elixir and iex was
not changed when the `man/Makefile` was merged into the top-level
`Makefile`.
2018-01-21 00:53:50 +01:00
José Valim f2b31a6626 Ensure do blocks do not exceed line length on single arguments
Closes #7232
2018-01-21 00:53:24 +01:00
José Valim 3b41c9f396 Update CHANGELOG 2018-01-19 12:04:15 +01:00
José Valim 0eb95c9c75 Improve undefined behaviour warning 2018-01-19 12:01:30 +01:00
José Valim b898f09870 Support zipping of any collection, closes #7219 2018-01-19 11:22:18 +01:00
José Valim e89139bcf0 Only rearrange not in if explicitly opted-in 2018-01-19 10:54:19 +01:00
José Valim ecad849319 Do not inject @opts on OTP behaviours, closes #7230 2018-01-19 09:59:23 +01:00
José Valim 2658f3666a Add missing CHANGELOG entry 2018-01-19 09:02:50 +01:00
Kelvin Stinghen 3907a3062f More docs to Float (#7214) 2018-01-18 23:20:20 +01:00
Wojtek Mach bd9751d669 Mention defguard on Guards page (#7226) 2018-01-18 23:20:15 +01:00
Fernando Tapia Rico e4ebe291f2 Update Tuple doc to express it's a composite type (#7222) 2018-01-18 23:02:45 +01:00
James Fish 6b6bcea76c Reorder kw blocks in Macro.to_string (#7225) 2018-01-18 23:01:51 +01:00
José Valim 96260316ca Add @spec to second clause of start_link/2
Closes #7218.
2018-01-18 10:26:53 +01:00
José Valim 47f2696820 Include examples to migrate to DynamicSupervisor 2018-01-17 21:10:43 +01:00
José Valim 740cc1ff0b Improve CHANGELOG 2018-01-17 20:58:29 +01:00
José Valim 63b8d0ba34 Release v1.6.0 2018-01-17 19:20:24 +01:00
Jean-Philippe Cugnet 12102d8015 Don’t add parens for defrecord/3 and defrecordp/3 (#7204) 2018-01-13 19:56:28 +01:00
José Valim 67e575eca7 Release v1.6.0-rc.1 2018-01-11 10:21:31 +01:00
José Valim f3960a2f34 Use flex_glue for bitstrings and tuples in the formatter (#7183)
Closes #7152
2018-01-11 10:09:16 +01:00
José Valim 824fcea04a Introduce a group for collections (#7185) 2018-01-11 10:09:14 +01:00
José Valim a6928f774c Add discard_threshold to Logger (#7193) 2018-01-11 10:09:11 +01:00
José Valim 3d08d243d8 Fetch new dependencies on mix deps.update, closes #7194 2018-01-10 11:55:17 +01:00
José Valim 301e0f6b63 Remove leftover inspect 2018-01-10 09:41:40 +01:00
José Valim 7c56a02b0a Allow :macros and :includes to be given to Record.extract, closes #7195 2018-01-10 09:33:56 +01:00
Devon Estes e442bc630b Add additional documentation for assert_in_delta and refute_in_delta (#7191) 2018-01-09 15:08:25 +01:00
José Valim 3dbefd7704 Fix precedence of & in regards to =, closes #7188 2018-01-09 14:14:04 +01:00
José Valim fd1d15a8c5 Consider case ignorable characters on Greek downcasing, closes #7149
Note this does not impact the runtime cost of other downcasing
operations. The final beam file grew only in 8kb.
2018-01-09 13:59:55 +01:00
Devon Estes 56fcd2a45d Change assert_in_delta better handle a delta of 0 (#7189)
Previously if the two values that we were checking were within a given
delta were equal, they would fail if the expected delta was `0`.
2018-01-09 13:13:16 +01:00
Andrea Leopardi 900043c7f2 Fix a typo in a heredoc-related warning
[ci skip]
2018-01-09 13:13:13 +01:00
José Valim 14f0625911 Remove : from mix path helpers to avoid issues on Windows 2018-01-07 22:13:23 +01:00
José Valim d76c35818e Warn if heredoc is outdented, closes #7174 2018-01-07 17:47:52 +01:00
José Valim 6a581275aa Do not autogenerate docs for defdelegate
This would cause code with multiple clauses where the last
one is a delegate to contain the wrong documentation.
2018-01-06 23:27:00 +01:00
José Valim bcbe91a25c Hint the node@host format in --remsh error, closes #7170 2018-01-06 14:23:40 +01:00
José Valim 949ac2b517 Tolerate modules that do not export :deprecated info, closes #7182 2018-01-06 14:06:18 +01:00
Frank Hunleth adb1fa1374 Fix inspect for non-decimal negative integers (#7181)
Before:

iex> inspect(-1, base: :hex)
"0x-1"

After:

iex> inspect(-1, base: :hex)
"-0x1"

This change also applies to the octal and binary base options.
2018-01-05 18:44:50 +01:00
José Valim bd275ea51c Raise if imported dependencies have not been checked out on mix format 2018-01-05 17:49:18 +01:00
José Valim d02755530a Revert "Attempt to soft_purge modules, closes #7047"
Soft purging modules was badfun/badarg errors when
invoking left over anonymous functions.
2018-01-05 17:23:07 +01:00
José Valim 2ab2bffc00 Properly handle :erlang.element in Exception blame 2018-01-05 17:23:04 +01:00
Aaron Tinio 701c0b433d Fix typo (#7178) 2018-01-05 17:23:00 +01:00
José Valim 895adcb001 Rearrange equals and inserts for shorter diff scripts
Closes #7169.
2018-01-05 17:22:57 +01:00
Tobias Pfeiffer e6e6f8d484 Changelog: mix test reports doctests separately now (#7176)
Couldn't find it in the Changelog, maybe I'm a bit daft :) But the feature is there and I love it. Thanks 💚

```
tobi@comfy ~/github/benchee $ mix test
.............................................................................................................................................................................................................................................................................................

Finished in 2.3 seconds
102 doctests, 183 tests, 0 failures
```
2018-01-03 20:21:39 +00:00
Wojtek Mach 09b0ee3f12 Add @impl true to StringIO & IEx.Pry (#7167) 2018-01-01 20:26:19 +01:00
Wojtek Mach 5c6f7b9ce5 Don't mention deprecated {Map,Keyword}.replace/3 in docs (#7166) 2018-01-01 20:25:59 +01:00
José Valim adbbba118d Do not leak variables during optimizations, closes #7161 2018-01-01 12:04:52 +01:00
Xavier Noria fab66ad834 s/when/after/ in Application.stop/1 docs [ci skip] (#7164) 2018-01-01 12:04:45 +01:00
José Valim 31ef3abff0 Improve docs for matching macro arguments on break!
See #7155.
2017-12-30 18:37:22 +01:00
Donald Little 021a84aafe Return right side expression value on struct matching (#7160) 2017-12-30 18:37:17 +01:00
Daniel Kempkens d8fd6ef6ff Fix Supervisor.child_spec() type (#7157) 2017-12-30 18:37:13 +01:00
José Valim f8f4e1dd79 Clear up deprecations page 2017-12-30 18:37:10 +01:00
Tomasz Marek Sulima d3af8ea8aa Make formatting options and task options clearer (#7150) 2017-12-27 09:38:17 +01:00
José Valim d587434be2 Improvements to greek handling of downcase, closes #7149 2017-12-26 23:18:51 +01:00
José Valim 6aadfba586 Store expanded expressions in defguard, closes #7147 2017-12-26 00:06:36 +01:00
José Valim 625f45930a Fixes to readme and deprecation pages 2017-12-25 10:12:57 +01:00
José Valim cb47d08c13 Update CHANGELOG 2017-12-25 10:12:46 +01:00
José Valim 9a5d1a39d1 Fix clean command for erlang-based compiler tasks 2017-12-24 23:22:42 +01:00
José Valim 27102e276d Release v1.6.0-rc.0 2017-12-24 23:22:23 +01:00
José Valim 0b646c6188 Improve wording in CHANGELOG 2017-12-24 15:23:28 +01:00
José Valim 3879f0d8b3 Update deprecations table, closes #7137 2017-12-24 12:15:51 +01:00
José Valim 24369d5724 Ensure we can still receive [term()] in start_child spec, closes #7138 2017-12-24 12:00:04 +01:00
José Valim 3d20f049e6 Update CHANGELOG 2017-12-23 23:02:55 +01:00
José Valim 8d07fe6f59 Update CHANGELOG and release info 2017-12-23 20:26:39 +01:00
José Valim d27b285687 Revert "Add support for supervisor fun to Task.Supervisor.async_stream/* (#6552)"
This reverts commit 91061bf8f9.
2017-12-23 20:01:15 +01:00
José Valim 1684c8067e Add advice regards deprecations 2017-12-23 19:48:52 +01:00
462 changed files with 18322 additions and 33894 deletions
+5 -1
View File
@@ -12,6 +12,10 @@
assert_same: 2,
# Errors tests
assert_eval_raise: 3
assert_eval_raise: 3,
# Mix tests
in_fixture: 2,
in_tmp: 2
]
]
+17 -24
View File
@@ -1,36 +1,29 @@
language: bash
language: erlang
sudo: false
env:
global:
- ELIXIR_ASSERT_TIMEOUT=2000
matrix:
- OTP_RELEASE=OTP-20.0
- OTP_RELEASE=OTP-20.1
- OTP_RELEASE=OTP-20.2
- OTP_RELEASE=OTP-20.3
- OTP_RELEASE=OTP-21.0
- OTP_RELEASE=OTP-21.1
- OTP_RELEASE=maint
- OTP_RELEASE=master
matrix:
fast_finish: true
allow_failures:
- env: OTP_RELEASE=maint
- env: OTP_RELEASE=master
include:
- os: linux
otp_release: 19.0
- os: linux
otp_release: 19.1
- os: linux
otp_release: 19.2
- os: linux
otp_release: 19.3
- os: linux
otp_release: 20.0
- os: linux
otp_release: 20.1
install:
- wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-14.04/${OTP_RELEASE}.tar.gz
- mkdir -p otp
- tar zxf otp.tar.gz -C otp --strip-components=1
- otp/Install -minimal $(pwd)/otp
- PATH=$(pwd)/otp/bin:$PATH
env:
- ELIXIR_ASSERT_TIMEOUT=2000
script:
- make compile
- rm -rf .git
- make test
- bin/elixir bin/mix format --dry-run --check-formatted
- dialyzer -pa lib/elixir/ebin --build_plt --output_plt elixir.plt --apps lib/elixir/ebin/elixir.beam lib/elixir/ebin/Elixir.Kernel.beam
notifications:
+249 -148
View File
@@ -1,214 +1,315 @@
# Changelog for Elixir v1.8
# Changelog for Elixir v1.6
Elixir v1.8 comes with many improvements at the infrastructure level, improving compilation time, speeding up common patterns, and adding features around introspection of the system.
## Code formatter
## Custom struct inspections
The big feature in Elixir v1.6 is the addition of a code formatter and an accompanying `mix format` task that adds automatic formatting to your projects.
Elixir now provides a derivable implementation of the `Inspect` protocol. In a nutshell, this means it is really easy to filter data from your data structures whenever they are inspected. For example, imagine you have a user struct with security and privacy sensitive information:
The goal of the formatter is to automate the styling of codebases into a unique and consistent layout used across teams and the whole community. Code is now easier to write, as you no longer need to concern yourself with formatting rules. Code is also easier to read, as you no longer need to convert the styles of other developers in your mind.
```elixir
defmodule User do
defstruct [:id, :name, :age, :email, :encrypted_password]
end
```
The formatter also helps new developers to learn the language, by giving immediate feedback on code structure, and eases code reviews by allowing teams to focus on business rules and code quality, rather than code style.
By default, if you inspect a user via `inspect(user)`, it will include all fields. This can cause fields such as `:email` and `:encrypted_password` to appear in logs, error reports, etc. You could always define a custom implementation of the `Inspect` protocol for such cases but Elixir v1.8 makes it simpler by allowing you to derive the `Inspect` protocol:
To automatically format your codebase, you can run the new `mix format` task. A `.formatter.exs` file may be added to your project root for rudimentary formatter configuration. The mix task also supports flags for CI integration. For instance, you can make your build or a Pull Request fail if the code is not formatted accordingly. We also recommend developers to check their favorite editor and see if they already provide key bindings for `mix format`, allowing a file or a code snippet to be formatted without ceremony.
```elixir
defmodule User do
@derive {Inspect, only: [:id, :name, :age]}
defstruct [:id, :name, :age, :email, :encrypted_password]
end
```
The Elixir codebase itself has been already fully formatted and all further contributions are expected to contain formatted code. We recommend existing codebases to be formatted in steps. While the formatter will correctly handle long lines and complex expressions, refactoring the code by breaking those into variables or smaller functions as you format them will lead to overall cleaner and more readable codebases.
Now all user structs will be printed with all remaining fields collapsed:
## Dynamic Supervisor
#User<id: 1, name: "Jane", age: 33, ...>
Supervisors in Elixir are responsible for starting, shutting down and restarting child process when things go wrong. Most of the interaction with supervisors happen with the Supervisor module and it contains three main strategies: `:one_for_one`, `:rest_for_one` and `:one_for_all`.
You can also pass `@derive {Inspect, except: [...]}` in case you want to keep all fields by default and exclude only some.
However, sometimes the children of a supervisor are not known upfront and are rather started dynamically. For example, if you are building a web server, you have each request beind handled by a separate supervised process. Those cases were handled in the Supervisor module under a special strategy called `:simple_one_for_one`.
## Time zone database support
Unfortunately, this special strategy changed the semantics of the supervisor in regards to initialization and shutdown. Plus some APIs expected different inputs or would be completely unavailable depending on the supervision strategy.
In Elixir v1.3, Elixir added four types, known as Calendar types, to work with dates and times: `Time`, `Date`, `NaiveDateTime` (without time zone) and `DateTime` (with time zone). Over the last releases we have added many enhancements to the Calendar types but the `DateTime` module always evolved at a slower pace since Elixir did not provide support for a time zone database.
Elixir v1.6 addresses this issue by introducing a new `DynamicSupervisor` module, which encapsulates the old `:simple_one_for_one` strategy and APIs in a proper module while allowing the documentation and API of the `Supervisor` module to focus on its main use cases. Having a separate `DynamicSupervisor` module also makes it simpler to add new features to the dynamic supervisor, such as the new `:max_children` option that limits the maximum number of children supervised dynamically.
Elixir v1.8 now defines a `Calendar.TimeZoneDatabase` behaviour, allowing developers to bring in their own time zone databases. By defining an explicit contract for time zone behaviours, Elixir can now extend the `DateTime` API, adding functions such as `DateTime.shift_zone/3`. By default, Elixir ships with a time zone database called `Calendar.UTCOnlyTimeZoneDatabase` that only handles UTC.
## `@deprecated` and `@since` attributes
Other Calendar related improvements include the addition of `Date.day_of_year/1`, `Date.quarter_of_year/1`, `Date.year_of_era/1`, and `Date.day_of_era/1`.
This release also introduces two new attributes associated to function definitions: `@deprecated` and `@since`. The former marks if a function or macro is deprecated, the latter annotates the version the API was introduced:
## Faster compilation and other performance improvements
@doc "Breaks a collection into chunks"
@since "1.0.0"
@deprecated "Use chunk_every/2 instead"
def chunk(collection, chunk_size) do
chunk_every(collection, chunk_size)
end
Due to improvements to the compiler made over the last year, Elixir v1.8 should compile code about 5% faster on average. This is yet another release where we have been able to reduce compilation times and provide a more joyful development experience to everyone.
The `mix xref` task was also updated to warn if your project calls deprecated code. So if a definition is marked as `@deprecated` and a module invokes it, a warning will be emitted during compilation. This effectively provides libraries and frameworks a mechanism to deprecate code without causing multiple warnings to be printed in runtime and without impacting performance.
The compiler also emits more efficient code for range checks in guards (such as `x in y..z`), for charlists with interpolation (such as `'foo #{bar} baz'`), and when working with records via the `Record` module.
Note those attributes are not yet available to tools that generate documentation. Such functionality will be added in future releases as it requires changes to how Elixir stores documentation in BEAM files. We still recommend developers to properly annotate their APIs, as the information will then be already available when the tooling is updated.
Finally, EEx templates got their own share of optimizations, emitting more compact code that runs faster.
## defguard and defguardp
## Improved instrumentation and ownership with `$callers`
Elixir provides the concepts of guards: expressions used alongside pattern matching to select a matching clause. Let's see an example straight from Elixir's home page:
The `Task` module is one of the most common ways to spawn light-weight processes to perform work concurrently. Whenever you spawn a new process, Elixir annotates the parent of that process through the `$ancestors` key. This information can be used by instrumentation tools to track the relationship between events occurring within multiple processes. However, many times, tracking only the `$ancestors` is not enough.
def serve_drinks(%User{age: age}) when age >= 21 do
# Code that serves drinks!
end
For example, we recommend developers to always start tasks under a supervisor. This provides more visibility and allows us to control how those tasks are terminated when a node shuts down. In your code, this can be done by invoking something like: `Task.Supervisor.start_child(MySupervisor, task_specification)`. This means that, although your code is the one who invokes the task, the actual parent of the task would be the supervisor, as the supervisor is the one spawning it. We would list the supervisor as one of the `$ancestors` for the task, but the relationship between your code and the task is lost.
`%User{age: age}` is matching on a `User` struct with an age field and `when age >= 21` is the guard.
In Elixir v1.8, we now track the relationship between your code and the task via the `$callers` key in the process dictionary, which aligns well with the existing `$ancestors` key. Therefore, assuming the `Task.Supervisor` call above, we have:
Since only a handful of constructs are [allowed in guards](https://hexdocs.pm/elixir/guards.html#content), if you were in a situation where you had to check the age to be more than or equal to 21 in multiple times, extracting the guard to a separate function would be [less than obvious and error prone](https://github.com/elixir-lang/elixir/issues/2469). To address those issues, this release introduces `defguard/1` and `defguardp/1`:
[your code] -- calls --> [supervisor] ---- spawns --> [task]
defguard is_drinking_age(age) when age >= 21
which means we store the following relationships:
def serve_drinks(%User{age: age}) when is_drinking_age(age) do
# Code that serves drinks!
end
[your code] [supervisor] <-- ancestor -- [task]
^ |
|--------------------- caller ---------------------|
## IEx improvements
When a task is spawned directly from your code, without a supervisor, then the process running your code will be listed under both `$ancestors` and `$callers`.
IEx also got its share of improvements. The new code formatter allows us to pretty print code snippets, types and specifications, improving the overall experience when exploring code through the terminal.
This small feature is very powerful. It allows instrumentation and monitoring tools to better track and relate the events happening in your system. This feature can also be used by tools like the "Ecto Sandbox". The "Ecto Sandbox" allows developers to run tests concurrently against the database, by using transactions and an ownership mechanism where each process explicitly gets a connection assigned to it. Without `$callers`, every time you spawned a task that queries the database, the task would not know its caller, and therefore it would be unable to know which connection was assigned to it. This often meant features that relies on tasks could not be tested concurrently. With `$callers`, figuring out this relationship is trivial and you have more tests using the full power of your machine.
The autocomplete mechanism also got smarter, being able to provide context autocompletion. For example, typing `t Enum.` and hitting TAB will autocomplete only the types in Enum (in contrast to all functions). Typing `b GenServer.` and hitting TAB will autocomplete only the behaviour callbacks.
## v1.8.2 (2019-05-11)
Finally, the breakpoint functionality added in Elixir v1.5 has been improved to support pattern matching and guards. For example, to pattern match on a function call when the first argument is the atom `:foo`, you may do:
### 1. Bug fixes
break! SomeFunction.call(:foo, _, _)
#### EEx
## mix xref
* [EEx] Raise readable error message on bad EEx state
`mix xref` is a task added in Elixir v1.3 which provides general information about how modules and files in an application depend on each other. This release brings many improvements to `xref`, extending the reach of the analysis and helping developers digest the vast amount of data it produces.
#### Elixir
One of such additions is the `--include-siblings` option that can be given to all `xref` commands inside umbrella projects. For example, to find all of the callers of a given module or function in an umbrella:
* [Protocol] Ensure `:debug_info` is kept in protocols
$ mix xref callers SomeModule --include-siblings
#### Logger
The `graph` command in `mix xref` now can also output general statistics about the graph. In [the hexpm project](https://github.com/hexpm/hexpm), you would get:
* [Logger] Make sure Logger v1.8 does not get stuck in discard mode
* [Logger.Translator] Translate remote process crash in Logger
$ mix xref graph --format stats
Tracked files: 129 (nodes)
Compile dependencies: 256 (edges)
Structs dependencies: 46 (edges)
Runtime dependencies: 266 (edges)
## v1.8.1 (2019-01-30)
Top 10 files with most outgoing dependencies:
* test/support/factory.ex (18)
* lib/hexpm/accounts/user.ex (13)
* lib/hexpm/accounts/audit_log.ex (12)
* lib/hexpm/web/controllers/dashboard_controller.ex (12)
* lib/hexpm/repository/package.ex (12)
* lib/hexpm/repository/releases.ex (11)
* lib/hexpm/repository/release.ex (10)
* lib/hexpm/web/controllers/package_controller.ex (10)
* lib/mix/tasks/hexpm.stats.ex (9)
* lib/hexpm/repository/registry_builder.ex (9)
### 1. Bug fixes
Top 10 files with most incoming dependencies:
* lib/hexpm/web/web.ex (84)
* lib/hexpm/web/router.ex (29)
* lib/hexpm/web/controllers/controller_helpers.ex (29)
* lib/hexpm/web/controllers/auth_helpers.ex (28)
* lib/hexpm/web/views/view_helpers.ex (27)
* lib/hexpm/web/views/icons.ex (27)
* lib/hexpm/web/endpoint.ex (23)
* lib/hexpm/ecto/changeset.ex (22)
* lib/hexpm/accounts/user.ex (19)
* lib/hexpm/repo.ex (19)
#### Elixir
`mix xref graph` also got the `--only-nodes` and `--label` options. The former asks Mix to only output file names (nodes) without the edges. The latter allows you to focus on certain relationships:
* [Float] Fix rounding for subnormal floats
# To get all files that depend on lib/foo.ex
mix xref graph --sink lib/foo.ex --only-nodes
#### IEx
# To get all files that depend on lib/foo.ex at compile time
mix xref graph --label compile --sink lib/foo.ex --only-nodes
* [IEx] Fix `IEx.pry` crash when IEx isn't running
* [IEx.CLI] Add IEx warning when using `--remsh` with dumb terminal
* [IEx.Helpers] Sort results by arity on `h` helper
# To get all files lib/foo.ex depends on
mix xref graph --source lib/foo.ex --only-nodes
#### Mix
# To limit statistics only to compile time dependencies
mix xref graph --format stats --label compile
* [mix compile] Do not include optional dependencies in extra applications as it is incompatible with shared deps in umbrellas
Those improvements will help developers better understand the relationship between files and reveal potentially complex parts of their systems.
## v1.8.0 (2019-01-14)
Other improvements in Mix include better compiler diagnostics for editor integration, support for the `--slowest N` flag in `mix test` that shows the slowest tests in your suite, and a new `mix profile.eprof` task that provides time based profiling, complementing the existing `mix profile.cprof` (count based) and `mix profile.fprof` (flame based).
## v1.6.2 (2018-02-28)
### 1. Enhancements
#### EEx
* [EEx] Optimize the default template engine to compile and execute more efficiently
#### Elixir
* [Calendar] Add `Calendar.TimeZoneDatabase` and a `Calendar.UTCOnlyTimeZoneDatabase` implementation
* [Calendar] Add callbacks `day_of_year/3`, `quarter_of_year/3`, `year_of_era/1`, and `day_of_era/3`
* [Code.Formatter] Preserve user's choice of new line after most operators
* [Date] Add `Date.day_of_year/1`, `Date.quarter_of_year/1`, `Date.year_of_era/1`, and `Date.day_of_era/1`
* [DateTime] Add `DateTime.from_naive/3`, `DateTime.now/1`, and `DateTime.shift_zone/3`
* [File] Allow `:raw` option in `File.exists?/2`, `File.regular?/2`, and `File.dir?/2`
* [File] Allow POSIX time as an integer in `File.touch/2` and `File.touch!/2`
* [Inspect] Allow `Inspect` protocol to be derivable with the `:only`/`:except` options
* [Kernel] Do not propagate counters to variables in quote inside another quote
* [Kernel] Warn on ambiguous use of `::` and `|` in typespecs
* [Kernel] Add `:delegate_to` `@doc` metadata tag when using `defdelegate`
* [Kernel] Improve compile-time building of ranges via the `..` operator
* [Kernel] Compile charlist interpolation more efficiently
* [Kernel] Add `floor/1` and `ceil/1` guards
* [Kernel.SpecialForms] Add `:reduce` option to `for` comprehensions
* [List] Add `List.myers_difference/3` and `List.improper?/1`
* [Macro] Add `Macro.struct!/2` for proper struct resolution during compile time
* [Map] Optimize and merge nested maps `put` and `merge` operations
* [Range] Add `Range.disjoint?/2`
* [Record] Reduce memory allocation when updating multiple fields in a record
* [Registry] Allow associating a value on `:via` tuple
* [String] Add `String.bag_distance/2`
* [Task] Add `$callers` tracking to `Task` - this makes it easier to find which process spawned a task and use it for tracking ownership and monitoring
#### ExUnit
* [ExUnit] Add `ExUnit.after_suite/1` callback
* [ExUnit.Assertions] Show last N messages (instead of first N) from mailbox on `assert_receive` fail
#### IEx
* [IEx.Helpers] Add `port/1` and `port/2`
* [IEx.Server] Expose `IEx.Server.run/1` for custom IEx sessions with the ability to broker pry sessions
#### Mix
* [Mix] Add `Mix.target/0` and `Mix.target/1` to control dependency management per target
* [Mix.Project] Add `:depth` and `:parents` options to `deps_paths/1`
* [mix archive.install] Add a timeout when installing archives
* [mix compile] Include optional dependencies in `:extra_applications`
* [mix escript.install] Add a timeout when installing escripts
* [mix format] Warn when the same file may be formatted by multiple `.formatter.exs`
* [mix test] Allow setting the maximum number of failures via `--max-failures`
* [mix test] Print a message instead of raising on unmatched tests inside umbrella projects
* [mix compile.erlang] Teach Mix erlang compiler alternative spelling for `-behavior` declaration
* [mix format] Support the `:subdirectories` configuration that points to other directories with their own `.formatter.exs` file. This is useful in umbrella applications. `mix new --umbrella` has also been changed to use this new configuration by default
* [mix format] Include the current environment for missing dependency errors
### 2. Bug fixes
#### Elixir
* [Calendar] Allow printing dates with more than 9999 years
* [Exception] Exclude deprecated functions in "did you mean?" hints
* [Float] Handle subnormal floats in `Float.ratio/1`
* [Kernel] Remove `Guard test tuple_size(...) can never succeed` Dialyzer warning on `try`
* [Kernel] Expand operands in `size*unit` bitstring modifier instead of expecting `size` and `unit` to be literal integers
* [Kernel] Do not deadlock on circular struct dependencies in typespecs
* [Kernel] Raise proper error message when passing flags to the Erlang compiler that Elixir cannot handle
* [Kernel] Do not leak variables in `cond` clauses with a single matching at compile-time clause
* [NaiveDateTime] Do not accept leap seconds in builder and parsing functions
* [String] Fix ZWJ handling in Unicode grapheme clusters
* [StringIO] Handle non-printable args in StringIO gracefully
#### IEx
* [IEx.Helpers] Use typespec info (instead of docs chunk) and properly format callbacks in `b/1`
#### Logger
* [Logger] Allow Logger backends to be dynamically removed when an application is shutting down
* [Code.Formatter] Ensure `->` does not exceed line length
* [DynamicSupervisor] Properly tag error reports generated by dynamic supervisors so they can be properly translated by `Logger`
* [DynamicSupervisor] Consider extra arguments during child restart
* [Kernel] Ensure arguments given to a guard defined with `defguard` are evaluated in the correct order
* [Module] Do not remove docs for previous function declaration when `@impl true` is used
* [Supervisor] Ensure `use Supervisor` properly adds the `@behaviour Supervisor` annotation
#### Mix
* [mix compile] Ensure changes in deps propagate to all umbrella children - this fix a long standing issue where updating a dependency would not recompile all projects accordingly, requiring a complete removal of `_build`
* [mix compile] Avoid time drift when checking and updating compiler manifest files
* [mix compile.app] Respect the `:only` option between umbrella siblings
* [mix compile.protocols] Reconsolidate protocols if local dependencies are stale
* [mix deps] Properly mark dependencies with different `:system_env` as diverged
* [mix new] Use `--module` value when setting up filenames
* [Mix.Shell] Bring back `Mix.Shell.cmd/2` - this arity was defined via a default argument that was accidentally removed
### 3. Soft-deprecations (no warnings emitted)
## v1.6.1 (2018-01-29)
None.
### 4. Hard-deprecations
### 1. Enhancements
#### Elixir
* [Enum] Passing a non-empty list to `Enum.into/2` was inconsistent with maps and is deprecated in favor of `Kernel.++/2` or `Keyword.merge/2`
* [Inspect.Algebra] `surround/3` is deprecated in favor of `Inspect.Algebra.concat/2` and `Inspect.Algebra.nest/2`
* [Inspect.Algebra] `surround_many/6` is deprecated in favor of `container_doc/6`
* [Kernel] Using `@since` will now emit a unused attribute warning. Use `@doc since: "1.7.2"` instead
* [Kernel] Passing a non-empty list as `:into` in `for` comprehensions was inconsistent with maps and is deprecated in favor of `Kernel.++/2` or `Keyword.merge/2`
* [Kernel.ParallelCompiler] `files/2` is deprecated in favor of `compile/2`
* [Kernel.ParallelCompiler] `files_to_path/2` is deprecated in favor of `compile_to_path/2`
* [Kernel.ParallelRequire] `files/2` is deprecated in favor of `Kernel.ParallelCompiler.require/2`
* [System] `:seconds`, `:milliseconds`, etc. as time units is deprecated in favor of `:second`, `:millisecond`, etc.
* [System] `System.cwd/0` and `System.cwd!/0` are deprecated in favor of `File.cwd/0` and `File.cwd!/0`
* [DynamicSupervisor] Implement `child_spec/1` for DynamicSupervisor
* [Kernel] Raise better error messages on invalid map syntax
### 2. Bug fixes
#### Elixir
* [Code.Formatter] Only rearrange `not in` operator if explicitly opted-in
* [Code.Formatter] Ensure `do` blocks do not exceed line length on calls with a single argument
* [Collectable] Support bitstrings in Collectable and for-comprehensions (regression in v1.6.0)
* [GenServer] Do not override user own `@opts` attribute
* [Enum] Reintroduce zipping of any enumerable of enumerables in `Enum.zip/1` (regression in v1.6.0)
* [Macro] Reorder kw blocks in `Macro.to_string/1` to avoid warnings
* [Protocol] Fix protocol consolidation when some chunks may be missing
* [Stream] Reintroduce zipping of any enumerable of enumerables in `Stream.zip/1` (regression in v1.6.0)
* [Supervisor] Do not override user own `@opts` attribute
* [Supervisor] Add `@spec` to second clause of `start_link/2`
#### ExUnit
* [ExUnit.Case] Reintroduce `:case` in ExUnit setup/setup_all/test context
## v1.6.0 (2018-01-17)
### 1. Enhancements
#### EEx
* [EEx] Allow markers `/` and `|` to be used in a custom EEx engine
#### Elixir
* [Calendar] Add truncate to `Time`, `DateTime` and `NaiveDateTime` to facilitate microsecond precision pruning
* [Code] Add `format_string!/2` and `format_file!/2` for automatic code formatting
* [Code] Support column annotations in quoted expressions with `columns: true` in `Code.string_to_quoted/2`
* [DynamicSupervisor] Add `DynamicSupervisor` designed to manage children that are added and removed dynamically
* [Exception] Make `Exception.blame/3` extensible by adding an optional `blame/2` callback to exceptions
* [Exception] Improve the printing of guards on blamed exceptions
* [Enumerable] Add `Enumerable.slice/1` and optimize many `Enum` operations with the new protocol. This allows data-structures with index-based random access to provide a non-linear implementation
* [Inspect] Show UTF-8 BOM on inspected strings
* [Inspect.Algebra] Add `:strict` and `:flex` breaks - this gives more control over the document fitting
* [Inspect.Algebra] Allow a group to inherit the parent group break
* [Inspect.Algebra] Add `force_unfit/1` and `next_break_fits/2` which give more control over document fitting
* [Inspect.Algebra] Add `collapse_lines/1` for collapsing multiple lines to a maximum value
* [Inspect.Algebra] Allow `nest/2` to be `:reset` or be set to the current `:cursor` position
* [Kernel] Prefix variables with V when emitting Erlang code. This improves the integration with tools such as Erlang code formatters and the GUI debugger
* [Kernel] Warn on the use of `length(x) == 0` in guards
* [Kernel] Warn if `catch` comes before `rescue` in try
* [Kernel] Warn if heredoc is outdented compared to its closing quotes
* [Kernel] Add `defguard/1` and `defguardp/1` to make it easier to build guard-safe macros
* [Kernel.ParallelCompiler] Add `compile/2`, `compile_to_path/3` and `require/2` which provide detailed information about warnings and errors
* [Kernel.SpecialForms] Support the `uniq: true` flag in `for` comprehensions
* [Module] Introduce `@deprecated` and `@since` attributes
* [Module] Emit conflicting behaviour warnings if the same behaviour is given more than once
* [List] Rearrange equals and inserts for shorter diff scripts in `List.myers_difference/2`
* [Record] Allow `:macros` and `:includes` to be given to `Record.extract/2`
* [Stream] Add `Stream.intersperse/2`
* [String] Update to Unicode 10
* [String] Allow passing empty string `match` to `String.replace/4`
* [String] Support context and language sensitive operations in `String.upcase/2` and `String.downcase/2`. Currently only the `:greek` context is supported
* [String] Support `:ascii` conversion in `String.upcase/2` and `String.downcase/2`
* [Time] Add `Time.add/3`
#### ExUnit
* [ExUnit.Assertions] Perform inclusive checks in `assert_in_delta`
* [ExUnit.Callbacks] Add `ExUnit.Callbacks.start_supervised!/2`
* [ExUnit.Case] Generate a random seed per test based on the test suite seed
#### IEx
* [IEx.Autocomplete] Provide contextual autocompletion: `t Enum.` will autocomplete types, `b Enum` will autocomplete callbacks
* [IEx.CLI] Provide hints for developers when a bad host name is given to `--remsh`
* [IEx.Helpers] Automatically include specs when showing documentation for functions/macros
* [IEx.Helpers] Improve formatting of behaviours and typespecs by using the formatter
* [IEx.Helpers] Allow pattern matching and guard expressions when on `IEx.break!`
#### Logger
* [Logger] Add `:discard_threshold` to Logger to help with message queue overflow
#### Mix
* [mix compile.erlang] Returning `{:ok, contents}` or `:error` as the callback in `Mix.Compilers.Erlang.compile/6` is deprecated in favor of returning `{:ok, contents, warnings}` or `{:error, errors, warnings}`
* [mix app.start] Add `--preload-modules` to `mix app.start`
* [mix archive.build] Allow `mix archive.build` to bundle dot files via an option
* [mix compile] Define a behavior for Mix compiler tasks and return diagnostics from compiler tasks
* [mix compile] Track struct dependencies between files and recompile them only if the struct changes
* [mix deps] Support `:system_env` option when specifying dependencies
* [mix format] Add a `mix format` task that formats the given files (or the files specified in a `.formatter.exs` file)
* [mix profile.eprof] Add a new task for time-based profiling with eprof
* [mix test] Run all functions in a describe block by giving the `file:line` the describe block starts
* [mix test] Report the top N slowest tests with the `--slowest N` flag
* [mix test] Report the number of doctests and tests separately
* [mix xref] Support `--include-siblings` in reports for umbrella support
* [mix xref] Add `mix xref graph --format stats`
* [mix xref] Add `--only-nodes` and `--label` filters to mix xref graph
* [mix xref] Add `mix xref deprecated` that shows the callsite of deprecated functions
## v1.7
### 2. Bug fixes
The CHANGELOG for v1.7 releases can be found [in the v1.7 branch](https://github.com/elixir-lang/elixir/blob/v1.7/CHANGELOG.md).
#### Elixir
* [CLI] Support path with spaces as argument to elixir.bat
* [Inspect] Properly handle minus signal for non-decimal negative integers
* [Integer] Do not raise on non-integer values in `is_odd`/`is_even`
* [Kernel] Solve a precedence issue between `&` and `|`, such as `[&Foo.bar/1 | &Baz.bat/2]`
* [Kernel] Do not load dynamic Elixir modules as `:in_memory` as this value is not officially supported by the code server. Instead, use an empty list, which is the same value used by Erlang.
* [Kernel] Validate variable struct name is atom when used in pattern matching
* [Kernel] No longer generate documentation for `defdelegate` functions automatically to avoid overriding previously specified `@doc`
* [Macro] Fix `Macro.to_string/2` for tuple calls, such as `alias Foo.{Bar, Baz}`
* [MapSet] Return valid MapSet when unioning a legacy MapSet
* [Regex] Return a leading empty space when splitting on empty pattern. This makes the `split` operation consistent with the other operations in the `Regex` module
* [Stream] Ensure `Stream.chunk_while/4` does not emit more elements than necessary when halted
* [String] Return a leading empty space when splitting on empty string. This makes the `split` operation consistent with the other operations in the `String` module
* [URI] Preserve empty fragments in `URI.parse/1`
#### Mix
* [mix app.start] Improve the quality of reports if app fails to boot
* [mix cmd] Allow `mix cmd` to be invoked multiple times without marking it as executed
* [mix deps] Ensure optional dependencies in umbrella applications are loaded
* [mix deps.update] Ensure transitive new non-Hex dependencies are also fetched when a repo is updated
* [mix xref] Take compile dependencies with higher priority than runtime ones when building a graph
* [mix xref] Handle external files for xref callers and warnings
### 3. Soft deprecations (no warnings emitted)
#### Elixir
* [GenServer] Warn if `init/1` is not defined in `GenServer`. This brings GenServer closer to the implementation in OTP and aligns all behaviours to require the `init/1` callback
* [Inspect.Algebra] `surround/3` and `surround_many/6` are deprecated in favor of `container_doc/6`
* [Kernel] Specifying map types with variable keys without defining the type as required/optional is deprecated
* [Kernel.ParallelCompiler] `files/2` and `files_to_path/3` are deprecated in favor of `compile/2` and `compile_to_path/3`
* [Kernel.ParallelRequire] `files/2` is deprecated in favor of `Kernel.ParallelCompiler.require/2`
* [Supervisor] The `:simple_one_for_one` strategy is deprecated in favor of `DynamicSupervisor`
* [Supervisor] Passing a list of args to `Supervisor.start_child/2` is deprecated in favor of `DynamicSupervisor`
* [Task.Supervisor] Passing `:restart` and `:shutdown` to `Task.Supervisor.start_link/2` is deprecated (it should be passed on start child instead)
#### ExUnit
* [ExUnit.Formatter] `:case_started` and `:case_finished` events are deprecated in favor of `:module_started` and `:module_finished`
#### Mix
* [Mix.Compilers.Erlang] Returning `{:ok, val} | :error` from custom Erlang compilers is deprecated in favor of `{:ok, val, warnings} | {:error, errors, warnings}`
### 4. Deprecations
#### Elixir
* [Enum] `Enum.partition/2` is deprecated in favor of `Enum.split_with/2`
* [Keyword] `Keyword.replace/3` is deprecated in favor of `Keyword.fetch/2` and `Keyword.put/3`
* [Map] `Map.replace/3` is deprecated in favor of `Map.fetch/2` and `Map.put/3`
* [Macro] `Macro.unescape_tokens/1` and `Macro.unescape_tokens/2` are deprecated in favor of `Enum.map/2`
* [Range] Deprecate `Range.range?/1` in favor of pattern matching on `_ .. _`
## v1.5
The CHANGELOG for v1.5 releases can be found [in the v1.5 branch](https://github.com/elixir-lang/elixir/blob/v1.5/CHANGELOG.md).
+2 -4
View File
@@ -1,14 +1,13 @@
### Precheck
* Do not use the issues tracker for help or support (try Elixir Forum, Stack Overflow, IRC, etc.)
* For proposing a new feature, please start a discussion on the Elixir Core mailing list: https://groups.google.com/group/elixir-lang-core
* For proposing a new feature, please start a discussion on the Elixir Core mailing list
* For bugs, do a quick search and make sure the bug has not yet been reported
* Please disclose security vulnerabilities privately at elixir-security@googlegroups.com
* Finally, be nice and have fun!
### Environment
* Elixir & Erlang/OTP versions (elixir --version):
* Elixir & Erlang versions (elixir --version):
* Operating system:
### Current behavior
@@ -17,4 +16,3 @@ Include code samples, errors and stacktraces if appropriate.
### Expected behavior
A short description on how you expect the code to behave.
+25
View File
@@ -174,3 +174,28 @@
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2012 Plataformatec
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+32 -48
View File
@@ -1,11 +1,10 @@
REBAR ?= "$(CURDIR)/rebar"
PREFIX ?= /usr/local
SHARE_PREFIX ?= $(PREFIX)/share
MAN_PREFIX ?= $(SHARE_PREFIX)/man
CANONICAL := v1.8/ # master/ or vMAJOR.MINOR/
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict --warnings-as-errors
ERLC := erlc -I lib/elixir/include +warnings_as_errors
CANONICAL := v1.6/
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict
ERLC := erlc -I lib/elixir/include
ERL := erl -I lib/elixir/include -noshell -pa lib/elixir/ebin
GENERATE_APP := $(CURDIR)/lib/elixir/generate_app.escript
VERSION := $(strip $(shell cat VERSION))
Q := @
LIBDIR := lib
@@ -17,15 +16,15 @@ INSTALL_PROGRAM = $(INSTALL) -m755
GIT_REVISION = $(strip $(shell git rev-parse HEAD 2> /dev/null ))
GIT_TAG = $(strip $(shell head="$(call GIT_REVISION)"; git tag --points-at $$head 2> /dev/null | tail -1) )
.PHONY: install compile erlang elixir unicode app build_plt clean_plt dialyze test clean clean_residual_files install_man clean_man docs Docs.zip Precompiled.zip zips
.PHONY: install compile erlang elixir build_plt clean_plt dialyze test clean clean_residual_files install_man clean_man docs Docs.zip Precompiled.zip zips
.NOTPARALLEL: compile
#==> 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 >= 20)])' -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 >= 19)])' -s erlang halt | grep -q '^true'; \
if [ $$? != 0 ]; then \
echo "At least Erlang/OTP 20.0 is required to build Elixir"; \
echo "At least Erlang 19.0 is required to build Elixir"; \
exit 1; \
fi
endef
@@ -34,7 +33,11 @@ define APP_TEMPLATE
$(1): lib/$(1)/ebin/Elixir.$(2).beam lib/$(1)/ebin/$(1).app
lib/$(1)/ebin/$(1).app: lib/$(1)/mix.exs
$(Q) cd lib/$(1) && ../../bin/elixir -e 'Mix.start(:permanent, [])' -r mix.exs -e 'Mix.Task.run("compile.app", ~w[--compile-path ebin])'
$(Q) mkdir -p lib/$(1)/_build/shared/lib/$(1)
$(Q) cp -R lib/$(1)/ebin lib/$(1)/_build/shared/lib/$(1)/
$(Q) cd lib/$(1) && ../../bin/elixir -e 'Mix.start(:permanent, [])' -r mix.exs -e 'Mix.Task.run("compile.app")'
$(Q) cp lib/$(1)/_build/shared/lib/$(1)/ebin/$(1).app lib/$(1)/ebin/$(1).app
$(Q) rm -rf lib/$(1)/_build
lib/$(1)/ebin/Elixir.$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1)/lib/*/*.ex) $(wildcard lib/$(1)/lib/*/*/*.ex)
@ echo "==> $(1) (compile)"
@@ -42,36 +45,31 @@ lib/$(1)/ebin/Elixir.$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1
$(Q) cd lib/$(1) && ../../$$(ELIXIRC) "lib/**/*.ex" -o ebin
test_$(1): compile $(1)
@ echo "==> $(1) (ex_unit)"
@ echo "==> $(1) (exunit)"
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/*_test.exs";
endef
#==> Compilation tasks
APP := lib/elixir/ebin/elixir.app
PARSER := lib/elixir/src/elixir_parser.erl
KERNEL := lib/elixir/ebin/Elixir.Kernel.beam
UNICODE := lib/elixir/ebin/Elixir.String.Unicode.beam
KERNEL:=lib/elixir/ebin/Elixir.Kernel.beam
UNICODE:=lib/elixir/ebin/Elixir.String.Unicode.beam
default: compile
compile: erlang $(APP) elixir
compile: erlang elixir
erlang: $(PARSER)
$(Q) if [ ! -f $(APP) ]; then $(call CHECK_ERLANG_RELEASE); fi
$(Q) cd lib/elixir && mkdir -p ebin && erl -make
$(PARSER): lib/elixir/src/elixir_parser.yrl
$(Q) erlc -o $@ +'{verbose,true}' +'{report,true}' $<
erlang:
$(Q) cd lib/elixir && $(REBAR) compile
# 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
$(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
$(Q) if [ ! -f $(KERNEL) ]; then \
$(call CHECK_ERLANG_RELEASE); \
echo "==> bootstrap (compile)"; \
$(ERL) -s elixir_compiler bootstrap -s erlang halt; \
fi
@@ -79,11 +77,8 @@ $(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/kernel.ex" -o ebin;
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/**/*.ex" -o ebin;
$(Q) $(MAKE) unicode
$(Q) $(MAKE) app
app: $(APP)
$(APP): lib/elixir/src/elixir.app.src lib/elixir/ebin VERSION $(GENERATE_APP)
$(Q) $(GENERATE_APP) $< $@ $(VERSION)
$(Q) rm -f lib/elixir/ebin/elixir.app
$(Q) cd lib/elixir && $(REBAR) compile
unicode: $(UNICODE)
$(UNICODE): lib/elixir/unicode/*
@@ -114,9 +109,9 @@ install: compile
$(MAKE) install_man
clean:
cd lib/elixir && $(REBAR) clean
rm -rf ebin
rm -rf lib/*/ebin
rm -rf $(PARSER)
$(Q) $(MAKE) clean_residual_files
clean_elixir:
@@ -138,7 +133,7 @@ clean_residual_files:
LOGO_PATH = $(shell test -f ../docs/logo.png && echo "--logo ../docs/logo.png")
SOURCE_REF = $(shell tag="$(call GIT_TAG)" revision="$(call GIT_REVISION)"; echo "$${tag:-$$revision}\c")
DOCS_FORMAT = html
COMPILE_DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" -m "$(3)" -u "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) -o doc/$(2) -n https://hexdocs.pm/$(2)/$(CANONICAL) -p https://elixir-lang.org/docs.html -f "$(DOCS_FORMAT)" $(4)
COMPILE_DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" -m "$(3)" -u "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) -o doc/$(2) -n https://hexdocs.pm/$(2)/$(CANONICAL) -p http://elixir-lang.org/docs.html -f "$(DOCS_FORMAT)" $(4)
docs: compile ../ex_doc/bin/ex_doc docs_elixir docs_eex docs_mix docs_iex docs_ex_unit docs_logger
@@ -189,18 +184,10 @@ Precompiled.zip: build_man compile
@ echo "Precompiled file created $(CURDIR)/Precompiled-v$(VERSION).zip"
zips: Precompiled.zip Docs.zip
@ echo ""
@ echo "## Checksums"
@ echo ""
@ shasum -a 1 < Precompiled-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA1:"
@ shasum -a 512 < Precompiled-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Precompiled.zip SHA512:"
@ shasum -a 1 < Docs-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA1:"
@ shasum -a 512 < Docs-v$(VERSION).zip | sed -e "s/-//" | xargs echo " * Docs.zip SHA512:"
@ echo ""
#==> Test tasks
test: test_formatted test_erlang test_elixir
test: test_erlang test_elixir
test_windows: test test_taskkill
@@ -212,9 +199,6 @@ TEST_ERL = lib/elixir/test/erlang
TEST_EBIN = lib/elixir/test/ebin
TEST_ERLS = $(addprefix $(TEST_EBIN)/, $(addsuffix .beam, $(basename $(notdir $(wildcard $(TEST_ERL)/*.erl)))))
test_formatted: compile
bin/elixir bin/mix format --check-formatted
test_erlang: compile $(TEST_ERLS)
@ echo "==> elixir (eunit)"
$(Q) $(ERL) -pa $(TEST_EBIN) -s test_helper test;
@@ -227,7 +211,7 @@ $(TEST_EBIN)/%.beam: $(TEST_ERL)/%.erl
test_elixir: test_stdlib test_ex_unit test_logger test_mix test_eex test_iex
test_stdlib: compile
@ echo "==> elixir (ex_unit)"
@ echo "==> elixir (exunit)"
$(Q) exec epmd & exit
$(Q) if [ "$(OS)" = "Windows_NT" ]; then \
cd lib/elixir && cmd //C call ../../bin/elixir.bat -r "test/elixir/test_helper.exs" -pr "test/elixir/**/*_test.exs"; \
@@ -251,7 +235,7 @@ build_plt: clean_plt $(PLT)
dialyze: compile $(PLT)
@ echo "==> Dialyzing Elixir..."
$(Q) dialyzer -pa lib/elixir/ebin --plt $(PLT) $(DIALYZER_OPTS) lib/*/ebin
$(Q) dialyzer --plt $(PLT) $(DIALYZER_OPTS) lib/*/ebin
#==> Man page tasks
@@ -276,9 +260,9 @@ clean_man:
rm -f man/iex.1.bak
install_man: build_man
$(Q) mkdir -p $(DESTDIR)$(MAN_PREFIX)/man1
$(Q) $(INSTALL_DATA) man/elixir.1 $(DESTDIR)$(MAN_PREFIX)/man1
$(Q) $(INSTALL_DATA) man/elixirc.1 $(DESTDIR)$(MAN_PREFIX)/man1
$(Q) $(INSTALL_DATA) man/iex.1 $(DESTDIR)$(MAN_PREFIX)/man1
$(Q) $(INSTALL_DATA) man/mix.1 $(DESTDIR)$(MAN_PREFIX)/man1
$(Q) mkdir -p $(DESTDIR)$(SHARE_PREFIX)/man/man1
$(Q) $(INSTALL_DATA) man/elixir.1 $(DESTDIR)$(SHARE_PREFIX)/man/man1
$(Q) $(INSTALL_DATA) man/elixirc.1 $(DESTDIR)$(SHARE_PREFIX)/man/man1
$(Q) $(INSTALL_DATA) man/iex.1 $(DESTDIR)$(SHARE_PREFIX)/man/man1
$(Q) $(INSTALL_DATA) man/mix.1 $(DESTDIR)$(SHARE_PREFIX)/man/man1
$(MAKE) clean_man
+3 -17
View File
@@ -1,7 +1,9 @@
LEGAL NOTICE INFORMATION
------------------------
All the files in this distribution are copyright to the terms below.
All the files in this distribution are copyright (c) 2012 Plataformatec
covered under Elixir's license (see the file LICENSE) except the cases
below.
== lib/elixir/src/elixir_parser.erl (generated by build scripts)
@@ -18,19 +20,3 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
== All other files
Copyright 2012 Plataformatec
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+18 -22
View File
@@ -3,15 +3,10 @@
[![Travis build](https://secure.travis-ci.org/elixir-lang/elixir.svg?branch=master
"Build Status")](https://travis-ci.org/elixir-lang/elixir)
Elixir is a dynamic, functional language designed for building scalable
and maintainable applications.
Elixir is a dynamic, functional language designed for building scalable and maintainable applications.
For more about Elixir, installation and documentation,
[check Elixir's website](https://elixir-lang.org/).
## Announcements
New releases are announced in the [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).
[check Elixir's website](http://elixir-lang.org/).
## Compiling from source
@@ -34,11 +29,11 @@ If Elixir fails to build (specifically when pulling in a new version via
If tests pass, you are ready to move on to the [Getting Started guide][1]
or to try Interactive Elixir by running `bin/iex` in your terminal.
However, if tests fail, it is likely you have an outdated Erlang/OTP version
(Elixir requires Erlang/OTP 20.0 or later). You can check your Erlang/OTP version
However, if tests fail, it is likely you have an outdated Erlang version
(Elixir requires Erlang 19.0 or later). You can check your Erlang version
by calling `erl` in the command line. You will see some information as follows:
Erlang/OTP 20 [erts-9.0] [smp:2:2] [async-threads:10] [kernel-poll:false]
Erlang/OTP 19 [erts-8.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.
@@ -46,8 +41,8 @@ you may want to open up a bug report, as explained next.
## Bug reports
For reporting bugs, [visit our issues tracker][2] and follow the steps
for reporting a new issue. **Please disclose security vulnerabilities
privately at elixir-security@googlegroups.com**.
for reporting a new issue. Please disclose security vulnerabilities
privately at elixir-security@googlegroups.com.
## Proposing new features
@@ -91,7 +86,7 @@ also run tests for a specific framework `make test_#{NAME}`, for example,
`make test_ex_unit`. If you just changed something in the Elixir's standard
library, you can run only that portion through `make test_stdlib`.
If you are changing just one file, you can choose to compile and run tests only
In case you are changing a single file, you can compile and run tests only
for that particular file for fast development cycles. For example, if you
are changing the String module, you can compile it and run its tests as:
@@ -106,9 +101,9 @@ To recompile (including Erlang modules):
make compile
```
After your changes are done, please remember to run `mix format` to guarantee
all files are properly formatted and then run the full suite with
`make test`.
After your changes are done, please remember to run the full suite with
`make test` and then `mix format` to guarantee all files are properly
formatted.
If your contribution fails during the bootstrapping of the language,
you can rebuild the language from scratch with:
@@ -127,9 +122,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
@@ -147,7 +142,7 @@ rather manage all changes yourself, you can disable "Allow edits from maintainer
feature when submitting your pull request.
The Elixir team may optionally assign someone to review a pull request.
If someone is assigned, they must explicitly approve the code before
In case someone is assigned, they must explicitly approve the code before
another team member can merge it.
When the review finishes, your pull request will be squashed and merged
@@ -184,7 +179,7 @@ the `doc` directory. If you are planning to contribute documentation,
[3]: https://groups.google.com/group/elixir-lang-core
[4]: https://webchat.freenode.net/?channels=#elixir-lang
[5]: http://www.freenode.net
[6]: https://elixir-lang.org/docs.html
[6]: http://elixir-lang.org/docs.html
[7]: CODE_OF_CONDUCT.md
## License
@@ -193,4 +188,5 @@ the `doc` directory. If you are planning to contribute documentation,
Elixir source code is released under Apache 2 License.
Check [NOTICE](NOTICE) and [LICENSE](LICENSE) files for more information.
Check [NOTICE](NOTICE) and [LICENSE](LICENSE) files for more
information.
+19 -27
View File
@@ -1,45 +1,37 @@
# Release process
## Shipping a new version
## All releases
This document simply outlines the release process:
1. Ensure you are running on the oldest supported Erlang version
2. Update version in /VERSION
2. Remove all `-dev` extension from versions (see below for all files)
3. Ensure /CHANGELOG.md is updated, versioned and add the current date
3. Ensure CHANGELOG is updated and add current date
4. Update "Compatibility and Deprecations" if a new OTP version is supported
4. Update "Compatibility and Deprecations" if a new OTP version is supported. If a new `vMAJOR.MINOR`, replace "master" with "vVERSION" in the "Deprecations" section
5. Commit changes above with title "Release vVERSION" and generate a new tag
5. If a new `vMAJOR.MINOR`, create a new branch "vMAJOR.MINOR" and set `CANONICAL=` in Makefile
6. Run `make clean test` to ensure all tests pass from scratch and the CI is green
6. Add an entry for the new version to the OTP compatibility table in the "Compatibility and Deprecations" page
7. Recompile an existing project (for example, Ecto) to ensure manifests can be upgraded
7. Commit changes above with title "Release vVERSION" and generate new tag
8. Push branch and the new tag
8. Run `make clean test` to ensure all tests pass from scratch and the CI is green
9. Publish new zips with `make zips`, upload `Precompiled.zip` and `Docs.zip` to GitHub Releases, and include SHAs+CHANGELOG
9. Recompile an existing project (for example, Ecto) to ensure manifests can be upgraded
10. Add the release to `elixir.csv` (all releases) and `_data/elixir-versions.yml` (except for RCs) files in `elixir-lang/elixir-lang.github.com`
10. Push branch and the new tag
11. Send an e-mail to elixir-lang-ann@googlegroups.com with title "Elixir vVERSION released". The body should be a link to the Release page on GitHub and the checksums. If it is a security release, prefix the title with the `[security]` tag
11. Publish new zips with `make zips`, upload `Precompiled.zip` and `Docs.zip` to GitHub Releases
## Creating a new vMAJOR.MINOR branch
12. Add the release to `elixir.csv` and `_data/elixir-versions.yml` files in `elixir-lang/elixir-lang.github.com`
### In the new branch
13. After a new `vMAJOR.MINOR`, move back to master, bump versions, start new CHANGELOG, add `-dev` back and commit "Start vMAJOR.MINOR+1"
1. Set `CANONICAL=` in /Makefile
## Places where version is mentioned
2. Update tables in "Compatibility and Deprecations"
3. Commit "Prepare vMAJOR.MINOR for release"
### Back in master
1. Bump /VERSION file
2. Start new /CHANGELOG.md
3. Update tables in "Compatibility and Deprecations"
4. Commit "Start vMAJOR.MINOR+1"
* VERSION
* CHANGELOG.md
* lib/elixir/src/elixir.app.src
+1 -1
View File
@@ -1 +1 @@
1.8.2
1.6.2
+5 -1
View File
@@ -117,7 +117,11 @@ if [ "$OS" = "Windows_NT" ] && [ $USE_WERL ]; then
fi
if [ -z "$ERL_PATH" ]; then
ERL_PATH="$ERL_EXEC"
if [ -f "$SCRIPT_PATH/../releases/RELEASES" ] && [ -f "$SCRIPT_PATH/erl" ]; then
ERL_PATH="$SCRIPT_PATH"/"$ERL_EXEC"
else
ERL_PATH="$ERL_EXEC"
fi
fi
exec "$ERL_PATH" -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL -extra "$@"
+1 -1
View File
@@ -50,7 +50,7 @@ set originPath=%~dp0
rem Optional parameters before the "-extra" parameter
set beforeExtra=
rem Option which determines whether or not to use werl vs erl
rem Flag which determines whether or not to use werl vs erl
set useWerl=0
rem Designates which mode / Elixir component to run as
+5 -6
View File
@@ -1,7 +1,6 @@
defmodule EEx.SyntaxError do
defexception [:message, :file, :line]
@impl true
def message(exception) do
"#{exception.file}:#{exception.line}: #{exception.message}"
end
@@ -12,7 +11,7 @@ defmodule EEx do
EEx stands for Embedded Elixir. It allows you to embed
Elixir code inside a string in a robust way.
iex> EEx.eval_string("foo <%= bar %>", bar: "baz")
iex> EEx.eval_string "foo <%= bar %>", [bar: "baz"]
"foo baz"
## API
@@ -84,7 +83,7 @@ defmodule EEx do
An example is the `@` macro which allows easy data access
in a template:
iex> EEx.eval_string("<%= @foo %>", assigns: [foo: 1])
iex> EEx.eval_string "<%= @foo %>", assigns: [foo: 1]
"1"
In other words, `<%= @foo %>` translates to:
@@ -167,7 +166,7 @@ defmodule EEx do
Gets a string `source` and generate a quoted expression
that can be evaluated by Elixir or compiled to a function.
"""
@spec compile_string(String.t(), keyword) :: Macro.t()
@spec compile_string(String.t(), keyword) :: Macro.t() | no_return
def compile_string(source, options \\ []) when is_binary(source) and is_list(options) do
EEx.Compiler.compile(source, options)
end
@@ -176,7 +175,7 @@ defmodule EEx do
Gets a `filename` and generate a quoted expression
that can be evaluated by Elixir or compiled to a function.
"""
@spec compile_file(String.t(), keyword) :: Macro.t()
@spec compile_file(String.t(), keyword) :: Macro.t() | no_return
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)
@@ -187,7 +186,7 @@ defmodule EEx do
## Examples
iex> EEx.eval_string("foo <%= bar %>", bar: "baz")
iex> EEx.eval_string "foo <%= bar %>", [bar: "baz"]
"foo baz"
"""
+1 -1
View File
@@ -9,7 +9,7 @@ defmodule EEx.Compiler do
and the engine together by handling the tokens and invoking
the engine every time a full expression or text is received.
"""
@spec compile(String.t(), keyword) :: Macro.t()
@spec compile(String.t(), keyword) :: Macro.t() | no_return
def compile(source, opts) when is_binary(source) and is_list(opts) do
file = opts[:file] || "nofile"
line = opts[:line] || 1
+97 -114
View File
@@ -2,70 +2,58 @@ defmodule EEx.Engine do
@moduledoc ~S"""
Basic EEx engine that ships with Elixir.
An engine needs to implement all callbacks below.
An engine needs to implement six functions:
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`.
* `init(opts)` - called at the beginning of every text
and it must return the initial state.
* `handle_body(state)` - receives the state of the document
and it must return a quoted expression.
* `handle_text(state, text)` - it receives the state,
the text and must return a new quoted expression.
* `handle_expr(state, marker, expr)` - it receives the state,
the marker, the expr and must return a new state.
* `handle_begin(state)` - called every time there a new state
is needed with an empty buffer. Typically called for do/end
blocks, case expressions, anonymous functions, etc
* `handle_end(state)` - opposite of `handle_begin(state)` and
it must return quoted expression
The marker is what follows exactly after `<%`. For example,
`<% foo %>` has an empty marker, but `<%= foo %>` has `"="`
as marker. The allowed markers so far are:
* `""`
* `"="`
* `"/"`
* `"|"`
Markers `"/"` and `"|"` are only for use in custom EEx engines
and are not implemented by default. Using them without the
implementation raises `EEx.SyntaxError`.
If your engine does not implement all markers, please ensure that
`handle_expr/3` falls back to `EEx.Engine.handle_expr/3`
to raise the proper error message.
Read `handle_expr/3` below for more information about the markers
implemented by default by this engine.
`EEx.Engine` can be used directly if one desires to use the
default implementations for the functions above.
"""
@type state :: term
@doc """
Called at the beginning of every template.
It must return the initial state.
"""
@callback init(opts :: keyword) :: state
@doc """
Called at the end of every template.
It must return Elixir's quoted expressions for the template.
"""
@callback handle_body(state) :: Macro.t()
@doc """
Called for the text/static parts of a template.
It must return the updated state.
"""
@callback handle_text(state, text :: String.t()) :: state
@doc """
Called for the dynamic/code parts of a template.
The marker is what follows exactly after `<%`. For example,
`<% foo %>` has an empty marker, but `<%= foo %>` has `"="`
as marker. The allowed markers so far are:
* `""`
* `"="`
* `"/"`
* `"|"`
Markers `"/"` and `"|"` are only for use in custom EEx engines
and are not implemented by default. Using them without an
appropriate implementation raises `EEx.SyntaxError`.
It must return the updated state.
"""
@callback handle_expr(state, marker :: String.t(), expr :: Macro.t()) :: state
@doc """
Invoked at the beginning of every nesting.
It must return a new state that is used only inside the nesting.
Once the nesting terminates, the current `state` is resumed.
"""
@callback handle_begin(state) :: state
@doc """
Invokes at the end of a nesting.
It must return Elixir's quoted expressions for the nesting.
"""
@callback handle_end(state) :: Macro.t()
@doc false
@@ -77,24 +65,24 @@ defmodule EEx.Engine do
EEx.Engine.init(opts)
end
def handle_body(state) do
EEx.Engine.handle_body(state)
def handle_body(quoted) do
EEx.Engine.handle_body(quoted)
end
def handle_begin(state) do
EEx.Engine.handle_begin(state)
def handle_begin(quoted) do
EEx.Engine.handle_begin(quoted)
end
def handle_end(state) do
EEx.Engine.handle_end(state)
def handle_end(quoted) do
EEx.Engine.handle_end(quoted)
end
def handle_text(state, text) do
EEx.Engine.handle_text(state, text)
def handle_text(buffer, text) do
EEx.Engine.handle_text(buffer, text)
end
def handle_expr(state, marker, expr) do
EEx.Engine.handle_expr(state, marker, expr)
def handle_expr(buffer, marker, expr) do
EEx.Engine.handle_expr(buffer, marker, expr)
end
defoverridable EEx.Engine
@@ -110,9 +98,9 @@ defmodule EEx.Engine do
This can be added to any custom engine by invoking
`handle_assign/1` with `Macro.prewalk/2`:
def handle_expr(state, token, expr) do
def handle_expr(buffer, token, expr) do
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
super(state, token, expr)
EEx.Engine.handle_expr(buffer, token, expr)
end
"""
@@ -147,73 +135,68 @@ defmodule EEx.Engine do
end
end
@doc false
@doc """
Returns an empty string as initial buffer.
"""
def init(_opts) do
%{
binary: [],
dynamic: [],
vars_count: 0
}
""
end
@doc false
def handle_begin(state) do
check_state!(state)
%{state | binary: [], dynamic: []}
@doc """
Returns an empty string as the new buffer.
"""
def handle_begin(_previous) do
""
end
@doc false
@doc """
End of the new buffer.
"""
def handle_end(quoted) do
handle_body(quoted)
quoted
end
@doc false
def handle_body(state) do
check_state!(state)
%{binary: binary, dynamic: dynamic} = state
binary = {:<<>>, [], Enum.reverse(binary)}
dynamic = [binary | dynamic]
{:__block__, [], Enum.reverse(dynamic)}
@doc """
The default implementation simply returns the given expression.
"""
def handle_body(quoted) do
quoted
end
@doc false
def handle_text(state, text) do
%{binary: binary} = state
%{state | binary: [text | binary]}
@doc """
The default implementation simply concatenates text to the buffer.
"""
def handle_text(buffer, text) do
quote(do: unquote(buffer) <> unquote(text))
end
@doc false
def handle_expr(state, "=", ast) do
%{binary: binary, dynamic: dynamic, vars_count: vars_count} = state
var = Macro.var(:"arg#{vars_count}", __MODULE__)
@doc """
Implements expressions according to the markers.
ast =
quote do
unquote(var) = String.Chars.to_string(unquote(ast))
end
<% Elixir expression - inline with output %>
<%= Elixir expression - replace with result %>
<%/ Elixir expression - raise EEx.SyntaxError, to be implemented by custom engines %>
<%| Elixir expression - raise EEx.SyntaxError, to be implemented by custom engines %>
segment =
quote do
unquote(var) :: binary
end
%{state | dynamic: [ast | dynamic], binary: [segment | binary], vars_count: vars_count + 1}
All other markers are not implemented by this engine.
"""
def handle_expr(buffer, "=", expr) do
quote do
tmp1 = unquote(buffer)
tmp1 <> String.Chars.to_string(unquote(expr))
end
end
def handle_expr(state, "", ast) do
%{dynamic: dynamic} = state
%{state | dynamic: [ast | dynamic]}
def handle_expr(buffer, "", expr) do
quote do
tmp2 = unquote(buffer)
unquote(expr)
tmp2
end
end
def handle_expr(_state, marker, _ast) when marker in ["/", "|"] do
def handle_expr(_buffer, marker, _expr) when marker in ["/", "|"] do
raise EEx.SyntaxError,
"unsupported EEx syntax <%#{marker} %> (the syntax is valid but not supported by the current EEx engine)"
end
defp check_state!(%{binary: _, dynamic: _, vars_count: _}), do: :ok
defp check_state!(state) do
raise "unexpected EEx.Engine state: #{inspect(state)}. " <>
"This typically means a bug or an outdated EEx.Engine or tool"
end
end
+14 -11
View File
@@ -4,7 +4,8 @@ require EEx
defmodule EExTest.Compiled do
def before_compile do
{__ENV__.line, hd(tl(get_stacktrace()))}
fill_in_stacktrace()
{__ENV__.line, hd(tl(System.stacktrace()))}
end
EEx.function_from_string(:def, :string_sample, "<%= a + b %>", [:a, :b])
@@ -18,19 +19,21 @@ defmodule EExTest.Compiled do
def file_sample(arg), do: private_file_sample(arg)
def after_compile do
{__ENV__.line, hd(tl(get_stacktrace()))}
fill_in_stacktrace()
{__ENV__.line, hd(tl(System.stacktrace()))}
end
@file "unknown"
def unknown do
{__ENV__.line, hd(tl(get_stacktrace()))}
fill_in_stacktrace()
{__ENV__.line, hd(tl(System.stacktrace()))}
end
defp get_stacktrace do
defp fill_in_stacktrace do
try do
:erlang.error("failed")
rescue
_ -> __STACKTRACE__
catch
:error, _ -> System.stacktrace()
end
end
end
@@ -444,13 +447,13 @@ defmodule EExTest do
file = to_charlist(Path.relative_to_cwd(__ENV__.file))
assert EExTest.Compiled.before_compile() ==
{7, {EExTest.Compiled, :before_compile, 0, [file: file, line: 7]}}
{8, {EExTest.Compiled, :before_compile, 0, [file: file, line: 7]}}
assert EExTest.Compiled.after_compile() ==
{21, {EExTest.Compiled, :after_compile, 0, [file: file, line: 21]}}
{23, {EExTest.Compiled, :after_compile, 0, [file: file, line: 22]}}
assert EExTest.Compiled.unknown() ==
{26, {EExTest.Compiled, :unknown, 0, [file: 'unknown', line: 26]}}
{29, {EExTest.Compiled, :unknown, 0, [file: 'unknown', line: 28]}}
end
end
@@ -501,7 +504,7 @@ defmodule EExTest do
test "begin/end" do
assert_eval(
~s[BODY(INIT:TEXT(foo):EQUAL(if do\n "BEGIN:TEXT(this):END"\nelse\n "BEGIN:TEXT(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
@@ -519,7 +522,7 @@ defmodule EExTest do
end
defp assert_eval(expected, actual, binding \\ [], opts \\ []) do
opts = Keyword.merge([file: __ENV__.file, engine: opts[:engine] || EEx.Engine], opts)
opts = Enum.into([file: __ENV__.file, engine: opts[:engine] || EEx.Engine], opts)
result = EEx.eval_string(actual, binding, opts)
assert result == expected
end
+17 -20
View File
@@ -1,10 +1,6 @@
# Returns config for Elixir docs
[
extras: Path.wildcard("lib/elixir/pages/*.md"),
groups_for_functions: [
Guards: & &1[:guard] == true
],
skip_undefined_reference_warnings_on: ["compatibility-and-deprecations"],
groups_for_modules: [
# [Kernel, Kernel.SpecialForms],
@@ -12,11 +8,12 @@
Atom,
Base,
Bitwise,
Calendar,
Calendar.ISO,
Date,
DateTime,
Exception,
Float,
Function,
Integer,
NaiveDateTime,
Record,
@@ -25,8 +22,9 @@
Time,
Tuple,
URI,
Version
Version,
],
"Collections & Enumerables": [
Access,
Date.Range,
@@ -36,8 +34,9 @@
Map,
MapSet,
Range,
Stream
Stream,
],
"IO & System": [
File,
File.Stat,
@@ -49,21 +48,17 @@
Path,
Port,
StringIO,
System
],
"Calendar": [
Calendar,
Calendar.ISO,
Calendar.TimeZoneDatabase,
Calendar.UTCOnlyTimeZoneDatabase
System,
],
"Modules & Code": [
Code,
Kernel.ParallelCompiler,
Macro,
Macro.Env,
Module
Module,
],
"Processes & Applications": [
Agent,
Application,
@@ -74,9 +69,10 @@
Registry,
Supervisor,
Task,
Task.Supervisor
Task.Supervisor,
],
Protocols: [
"Protocols": [
Collectable,
Enumerable,
Inspect,
@@ -84,9 +80,10 @@
Inspect.Opts,
List.Chars,
Protocol,
String.Chars
String.Chars,
],
Deprecated: [
"Deprecated": [
Behaviour,
Dict,
GenEvent,
@@ -94,6 +91,6 @@
HashSet,
Set,
Supervisor.Spec
]
],
]
]
-13
View File
@@ -1,13 +0,0 @@
#!/usr/bin/env escript
%% -*- erlang -*-
main([Source, Target, Version]) ->
{ok, [{application, Name, Props0}]} = file:consult(Source),
Ebin = filename:dirname(Target),
Files = filelib:wildcard(filename:join(Ebin, "*.beam")),
Mods = [list_to_atom(filename:basename(F, ".beam")) || F <- Files],
Props1 = lists:keyreplace(modules, 1, Props0, {modules, Mods}),
Props = lists:keyreplace(vsn, 1, Props1, {vsn, Version}),
AppDef = io_lib:format("~tp.~n", [{application, Name, Props}]),
ok = file:write_file(Target, AppDef),
io:format("Generated ~ts.app~n", [Name]).
+132 -151
View File
@@ -1,53 +1,27 @@
defmodule Access do
@moduledoc """
Key-based access to data structures.
Key-based access to data structures using the `data[key]` syntax.
Elixir supports three main key-value constructs: keywords,
maps, and structs. It also supports two mechanisms to access those keys:
by brackets (via `data[key]`) and by dot-syntax (via `data.field`).
Elixir provides two syntaxes for accessing values. `user[:name]`
is used by dynamic structures, like maps and keywords, while
`user.name` is used by structs. The main difference is that
`user[:name]` won't raise if the key `:name` is missing but
`user.name` will raise if there is no `:name` key.
In the next section we will briefly recap the key-value constructs and then
discuss the access mechanisms.
Besides the cases above, this module provides convenience
functions for accessing other structures, like `at/1` for
lists and `elem/1` for tuples. Those functions can be used
by the nested update functions in `Kernel`, such as
`Kernel.get_in/2`, `Kernel.put_in/3`, `Kernel.update_in/3`,
`Kernel.get_and_update_in/3` and friends.
## Key-value constructs
## Dynamic lookups
Elixir provides three main key-value constructs, summarized below:
* keyword lists - they are lists of two-element tuples where
the first element is an atom. Commonly written in the
`[key: value]` syntax, they support only atom keys. Keyword
lists are used almost exclusively to pass options to functions
and macros. They keep the user ordering and allow duplicate
keys. See the `Keyword` module.
* maps - they are the "go to" key-value data structure in Elixir.
They are capable of supporting billions of keys of any type. They are
written using the `%{key => value}` syntax and also support the
`%{key: value}` syntax when the keys are atoms. They do not
have any specified ordering and do not allow duplicate keys.
See the `Map` module.
* structs - they are named maps with a pre-determined set of keys.
They are defined with `defstruct/1` and written using the
`%StructName{key: value}` syntax.
## Key-based accessors
Elixir provides two mechanisms to access data structures by key,
described next.
### Bracket-based access
The `data[key]` syntax is used to access data structures with a
dynamic number of keys, such as keywords and maps. The key can
be of any type. The bracket-based access syntax returns `nil`
if the key does not exist:
Out of the box, `Access` works with `Keyword` and `Map`:
iex> keywords = [a: 1, b: 2]
iex> keywords[:a]
1
iex> keywords[:c]
nil
iex> map = %{a: 1, b: 2}
iex> map[:a]
@@ -57,95 +31,110 @@ defmodule Access do
iex> star_ratings[1.5]
"★☆"
Note that the dynamic lookup syntax (`term[key]`) roughly translates to
`Access.get(term, key, nil)`.
`Access` can be combined with `Kernel.put_in/3` to put a value
in a given key:
iex> map = %{a: 1, b: 2}
iex> put_in map[:a], 3
%{a: 3, b: 2}
This syntax is very convenient as it can be nested arbitrarily:
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> put_in(users["john"][:age], 28)
iex> put_in users["john"][:age], 28
%{"john" => %{age: 28}, "meg" => %{age: 23}}
Furthermore, the bracket-based access syntax transparently ignores
`nil` values. When trying to access anything on a `nil` value, `nil`
is returned:
Furthermore, `Access` transparently ignores `nil` values:
iex> keywords = [a: 1, b: 2]
iex> keywords[:c][:unknown]
nil
iex> nil[:a]
nil
Since `Access` is a behaviour, it can be implemented for key-value
data structures. The implementation should be added to the
module that defines the struct being accessed. `Access` requires the
key comparison to be implemented using the `===` operator.
Internally, `data[key]` translates to `Access.get(term, key, nil)`.
Developers interested in implementing their own key-value data
structures can implement the `Access` behaviour to provide the
bracket-based access syntax. `Access` requires the key comparison
to be implemented using the `===/2` operator.
## Static lookups
### Dot-based syntax
The `Access` syntax (`data[key]`) cannot be used to access fields in
structs, since structs do not implement the `Access` behaviour by
default. It is also a design decision: the dynamic access lookup
is meant to be used for dynamic key-value structures, like maps
and keywords, and not by static ones like structs (where fields are
known and not dynamic).
The `data.field` syntax is used exclusively to access atom fields
in maps and structs. If the accessed field does not exist, an error is
raised. This is a deliberate decision: since all of the
fields in a struct are pre-determined, structs support only the
dot-based syntax and not the access one.
Imagine a struct named `User` with a `:name` field. The following would raise:
Therefore Elixir provides a static lookup for struct fields and for atom
fields in maps. Imagine a struct named `User` with a `:name` field.
The following would raise:
user = %User{name: "John"}
user[:name]
# ** (UndefinedFunctionError) undefined function User.fetch/2 (User does not implement the Access behaviour)
Instead we should use the `user.name` syntax to access fields:
Structs instead use the `user.name` syntax to access fields:
user.name
#=> "John"
The same `user.name` syntax can also be used by `Kernel.put_in/2`
for updating structs fields:
put_in user.name, "Mary"
#=> %User{name: "Mary"}
Differently from `user[:name]`, `user.name` is not extensible via
a behaviour and is restricted only to structs and atom keys in maps.
### Summing up
As mentioned above, this works for atom keys in maps as well. Refer to the
`Map` module for more information on this.
The bracket-based syntax, `user[:name]`, is used by dynamic structures,
is extensible and returns nil on missing keys.
Summing up:
The dot-based syntax, `user.name`, is used exclusively to access atom
keys in maps and structs, and it raises on missing keys.
* `user[:name]` is used by dynamic structures, is extensible and
does not raise on missing keys
* `user.name` is used by static structures, it is not extensible
and it will raise on missing keys
## Nested data structures
## Accessors
Both key-based access syntaxes can be used with the nested update
functions and macros in `Kernel`, such as `Kernel.get_in/2`, `Kernel.put_in/3`,
`Kernel.update_in/3`, `Kernel.pop_in/2`, and `Kernel.get_and_update_in/3`.
While Elixir provides built-in syntax only for traversing dynamic
and static key-value structures, this module provides convenience
functions for traversing other structures, like tuples and lists,
to be used alongside `Kernel.put_in/2` in others.
For example, to update a map inside another map:
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
iex> put_in(users["john"].age, 28)
%{"john" => %{age: 28}, "meg" => %{age: 23}}
This module provides convenience functions for traversing other
structures, like tuples and lists. These functions can be used
in all the `Access`-related functions and macros in `Kernel`.
For instance, given a user map with the `:name` and `:languages` keys,
here is how to deeply traverse the map and convert all language names
to uppercase:
For instance, given a user map with `:name` and `:languages` keys, here is how
to deeply traverse the map and convert all language names to uppercase:
iex> languages = [
...> %{name: "elixir", type: :functional},
...> %{name: "c", type: :procedural}
...> %{name: "c", type: :procedural},
...> ]
iex> user = %{name: "john", languages: languages}
iex> update_in(user, [:languages, Access.all(), :name], &String.upcase/1)
%{
name: "john",
languages: [
%{name: "ELIXIR", type: :functional},
%{name: "C", type: :procedural}
]
}
iex> update_in user, [:languages, Access.all(), :name], &String.upcase/1
%{name: "john",
languages: [%{name: "ELIXIR", type: :functional},
%{name: "C", type: :procedural}]}
See the functions `key/1`, `key!/1`, `elem/1`, and `all/0` for some of the
available accessors.
## Implementing the Access behaviour for custom data structures
In order to be able to use the `Access` behaviour with custom data structures
(which have to be structs), such structures have to implement the `Access`
behaviour. For example, for a `User` struct, this would have to be done:
defmodule User do
defstruct [:name, :email]
@behaviour Access
# Implementation of the Access callbacks...
end
"""
@type container :: keyword | struct | map
@@ -185,6 +174,28 @@ defmodule Access do
"""
@callback fetch(term :: t, key) :: {:ok, value} | :error
@doc """
Invoked in order to access the value stored under `key` in the given term `term`,
defaulting to `default` if not present.
This function should return the value under `key` in `term` if there's
such key, otherwise `default`.
For most data structures, this can be implemented using `fetch/2` internally;
for example:
def get(structure, key, default) do
case fetch(structure, key) do
{:ok, value} -> value
:error -> default
end
end
See the `Map.get/3` and `Keyword.get/3` implementations for examples of
how to implement this callback.
"""
@callback get(term :: t, key, default :: value) :: value
@doc """
Invoked in order to access the value under `key` and update it at the same time.
@@ -194,12 +205,9 @@ defmodule Access do
If the passed function returns `{get_value, update_value}`,
the return value of this callback should be `{get_value, new_data}`, where:
* `get_value` is the retrieved value (which can be operated on before being returned)
* `update_value` is the new value to be stored under `key`
* `new_data` is `data` after updating the value of `key` with `update_value`.
- `get_value` is the retrieved value (which can be operated on before being returned)
- `update_value` is the new value to be stored under `key`
- `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`
@@ -227,8 +235,10 @@ defmodule Access do
defmacrop raise_undefined_behaviour(exception, module, top) do
quote do
stacktrace = System.stacktrace()
exception =
case __STACKTRACE__ do
case stacktrace do
[unquote(top) | _] ->
reason = "#{inspect(unquote(module))} does not implement the Access behaviour"
%{unquote(exception) | reason: reason}
@@ -237,7 +247,7 @@ defmodule Access do
unquote(exception)
end
reraise exception, __STACKTRACE__
reraise exception, stacktrace
end
end
@@ -247,15 +257,6 @@ defmodule Access do
Returns `{:ok, value}` where `value` is the value under `key` if there is such
a key, or `:error` if `key` is not found.
## Examples
iex> Access.fetch(%{name: "meg", age: 26}, :name)
{:ok, "meg"}
iex> Access.fetch([ordered: true, on_timeout: :exit], :timeout)
:error
"""
@spec fetch(container, term) :: {:ok, term} | :error
@spec fetch(nil_container, any) :: :error
@@ -297,26 +298,11 @@ defmodule Access do
Returns the value under `key` if there is such a key, or `default` if `key` is
not found.
## Examples
iex> Access.get(%{name: "john"}, :name, "default name")
"john"
iex> Access.get(%{name: "john"}, :age, 25)
25
iex> Access.get([ordered: true], :timeout)
nil
"""
@spec get(container, term, term) :: term
@spec get(nil_container, any, default) :: default when default: var
def get(container, key, default \\ nil)
# Reimplementing the same logic as Access.fetch/2 here is done for performance, since
# this is called a lot and calling fetch/2 means introducing some overhead (like
# building the "{:ok, _}" tuple and deconstructing it back right away).
def get(%module{} = container, key, default) do
try do
module.fetch(container, key)
@@ -450,8 +436,8 @@ 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, %{name: "meg"}), Access.key(:name)])
"meg"
iex> get_in(%{}, [Access.key(:user, %{}), Access.key(:name)])
nil
Such is also useful when using update functions, allowing us to introduce
values as we traverse the data structure for updates:
@@ -464,8 +450,8 @@ defmodule Access do
iex> map = %{user: %{name: "john"}}
iex> get_in(map, [Access.key(:unknown, %{}), Access.key(:name, "john")])
"john"
iex> get_and_update_in(map, [Access.key(:user), Access.key(:name)], fn prev ->
...> {prev, String.upcase(prev)}
iex> get_and_update_in(map, [Access.key(:user), Access.key(:name)], fn
...> prev -> {prev, String.upcase(prev)}
...> end)
{"john", %{user: %{name: "JOHN"}}}
iex> pop_in(map, [Access.key(:user), Access.key(:name)])
@@ -502,15 +488,15 @@ defmodule Access do
The returned function is typically passed as an accessor to `Kernel.get_in/2`,
`Kernel.get_and_update_in/3`, and friends.
Similar to `key/2`, but the returned function raises if the key does not exist.
The returned function raises if the key does not exist.
## Examples
iex> map = %{user: %{name: "john"}}
iex> get_in(map, [Access.key!(:user), Access.key!(:name)])
"john"
iex> get_and_update_in(map, [Access.key!(:user), Access.key!(:name)], fn prev ->
...> {prev, String.upcase(prev)}
iex> get_and_update_in(map, [Access.key!(:user), Access.key!(:name)], fn
...> prev -> {prev, String.upcase(prev)}
...> end)
{"john", %{user: %{name: "JOHN"}}}
iex> pop_in(map, [Access.key!(:user), Access.key!(:name)])
@@ -551,16 +537,13 @@ defmodule Access do
The returned function raises if `index` is out of bounds.
Note that popping elements out of tuples is not possible and raises an
error.
## Examples
iex> map = %{user: {"john", 27}}
iex> get_in(map, [:user, Access.elem(0)])
"john"
iex> get_and_update_in(map, [:user, Access.elem(0)], fn prev ->
...> {prev, String.upcase(prev)}
iex> get_and_update_in(map, [:user, Access.elem(0)], fn
...> prev -> {prev, String.upcase(prev)}
...> end)
{"john", %{user: {"JOHN", 27}}}
iex> pop_in(map, [:user, Access.elem(0)])
@@ -573,7 +556,7 @@ defmodule Access do
"""
@spec elem(non_neg_integer) :: access_fun(data :: tuple, get_value :: term)
def elem(index) when is_integer(index) and index >= 0 do
def elem(index) when is_integer(index) do
pos = index + 1
fn
@@ -604,8 +587,8 @@ defmodule Access do
iex> list = [%{name: "john"}, %{name: "mary"}]
iex> get_in(list, [Access.all(), :name])
["john", "mary"]
iex> get_and_update_in(list, [Access.all(), :name], fn prev ->
...> {prev, String.upcase(prev)}
iex> get_and_update_in(list, [Access.all(), :name], fn
...> prev -> {prev, String.upcase(prev)}
...> end)
{["john", "mary"], [%{name: "JOHN"}, %{name: "MARY"}]}
iex> pop_in(list, [Access.all(), :name])
@@ -615,8 +598,8 @@ defmodule Access do
numbers and multiplying odd numbers by 2:
iex> require Integer
iex> get_and_update_in([1, 2, 3, 4, 5], [Access.all()], fn num ->
...> if Integer.is_even(num), do: :pop, else: {num, num * 2}
iex> get_and_update_in([1, 2, 3, 4, 5], [Access.all], fn
...> num -> if Integer.is_even(num), do: :pop, else: {num, num * 2}
...> end)
{[1, 2, 3, 4, 5], [2, 6, 10]}
@@ -665,8 +648,8 @@ defmodule Access do
iex> list = [%{name: "john"}, %{name: "mary"}]
iex> get_in(list, [Access.at(1), :name])
"mary"
iex> get_and_update_in(list, [Access.at(0), :name], fn prev ->
...> {prev, String.upcase(prev)}
iex> get_and_update_in(list, [Access.at(0), :name], fn
...> prev -> {prev, String.upcase(prev)}
...> end)
{"john", [%{name: "JOHN"}, %{name: "mary"}]}
@@ -684,8 +667,8 @@ defmodule Access do
iex> list = [%{name: "john"}, %{name: "mary"}]
iex> get_in(list, [Access.at(10), :name])
nil
iex> get_and_update_in(list, [Access.at(10), :name], fn prev ->
...> {prev, String.upcase(prev)}
iex> get_and_update_in(list, [Access.at(10), :name], fn
...> prev -> {prev, String.upcase(prev)}
...> end)
{nil, [%{name: "john"}, %{name: "mary"}]}
@@ -740,11 +723,11 @@ defmodule Access do
## Examples
iex> list = [%{name: "john", salary: 10}, %{name: "francine", salary: 30}]
iex> list = [%{name: "john", salary: 10}, %{name: "francine", salary: 30}]
iex> get_in(list, [Access.filter(&(&1.salary > 20)), :name])
["francine"]
iex> get_and_update_in(list, [Access.filter(&(&1.salary <= 20)), :name], fn prev ->
...> {prev, String.upcase(prev)}
iex> get_and_update_in(list, [Access.filter(&(&1.salary <= 20)), :name], fn
...> prev -> {prev, String.upcase(prev)}
...> end)
{["john"], [%{name: "JOHN", salary: 10}, %{name: "francine", salary: 30}]}
@@ -762,8 +745,8 @@ defmodule Access do
iex> list = [%{name: "john", salary: 10}, %{name: "francine", salary: 30}]
iex> get_in(list, [Access.filter(&(&1.salary >= 50)), :name])
[]
iex> get_and_update_in(list, [Access.filter(&(&1.salary >= 50)), :name], fn prev ->
...> {prev, String.upcase(prev)}
iex> get_and_update_in(list, [Access.filter(&(&1.salary >= 50)), :name], fn
...> prev -> {prev, String.upcase(prev)}
...> end)
{[], [%{name: "john", salary: 10}, %{name: "francine", salary: 30}]}
@@ -776,9 +759,7 @@ defmodule Access do
iex> get_in(%{}, [Access.filter(fn a -> a == 10 end)])
** (RuntimeError) Access.filter/1 expected a list, got: %{}
"""
@doc since: "1.6.0"
@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
+44 -86
View File
@@ -11,42 +11,44 @@ defmodule Agent do
## Examples
For example, the following agent implements a counter:
For example, in the Mix tool that ships with Elixir, we need
to keep a set of all tasks executed by a given project. Since
this set is shared, we can implement it with an agent:
defmodule Counter do
defmodule Mix.TasksServer do
use Agent
def start_link(initial_value) do
Agent.start_link(fn -> initial_value end, name: __MODULE__)
def start_link(_) do
Agent.start_link(fn -> MapSet.new end, name: __MODULE__)
end
def value do
Agent.get(__MODULE__, & &1)
@doc "Checks if the task has already executed"
def executed?(task, project) do
item = {task, project}
Agent.get(__MODULE__, fn set ->
item in set
end)
end
def increment do
Agent.update(__MODULE__, &(&1 + 1))
@doc "Marks a task as executed"
def put_task(task, project) do
item = {task, project}
Agent.update(__MODULE__, &MapSet.put(&1, item))
end
@doc "Resets the executed tasks and returns the previous list of tasks"
def take_all() do
Agent.get_and_update(__MODULE__, fn set ->
{Enum.into(set, []), MapSet.new}
end)
end
end
Usage would be:
Counter.start_link(0)
Counter.value #=> 0
Counter.increment #=> :ok
Counter.increment #=> :ok
Counter.value #=> 2
Thanks to the agent server process, the counter can be safely incremented
concurrently.
Agents provide a segregation between the client and server APIs (similar to
`GenServer`s). In particular, the functions passed as arguments to the calls to
`Agent` functions are invoked inside the agent (the server). This distinction
is important because you may want to avoid expensive operations inside the
agent, as they will effectively block the agent until the request is
fulfilled.
Agents provide a segregation between the client and server APIs (similar
to GenServers). In particular, the anonymous functions given to the `Agent`
are executed inside the agent (the server). This distinction is important
because you may want to avoid expensive operations inside the agent,
as they will effectively block the agent until the request is fulfilled.
Consider these two examples:
@@ -57,7 +59,7 @@ defmodule Agent do
# Compute in the agent/client
def get_something(agent) do
Agent.get(agent, & &1) |> do_something_expensive()
Agent.get(agent, &(&1)) |> do_something_expensive()
end
The first function blocks the agent. The second function copies all the state
@@ -71,61 +73,20 @@ defmodule Agent do
than in the server can lead to race conditions if multiple clients are trying
to update the same state to different values.
## How to supervise
Finally note `use Agent` defines a `child_spec/1` function, allowing the
defined module to be put under a supervision tree. The generated
`child_spec/1` can be customized with the following options:
An `Agent` is most commonly started under a supervision tree.
When we invoke `use Agent`, it automatically defines a `child_spec/1`
function that allows us to start the agent directly under a supervisor.
To start an agent under a supervisor with an initial counter of 0,
one may do:
children = [
{Counter, 0}
]
Supervisor.start_link(children, strategy: :one_for_all)
While one could also simply pass the `Counter` as a child to the supervisor,
such as:
children = [
Counter # Same as {Counter, []}
]
Supervisor.start_link(children, strategy: :one_for_all)
The definition above wouldn't work for this particular example,
as it would attempt to start the counter with an initial value
of an empty list. However, this may be a viable option in your
own agents. A common approach is to use a keyword list, as that
would allow setting the initial value and giving a name to the
counter process, for example:
def start_link(opts) do
{initial_value, opts} = Keyword.pop(opts, :initial_value, 0)
Agent.start_link(fn -> initial_value end, opts)
end
and then you can use `Counter`, `{Counter, name: :my_counter}` or
even `{Counter, initial_value: 0, name: :my_counter}` as a child
specification.
`use Agent` also accepts a list of options which configures the
child specification and therefore how it runs under a supervisor.
The generated `child_spec/1` can be customized with the following options:
* `:id` - the child specification identifier, defaults to the current module
* `:id` - the child specification id, defaults to the current module
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
* `:restart` - when the child should be restarted, defaults to `:permanent`
* `:shutdown` - how to shut down the child, either immediately or by giving it time to shut down
* `:shutdown` - how to shut down the child
For example:
use Agent, restart: :transient, shutdown: 10_000
See the "Child specification" section in the `Supervisor` module for more
detailed information. The `@doc` annotation immediately preceding
`use Agent` will be attached to the generated `child_spec/1` function.
See the `Supervisor` docs for more information.
## Name registration
@@ -181,9 +142,9 @@ defmodule Agent do
@doc """
Returns a specification to start an agent under a supervisor.
See the "Child specification" section in the `Supervisor` module for more detailed information.
See `Supervisor`.
"""
@doc since: "1.5.0"
@since "1.5.0"
def child_spec(arg) do
%{
id: Agent,
@@ -194,14 +155,11 @@ defmodule Agent do
@doc false
defmacro __using__(opts) do
quote location: :keep, bind_quoted: [opts: opts] do
if Module.get_attribute(__MODULE__, :doc) == nil do
@doc """
Returns a specification to start this module under a supervisor.
See `Supervisor`.
"""
end
@doc """
Returns a specification to start this module under a supervisor.
See `Supervisor`.
"""
def child_spec(arg) do
default = %{
id: __MODULE__,
@@ -220,9 +178,9 @@ defmodule Agent do
This is often used to start the agent as part of a supervision tree.
Once the agent is spawned, the given function `fun` is invoked in the server
process, and should return the initial agent state. Note that `start_link/2`
does not return until the given function has returned.
Once the agent is spawned, the given function `fun` is invoked and its return
value is used as the agent state. Note that `start_link/2` does not return
until the given function has returned.
## Options
+10 -2
View File
@@ -23,10 +23,18 @@ defmodule Agent.Server do
{:reply, :ok, run(fun, [state])}
end
def handle_call(msg, from, state) do
super(msg, from, state)
end
def handle_cast({:cast, fun}, state) do
{:noreply, run(fun, [state])}
end
def handle_cast(msg, state) do
super(msg, state)
end
def code_change(_old, state, fun) do
{:ok, run(fun, [state])}
end
@@ -37,8 +45,8 @@ defmodule Agent.Server do
end
defp get_initial_call(fun) when is_function(fun, 0) do
{:module, module} = Function.info(fun, :module)
{:name, name} = Function.info(fun, :name)
{:module, module} = :erlang.fun_info(fun, :module)
{:name, name} = :erlang.fun_info(fun, :name)
{module, name, 0}
end
+109 -249
View File
@@ -2,87 +2,61 @@ defmodule Application do
@moduledoc """
A module for working with applications and defining application callbacks.
Applications are the idiomatic way to package software in Erlang/OTP. To get
the idea, they are similar to the "library" concept common in other
programming languages, but with some additional characteristics.
In Elixir (actually, in Erlang/OTP), an application is a component
implementing some specific functionality, that can be started and stopped
as a unit, and which can be re-used in other systems.
An application is a component implementing some specific functionality, with a
standardized directory structure, configuration, and lifecycle. Applications
are *loaded*, *started*, and *stopped*.
Applications are defined with an application file named `APP.app` where
`APP` is the application name, usually in `underscore_case`. The application
file must reside in the same `ebin` directory as the compiled modules of the
application. In Elixir, the Mix build tool is responsible for compiling your
source code and generating your application `.app` file. You can learn more
about the generation of `.app` files by typing `mix help compile.app`.
## The application resource file
Once your application is compiled, running your system is a matter of starting
your current application and its dependencies. Differently from other languages,
Elixir does not have a `main` procedure that is responsible for starting your
system. Instead, you start one or more applications, each with their own
initialization and termination logic.
Applications are specified in their [*resource
file*](http://erlang.org/doc/man/app.html), which is a file called `APP.app`,
where `APP` is the application name. For example, the application resource
file of the OTP application `ex_unit` is called `ex_unit.app`.
Applications also provide an "application environment", which provides one
mechanism for configuring long running applications. We will learn more about
the tooling, start and shutdown and the application environment in the next
sections.
You'll find the resource file of an application in its `ebin` directory, it is
generated automatically by Mix. Some of its keys are taken from the keyword
lists returned by the `project/0` and `application/0` functions defined in
`mix.exs`, and others are generated by Mix itself.
## Start and shutdown
You can learn more about the generation of application resource files in the
documentation of `Mix.Tasks.Compile.App`, available as well by running
`mix help compile.app`.
Starting an application is done via the "application module callback", which
is a module that defines the `start/2` function. The `start/2` function should
then start a supervisor, which is often called as the top-level supervisor, since
it sits at the root of a potentially long supervision tree. When the system is
shutting down, all applications shut down their top-level supervisor, which
terminates children in the opposite order they are started.
## The application environment
Shutting down a live system cleanly can be done by calling `System.stop/1`.
It will shut down all applications in the opposite order they are started.
Each application will then shutdown its top-level supervisor, if one is
available, [which then shuts down its children](Supervisor.html#module-start-and-shutdown).
The key `env` of an application resource file has a list of tuples that map
atoms to terms, and its contents are known as the application *environment*.
Note that this environment is unrelated to the operating system environment.
From Erlang/OTP 19.1, a SIGTERM from the operating system will automatically
translate to `System.stop/0`. Erlang/OTP 20 gives user more explicit control
over OS signals via the `:os.set_signal/2` function.
By default, the environment of an application is an empty list. In a Mix
project you can set that key in `application/0`:
### Application module callback
def application do
[env: [redis_host: "localhost"]]
end
An application may start and stop a supervision tree when it boots via
the application module callback.
and the generated application resource file is going to have it included.
The environment is available after loading the application, which is a process
explained later:
Application.load(:APP_NAME)
#=> :ok
Application.get_env(:APP_NAME, :redis_host)
#=> "localhost"
In Mix projects, the environment of the application and its dependencies can
be overridden via the `config/config.exs` file. If you start the application
with Mix, that configuration is available at compile time, and at runtime too,
but take into account it is not included in the generated application resource
file, and it is not available if you start the application without Mix.
For example, someone using your application can override its `:redis_host`
environment variable as follows:
config :APP_NAME, redis_host: "redis.local"
The function `put_env/3` allows dynamic configuration of the application
environment, but as a rule of thumb each application is responsible for its
own environment. Please do not use the functions in this module for directly
accessing or modifying the environment of other applications.
The application environment can be overridden via the `-config` option of
`erl`, as well as command-line options, as we are going to see below.
## The application callback module
The `mod` key of an application resource file configures an application
callback module and start argument:
The first step is to pass the module callback in the application definition
in the `mix.exs` file:
def application do
[mod: {MyApp, []}]
end
This key is optional, only needed for applications that start a supervision tree.
The `MyApp` module given to `:mod` needs to implement the `Application` behaviour.
This can be done by putting `use Application` in that module and implementing the
`c:start/2` callback, for example:
Our application now requires the `MyApp` module to provide an application
callback. This can be done by invoking `use Application` in that module and
defining a `start/2` callback, for example:
defmodule MyApp do
use Application
@@ -93,14 +67,17 @@ defmodule Application do
end
end
The `c:start/2` callback has to spawn and link a supervisor and return `{:ok,
pid}` or `{:ok, pid, state}`, where `pid` is the PID of the supervisor, and
`state` is an optional application state. `args` is the second element of the
tuple given to the `:mod` option.
`start/2` typically returns `{:ok, pid}` or `{:ok, pid, state}` where
`pid` identifies the supervision tree and `state` is the application state.
`args` is the second element of the tuple given to the `:mod` option.
The `type` argument passed to `c:start/2` is usually `:normal` unless in a
The `type` argument passed to `start/2` is usually `:normal` unless in a
distributed setup where application takeovers and failovers are configured.
Distributed applications are beyond the scope of this documentation.
Distributed applications is beyond the scope of this documentation. For those
interested on the topic, please access the OTP documentation:
* [`:application` module](http://www.erlang.org/doc/man/application.html)
* [Applications – OTP Design Principles](http://www.erlang.org/doc/design_principles/applications.html)
When an application is shutting down, its `c:stop/1` callback is called after
the supervision tree has been stopped by the runtime. This callback allows the
@@ -116,90 +93,11 @@ defmodule Application do
tree is terminated. Its argument is the state returned by `c:start/2`, if it did,
or `[]` otherwise, and its return value is passed to `c:stop/1`.
## The application lifecycle
### Loading applications
Applications are *loaded*, which means that the runtime finds and processes
their resource files:
Application.load(:ex_unit)
#=> :ok
If an application has included applications, they are also loaded. And the
procedure recurses if they in turn have included applications. Included
applications are unrelated to applications in Mix umbrella projects, they are
an Erlang/OTP concept that has to do with coordinated starts.
When an application is loaded, the environment specified in its resource file
is merged with any overrides from config files passed to `erl` via the
`-config` option. It is worth highlighting that releases pass `sys.config`
this way. The resulting environment can still be overridden again via specific
`-Application` options passed to `erl`.
Loading an application *does not* load its modules.
In practice, you rarely load applications by hand because that is part of the
start process, explained next.
### Starting applications
Applications are also *started*:
Application.start(:ex_unit)
#=> :ok
Once your application is compiled, running your system is a matter of starting
your current application and its dependencies. Differently from other languages,
Elixir does not have a `main` procedure that is responsible for starting your
system. Instead, you start one or more applications, each with their own
initialization and termination logic.
When an application is started, the runtime loads it if it hasn't been loaded
yet (in the technical sense described above). Then, it checks if the
dependencies listed in the `applications` key of the resource file are already
started. Having at least one dependency not started is an error condition, but
when you start an application with `mix run`, Mix takes care of starting all
the dependencies for you, so in practice you don't need to worry about it
unless you are starting applications manually with the API provided by this
module.
If the application does not have a callback module configured, starting is
done at this point. Otherwise, its `c:start/2` callback if invoked. The PID of
the top-level supervisor returned by this function is stored by the runtime
for later use, and the returned application state is saved too, if any.
### Stopping applications
Started applications are, finally, *stopped*:
Application.stop(:ex_unit)
#=> :ok
Stopping an application without a callback module is defined, but except for
some system tracing, it is in practice a no-op.
Stopping an application with a callback module has three steps:
1. If present, invoke the optional callback `c:prep_stop/1`.
2. Terminate the top-level supervisor.
3. Invoke the required callback `c:stop/1`.
The arguments passed to the callbacks are related to the state optionally
returned by `c:start/2`, and are documented in the section about the callback
module above.
It is important to highlight that step 2 is a blocking one. Termination of a
supervisor triggers a recursive chain of children terminations, therefore
orderly shutting down all descendant processes. The `c:stop/1` callback is
invoked only after termination of the whole supervision tree.
Shutting down a live system cleanly can be done by calling `System.stop/1`. It
will shut down every application in the opposite order they had been started.
By default, a SIGTERM from the operating system will automatically translate to
`System.stop/0`. You can also have more explicit control over OS signals via the
`:os.set_signal/2` function.
An application without a supervision tree doesn't define an application
module callback in the application definition in `mix.exs` file. Even though
there is no module with application callbacks such as `start/2` and
`stop/1`, the application can be started and stopped the same way as an
application with a supervision tree.
## Tooling
@@ -220,14 +118,41 @@ defmodule Application do
when tools must be shared between developers and not as deployment options.
See `mix help archive.build` and `mix help escript.build` for more detail.
## Further information
## Application environment
For further details on applications please check the documentation of the
[`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](http://erlang.org/doc/design_principles/users_guide.html).
Once an application is started, OTP provides an application environment
that can be used to configure the application.
Assuming you are inside a Mix project, you can edit the `application/0`
function in the `mix.exs` file to the following:
def application do
[env: [hello: :world]]
end
In the application function, we can define the default environment values
for our application. By starting your application with `iex -S mix`, you
can access the default value:
Application.get_env(:APP_NAME, :hello)
#=> :world
Applications and dependencies in Mix projects are typically configured
via the `config/config.exs` file. For example, someone using your
application can configure the `:hello` key as follows:
config :APP_NAME, hello: :brand_new_world
Keep in mind configuration files are only useful to configure static
values. For example, if you need to configure your applications based
on the system environment, the file system or on database entries,
then those configurations are better placed at runtime. For example,
one may configure applications dynamically via `put_env/3`.
Keep in mind that each application is responsible for its environment.
Do not use the functions in this module for directly accessing or modifying
the environment of other applications (as it may lead to inconsistent
data in the application environment).
"""
@doc """
@@ -296,7 +221,7 @@ defmodule Application do
@callback stop(state) :: term
@doc """
Starts an application in synchronous phases.
Start an application in synchronous phases.
This function is called after `start/2` finishes but before
`Application.start/2` returns. It will be called once for every start phase
@@ -306,18 +231,7 @@ defmodule Application do
@callback start_phase(phase :: term, start_type, phase_args :: term) ::
:ok | {:error, reason :: term}
@doc """
Callback invoked after code upgrade, if the application environment
has changed.
`changed` is a keyword list of keys and their changed values in the
application environment. `new` is a keyword list with all new keys
and their values. `removed` is a list with all removed keys.
"""
@callback config_change(changed, new, removed) :: :ok
when changed: keyword, new: keyword, removed: [atom]
@optional_callbacks start_phase: 3, prep_stop: 1, config_change: 3
@optional_callbacks start_phase: 3, prep_stop: 1
@doc false
defmacro __using__(_) do
@@ -337,8 +251,7 @@ defmodule Application do
@type key :: atom
@type value :: term
@type state :: term
@type start_type :: :normal | {:takeover, node} | {:failover, node}
@type restart_type :: :permanent | :transient | :temporary
@type start_type :: :permanent | :transient | :temporary
@application_keys [
:description,
@@ -365,7 +278,7 @@ defmodule Application do
`fetch_env/2`. Returns `nil` if the application is not loaded.
"""
@spec spec(app) :: [{key, value}] | nil
def spec(app) when is_atom(app) do
def spec(app) do
case :application.get_all_key(app) do
{:ok, info} -> :lists.keydelete(:env, 1, info)
:undefined -> nil
@@ -380,7 +293,7 @@ defmodule Application do
will raise. Returns `nil` if the application is not loaded.
"""
@spec spec(app, key) :: value | nil
def spec(app, key) when is_atom(app) and key in @application_keys do
def spec(app, key) when key in @application_keys do
case :application.get_key(app, key) do
{:ok, value} -> value
:undefined -> nil
@@ -406,7 +319,7 @@ defmodule Application do
Returns all key-value pairs for `app`.
"""
@spec get_all_env(app) :: [{key, value}]
def get_all_env(app) when is_atom(app) do
def get_all_env(app) do
:application.get_all_env(app)
end
@@ -415,44 +328,9 @@ defmodule Application do
If the configuration parameter does not exist, the function returns the
`default` value.
## Examples
`get_env/3` is commonly used to read the configuration of your OTP applications.
Since Mix configurations are commonly used to configure applications, we will use
this as a point of illustration.
Consider a new application `:my_app`. `:my_app` contains a database engine which
supports a pool of databases. The database engine needs to know the configuration for
each of those databases, and that configuration is supplied by key-value pairs in
environment of `:my_app`.
config :my_app, Databases.RepoOne,
# A database configuration
ip: "localhost",
port: 5433
config :my_app, Databases.RepoTwo,
# Another database configuration (for the same OTP app)
ip: "localhost",
port: 20717
config :my_app, my_app_databases: [Databases.RepoOne, Databases.RepoTwo]
Our database engine used by `:my_app` needs to know what databases exist, and
what the database configurations are. The database engine can make a call to
`get_env(:my_app, :my_app_databases)` to retrieve the list of databases (specified
by module names). Our database engine can then traverse each repository in the
list and then call `get_env(:my_app, Databases.RepoOne)` and so forth to retrieve
the configuration of each one.
**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. For more information,
read our [library guidelines](library-guidelines.html).
"""
@spec get_env(app, key, value) :: value
def get_env(app, key, default \\ nil) when is_atom(app) do
def get_env(app, key, default \\ nil) do
:application.get_env(app, key, default)
end
@@ -462,7 +340,7 @@ defmodule Application do
If the configuration parameter does not exist, the function returns `:error`.
"""
@spec fetch_env(app, key) :: {:ok, value} | :error
def fetch_env(app, key) when is_atom(app) do
def fetch_env(app, key) do
case :application.get_env(app, key) do
{:ok, value} -> {:ok, value}
:undefined -> :error
@@ -474,8 +352,8 @@ defmodule Application do
If the configuration parameter does not exist, raises `ArgumentError`.
"""
@spec fetch_env!(app, key) :: value
def fetch_env!(app, key) when is_atom(app) do
@spec fetch_env!(app, key) :: value | no_return
def fetch_env!(app, key) do
case fetch_env(app, key) do
{:ok, value} ->
value
@@ -513,13 +391,13 @@ defmodule Application do
environment values specified in the `.app` file will override the ones
previously set.
The `:persistent` option can be set to `true` when there is a need to guarantee
The persistent option can be set to `true` when there is a need to guarantee
parameters set with this function will not be overridden by the ones defined
in the application resource file on load. This means persistent values will
stick after the application is loaded and also on application reload.
"""
@spec put_env(app, key, value, timeout: timeout, persistent: boolean) :: :ok
def put_env(app, key, value, opts \\ []) when is_atom(app) do
def put_env(app, key, value, opts \\ []) do
:application.set_env(app, key, value, opts)
end
@@ -529,7 +407,7 @@ defmodule Application do
See `put_env/4` for a description of the options.
"""
@spec delete_env(app, key, timeout: timeout, persistent: boolean) :: :ok
def delete_env(app, key, opts \\ []) when is_atom(app) do
def delete_env(app, key, opts \\ []) do
:application.unset_env(app, key, opts)
end
@@ -543,7 +421,7 @@ defmodule Application do
:ok = Application.ensure_started(:my_test_dep)
"""
@spec ensure_started(app, restart_type) :: :ok | {:error, term}
@spec ensure_started(app, start_type) :: :ok | {:error, term}
def ensure_started(app, type \\ :temporary) when is_atom(app) do
:application.ensure_started(app, type)
end
@@ -555,7 +433,7 @@ defmodule Application do
`:applications` in the `.app` file in case they were not previously
started.
"""
@spec ensure_all_started(app, restart_type) :: {:ok, [app]} | {:error, {app, term}}
@spec ensure_all_started(app, start_type) :: {:ok, [app]} | {:error, {app, term}}
def ensure_all_started(app, type \\ :temporary) when is_atom(app) do
:application.ensure_all_started(app, type)
end
@@ -594,7 +472,7 @@ defmodule Application do
Note also that the `:transient` type is of little practical use, since when a
supervision tree terminates, the reason is set to `:shutdown`, not `:normal`.
"""
@spec start(app, restart_type) :: :ok | {:error, term}
@spec start(app, start_type) :: :ok | {:error, term}
def start(app, type \\ :temporary) when is_atom(app) do
:application.start(app, type)
end
@@ -605,7 +483,7 @@ defmodule Application do
When stopped, the application is still loaded.
"""
@spec stop(app) :: :ok | {:error, term}
def stop(app) when is_atom(app) do
def stop(app) do
:application.stop(app)
end
@@ -668,38 +546,20 @@ defmodule Application do
@doc """
Returns the given path inside `app_dir/1`.
If `path` is a string, then it will be used as the path inside `app_dir/1`. If
`path` is a list of strings, it will be joined (see `Path.join/1`) and the result
will be used as the path inside `app_dir/1`.
## Examples
File.mkdir_p!("foo/ebin")
Code.prepend_path("foo/ebin")
Application.app_dir(:foo, "my_path")
#=> "foo/my_path"
Application.app_dir(:foo, ["my", "nested", "path"])
#=> "foo/my/nested/path"
"""
@spec app_dir(app, String.t() | [String.t()]) :: String.t()
def app_dir(app, path)
def app_dir(app, path) when is_atom(app) and is_binary(path) do
def app_dir(app, path) when is_binary(path) do
Path.join(app_dir(app), path)
end
def app_dir(app, path) when is_atom(app) and is_list(path) do
def app_dir(app, path) when is_list(path) do
Path.join([app_dir(app) | path])
end
@doc """
Returns a list with information about the applications which are currently running.
"""
@spec started_applications(timeout) :: [{app, description :: charlist(), vsn :: charlist()}]
@spec started_applications(timeout) :: [tuple]
def started_applications(timeout \\ 5000) do
:application.which_applications(timeout)
end
@@ -707,7 +567,7 @@ defmodule Application do
@doc """
Returns a list with information about the applications which have been loaded.
"""
@spec loaded_applications :: [{app, description :: charlist(), vsn :: charlist()}]
@spec loaded_applications :: [tuple]
def loaded_applications do
:application.loaded_applications()
end
+1 -1
View File
@@ -38,8 +38,8 @@ defmodule Atom do
end
# TODO: Remove by 2.0
# (hard-deprecated in elixir_dispatch)
@doc false
@deprecated "Use Atom.to_charlist/1 instead"
@spec to_char_list(atom) :: charlist
def to_char_list(atom), do: Atom.to_charlist(atom)
end
+10 -13
View File
@@ -184,19 +184,16 @@ defmodule Base do
end
defp decode_char_clauses(alphabet, :mixed) when length(alphabet) == 32 do
clauses =
alphabet
|> Stream.with_index()
|> Enum.flat_map(fn {encoding, value} = pair ->
if encoding in ?A..?Z do
[pair, {encoding - ?A + ?a, value}]
else
[pair]
end
end)
|> decode_clauses()
clauses ++ bad_digit_clause()
alphabet
|> Stream.with_index()
|> Enum.flat_map(fn {encoding, value} = pair ->
if encoding in ?A..?Z do
[pair, {encoding - ?A + ?a, value}]
else
[pair]
end
end)
|> decode_clauses()
end
defp decode_mixed_clauses(first, second) do
+7 -17
View File
@@ -1,19 +1,15 @@
defmodule Behaviour do
@moduledoc """
Mechanism for handling behaviours.
WARNING: this module is deprecated.
This module is deprecated. Instead of `defcallback/1` and
`defmacrocallback/1`, the `@callback` and `@macrocallback`
module attributes can be used (respectively). See the
documentation for `Module` for more information on these
attributes.
Instead of `defcallback/1` and `defmacrocallback/1`, the `@callback` and
`@macrocallback` module attributes can be used (respectively). See the
documentation for `Module` for more information on these attributes.
Instead of `MyModule.__behaviour__(:callbacks)`,
`MyModule.behaviour_info(:callbacks)` can be used.
"""
@moduledoc deprecated: "Use @callback and @macrocallback attributes instead"
@doc """
Defines a function callback according to the given type specification.
"""
@@ -101,20 +97,14 @@ defmodule Behaviour do
end
def __behaviour__(:docs) do
{:docs_v1, _, :elixir, _, _, _, docs} = Code.fetch_docs(__MODULE__)
for {{kind, name, arity}, line, _, doc, _} <- docs, kind in [:callback, :macrocallback] do
for {tuple, line, kind, docs} <- Code.get_docs(__MODULE__, :callback_docs) do
case kind do
:callback -> {{name, arity}, line, :def, __behaviour__doc_value(doc)}
:macrocallback -> {{name, arity}, line, :defmacro, __behaviour__doc_value(doc)}
:callback -> {tuple, line, :def, docs}
:macrocallback -> {tuple, line, :defmacro, docs}
end
end
end
defp __behaviour__doc_value(:none), do: nil
defp __behaviour__doc_value(:hidden), do: false
defp __behaviour__doc_value(%{"en" => doc}), do: doc
import unquote(__MODULE__)
end
end
+3 -18
View File
@@ -6,9 +6,9 @@ defmodule Bitwise do
operators. For example:
iex> use Bitwise
iex> bnot(1) # named
iex> bnot 1 # named
-2
iex> 1 &&& 1 # operator
iex> 1 &&& 1 # operator
1
If you prefer to use only operators or skip them, you can
@@ -29,10 +29,7 @@ defmodule Bitwise do
All bitwise macros can be used in guards:
iex> use Bitwise
iex> odd? = fn
...> int when band(int, 1) == 1 -> true
...> _ -> false
...> end
iex> odd? = fn int when band(int, 1) == 1 -> true; _ -> false end
iex> odd?.(1)
true
@@ -66,7 +63,6 @@ defmodule Bitwise do
1
"""
@doc guard: true
defmacro bnot(expr) do
quote(do: :erlang.bnot(unquote(expr)))
end
@@ -80,7 +76,6 @@ defmodule Bitwise do
1
"""
@doc guard: true
defmacro ~~~expr do
quote(do: :erlang.bnot(unquote(expr)))
end
@@ -92,7 +87,6 @@ defmodule Bitwise do
1
"""
@doc guard: true
defmacro band(left, right) do
quote(do: :erlang.band(unquote(left), unquote(right)))
end
@@ -104,7 +98,6 @@ defmodule Bitwise do
1
"""
@doc guard: true
defmacro left &&& right do
quote(do: :erlang.band(unquote(left), unquote(right)))
end
@@ -116,7 +109,6 @@ defmodule Bitwise do
11
"""
@doc guard: true
defmacro bor(left, right) do
quote(do: :erlang.bor(unquote(left), unquote(right)))
end
@@ -128,7 +120,6 @@ defmodule Bitwise do
11
"""
@doc guard: true
defmacro left ||| right do
quote(do: :erlang.bor(unquote(left), unquote(right)))
end
@@ -140,7 +131,6 @@ defmodule Bitwise do
10
"""
@doc guard: true
defmacro bxor(left, right) do
quote(do: :erlang.bxor(unquote(left), unquote(right)))
end
@@ -152,7 +142,6 @@ defmodule Bitwise do
10
"""
@doc guard: true
defmacro left ^^^ right do
quote(do: :erlang.bxor(unquote(left), unquote(right)))
end
@@ -170,7 +159,6 @@ defmodule Bitwise do
-1
"""
@doc guard: true
defmacro bsl(left, right) do
quote(do: :erlang.bsl(unquote(left), unquote(right)))
end
@@ -188,7 +176,6 @@ defmodule Bitwise do
-1
"""
@doc guard: true
defmacro left <<< right do
quote(do: :erlang.bsl(unquote(left), unquote(right)))
end
@@ -206,7 +193,6 @@ defmodule Bitwise do
-4
"""
@doc guard: true
defmacro bsr(left, right) do
quote(do: :erlang.bsr(unquote(left), unquote(right)))
end
@@ -224,7 +210,6 @@ defmodule Bitwise do
-4
"""
@doc guard: true
defmacro left >>> right do
quote(do: :erlang.bsr(unquote(left), unquote(right)))
end
+17 -83
View File
@@ -17,15 +17,11 @@ defmodule Calendar do
"""
@type year :: integer
@type month :: pos_integer
@type day :: pos_integer
@type week :: pos_integer
@type day_of_week :: non_neg_integer
@type era :: non_neg_integer
@type hour :: non_neg_integer
@type minute :: non_neg_integer
@type second :: non_neg_integer
@type month :: integer
@type day :: integer
@type hour :: integer
@type minute :: integer
@type second :: integer
@typedoc """
The internal time format is used when converting between calendars.
@@ -40,8 +36,13 @@ defmodule Calendar do
The internal date format that is used when converting between calendars.
This is the number of days including the fractional part that has passed of
the last day since 0000-01-01+00:00T00:00.000000 in ISO 8601 notation (also
the last day since 0000-01-01+00:00T00:00.00000 in ISO 8601 notation (also
known as midnight 1 January BC 1 of the proleptic Gregorian calendar).
The `parts_per_day` represent how many subparts the current day is subdivided in
(for different calendars, picking a different `parts_per_day` might make sense).
The `parts_in_day` represents how many of these `parts_per_day` have passed in the
last day.
"""
@type iso_days :: {days :: integer, day_fraction}
@@ -111,40 +112,13 @@ defmodule Calendar do
std_offset: std_offset
}
@typedoc """
Specifies the time zone database for calendar operations.
Many functions in the `DateTime` module require a time zone database.
By default, it uses the default time zone database returned by
`Calendar.get_time_zone_database/0`, which defaults to
`Calendar.UTCOnlyTimeZoneDatabase` which only handles "Etc/UTC"
datetimes and returns `{:error, :utc_only_time_zone_database}`
for any other time zone.
Other time zone databases (including ones provided by packages)
can be configure as default either via configuration:
config :elixir, :time_zone_database, CustomTimeZoneDatabase
or by calling `Calendar.put_time_zone_database/1`.
See `Calendar.TimeZoneDatabase` for more information on custom
time zone databases.
"""
@type time_zone_database :: module()
@doc """
Returns how many days there are in the given year-month.
"""
@callback days_in_month(year, month) :: day
@doc """
Returns how many months there are in the given year.
"""
@callback months_in_year(year) :: month
@doc """
Returns `true` if the given year is a leap year.
Returns true if the given year is a leap year.
A leap year is a year of a longer length than normal. The exact meaning
is up to the calendar. A calendar must return `false` if it does not support
@@ -155,27 +129,7 @@ defmodule Calendar do
@doc """
Calculates the day of the week from the given `year`, `month`, and `day`.
"""
@callback day_of_week(year, month, day) :: day_of_week()
@doc """
Calculates the day of the year from the given `year`, `month`, and `day`.
"""
@callback day_of_year(year, month, day) :: non_neg_integer()
@doc """
Calculates the quarter of the year from the given `year`, `month`, and `day`.
"""
@callback quarter_of_year(year, month, day) :: non_neg_integer()
@doc """
Calculates the year and era from the given `year`.
"""
@callback year_of_era(year) :: {year, era}
@doc """
Calculates the day and era from the given `year`, `month`, and `day`.
"""
@callback day_of_era(year, month, day) :: {non_neg_integer(), era}
@callback day_of_week(year, month, day) :: non_neg_integer()
@doc """
Converts the date into a string according to the calendar.
@@ -211,24 +165,24 @@ defmodule Calendar do
@callback time_to_string(hour, minute, second, microsecond) :: String.t()
@doc """
Converts the given datetime (without time zone) into the `t:iso_days/0` format.
Converts the given datetime (with time zone) into the `t:iso_days` format.
"""
@callback naive_datetime_to_iso_days(year, month, day, hour, minute, second, microsecond) ::
iso_days
@doc """
Converts `t:iso_days/0` to the Calendar's datetime format.
Converts `t:iso_days` to the Calendar's datetime format.
"""
@callback naive_datetime_from_iso_days(iso_days) ::
{year, month, day, hour, minute, second, microsecond}
@doc """
Converts the given time to the `t:day_fraction/0` format.
Converts the given time to the `t:day_fraction` format.
"""
@callback time_to_day_fraction(hour, minute, second, microsecond) :: day_fraction
@doc """
Converts `t:day_fraction/0` to the Calendar's time format.
Converts `t:day_fraction` to the Calendar's time format.
"""
@callback time_from_day_fraction(day_fraction) :: {hour, minute, second, microsecond}
@@ -275,7 +229,6 @@ defmodule Calendar do
between them. If they are compatible, this means that we can also convert
dates as well as naive datetimes between them.
"""
@doc since: "1.5.0"
@spec compatible_calendars?(Calendar.calendar(), Calendar.calendar()) :: boolean
def compatible_calendars?(calendar, calendar), do: true
@@ -288,7 +241,6 @@ defmodule Calendar do
Returns a microsecond tuple truncated to a given precision (`:microsecond`,
`:millisecond` or `:second`).
"""
@doc since: "1.6.0"
@spec truncate(Calendar.microsecond(), :microsecond | :millisecond | :second) ::
Calendar.microsecond()
def truncate(microsecond_tuple, :microsecond), do: microsecond_tuple
@@ -299,22 +251,4 @@ defmodule Calendar do
end
def truncate(_, :second), do: {0, 0}
@doc """
Sets the current time zone database.
"""
@doc since: "1.8.0"
@spec put_time_zone_database(time_zone_database()) :: :ok
def put_time_zone_database(database) do
Application.put_env(:elixir, :time_zone_database, database)
end
@doc """
Gets the current time zone database.
"""
@doc since: "1.8.0"
@spec get_time_zone_database() :: time_zone_database()
def get_time_zone_database() do
Application.get_env(:elixir, :time_zone_database, Calendar.UTCOnlyTimeZoneDatabase)
end
end
+24 -190
View File
@@ -3,8 +3,8 @@ defmodule Date do
A Date struct and functions.
The Date struct contains the fields year, month, day and calendar.
New dates can be built with the `new/3` function or using the
`~D` (see `Kernel.sigil_D/2`) sigil:
New dates can be built with the `new/3` function or using the `~D`
sigil:
iex> ~D[2000-01-01]
~D[2000-01-01]
@@ -25,11 +25,11 @@ defmodule Date do
Developers should avoid creating the Date structs directly
and instead rely on the functions provided by this module as well
as the ones in third-party calendar libraries.
as the ones in 3rd party calendar libraries.
## Comparing dates
Comparisons in Elixir using `==/2`, `>/2`, `</2` and similar are structural
Comparisons in Elixir using `==`, `>`, `<` and similar are structural
and based on the `Date` struct fields. For proper comparison between
dates, use the `compare/2` function.
@@ -53,7 +53,7 @@ defmodule Date do
@enforce_keys [:year, :month, :day]
defstruct [:year, :month, :day, calendar: Calendar.ISO]
@type t :: %__MODULE__{
@type t :: %Date{
year: Calendar.year(),
month: Calendar.month(),
day: Calendar.day(),
@@ -85,9 +85,8 @@ defmodule Date do
true
iex> Enum.reduce(range, 0, fn _date, acc -> acc - 1 end)
-366
"""
@doc since: "1.5.0"
@spec range(Date.t(), Date.t()) :: Date.Range.t()
def range(%Date{calendar: calendar} = first, %Date{calendar: calendar} = last) do
{first_days, _} = to_iso_days(first)
@@ -115,7 +114,6 @@ defmodule Date do
true
"""
@doc since: "1.4.0"
@spec utc_today(Calendar.calendar()) :: t
def utc_today(calendar \\ Calendar.ISO)
@@ -131,7 +129,7 @@ defmodule Date do
end
@doc """
Returns `true` if the year in the given `date` is a leap year.
Returns true if the year in the given `date` is a leap year.
## Examples
@@ -147,7 +145,6 @@ defmodule Date do
true
"""
@doc since: "1.4.0"
@spec leap_year?(Calendar.date()) :: boolean()
def leap_year?(date)
@@ -168,7 +165,6 @@ defmodule Date do
29
"""
@doc since: "1.4.0"
@spec days_in_month(Calendar.date()) :: Calendar.day()
def days_in_month(date)
@@ -176,23 +172,6 @@ defmodule Date do
calendar.days_in_month(year, month)
end
@doc """
Returns the number of months in the given `date` year.
## Example
iex> Date.months_in_year(~D[1900-01-13])
12
"""
@doc since: "1.7.0"
@spec months_in_year(Calendar.date()) :: Calendar.month()
def months_in_year(date)
def months_in_year(%{calendar: calendar, year: year}) do
calendar.months_in_year(year)
end
@doc """
Builds a new ISO date.
@@ -233,8 +212,6 @@ defmodule Date do
"2000-02-28"
iex> Date.to_string(~N[2000-02-28 01:23:45])
"2000-02-28"
iex> Date.to_string(~D[-0100-12-15])
"-0100-12-15"
"""
@spec to_string(Calendar.date()) :: String.t()
@@ -248,8 +225,6 @@ defmodule Date do
Parses the extended "Dates" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
The year parsed by this function is limited to four digits.
## Examples
iex> Date.from_iso8601("2015-01-23")
@@ -265,31 +240,20 @@ defmodule Date do
@spec from_iso8601(String.t(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
def from_iso8601(string, calendar \\ Calendar.ISO)
def from_iso8601(<<?-, rest::binary>>, calendar) do
with {:ok, %{year: year} = date} <- raw_from_iso8601(rest, calendar) do
{:ok, %{date | year: -year}}
end
end
def from_iso8601(<<rest::binary>>, calendar) do
raw_from_iso8601(rest, calendar)
end
[match_date, guard_date, read_date] = Calendar.ISO.__match_date__()
defp raw_from_iso8601(string, calendar) do
with unquote(match_date) <- string,
true <- unquote(guard_date) do
{year, month, day} = unquote(read_date)
with {:ok, date} <- new(year, month, day, Calendar.ISO) do
convert(date, calendar)
end
def from_iso8601(<<year::4-bytes, ?-, month::2-bytes, ?-, day::2-bytes>>, calendar) do
with {year, ""} <- Integer.parse(year),
{month, ""} <- Integer.parse(month),
{day, ""} <- Integer.parse(day) do
with {:ok, date} <- new(year, month, day, Calendar.ISO), do: convert(date, calendar)
else
_ -> {:error, :invalid_format}
end
end
def from_iso8601(<<_::binary>>, _calendar) do
{:error, :invalid_format}
end
@doc """
Parses the extended "Dates" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
@@ -302,7 +266,6 @@ defmodule Date do
~D[2015-01-23]
iex> Date.from_iso8601!("2015:01:23")
** (ArgumentError) cannot parse "2015:01:23" as date, reason: :invalid_format
"""
@spec from_iso8601!(String.t(), Calendar.calendar()) :: t
def from_iso8601!(string, calendar \\ Calendar.ISO) do
@@ -339,19 +302,11 @@ defmodule Date do
"""
@spec to_iso8601(Calendar.date(), :extended | :basic) :: String.t()
def to_iso8601(date, format \\ :extended)
def to_iso8601(%{calendar: Calendar.ISO} = date, format) when format in [:basic, :extended] do
%{year: year, month: month, day: day} = date
def to_iso8601(date, format \\ :extended) when format in [:basic, :extended] do
%{year: year, month: month, day: day} = convert!(date, Calendar.ISO)
Calendar.ISO.date_to_iso8601(year, month, day, format)
end
def to_iso8601(%{calendar: _} = date, format) when format in [:basic, :extended] do
date
|> convert!(Calendar.ISO)
|> to_iso8601()
end
@doc """
Converts the given `date` to an Erlang date tuple.
@@ -442,7 +397,6 @@ defmodule Date do
:eq
"""
@doc since: "1.4.0"
@spec compare(Calendar.date(), Calendar.date()) :: :lt | :eq | :gt
def compare(%{calendar: calendar} = date1, %{calendar: calendar} = date2) do
%{year: year1, month: month1, day: day1} = date1
@@ -467,7 +421,7 @@ defmodule Date do
cannot compare #{inspect(date1)} with #{inspect(date2)}.
This comparison would be ambiguous as their calendars have incompatible day rollover moments.
Specify an exact time of day (using DateTime) to resolve this ambiguity
Specify an exact time of day (using `DateTime`s) to resolve this ambiguity
"""
end
end
@@ -490,7 +444,6 @@ defmodule Date do
{:ok, %Date{calendar: Calendar.Holocene, year: 12000, month: 1, day: 1}}
"""
@doc since: "1.5.0"
@spec convert(Calendar.date(), Calendar.calendar()) ::
{:ok, t} | {:error, :incompatible_calendars}
def convert(%{calendar: calendar, year: year, month: month, day: day}, calendar) do
@@ -524,7 +477,6 @@ defmodule Date do
%Date{calendar: Calendar.Holocene, year: 12000, month: 1, day: 1}
"""
@doc since: "1.5.0"
@spec convert!(Calendar.date(), Calendar.calendar()) :: t
def convert!(date, calendar) do
case convert(date, calendar) do
@@ -550,28 +502,15 @@ defmodule Date do
~D[2000-01-01]
iex> Date.add(~D[2000-01-01], 2)
~D[2000-01-03]
iex> Date.add(~N[2000-01-01 09:00:00], 2)
~D[2000-01-03]
iex> Date.add(~D[-0010-01-01], -2)
~D[-0011-12-30]
"""
@doc since: "1.5.0"
@spec add(Calendar.date(), integer()) :: t
def add(%{calendar: Calendar.ISO} = date, days) do
%{year: year, month: month, day: day} = date
{year, month, day} =
Calendar.ISO.date_to_iso_days(year, month, day)
|> Kernel.+(days)
|> Calendar.ISO.date_from_iso_days()
%Date{calendar: Calendar.ISO, year: year, month: month, day: day}
end
def add(%{calendar: calendar} = date, days) do
{base_days, fraction} = to_iso_days(date)
from_iso_days({base_days + days, fraction}, calendar)
{iso_days, fraction} = to_iso_days(date)
from_iso_days({iso_days + days, fraction}, calendar)
end
@doc """
@@ -587,13 +526,11 @@ defmodule Date do
2
iex> Date.diff(~D[2000-01-01], ~D[2000-01-03])
-2
iex> Date.diff(~D[0000-01-02], ~D[-0001-12-30])
3
iex> Date.diff(~D[2000-01-01], ~N[2000-01-03 09:00:00])
-2
"""
@doc since: "1.5.0"
@spec diff(Calendar.date(), Calendar.date()) :: integer
def diff(%{calendar: Calendar.ISO} = date1, %{calendar: Calendar.ISO} = date2) do
%{year: year1, month: month1, day: day1} = date1
@@ -647,118 +584,15 @@ defmodule Date do
2
iex> Date.day_of_week(~N[2016-11-01 01:23:45])
2
iex> Date.day_of_week(~D[-0015-10-30])
3
"""
@doc since: "1.4.0"
@spec day_of_week(Calendar.date()) :: Calendar.day()
@spec day_of_week(Calendar.date()) :: non_neg_integer()
def day_of_week(date)
def day_of_week(%{calendar: calendar, year: year, month: month, day: day}) do
calendar.day_of_week(year, month, day)
end
@doc """
Calculates the day of the year of a given `date`.
Returns the day of the year as an integer. For the ISO 8601
calendar (the default), it is an integer from 1 to 366.
## Examples
iex> Date.day_of_year(~D[2016-01-01])
1
iex> Date.day_of_year(~D[2016-11-01])
306
iex> Date.day_of_year(~D[-0015-10-30])
303
iex> Date.day_of_year(~D[2004-12-31])
366
"""
@doc since: "1.8.0"
@spec day_of_year(Calendar.date()) :: Calendar.day()
def day_of_year(date)
def day_of_year(%{calendar: calendar, year: year, month: month, day: day}) do
calendar.day_of_year(year, month, day)
end
@doc """
Calculates the quarter of the year of a given `date`.
Returns the day of the year as an integer. For the ISO 8601
calendar (the default), it is an integer from 1 to 4.
## Examples
iex> Date.quarter_of_year(~D[2016-10-31])
4
iex> Date.quarter_of_year(~D[2016-01-01])
1
iex> Date.quarter_of_year(~N[2016-04-01 01:23:45])
2
iex> Date.quarter_of_year(~D[-0015-09-30])
3
"""
@doc since: "1.8.0"
@spec quarter_of_year(Calendar.date()) :: non_neg_integer()
def quarter_of_year(date)
def quarter_of_year(%{calendar: calendar, year: year, month: month, day: day}) do
calendar.quarter_of_year(year, month, day)
end
@doc """
Calculates the year-of-era and era for a given
calendar year.
Returns a tuple `{year, era}` representing the
year within the era and the era number.
## Examples
iex> Date.year_of_era(~D[0001-01-01])
{1, 1}
iex> Date.year_of_era(~D[0000-12-31])
{1, 0}
iex> Date.year_of_era(~D[-0001-01-01])
{2, 0}
"""
@doc since: "1.8.0"
@spec year_of_era(Calendar.date()) :: {Calendar.year(), non_neg_integer()}
def year_of_era(date)
def year_of_era(%{calendar: calendar, year: year}) do
calendar.year_of_era(year)
end
@doc """
Calculates the day-of-era and era for a given
calendar `date`.
Returns a tuple `{day, era}` representing the
day within the era and the era number.
## Examples
iex> Date.day_of_era(~D[0001-01-01])
{1, 1}
iex> Date.day_of_era(~D[0000-12-31])
{1, 0}
"""
@doc since: "1.8.0"
@spec day_of_era(Calendar.date()) :: {Calendar.day(), non_neg_integer()}
def day_of_era(date)
def day_of_era(%{calendar: calendar, year: year, month: month, day: day}) do
calendar.day_of_era(year, month, day)
end
## Helpers
defimpl String.Chars do
+2 -4
View File
@@ -15,12 +15,10 @@ defmodule Date.Range do
@type t :: %__MODULE__{
first: Date.t(),
last: Date.t(),
first_in_iso_days: iso_days(),
last_in_iso_days: iso_days()
first_in_iso_days: Calendar.iso_days(),
last_in_iso_days: Calendar.iso_days()
}
@typep iso_days() :: Calendar.iso_days()
defstruct [:first, :last, :first_in_iso_days, :last_in_iso_days]
defimpl Enumerable do
+149 -526
View File
@@ -8,29 +8,30 @@ defmodule DateTime do
well as the zone abbreviation field used exclusively
for formatting purposes.
Remember, comparisons in Elixir using `==/2`, `>/2`, `</2` and friends
Remember, comparisons in Elixir using `==`, `>`, `<` and friends
are structural and based on the DateTime struct fields. For proper
comparison between datetimes, use the `compare/2` function.
Developers should avoid creating the `DateTime` struct directly
The functions on this module work with the `DateTime` struct as well
as any struct that contains the same fields as the `DateTime` struct.
Such functions expect `t:Calendar.datetime/0` in their typespecs
(instead of `t:t/0`).
Developers should avoid creating the DateTime struct directly
and instead rely on the functions provided by this module as
well as the ones in third-party calendar libraries.
well as the ones in 3rd party calendar libraries.
## Time zone database
## Where are my functions?
Many functions in this module require a time zone database.
By default, it uses the default time zone database returned by
`Calendar.get_time_zone_database/0`, which defaults to
`Calendar.UTCOnlyTimeZoneDatabase` which only handles "Etc/UTC"
datetimes and returns `{:error, :utc_only_time_zone_database}`
for any other time zone.
You will notice this module only contains conversion
functions as well as functions that work on UTC. This
is because a proper DateTime implementation requires a
TimeZone database which currently is not provided as part
of Elixir.
Other time zone databases (including ones provided by packages)
can be configure as default either via configuration:
config :elixir, :time_zone_database, CustomTimeZoneDatabase
or by calling `Calendar.put_time_zone_database/1`.
Such may be addressed in upcoming versions, meanwhile,
use 3rd party packages to provide DateTime building and
similar functionality with time zone backing.
"""
@enforce_keys [:year, :month, :day, :hour, :minute, :second] ++
@@ -95,17 +96,17 @@ defmodule DateTime do
## Examples
iex> {:ok, datetime} = DateTime.from_unix(1_464_096_368)
iex> {:ok, datetime} = DateTime.from_unix(1464096368)
iex> datetime
#DateTime<2016-05-24 13:26:08Z>
iex> {:ok, datetime} = DateTime.from_unix(1_432_560_368_868_569, :microsecond)
iex> {:ok, datetime} = DateTime.from_unix(1432560368868569, :microsecond)
iex> datetime
#DateTime<2015-05-25 13:26:08.868569Z>
The unit can also be an integer as in `t:System.time_unit/0`:
iex> {:ok, datetime} = DateTime.from_unix(143_256_036_886_856, 1024)
iex> {:ok, datetime} = DateTime.from_unix(143256036886856, 1024)
iex> datetime
#DateTime<6403-03-17 07:05:22.320Z>
@@ -154,10 +155,10 @@ defmodule DateTime do
iex> DateTime.from_unix!(0)
#DateTime<1970-01-01 00:00:00Z>
iex> DateTime.from_unix!(1_464_096_368)
iex> DateTime.from_unix!(1464096368)
#DateTime<2016-05-24 13:26:08Z>
iex> DateTime.from_unix!(1_432_560_368_868_569, :microsecond)
iex> DateTime.from_unix!(1432560368868569, :microsecond)
#DateTime<2015-05-25 13:26:08.868569Z>
"""
@@ -175,11 +176,8 @@ defmodule DateTime do
@doc """
Converts the given `NaiveDateTime` to `DateTime`.
It expects a time zone to put the `NaiveDateTime` in.
If the time zone is "Etc/UTC", it always succeeds. Otherwise,
the NaiveDateTime is checked against the time zone database
given as `time_zone_database`. See the "Time zone database"
section in the module documentation.
It expects a time zone to put the NaiveDateTime in.
Currently it only supports "Etc/UTC" as time zone.
## Examples
@@ -187,136 +185,11 @@ defmodule DateTime do
iex> datetime
#DateTime<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. 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
#DateTime<2018-10-28 02:30:00+02:00 CEST Europe/Copenhagen>
iex> second_dt
#DateTime<2018-10-28 02:30:00+01:00 CET Europe/Copenhagen>
When there is a gap in wall time - for instance in spring when the clocks are
turned forward - the latest valid datetime just before the gap and the first
valid datetime just after the gap.
iex> {:gap, just_before, just_after} = DateTime.from_naive(~N[2019-03-31 02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> just_before
#DateTime<2019-03-31 01:59:59.999999+01:00 CET Europe/Copenhagen>
iex> just_after
#DateTime<2019-03-31 03:00:00+02:00 CEST Europe/Copenhagen>
Most of the time there is one, and just one, valid datetime for a certain
date and time in a certain time zone.
iex> {:ok, datetime} = DateTime.from_naive(~N[2018-07-28 12:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> datetime
#DateTime<2018-07-28 12:30:00+02:00 CEST Europe/Copenhagen>
This function accepts any map or struct that contains at least the same fields as a `NaiveDateTime`
struct. The most common example of that is a `DateTime`. In this case the information about the time
zone of that `DateTime` is completely ignored. This is the same principle as passing a `DateTime` to
`Date.to_iso8601/2`. `Date.to_iso8601/2` extracts only the date-specific fields (calendar, year,
month and day) of the given structure and ignores all others.
This way if you have a `DateTime` in one time zone, you can get the same wall time in another time zone.
For instance if you have 2018-08-24 10:00:00 in Copenhagen and want a `DateTime` for 2018-08-24 10:00:00
in UTC you can do:
iex> cph_datetime = DateTime.from_naive!(~N[2018-08-24 10:00:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> {:ok, utc_datetime} = DateTime.from_naive(cph_datetime, "Etc/UTC", FakeTimeZoneDatabase)
iex> utc_datetime
#DateTime<2018-08-24 10:00:00Z>
If instead you want a `DateTime` for the same point time in a different time zone see the
`DateTime.shift_zone/3` function which would convert 2018-08-24 10:00:00 in Copenhagen
to 2018-08-24 08:00:00 in UTC.
"""
@doc since: "1.4.0"
@spec from_naive(
Calendar.naive_datetime(),
Calendar.time_zone(),
Calendar.time_zone_database()
) ::
{:ok, t}
| {:ambiguous, t, t}
| {:gap, t, t}
| {:error,
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
def from_naive(
naive_datetime,
time_zone,
time_zone_database \\ Calendar.get_time_zone_database()
)
def from_naive(naive_datetime, "Etc/UTC", _) do
utc_period = %{std_offset: 0, utc_offset: 0, zone_abbr: "UTC"}
{:ok, from_naive_with_period(naive_datetime, "Etc/UTC", utc_period)}
end
def from_naive(%{calendar: Calendar.ISO} = naive_datetime, time_zone, time_zone_database) do
case time_zone_database.time_zone_periods_from_wall_datetime(naive_datetime, time_zone) do
{:ok, period} ->
{:ok, from_naive_with_period(naive_datetime, time_zone, period)}
{:ambiguous, first_period, second_period} ->
first_datetime = from_naive_with_period(naive_datetime, time_zone, first_period)
second_datetime = from_naive_with_period(naive_datetime, time_zone, second_period)
{:ambiguous, first_datetime, second_datetime}
{:gap, {first_period, first_period_until_wall}, {second_period, second_period_from_wall}} ->
# `until_wall` is not valid, but any time just before is.
# So by subtracting a second and adding .999999 seconds
# we get the last microsecond just before.
before_naive =
first_period_until_wall
|> Map.put(:microsecond, {999_999, 6})
|> NaiveDateTime.add(-1)
after_naive = second_period_from_wall
latest_datetime_before = from_naive_with_period(before_naive, time_zone, first_period)
first_datetime_after = from_naive_with_period(after_naive, time_zone, second_period)
{:gap, latest_datetime_before, first_datetime_after}
{:error, _} = error ->
error
end
end
def from_naive(%{calendar: calendar} = naive_datetime, time_zone, time_zone_database)
when calendar != Calendar.ISO do
# For non-ISO calendars, convert to ISO, create ISO DateTime, and then
# convert to original calendar
iso_result =
with {:ok, in_iso} <- NaiveDateTime.convert(naive_datetime, Calendar.ISO) do
from_naive(in_iso, time_zone, time_zone_database)
end
case iso_result do
{:ok, dt} ->
convert(dt, calendar)
{:ambiguous, dt1, dt2} ->
with {:ok, dt1converted} <- convert(dt1, calendar),
{:ok, dt2converted} <- convert(dt2, calendar),
do: {:ambiguous, dt1converted, dt2converted}
{:gap, dt1, dt2} ->
with {:ok, dt1converted} <- convert(dt1, calendar),
{:ok, dt2converted} <- convert(dt2, calendar),
do: {:gap, dt1converted, dt2converted}
{:error, _} = error ->
error
end
end
defp from_naive_with_period(naive_datetime, time_zone, period) do
%{std_offset: std_offset, utc_offset: utc_offset, zone_abbr: zone_abbr} = period
@spec from_naive(NaiveDateTime.t(), Calendar.time_zone()) :: {:ok, t}
def from_naive(naive_datetime, time_zone)
def from_naive(%NaiveDateTime{} = naive_datetime, "Etc/UTC") do
%{
calendar: calendar,
hour: hour,
@@ -328,7 +201,7 @@ defmodule DateTime do
day: day
} = naive_datetime
%DateTime{
datetime = %DateTime{
calendar: calendar,
year: year,
month: month,
@@ -337,171 +210,39 @@ defmodule DateTime do
minute: minute,
second: second,
microsecond: microsecond,
std_offset: std_offset,
utc_offset: utc_offset,
zone_abbr: zone_abbr,
time_zone: time_zone
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
}
{:ok, datetime}
end
@doc """
Converts the given `NaiveDateTime` to `DateTime`.
It expects a time zone to put the NaiveDateTime in.
If the time zone is "Etc/UTC", it always succeeds. Otherwise,
the NaiveDateTime is checked against the time zone database
given as `time_zone_database`. See the "Time zone database"
section in the module documentation.
Currently it only supports "Etc/UTC" as time zone.
## Examples
iex> DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC")
#DateTime<2016-05-24 13:26:08.003Z>
iex> DateTime.from_naive!(~N[2018-05-24 13:26:08.003], "Europe/Copenhagen", FakeTimeZoneDatabase)
#DateTime<2018-05-24 13:26:08.003+02:00 CEST Europe/Copenhagen>
"""
@doc since: "1.4.0"
@spec from_naive!(
NaiveDateTime.t(),
Calendar.time_zone(),
Calendar.time_zone_database()
) :: t
def from_naive!(
naive_datetime,
time_zone,
time_zone_database \\ Calendar.get_time_zone_database()
) do
case from_naive(naive_datetime, time_zone, time_zone_database) do
@spec from_naive!(NaiveDateTime.t(), Calendar.time_zone()) :: t
def from_naive!(naive_datetime, time_zone) do
case from_naive(naive_datetime, time_zone) do
{:ok, datetime} ->
datetime
{:ambiguous, dt1, dt2} ->
raise ArgumentError,
"cannot convert #{inspect(naive_datetime)} to datetime because such " <>
"instant is ambiguous in time zone #{time_zone} as there is an overlap " <>
"between #{inspect(dt1)} and #{inspect(dt2)}"
{:gap, dt1, dt2} ->
raise ArgumentError,
"cannot convert #{inspect(naive_datetime)} to datetime because such " <>
"instant does not exist in time zone #{time_zone} as there is a gap " <>
"between #{inspect(dt1)} and #{inspect(dt2)}"
{:error, reason} ->
raise ArgumentError,
"cannot convert #{inspect(naive_datetime)} to datetime, reason: #{inspect(reason)}"
"cannot parse #{inspect(naive_datetime)} to datetime, reason: #{inspect(reason)}"
end
end
@doc """
Changes the time zone of a `DateTime`.
Returns a `DateTime` for the same point in time, but instead at
the time zone provided. It assumes that `DateTime` is valid and
exists in the given time zone and calendar.
By default, it uses the default time zone database returned by
`Calendar.get_time_zone_database/0`, which defaults to
`Calendar.UTCOnlyTimeZoneDatabase` which only handles "Etc/UTC" datetimes.
Other time zone databases can be passed as argument or set globally.
See the "Time zone database" section in the module docs.
## Examples
iex> cph_datetime = DateTime.from_naive!(~N[2018-07-16 12:00:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> {:ok, pacific_datetime} = DateTime.shift_zone(cph_datetime, "America/Los_Angeles", FakeTimeZoneDatabase)
iex> pacific_datetime
#DateTime<2018-07-16 03:00:00-07:00 PDT America/Los_Angeles>
"""
@doc since: "1.8.0"
@spec shift_zone(t, Calendar.time_zone(), Calendar.time_zone_database()) ::
{:ok, t} | {:error, :time_zone_not_found | :utc_only_time_zone_database}
def shift_zone(datetime, time_zone, time_zone_database \\ Calendar.get_time_zone_database())
def shift_zone(%{time_zone: time_zone} = datetime, time_zone, _) do
{:ok, datetime}
end
def shift_zone(datetime, time_zone, time_zone_database) do
%{
std_offset: std_offset,
utc_offset: utc_offset,
calendar: calendar,
microsecond: {_, precision}
} = datetime
datetime
|> to_iso_days()
|> apply_tz_offset(utc_offset + std_offset)
|> shift_zone_for_iso_days_utc(calendar, precision, time_zone, time_zone_database)
end
defp shift_zone_for_iso_days_utc(iso_days_utc, calendar, precision, time_zone, time_zone_db) do
case time_zone_db.time_zone_period_from_utc_iso_days(iso_days_utc, time_zone) do
{:ok, %{std_offset: std_offset, utc_offset: utc_offset, zone_abbr: zone_abbr}} ->
{year, month, day, hour, minute, second, {microsecond_without_precision, _}} =
iso_days_utc
|> apply_tz_offset(-(utc_offset + std_offset))
|> calendar.naive_datetime_from_iso_days()
datetime = %DateTime{
calendar: calendar,
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
microsecond: {microsecond_without_precision, precision},
std_offset: std_offset,
utc_offset: utc_offset,
zone_abbr: zone_abbr,
time_zone: time_zone
}
{:ok, datetime}
{:error, _} = error ->
error
end
end
@doc """
Returns the current datetime in the provided time zone.
By default, it uses the default time_zone returned by
`Calendar.get_time_zone_database/0`, which defaults to
`Calendar.UTCOnlyTimeZoneDatabase` which only handles "Etc/UTC" datetimes.
Other time zone databases can be passed as argument or set globally.
See the "Time zone database" section in the module docs.
## Examples
iex> {:ok, datetime} = DateTime.now("Etc/UTC")
iex> datetime.time_zone
"Etc/UTC"
iex> DateTime.now("Europe/Copenhagen")
{:error, :utc_only_time_zone_database}
iex> DateTime.now("not a real time zone name", FakeTimeZoneDatabase)
{:error, :time_zone_not_found}
"""
@doc since: "1.8.0"
@spec now(Calendar.time_zone(), Calendar.time_zone_database()) ::
{:ok, t} | {:error, :time_zone_not_found | :utc_only_time_zone_database}
def now(time_zone, time_zone_database \\ Calendar.get_time_zone_database())
def now("Etc/UTC", _) do
{:ok, utc_now()}
end
def now(time_zone, time_zone_database) do
shift_zone(utc_now(), time_zone, time_zone_database)
end
@doc """
Converts the given `datetime` to Unix time.
@@ -513,7 +254,7 @@ defmodule DateTime do
## Examples
iex> 1_464_096_368 |> DateTime.from_unix!() |> DateTime.to_unix()
iex> 1464096368 |> DateTime.from_unix!() |> DateTime.to_unix()
1464096368
iex> dt = %DateTime{calendar: Calendar.ISO, day: 20, hour: 18, microsecond: {273806, 6},
@@ -554,9 +295,9 @@ defmodule DateTime do
~N[2000-02-29 23:00:07.0]
"""
@spec to_naive(Calendar.datetime()) :: NaiveDateTime.t()
def to_naive(datetime) do
%{
@spec to_naive(t) :: NaiveDateTime.t()
def to_naive(%DateTime{} = datetime) do
%DateTime{
calendar: calendar,
year: year,
month: month,
@@ -594,8 +335,8 @@ defmodule DateTime do
~D[2000-02-29]
"""
@spec to_date(Calendar.datetime()) :: Date.t()
def to_date(datetime) do
@spec to_date(t) :: Date.t()
def to_date(%DateTime{} = datetime) do
%{year: year, month: month, day: day, calendar: calendar} = datetime
%Date{year: year, month: month, day: day, calendar: calendar}
end
@@ -615,8 +356,8 @@ defmodule DateTime do
~T[23:00:07.0]
"""
@spec to_time(Calendar.datetime()) :: Time.t()
def to_time(datetime) do
@spec to_time(t) :: Time.t()
def to_time(%DateTime{} = datetime) do
%{hour: hour, minute: minute, second: second, microsecond: microsecond, calendar: calendar} =
datetime
@@ -673,8 +414,12 @@ defmodule DateTime do
@spec to_iso8601(Calendar.datetime(), :extended | :basic) :: String.t()
def to_iso8601(datetime, format \\ :extended)
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format)
when format in [:extended, :basic] do
def to_iso8601(_, format) when format not in [:extended, :basic] do
raise ArgumentError,
"DateTime.to_iso8601/2 expects format to be :extended or :basic, got: #{inspect(format)}"
end
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format) do
%{
year: year,
month: month,
@@ -705,7 +450,7 @@ defmodule DateTime do
)
end
def to_iso8601(%{calendar: _} = datetime, format) when format in [:extended, :basic] do
def to_iso8601(%{calendar: _} = datetime, format) do
datetime
|> convert!(Calendar.ISO)
|> to_iso8601(format)
@@ -723,10 +468,10 @@ 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.
Time representations with reduced accuracy are not supported.
Note that 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.
## Examples
@@ -742,113 +487,67 @@ defmodule DateTime do
iex> datetime
#DateTime<2015-01-23 21:20:07.123Z>
iex> {:ok, datetime, 0} = DateTime.from_iso8601("-2015-01-23T23:50:07Z")
iex> datetime
#DateTime<-2015-01-23 23:50:07Z>
iex> {:ok, datetime, 9000} = DateTime.from_iso8601("-2015-01-23T23:50:07,123+02:30")
iex> datetime
#DateTime<-2015-01-23 21:20:07.123Z>
iex> DateTime.from_iso8601("2015-01-23P23:50:07")
{:error, :invalid_format}
iex> DateTime.from_iso8601("2015-01-23 23:50:07A")
{:error, :invalid_format}
iex> DateTime.from_iso8601("2015-01-23T23:50:07")
{:error, :missing_offset}
iex> DateTime.from_iso8601("2015-01-23 23:50:61")
{:error, :invalid_time}
iex> DateTime.from_iso8601("2015-01-32 23:50:07")
{:error, :invalid_date}
iex> DateTime.from_iso8601("2015-01-23T23:50:07.123-00:00")
{:error, :invalid_format}
iex> DateTime.from_iso8601("2015-01-23T23:50:07.123-00:60")
{:error, :invalid_format}
"""
@doc since: "1.4.0"
@spec from_iso8601(String.t(), Calendar.calendar()) ::
{:ok, t, Calendar.utc_offset()} | {:error, atom}
def from_iso8601(string, calendar \\ Calendar.ISO)
def from_iso8601(<<?-, rest::binary>>, calendar) do
raw_from_iso8601(rest, calendar, true)
end
def from_iso8601(<<rest::binary>>, calendar) do
raw_from_iso8601(rest, calendar, false)
end
@sep [?\s, ?T]
[match_date, guard_date, read_date] = Calendar.ISO.__match_date__()
[match_time, guard_time, read_time] = Calendar.ISO.__match_time__()
defp raw_from_iso8601(string, calendar, is_year_negative) do
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
def from_iso8601(string, calendar \\ Calendar.ISO) when is_binary(string) do
with <<year::4-bytes, ?-, month::2-bytes, ?-, day::2-bytes, sep, rest::binary>> <- string,
true <- sep in [?\s, ?T],
<<hour::2-bytes, ?:, min::2-bytes, ?:, sec::2-bytes, rest::binary>> <- rest,
{year, ""} <- Integer.parse(year),
{month, ""} <- Integer.parse(month),
{day, ""} <- Integer.parse(day),
{hour, ""} <- Integer.parse(hour),
{minute, ""} <- Integer.parse(min),
{second, ""} <- Integer.parse(sec),
{microsecond, rest} <- Calendar.ISO.parse_microsecond(rest),
{offset, ""} <- Calendar.ISO.parse_offset(rest) do
{year, month, day} = unquote(read_date)
{hour, minute, second} = unquote(read_time)
year = if is_year_negative, do: -year, else: year
{:ok, date} <- Date.new(year, month, day),
{:ok, time} <- Time.new(hour, minute, second, microsecond),
{:ok, offset} <- parse_offset(rest) do
%{year: year, month: month, day: day} = date
%{hour: hour, minute: minute, second: second, microsecond: microsecond} = time
{_, precision} = microsecond
cond do
not calendar.valid_date?(year, month, day) ->
{:error, :invalid_date}
datetime =
Calendar.ISO.naive_datetime_to_iso_days(
year,
month,
day,
hour,
minute,
second,
microsecond
)
|> apply_tz_offset(offset)
|> from_iso_days("Etc/UTC", "UTC", 0, 0, calendar, precision)
not calendar.valid_time?(hour, minute, second, microsecond) ->
{:error, :invalid_time}
offset == 0 ->
datetime = %DateTime{
calendar: calendar,
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
microsecond: microsecond,
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
}
{:ok, datetime, 0}
is_nil(offset) ->
{:error, :missing_offset}
true ->
day_fraction = Calendar.ISO.time_to_day_fraction(hour, minute, second, {0, 0})
{{year, month, day}, {hour, minute, second, _}} =
case apply_tz_offset({0, day_fraction}, offset) do
{0, day_fraction} ->
{{year, month, day}, Calendar.ISO.time_from_day_fraction(day_fraction)}
{extra_days, day_fraction} ->
base_days = Calendar.ISO.date_to_iso_days(year, month, day)
{Calendar.ISO.date_from_iso_days(base_days + extra_days),
Calendar.ISO.time_from_day_fraction(day_fraction)}
end
datetime = %DateTime{
calendar: calendar,
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
microsecond: microsecond,
std_offset: 0,
utc_offset: 0,
zone_abbr: "UTC",
time_zone: "Etc/UTC"
}
{:ok, datetime, offset}
end
{:ok, %{datetime | microsecond: microsecond}, offset}
else
{:error, reason} -> {:error, reason}
_ -> {:error, :invalid_format}
end
end
defp parse_offset(rest) do
case Calendar.ISO.parse_offset(rest) do
{offset, ""} when is_integer(offset) -> {:ok, offset}
{nil, ""} -> {:error, :missing_offset}
_ -> {:error, :invalid_format}
end
end
@@ -876,12 +575,6 @@ defmodule DateTime do
iex> DateTime.to_string(dt)
"2000-02-29 23:00:07-04:00 AMT America/Manaus"
iex> dt = %DateTime{year: -100, month: 12, day: 19, zone_abbr: "CET",
...> hour: 3, minute: 20, second: 31, microsecond: {0, 0},
...> utc_offset: 3600, std_offset: 0, time_zone: "Europe/Stockholm"}
iex> DateTime.to_string(dt)
"-0100-12-19 03:20:31+01:00 CET Europe/Stockholm"
"""
@spec to_string(Calendar.datetime()) :: String.t()
def to_string(%{calendar: calendar} = datetime) do
@@ -936,11 +629,10 @@ defmodule DateTime do
:gt
"""
@doc since: "1.4.0"
@spec compare(Calendar.datetime(), Calendar.datetime()) :: :lt | :eq | :gt
def compare(
%{utc_offset: utc_offset1, std_offset: std_offset1} = datetime1,
%{utc_offset: utc_offset2, std_offset: std_offset2} = datetime2
%{calendar: _, utc_offset: utc_offset1, std_offset: std_offset1} = datetime1,
%{calendar: _, utc_offset: utc_offset2, std_offset: std_offset2} = datetime2
) do
{days1, {parts1, ppd1}} =
datetime1
@@ -953,13 +645,13 @@ defmodule DateTime do
|> apply_tz_offset(utc_offset2 + std_offset2)
# Ensure fraction tuples have same denominator.
first = {days1, parts1 * ppd2}
second = {days2, parts2 * ppd1}
iso_days1 = {days1, parts1 * ppd2}
iso_days2 = {days2, parts2 * ppd1}
cond do
first > second -> :gt
first < second -> :lt
true -> :eq
case {iso_days1, iso_days2} do
{first, second} when first > second -> :gt
{first, second} when first < second -> :lt
_ -> :eq
end
end
@@ -968,8 +660,6 @@ defmodule DateTime do
The answer can be returned in any `unit` available from `t:System.time_unit/0`.
Leap seconds are not taken into account.
This function returns the difference in seconds where seconds are measured
according to `Calendar.ISO`.
@@ -987,8 +677,7 @@ defmodule DateTime do
-18000
"""
@doc since: "1.5.0"
@spec diff(Calendar.datetime(), Calendar.datetime(), System.time_unit()) :: integer()
@spec diff(Calendar.datetime(), Calendar.datetime()) :: integer()
def diff(
%{utc_offset: utc_offset1, std_offset: std_offset1} = datetime1,
%{utc_offset: utc_offset2, std_offset: std_offset2} = datetime2,
@@ -1002,81 +691,10 @@ defmodule DateTime do
naive_diff + System.convert_time_unit(offset_diff, :second, unit)
end
@doc """
Adds a specified amount of time to a `DateTime`.
Accepts an `amount_to_add` in any `unit` available from `t:System.time_unit/0`.
Negative values will move backwards in time.
Takes changes such as summer time/DST into account. This means that adding time
can cause the wall time to "go backwards" during "fall back" during autumn.
Adding just a few seconds to a datetime just before "spring forward" can cause wall
time to increase by more than an hour.
Fractional second precision stays the same in a similar way to `NaiveDateTime.add/2`.
### Examples
iex> dt = DateTime.from_naive!(~N[2018-11-15 10:00:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> dt |> DateTime.add(3600, :second, FakeTimeZoneDatabase)
#DateTime<2018-11-15 11:00:00+01:00 CET Europe/Copenhagen>
iex> dt = DateTime.from_naive!(~N[2018-11-15 10:00:00], "Etc/UTC")
iex> dt |> DateTime.add(3600, :second)
#DateTime<2018-11-15 11:00:00Z>
When adding 3 seconds just before "spring forward" we go from 1:59:59 to 3:00:02
iex> dt = DateTime.from_naive!(~N[2019-03-31 01:59:59.123], "Europe/Copenhagen", FakeTimeZoneDatabase)
iex> dt |> DateTime.add(3, :second, FakeTimeZoneDatabase)
#DateTime<2019-03-31 03:00:02.123+02:00 CEST Europe/Copenhagen>
"""
@doc since: "1.8.0"
@spec add(Calendar.datetime(), integer, System.time_unit(), Calendar.time_zone_database()) ::
t()
def add(
datetime,
amount_to_add,
unit \\ :second,
time_zone_database \\ Calendar.get_time_zone_database()
)
when is_integer(amount_to_add) do
%{
utc_offset: utc_offset,
std_offset: std_offset,
calendar: calendar,
microsecond: {_, precision}
} = datetime
ppd = System.convert_time_unit(86400, :second, unit)
total_offset = System.convert_time_unit(utc_offset + std_offset, :second, unit)
result =
datetime
|> to_iso_days()
# Subtract total offset in order to get UTC and add the integer for the addition
|> Calendar.ISO.add_day_fraction_to_iso_days(amount_to_add - total_offset, ppd)
|> shift_zone_for_iso_days_utc(calendar, precision, datetime.time_zone, time_zone_database)
case result do
{:ok, result_datetime} ->
result_datetime
{:error, error} ->
raise ArgumentError,
"cannot add #{amount_to_add} #{unit} to #{inspect(datetime)} (with time zone " <>
"database #{inspect(time_zone_database)}), reason: #{inspect(error)}"
end
end
@doc """
Returns the given datetime with the microsecond field truncated to the given
precision (`:microsecond`, `millisecond` or `:second`).
The given datetime is returned unchanged if it already has lower precision than
the given precision.
## Examples
iex> dt1 = %DateTime{year: 2017, month: 11, day: 7, zone_abbr: "CET",
@@ -1098,16 +716,11 @@ defmodule DateTime do
#DateTime<2017-11-07 11:45:18+01:00 CET Europe/Paris>
"""
@doc since: "1.6.0"
@spec truncate(Calendar.datetime(), :microsecond | :millisecond | :second) :: t()
@spec truncate(t(), :microsecond | :millisecond | :second) :: t()
def truncate(%DateTime{microsecond: microsecond} = datetime, precision) do
%{datetime | microsecond: Calendar.truncate(microsecond, precision)}
end
def truncate(%{} = datetime_map, precision) do
truncate(from_map(datetime_map), precision)
end
@doc """
Converts a given `datetime` from one calendar to another.
@@ -1131,16 +744,43 @@ defmodule DateTime do
zone_abbr: "AMT"}}
"""
@doc since: "1.5.0"
@spec convert(Calendar.datetime(), Calendar.calendar()) ::
{:ok, t} | {:error, :incompatible_calendars}
def convert(%DateTime{calendar: calendar} = datetime, calendar) do
{:ok, datetime}
end
# Keep it multiline for proper function clause errors.
def convert(
%{
calendar: calendar,
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
microsecond: microsecond,
time_zone: time_zone,
zone_abbr: zone_abbr,
utc_offset: utc_offset,
std_offset: std_offset
},
calendar
) do
datetime = %DateTime{
calendar: calendar,
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
microsecond: microsecond,
time_zone: time_zone,
zone_abbr: zone_abbr,
utc_offset: utc_offset,
std_offset: std_offset
}
def convert(%{calendar: calendar} = datetime, calendar) do
{:ok, from_map(datetime)}
{:ok, datetime}
end
def convert(%{calendar: dt_calendar, microsecond: {_, precision}} = datetime, calendar) do
@@ -1178,8 +818,7 @@ defmodule DateTime do
zone_abbr: "AMT"}
"""
@doc since: "1.5.0"
@spec convert!(Calendar.datetime(), Calendar.calendar()) :: t
@spec convert!(Calendar.datetime(), Calendar.calendar()) :: t | no_return
def convert!(datetime, calendar) do
case convert(datetime, calendar) do
{:ok, value} ->
@@ -1211,6 +850,10 @@ defmodule DateTime do
%{time_zone: time_zone, zone_abbr: zone_abbr, utc_offset: utc_offset, std_offset: std_offset} =
datetime
from_iso_days(iso_days, time_zone, zone_abbr, utc_offset, std_offset, calendar, precision)
end
defp from_iso_days(iso_days, time_zone, zone_abbr, utc_offset, std_offset, calendar, precision) do
{year, month, day, hour, minute, second, {microsecond, _}} =
calendar.naive_datetime_from_iso_days(iso_days)
@@ -1230,30 +873,10 @@ defmodule DateTime do
}
end
defp apply_tz_offset(iso_days, 0) do
iso_days
end
defp apply_tz_offset(iso_days, offset) do
Calendar.ISO.add_day_fraction_to_iso_days(iso_days, -offset, 86400)
end
defp from_map(%{} = datetime_map) do
%DateTime{
year: datetime_map.year,
month: datetime_map.month,
day: datetime_map.day,
hour: datetime_map.hour,
minute: datetime_map.minute,
second: datetime_map.second,
microsecond: datetime_map.microsecond,
time_zone: datetime_map.time_zone,
zone_abbr: datetime_map.zone_abbr,
utc_offset: datetime_map.utc_offset,
std_offset: datetime_map.std_offset
}
end
defimpl String.Chars do
def to_string(datetime) do
%{
+35 -380
View File
@@ -16,67 +16,26 @@ defmodule Calendar.ISO do
@behaviour Calendar
@unix_epoch 62_167_219_200
unix_start = (315_537_897_600 + @unix_epoch) * -1_000_000
unix_end = 315_569_519_999_999_999 - @unix_epoch * 1_000_000
@unix_range_microseconds unix_start..unix_end
@unix_start 1_000_000 * -@unix_epoch
@unix_end 315_569_519_999_999_999 - @unix_epoch * 1_000_000
@unix_range_microseconds @unix_start..@unix_end
@type year :: -9999..9999
@type year :: 0..9999
@type month :: 1..12
@type day :: 1..31
@seconds_per_minute 60
@seconds_per_hour 60 * 60
# Note that this does *not* handle leap seconds.
# Note that this does _not_ handle leap seconds.
@seconds_per_day 24 * 60 * 60
@last_second_of_the_day @seconds_per_day - 1
@microseconds_per_second 1_000_000
@parts_per_day @seconds_per_day * @microseconds_per_second
@days_per_nonleap_year 365
@days_per_leap_year 366
@months_in_year 12
# The ISO epoch starts, in this implementation,
# with ~D[0000-01-01]. Era "1" starts
# on ~D[0001-01-01] which is 366 days later.
@iso_epoch 366
@doc false
def __match_date__ do
quote do
[
<<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,
{
(y1 - ?0) * 1000 + (y2 - ?0) * 100 + (y3 - ?0) * 10 + (y4 - ?0),
(m1 - ?0) * 10 + (m2 - ?0),
(d1 - ?0) * 10 + (d2 - ?0)
}
]
end
end
@doc false
def __match_time__ do
quote do
[
<<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,
{
(h1 - ?0) * 10 + (h2 - ?0),
(i1 - ?0) * 10 + (i2 - ?0),
(s1 - ?0) * 10 + (s2 - ?0)
}
]
end
end
@doc """
Returns the `t:Calendar.iso_days/0` format of the specified date.
Returns the `t:Calendar.iso_days` format of the specified date.
## Examples
@@ -86,11 +45,8 @@ defmodule Calendar.ISO do
{730485, {43200000000, 86400000000}}
iex> Calendar.ISO.naive_datetime_to_iso_days(2000, 1, 1, 13, 0, 0, {0, 6})
{730485, {46800000000, 86400000000}}
iex> Calendar.ISO.naive_datetime_to_iso_days(-1, 1, 1, 0, 0, 0, {0, 6})
{-365, {0, 86400000000}}
"""
@doc since: "1.5.0"
@impl true
@spec naive_datetime_to_iso_days(
Calendar.year(),
@@ -106,21 +62,18 @@ defmodule Calendar.ISO do
end
@doc """
Converts the `t:Calendar.iso_days/0` format to the datetime format specified by this calendar.
Converts the `t:Calendar.iso_days` format to the datetime format specified by this calendar.
## Examples
iex> Calendar.ISO.naive_datetime_from_iso_days({0, {0, 86400}})
{0, 1, 1, 0, 0, 0, {0, 6}}
iex> Calendar.ISO.naive_datetime_from_iso_days({730_485, {0, 86400}})
iex> Calendar.ISO.naive_datetime_from_iso_days({730485, {0, 86400}})
{2000, 1, 1, 0, 0, 0, {0, 6}}
iex> Calendar.ISO.naive_datetime_from_iso_days({730_485, {43200, 86400}})
iex> Calendar.ISO.naive_datetime_from_iso_days({730485, {43200, 86400}})
{2000, 1, 1, 12, 0, 0, {0, 6}}
iex> Calendar.ISO.naive_datetime_from_iso_days({-365, {0, 86400000000}})
{-1, 1, 1, 0, 0, 0, {0, 6}}
"""
@doc since: "1.5.0"
@spec naive_datetime_from_iso_days(Calendar.iso_days()) :: {
Calendar.year(),
Calendar.month(),
@@ -148,7 +101,6 @@ defmodule Calendar.ISO do
{45296000123, 86400000000}
"""
@doc since: "1.5.0"
@impl true
@spec time_to_day_fraction(
Calendar.hour(),
@@ -170,22 +122,17 @@ defmodule Calendar.ISO do
## Examples
iex> Calendar.ISO.time_from_day_fraction({1, 2})
iex> Calendar.ISO.time_from_day_fraction({1,2})
{12, 0, 0, {0, 6}}
iex> Calendar.ISO.time_from_day_fraction({13, 24})
iex> Calendar.ISO.time_from_day_fraction({13,24})
{13, 0, 0, {0, 6}}
"""
@doc since: "1.5.0"
@impl true
@spec time_from_day_fraction(Calendar.day_fraction()) ::
{Calendar.hour(), Calendar.minute(), Calendar.second(), Calendar.microsecond()}
def time_from_day_fraction({0, _}) do
{0, 0, 0, {0, 6}}
end
def time_from_day_fraction({parts_in_day, parts_per_day}) do
total_microseconds = divide_by_parts_per_day(parts_in_day, parts_per_day)
total_microseconds = div(parts_in_day * @parts_per_day, parts_per_day)
{hours, rest_microseconds1} =
div_mod(total_microseconds, @seconds_per_hour * @microseconds_per_second)
@@ -197,11 +144,6 @@ defmodule Calendar.ISO do
{hours, minutes, seconds, {microseconds, 6}}
end
defp divide_by_parts_per_day(parts_in_day, @parts_per_day), do: parts_in_day
defp divide_by_parts_per_day(parts_in_day, parts_per_day),
do: div(parts_in_day * @parts_per_day, parts_per_day)
# Converts year, month, day to count of days since 0000-01-01.
@doc false
def date_to_iso_days(0, 1, 1) do
@@ -212,7 +154,7 @@ defmodule Calendar.ISO do
719_528
end
def date_to_iso_days(year, month, day) when year in -9999..9999 do
def date_to_iso_days(year, month, day) when year in 0..9999 do
true = day <= days_in_month(year, month)
days_in_previous_years(year) + days_before_month(month) + leap_day_offset(year, month) + day -
@@ -221,7 +163,7 @@ defmodule Calendar.ISO do
# Converts count of days since 0000-01-01 to {year, month, day} tuple.
@doc false
def date_from_iso_days(days) when days in -3_652_059..3_652_424 do
def date_from_iso_days(days) when days in 0..3_652_424 do
{year, day_of_year} = days_to_year(days)
extra_day = if leap_year?(year), do: 1, else: 0
{month, day_in_month} = year_day_to_year_date(extra_day, day_of_year)
@@ -230,13 +172,8 @@ defmodule Calendar.ISO do
defp div_mod(int1, int2) do
div = div(int1, int2)
rem = int1 - div * int2
if rem >= 0 do
{div, rem}
else
{div - 1, rem + int2}
end
mod = int1 - div * int2
{div, mod}
end
@doc """
@@ -256,8 +193,6 @@ defmodule Calendar.ISO do
29
iex> Calendar.ISO.days_in_month(2004, 4)
30
iex> Calendar.ISO.days_in_month(-1, 5)
31
"""
@spec days_in_month(year, month) :: 28..31
@@ -271,22 +206,6 @@ defmodule Calendar.ISO do
def days_in_month(_, month) when month in [4, 6, 9, 11], do: 30
def days_in_month(_, month) when month in 1..12, do: 31
@doc """
Returns how many months there are in the given year.
## Example
iex> Calendar.ISO.months_in_year(2004)
12
"""
@doc since: "1.7.0"
@impl true
@spec months_in_year(year) :: 12
def months_in_year(_year) do
@months_in_year
end
@doc """
Returns if the given year is a leap year.
@@ -300,14 +219,12 @@ defmodule Calendar.ISO do
true
iex> Calendar.ISO.leap_year?(1900)
false
iex> Calendar.ISO.leap_year?(-4)
true
"""
@spec leap_year?(year) :: boolean()
@impl true
def leap_year?(year) when is_integer(year) do
rem(year, 4) === 0 and (rem(year, 100) !== 0 or rem(year, 400) === 0)
def leap_year?(year) when is_integer(year) and year >= 0 do
rem(year, 4) === 0 and (rem(year, 100) > 0 or rem(year, 400) === 0)
end
@doc """
@@ -319,164 +236,29 @@ defmodule Calendar.ISO do
iex> Calendar.ISO.day_of_week(2016, 10, 31)
1
iex> Calendar.ISO.day_of_week(2016, 11, 1)
iex> Calendar.ISO.day_of_week(2016, 11, 01)
2
iex> Calendar.ISO.day_of_week(2016, 11, 2)
iex> Calendar.ISO.day_of_week(2016, 11, 02)
3
iex> Calendar.ISO.day_of_week(2016, 11, 3)
iex> Calendar.ISO.day_of_week(2016, 11, 03)
4
iex> Calendar.ISO.day_of_week(2016, 11, 4)
iex> Calendar.ISO.day_of_week(2016, 11, 04)
5
iex> Calendar.ISO.day_of_week(2016, 11, 5)
iex> Calendar.ISO.day_of_week(2016, 11, 05)
6
iex> Calendar.ISO.day_of_week(2016, 11, 6)
iex> Calendar.ISO.day_of_week(2016, 11, 06)
7
iex> Calendar.ISO.day_of_week(-99, 1, 31)
4
"""
@spec day_of_week(year, month, day) :: 1..7
@impl true
def day_of_week(year, month, day)
when is_integer(year) and is_integer(month) and is_integer(day) do
iso_days_to_day_of_week(date_to_iso_days(year, month, day))
end
defp iso_days_to_day_of_week(iso_days) do
Integer.mod(iso_days + 5, 7) + 1
end
@doc """
Calculates the day of the year from the given `year`, `month`, and `day`.
It is an integer from 1 to 366.
## Examples
iex> Calendar.ISO.day_of_year(2016, 1, 31)
31
iex> Calendar.ISO.day_of_year(-99, 2, 1)
32
iex> Calendar.ISO.day_of_year(2018, 2, 28)
59
"""
@doc since: "1.8.0"
@spec day_of_year(year, month, day) :: 1..366
@impl true
def day_of_year(year, month, day)
when is_integer(year) and is_integer(month) and is_integer(day) do
true = day <= days_in_month(year, month)
days_before_month(month) + leap_day_offset(year, month) + day
end
@doc """
Calculates the quarter of the year from the given `year`, `month`, and `day`.
It is an integer from 1 to 4.
## Examples
iex> Calendar.ISO.quarter_of_year(2016, 1, 31)
1
iex> Calendar.ISO.quarter_of_year(2016, 4, 3)
2
iex> Calendar.ISO.quarter_of_year(-99, 9, 31)
3
iex> Calendar.ISO.quarter_of_year(2018, 12, 28)
4
"""
@doc since: "1.8.0"
@spec quarter_of_year(year, month, day) :: 1..4
@impl true
def quarter_of_year(year, month, day)
when is_integer(year) and is_integer(month) and is_integer(day) do
div(month - 1, 3) + 1
end
@doc """
Calculates the year and era from the given `year`.
The ISO calendar has two eras: the current era which
starts in year 1 and is defined as era "1". And a
second era for those years less than 1 defined as
era "0".
## Examples
iex> Calendar.ISO.year_of_era(1)
{1, 1}
iex> Calendar.ISO.year_of_era(2018)
{2018, 1}
iex> Calendar.ISO.year_of_era(0)
{1, 0}
iex> Calendar.ISO.year_of_era(-1)
{2, 0}
"""
@doc since: "1.8.0"
@spec year_of_era(year) :: {year, era :: 0..1}
@impl true
def year_of_era(year) when is_integer(year) and year > 0 do
{year, 1}
end
def year_of_era(year) when is_integer(year) and year < 1 do
{abs(year) + 1, 0}
end
@doc """
Calculates the day and era from the given `year`, `month`, and `day`.
## Examples
iex> Calendar.ISO.day_of_era(0, 1, 1)
{366, 0}
iex> Calendar.ISO.day_of_era(1, 1, 1)
{1, 1}
iex> Calendar.ISO.day_of_era(0, 12, 31)
{1, 0}
iex> Calendar.ISO.day_of_era(0, 12, 30)
{2, 0}
iex> Calendar.ISO.day_of_era(-1, 12, 31)
{367, 0}
"""
@doc since: "1.8.0"
@spec day_of_era(year, month, day) :: {day :: pos_integer(), era :: 0..1}
@impl true
def day_of_era(year, month, day)
when is_integer(year) and is_integer(month) and is_integer(day) and year > 0 do
day = date_to_iso_days(year, month, day) - @iso_epoch + 1
{day, 1}
end
def day_of_era(year, month, day)
when is_integer(year) and is_integer(month) and is_integer(day) and year < 1 do
day = abs(date_to_iso_days(year, month, day) - @iso_epoch)
{day, 0}
Integer.mod(date_to_iso_days(year, month, day) + 5, 7) + 1
end
@doc """
Converts the given time into a string.
## Examples
iex> Calendar.ISO.time_to_string(2, 2, 2, {2, 6})
"02:02:02.000002"
iex> Calendar.ISO.time_to_string(2, 2, 2, {2, 2})
"02:02:02.00"
iex> Calendar.ISO.time_to_string(2, 2, 2, {2, 0})
"02:02:02"
"""
@spec time_to_string(
Calendar.hour(),
Calendar.minute(),
Calendar.second(),
Calendar.microsecond()
) :: String.t()
@impl true
def time_to_string(hour, minute, second, microsecond) do
time_to_string(hour, minute, second, microsecond, :extended)
@@ -501,18 +283,7 @@ defmodule Calendar.ISO do
@doc """
Converts the given date into a string.
## Examples
iex> Calendar.ISO.date_to_string(2015, 2, 28)
"2015-02-28"
iex> Calendar.ISO.date_to_string(2017, 8, 1)
"2017-08-01"
iex> Calendar.ISO.date_to_string(-99, 1, 31)
"-0099-01-31"
"""
@spec date_to_string(year, month, day) :: String.t()
@impl true
def date_to_string(year, month, day) do
date_to_string(year, month, day, :extended)
@@ -528,61 +299,16 @@ defmodule Calendar.ISO do
@doc """
Converts the datetime (without time zone) into a string.
## Examples
iex> Calendar.ISO.naive_datetime_to_string(2015, 2, 28, 1, 2, 3, {4, 6})
"2015-02-28 01:02:03.000004"
iex> Calendar.ISO.naive_datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5})
"2017-08-01 01:02:03.00000"
"""
@impl true
@spec naive_datetime_to_string(
year,
month,
day,
Calendar.hour(),
Calendar.minute(),
Calendar.second(),
Calendar.microsecond()
) :: String.t()
def naive_datetime_to_string(year, month, day, hour, minute, second, microsecond) do
date_to_string(year, month, day) <> " " <> time_to_string(hour, minute, second, microsecond)
end
@doc """
Converts the datetime (with time zone) into a string.
## Examples
iex> time_zone = "Europe/Berlin"
iex> Calendar.ISO.datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5}, time_zone, "CET", 3600, 0)
"2017-08-01 01:02:03.00000+01:00 CET Europe/Berlin"
iex> Calendar.ISO.datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5}, time_zone, "CDT", 3600, 3600)
"2017-08-01 01:02:03.00000+02:00 CDT Europe/Berlin"
iex> time_zone = "America/Los_Angeles"
iex> Calendar.ISO.datetime_to_string(2015, 2, 28, 1, 2, 3, {4, 5}, time_zone, "PST", -28800, 0)
"2015-02-28 01:02:03.00000-08:00 PST America/Los_Angeles"
iex> Calendar.ISO.datetime_to_string(2015, 2, 28, 1, 2, 3, {4, 5}, time_zone, "PDT", -28800, 3600)
"2015-02-28 01:02:03.00000-07:00 PDT America/Los_Angeles"
Convers the datetime (with time zone) into a string.
"""
@impl true
@spec datetime_to_string(
year,
month,
day,
Calendar.hour(),
Calendar.minute(),
Calendar.second(),
Calendar.microsecond(),
Calendar.time_zone(),
Calendar.zone_abbr(),
Calendar.utc_offset(),
Calendar.std_offset()
) :: String.t()
def datetime_to_string(
year,
month,
@@ -603,61 +329,18 @@ defmodule Calendar.ISO do
zone_to_string(utc_offset, std_offset, zone_abbr, time_zone)
end
@doc """
Determines if the date given is valid according to the proleptic Gregorian calendar.
## Examples
iex> Calendar.ISO.valid_date?(2015, 2, 28)
true
iex> Calendar.ISO.valid_date?(2015, 2, 30)
false
iex> Calendar.ISO.valid_date?(-1, 12, 31)
true
iex> Calendar.ISO.valid_date?(-1, 12, 32)
false
"""
@doc since: "1.5.0"
@impl true
@spec valid_date?(year, month, day) :: boolean
def valid_date?(year, month, day) do
month in 1..12 and year in -9999..9999 and
(is_integer(day) and day >= 1 and day <= days_in_month(year, month))
month in 1..12 and year in 0..9999 and day in 1..days_in_month(year, month)
end
@doc """
Determines if the date given is valid according to the proleptic Gregorian calendar.
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> Calendar.ISO.valid_time?(10, 50, 25, {3006, 6})
true
iex> Calendar.ISO.valid_time?(23, 59, 60, {0, 0})
false
iex> Calendar.ISO.valid_time?(24, 0, 0, {0, 0})
false
"""
@doc since: "1.5.0"
@impl true
@spec valid_time?(Calendar.hour(), Calendar.minute(), Calendar.second(), Calendar.microsecond()) ::
boolean
def valid_time?(hour, minute, second, {microsecond, precision}) do
hour in 0..23 and minute in 0..59 and second in 0..59 and microsecond in 0..999_999 and
hour in 0..23 and minute in 0..59 and second in 0..60 and microsecond in 0..999_999 and
precision in 0..6
end
@doc """
See `c:Calendar.day_rollover_relative_to_midnight_utc/0` for documentation.
"""
@doc since: "1.5.0"
@impl true
@spec day_rollover_relative_to_midnight_utc() :: {0, 1}
def day_rollover_relative_to_midnight_utc() do
{0, 1}
end
@@ -687,13 +370,9 @@ defmodule Calendar.ISO do
defp sign(total) when total < 0, do: "-"
defp sign(_), do: "+"
defp zero_pad(val, count) when val >= 0 do
num = Integer.to_string(val)
:binary.copy("0", max(count - byte_size(num), 0)) <> num
end
defp zero_pad(val, count) do
"-" <> zero_pad(-val, count)
num = Integer.to_string(val)
:binary.copy("0", count - byte_size(num)) <> num
end
## Helpers
@@ -831,7 +510,7 @@ defmodule Calendar.ISO do
@doc false
def iso_days_to_unit({days, {parts, ppd}}, unit) do
day_microseconds = days * @parts_per_day
microseconds = divide_by_parts_per_day(parts, ppd)
microseconds = div(parts * @parts_per_day, ppd)
System.convert_time_unit(day_microseconds + microseconds, :microsecond, unit)
end
@@ -882,30 +561,13 @@ defmodule Calendar.ISO do
if leap_year?(year), do: 1, else: 0
end
defp days_to_year(days) when days < 0 do
year_estimate = -div(-days, @days_per_nonleap_year) - 1
{year, days_before_year} =
days_to_year(year_estimate, days, days_to_end_of_epoch(year_estimate))
leap_year_pad = if leap_year?(year), do: 1, else: 0
{year, leap_year_pad + @days_per_nonleap_year + days - days_before_year}
end
defp days_to_year(days) do
year_estimate = div(days, @days_per_nonleap_year)
{year, days_before_year} =
days_to_year(year_estimate, days, days_in_previous_years(year_estimate))
year = Integer.floor_div(days, @days_per_nonleap_year)
{year, days_before_year} = days_to_year(year, days, days_in_previous_years(year))
{year, days - days_before_year}
end
defp days_to_year(year, days1, days2) when year < 0 and days1 >= days2 do
days_to_year(year + 1, days1, days_to_end_of_epoch(year + 1))
end
defp days_to_year(year, days1, days2) when year >= 0 and days1 < days2 do
defp days_to_year(year, days1, days2) when days1 < days2 do
days_to_year(year - 1, days1, days_in_previous_years(year - 1))
end
@@ -913,13 +575,6 @@ defmodule Calendar.ISO do
{year, days2}
end
defp days_to_end_of_epoch(year) when year < 0 do
previous_year = year + 1
div(previous_year, 4) - div(previous_year, 100) + div(previous_year, 400) +
previous_year * @days_per_nonleap_year
end
defp days_in_previous_years(0), do: 0
defp days_in_previous_years(year) do
@@ -987,7 +642,7 @@ defmodule Calendar.ISO do
{date, time}
end
defp seconds_to_time(seconds) when seconds in 0..@last_second_of_the_day do
defp seconds_to_time(seconds) when seconds in 0..(@seconds_per_day - 1) do
{hour, rest_seconds} = div_mod(seconds, @seconds_per_hour)
{minute, second} = div_mod(rest_seconds, @seconds_per_minute)
+70 -183
View File
@@ -4,8 +4,7 @@ defmodule NaiveDateTime do
The NaiveDateTime struct contains the fields year, month, day, hour,
minute, second, microsecond and calendar. New naive datetimes can be
built with the `new/2` and `new/8` functions or using the
`~N` (see `Kernel.sigil_N/2`) sigil:
built with the `new/2` and `new/7` functions or using the `~N` sigil:
iex> ~N[2000-01-01 23:00:07]
~N[2000-01-01 23:00:07]
@@ -28,13 +27,18 @@ defmodule NaiveDateTime do
`NaiveDateTime` is not validated against a time zone, such errors
would go unnoticed.
The functions on this module work with the `NaiveDateTime` struct as well
as any struct that contains the same fields as the `NaiveDateTime` struct,
such as `DateTime`. Such functions expect
`t:Calendar.naive_datetime/0` in their typespecs (instead of `t:t/0`).
Developers should avoid creating the NaiveDateTime structs directly
and instead, rely on the functions provided by this module as well
as the ones in third-party calendar libraries.
and instead rely on the functions provided by this module as well
as the ones in 3rd party calendar libraries.
## Comparing naive date times
Comparisons in Elixir using `==/2`, `>/2`, `</2` and similar are structural
Comparisons in Elixir using `==`, `>`, `<` and similar are structural
and based on the `NaiveDateTime` struct fields. For proper comparison
between naive datetimes, use the `compare/2` function.
@@ -48,7 +52,7 @@ defmodule NaiveDateTime do
iex> NaiveDateTime.diff(~N[2010-04-17 14:00:00], ~N[1970-01-01 00:00:00])
1271512800
iex> NaiveDateTime.add(~N[1970-01-01 00:00:00], 1_271_512_800)
iex> NaiveDateTime.add(~N[1970-01-01 00:00:00], 1271512800)
~N[2010-04-17 14:00:00]
Those functions are optimized to deal with common epochs, such
@@ -67,7 +71,7 @@ defmodule NaiveDateTime do
calendar: Calendar.ISO
]
@type t :: %__MODULE__{
@type t :: %NaiveDateTime{
year: Calendar.year(),
month: Calendar.month(),
day: Calendar.day(),
@@ -91,7 +95,6 @@ defmodule NaiveDateTime do
true
"""
@doc since: "1.4.0"
@spec utc_now(Calendar.calendar()) :: t
def utc_now(calendar \\ Calendar.ISO)
@@ -141,18 +144,17 @@ defmodule NaiveDateTime do
{:ok, ~N[2000-01-01 23:59:59.0]}
iex> NaiveDateTime.new(2000, 1, 1, 23, 59, 59, 999_999)
{:ok, ~N[2000-01-01 23:59:59.999999]}
iex> NaiveDateTime.new(2000, 1, 1, 23, 59, 60, 999_999)
{:ok, ~N[2000-01-01 23:59:60.999999]}
iex> NaiveDateTime.new(2000, 1, 1, 24, 59, 59, 999_999)
{:error, :invalid_time}
iex> NaiveDateTime.new(2000, 1, 1, 23, 60, 59, 999_999)
{:error, :invalid_time}
iex> NaiveDateTime.new(2000, 1, 1, 23, 59, 60, 999_999)
iex> NaiveDateTime.new(2000, 1, 1, 23, 59, 61, 999_999)
{:error, :invalid_time}
iex> NaiveDateTime.new(2000, 1, 1, 23, 59, 59, 1_000_000)
{:error, :invalid_time}
iex> NaiveDateTime.new(2000, 1, 1, 23, 59, 59, {0, 1}, Calendar.ISO)
{:ok, ~N[2000-01-01 23:59:59.0]}
"""
@spec new(
Calendar.year(),
@@ -164,35 +166,10 @@ defmodule NaiveDateTime do
Calendar.microsecond(),
Calendar.calendar()
) :: {:ok, t} | {:error, atom}
def new(year, month, day, hour, minute, second, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
def new(year, month, day, hour, minute, second, microsecond, calendar)
when is_integer(microsecond) do
new(year, month, day, hour, minute, second, {microsecond, 6}, calendar)
end
def new(year, month, day, hour, minute, second, microsecond, calendar) do
cond do
not calendar.valid_date?(year, month, day) ->
{:error, :invalid_date}
not calendar.valid_time?(hour, minute, second, microsecond) ->
{:error, :invalid_time}
true ->
naive_datetime = %NaiveDateTime{
calendar: calendar,
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
microsecond: microsecond
}
{:ok, naive_datetime}
end
def new(year, month, day, hour, minute, second, microsecond \\ {0, 0}, calendar \\ Calendar.ISO) do
with {:ok, date} <- Date.new(year, month, day, calendar),
{:ok, time} <- Time.new(hour, minute, second, microsecond, calendar),
do: new(date, time)
end
@doc """
@@ -228,8 +205,10 @@ defmodule NaiveDateTime do
@doc """
Adds a specified amount of time to a `NaiveDateTime`.
Accepts an `amount_to_add` in any `unit` available from `t:System.time_unit/0`.
Negative values will move backwards in time.
Accepts an `integer` in any `unit` available from `t:System.time_unit/0`.
Negative values will be move backwards in time.
This operation is only possible if both calendars are convertible to `Calendar.ISO`.
## Examples
@@ -251,36 +230,23 @@ defmodule NaiveDateTime do
# changes below the precision will not be visible
iex> hidden = NaiveDateTime.add(~N[2014-10-02 00:29:10], 21, :millisecond)
iex> hidden.microsecond # ~N[2014-10-02 00:29:10]
iex> hidden.microsecond # ~N[2014-10-02 00:29:10]
{21000, 0}
# from Gregorian seconds
iex> NaiveDateTime.add(~N[0000-01-01 00:00:00], 63_579_428_950)
iex> NaiveDateTime.add(~N[0000-01-01 00:00:00], 63579428950)
~N[2014-10-02 00:29:10]
Passing a `Datetime` automatically converts it to `NaiveDateTime`,
discarding the time zone information:
iex> dt = %DateTime{year: 2000, month: 2, day: 29, zone_abbr: "CET",
...> hour: 23, minute: 0, second: 7, microsecond: {0, 0},
...> utc_offset: 3600, std_offset: 0, time_zone: "Europe/Warsaw"}
iex> NaiveDateTime.add(dt, 21, :second)
~N[2000-02-29 23:00:28]
"""
@doc since: "1.4.0"
@spec add(Calendar.naive_datetime(), integer, System.time_unit()) :: t
def add(
%{microsecond: {_, precision}, calendar: calendar} = naive_datetime,
amount_to_add,
unit \\ :second
)
when is_integer(amount_to_add) do
@spec add(t, integer, System.time_unit()) :: t
def add(%NaiveDateTime{} = naive_datetime, integer, unit \\ :second)
when is_integer(integer) do
%{microsecond: {_, precision}, calendar: calendar} = naive_datetime
ppd = System.convert_time_unit(86400, :second, unit)
naive_datetime
|> to_iso_days()
|> Calendar.ISO.add_day_fraction_to_iso_days(amount_to_add, ppd)
|> Calendar.ISO.add_day_fraction_to_iso_days(integer, ppd)
|> from_iso_days(calendar, precision)
end
@@ -302,39 +268,29 @@ defmodule NaiveDateTime do
21
iex> NaiveDateTime.diff(~N[2014-10-02 00:29:10], ~N[2014-10-02 00:29:12])
-2
iex> NaiveDateTime.diff(~N[-0001-10-02 00:29:10], ~N[-0001-10-02 00:29:12])
-2
# to Gregorian seconds
iex> NaiveDateTime.diff(~N[2014-10-02 00:29:10], ~N[0000-01-01 00:00:00])
63579428950
"""
@doc since: "1.4.0"
@spec diff(Calendar.naive_datetime(), Calendar.naive_datetime(), System.time_unit()) :: integer
def diff(
%{calendar: calendar1} = naive_datetime1,
%{calendar: calendar2} = naive_datetime2,
unit \\ :second
) do
if not Calendar.compatible_calendars?(calendar1, calendar2) do
@spec diff(t, t, System.time_unit()) :: integer
def diff(%NaiveDateTime{} = ndatetime1, %NaiveDateTime{} = ndatetime2, unit \\ :second) do
if not Calendar.compatible_calendars?(ndatetime1.calendar, ndatetime2.calendar) do
raise ArgumentError,
"cannot calculate the difference between #{inspect(naive_datetime1)} and " <>
"#{inspect(naive_datetime2)} because their calendars are not compatible " <>
"cannot calculate the difference between #{inspect(ndatetime1)} and " <>
"#{inspect(ndatetime2)} because their calendars are not compatible " <>
"and thus the result would be ambiguous"
end
units1 = naive_datetime1 |> to_iso_days() |> Calendar.ISO.iso_days_to_unit(unit)
units2 = naive_datetime2 |> to_iso_days() |> Calendar.ISO.iso_days_to_unit(unit)
units1 = ndatetime1 |> to_iso_days() |> Calendar.ISO.iso_days_to_unit(unit)
units2 = ndatetime2 |> to_iso_days() |> Calendar.ISO.iso_days_to_unit(unit)
units1 - units2
end
@doc """
Returns the given naive datetime with the microsecond field truncated to the
given precision (`:microsecond`, `:millisecond` or `:second`).
The given naive datetime is returned unchanged if it already has lower precision
than the given precision.
given precision (`:microsecond`, `millisecond` or `:second`).
## Examples
@@ -348,35 +304,9 @@ defmodule NaiveDateTime do
~N[2017-11-06 00:23:51]
"""
@doc since: "1.6.0"
@spec truncate(t(), :microsecond | :millisecond | :second) :: t()
def truncate(%NaiveDateTime{microsecond: microsecond} = naive_datetime, precision) do
%{naive_datetime | microsecond: Calendar.truncate(microsecond, precision)}
end
def truncate(
%{
calendar: calendar,
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
microsecond: microsecond
},
precision
) do
%NaiveDateTime{
calendar: calendar,
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
microsecond: Calendar.truncate(microsecond, precision)
}
def truncate(%NaiveDateTime{microsecond: microsecond} = ndatetime, precision) do
%{ndatetime | microsecond: Calendar.truncate(microsecond, precision)}
end
@doc """
@@ -391,24 +321,11 @@ defmodule NaiveDateTime do
~D[2002-01-13]
"""
@spec to_date(Calendar.naive_datetime()) :: Date.t()
@spec to_date(t) :: Date.t()
def to_date(%NaiveDateTime{year: year, month: month, day: day, calendar: calendar}) do
%Date{year: year, month: month, day: day, calendar: calendar}
end
def to_date(%{
year: year,
month: month,
day: day,
calendar: calendar,
hour: _,
minute: _,
second: _,
microsecond: _
}) do
%Date{year: year, month: month, day: day, calendar: calendar}
end
@doc """
Converts a `NaiveDateTime` into `Time`.
@@ -421,7 +338,7 @@ defmodule NaiveDateTime do
~T[23:00:07]
"""
@spec to_time(Calendar.naive_datetime()) :: Time.t()
@spec to_time(t) :: Time.t()
def to_time(%NaiveDateTime{} = naive_datetime) do
%{
hour: hour,
@@ -440,25 +357,6 @@ defmodule NaiveDateTime do
}
end
def to_time(%{
year: _,
month: _,
day: _,
calendar: calendar,
hour: hour,
minute: minute,
second: second,
microsecond: microsecond
}) do
%Time{
hour: hour,
minute: minute,
second: second,
microsecond: microsecond,
calendar: calendar
}
end
@doc """
Converts the given naive datetime to a string according to its calendar.
@@ -468,8 +366,6 @@ defmodule NaiveDateTime do
"2000-02-28 23:00:13"
iex> NaiveDateTime.to_string(~N[2000-02-28 23:00:13.001])
"2000-02-28 23:00:13.001"
iex> NaiveDateTime.to_string(~N[-0100-12-15 03:20:31])
"-0100-12-15 03:20:31"
This function can also be used to convert a DateTime to a string without
the time zone information:
@@ -500,17 +396,17 @@ defmodule NaiveDateTime do
Parses the extended "Date and time of day" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Time zone offset may be included in the string but they will be
Timezone offset may be included in the string but they will be
simply discarded as such information is not included in naive date
times.
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.
Time representations with reduced accuracy are not supported.
Note that 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.
## Examples
@@ -556,33 +452,20 @@ defmodule NaiveDateTime do
"""
@spec from_iso8601(String.t(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
def from_iso8601(string, calendar \\ Calendar.ISO)
def from_iso8601(<<?-, rest::binary>>, calendar) do
with {:ok, %{year: year} = naive_datetime} <- raw_from_iso8601(rest, calendar) do
{:ok, %{naive_datetime | year: -year}}
end
end
def from_iso8601(<<rest::binary>>, calendar) do
raw_from_iso8601(rest, calendar)
end
@sep [?\s, ?T]
[match_date, guard_date, read_date] = Calendar.ISO.__match_date__()
[match_time, guard_time, read_time] = Calendar.ISO.__match_time__()
defp raw_from_iso8601(string, calendar) do
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
def from_iso8601(string, calendar \\ Calendar.ISO) when is_binary(string) do
with <<year::4-bytes, ?-, month::2-bytes, ?-, day::2-bytes, sep, rest::binary>> <- string,
true <- sep in [?\s, ?T],
<<hour::2-bytes, ?:, min::2-bytes, ?:, sec::2-bytes, rest::binary>> <- rest,
{year, ""} <- Integer.parse(year),
{month, ""} <- Integer.parse(month),
{day, ""} <- Integer.parse(day),
{hour, ""} <- Integer.parse(hour),
{min, ""} <- Integer.parse(min),
{sec, ""} <- Integer.parse(sec),
{microsec, rest} <- Calendar.ISO.parse_microsecond(rest),
{_offset, ""} <- Calendar.ISO.parse_offset(rest) do
{year, month, day} = unquote(read_date)
{hour, min, sec} = unquote(read_time)
with {:ok, iso_naive_dt} <- new(year, month, day, hour, min, sec, microsec, Calendar.ISO) do
convert(iso_naive_dt, calendar)
end
with {:ok, utc_date} <- new(year, month, day, hour, min, sec, microsec, Calendar.ISO),
do: convert(utc_date, calendar)
else
_ -> {:error, :invalid_format}
end
@@ -604,7 +487,7 @@ defmodule NaiveDateTime do
** (ArgumentError) cannot parse "2015-01-23P23:50:07" as naive datetime, reason: :invalid_format
"""
@spec from_iso8601!(String.t(), Calendar.calendar()) :: t
@spec from_iso8601!(String.t(), Calendar.calendar()) :: t | no_return
def from_iso8601!(string, calendar \\ Calendar.ISO) do
case from_iso8601(string, calendar) do
{:ok, value} ->
@@ -680,6 +563,12 @@ defmodule NaiveDateTime do
|> to_iso8601(format)
end
def to_iso8601(_date, format) do
raise ArgumentError,
"NaiveDateTime.to_iso8601/2 expects format to be :extended or :basic, " <>
"got: #{inspect(format)}"
end
@doc """
Converts a `NaiveDateTime` struct to an Erlang datetime tuple.
@@ -725,7 +614,7 @@ defmodule NaiveDateTime do
{:ok, ~N[2000-01-01 13:30:15.005]}
iex> NaiveDateTime.from_erl({{2000, 13, 1}, {13, 30, 15}})
{:error, :invalid_date}
iex> NaiveDateTime.from_erl({{2000, 13, 1}, {13, 30, 15}})
iex> NaiveDateTime.from_erl({{2000, 13, 1},{13, 30, 15}})
{:error, :invalid_date}
"""
@@ -734,8 +623,8 @@ defmodule NaiveDateTime do
def from_erl(tuple, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
def from_erl({{year, month, day}, {hour, minute, second}}, microsecond, calendar) do
with {:ok, iso_naive_dt} <- new(year, month, day, hour, minute, second, microsecond),
do: convert(iso_naive_dt, calendar)
with {:ok, utc_date} <- new(year, month, day, hour, minute, second, microsecond),
do: convert(utc_date, calendar)
end
@doc """
@@ -754,7 +643,8 @@ defmodule NaiveDateTime do
** (ArgumentError) cannot convert {{2000, 13, 1}, {13, 30, 15}} to naive datetime, reason: :invalid_date
"""
@spec from_erl!(:calendar.datetime(), Calendar.microsecond(), Calendar.calendar()) :: t
@spec from_erl!(:calendar.datetime(), Calendar.microsecond(), Calendar.calendar()) ::
t | no_return
def from_erl!(tuple, microsecond \\ {0, 0}, calendar \\ Calendar.ISO) do
case from_erl(tuple, microsecond, calendar) do
{:ok, value} ->
@@ -794,7 +684,6 @@ defmodule NaiveDateTime do
:lt
"""
@doc since: "1.4.0"
@spec compare(Calendar.naive_datetime(), Calendar.naive_datetime()) :: :lt | :eq | :gt
def compare(%{calendar: calendar1} = naive_datetime1, %{calendar: calendar2} = naive_datetime2) do
if Calendar.compatible_calendars?(calendar1, calendar2) do
@@ -831,7 +720,6 @@ defmodule NaiveDateTime do
hour: 13, minute: 30, second: 15, microsecond: {0, 0}}}
"""
@doc since: "1.5.0"
@spec convert(Calendar.naive_datetime(), Calendar.calendar()) ::
{:ok, t} | {:error, :incompatible_calendars}
@@ -893,7 +781,6 @@ defmodule NaiveDateTime do
hour: 13, minute: 30, second: 15, microsecond: {0, 0}}
"""
@doc since: "1.5.0"
@spec convert!(Calendar.naive_datetime(), Calendar.calendar()) :: t
def convert!(naive_datetime, calendar) do
case convert(naive_datetime, calendar) do
+40 -99
View File
@@ -3,8 +3,8 @@ defmodule Time do
A Time struct and functions.
The Time struct contains the fields hour, minute, second and microseconds.
New times can be built with the `new/4` function or using the
`~T` (see `Kernel.sigil_T/2`) sigil:
New times can be built with the `new/4` function or using the `~T`
sigil:
iex> ~T[23:00:07.001]
~T[23:00:07.001]
@@ -25,11 +25,11 @@ defmodule Time do
Developers should avoid creating the Time structs directly
and instead rely on the functions provided by this module as well
as the ones in third-party calendar libraries.
as the ones in 3rd party calendar libraries.
## Comparing times
Comparisons in Elixir using `==/2`, `>/2`, `</2` and similar are structural
Comparisons in Elixir using `==`, `>`, `<` and similar are structural
and based on the `Time` struct fields. For proper comparison between
times, use the `compare/2` function.
"""
@@ -37,7 +37,7 @@ defmodule Time do
@enforce_keys [:hour, :minute, :second]
defstruct [:hour, :minute, :second, microsecond: {0, 0}, calendar: Calendar.ISO]
@type t :: %__MODULE__{
@type t :: %Time{
hour: Calendar.hour(),
minute: Calendar.minute(),
second: Calendar.second(),
@@ -45,8 +45,6 @@ defmodule Time do
calendar: Calendar.calendar()
}
@parts_per_day 86_400_000_000
@doc """
Returns the current time in UTC.
@@ -57,7 +55,6 @@ defmodule Time do
true
"""
@doc since: "1.4.0"
@spec utc_now(Calendar.calendar()) :: t
def utc_now(calendar \\ Calendar.ISO) do
{:ok, _, time, microsecond} = Calendar.ISO.from_unix(:os.system_time(), :native)
@@ -80,10 +77,9 @@ defmodule Time do
Expects all values to be integers. Returns `{:ok, time}` if each
entry fits its appropriate range, returns `{:error, reason}` otherwise.
Microseconds can also be given with a precision, which must be an
integer between 0 and 6.
The built-in calendar does not support leap seconds.
Note a time may have 60 seconds in case of leap seconds. Microseconds
can also be given with a precision, which must be an integer between
0 and 6.
## Examples
@@ -91,12 +87,18 @@ defmodule Time do
{:ok, ~T[00:00:00.000000]}
iex> Time.new(23, 59, 59, 999_999)
{:ok, ~T[23:59:59.999999]}
iex> Time.new(23, 59, 60, 999_999)
{:ok, ~T[23:59:60.999999]}
# Time with microseconds and their precision
iex> Time.new(23, 59, 60, {10_000, 2})
{:ok, ~T[23:59:60.01]}
iex> Time.new(24, 59, 59, 999_999)
{:error, :invalid_time}
iex> Time.new(23, 60, 59, 999_999)
{:error, :invalid_time}
iex> Time.new(23, 59, 60, 999_999)
iex> Time.new(23, 59, 61, 999_999)
{:error, :invalid_time}
iex> Time.new(23, 59, 59, 1_000_000)
{:error, :invalid_time}
@@ -174,7 +176,7 @@ defmodule Time do
Parses the extended "Local time" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
Time zone offset may be included in the string but they will be
Timezone offset may be included in the string but they will be
simply discarded as such information is not included in times.
As specified in the standard, the separator "T" may be omitted if
@@ -184,7 +186,6 @@ defmodule Time do
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
@@ -215,31 +216,27 @@ defmodule Time do
@spec from_iso8601(String.t(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
def from_iso8601(string, calendar \\ Calendar.ISO)
def from_iso8601(<<?T, rest::binary>>, calendar) do
raw_from_iso8601(rest, calendar)
def from_iso8601(<<?T, h, rest::binary>>, calendar) when h in ?0..?9 do
from_iso8601(<<h, rest::binary>>, calendar)
end
def from_iso8601(<<rest::binary>>, calendar) do
raw_from_iso8601(rest, calendar)
end
[match_time, guard_time, read_time] = Calendar.ISO.__match_time__()
defp raw_from_iso8601(string, calendar) do
with <<unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_time),
def from_iso8601(<<hour::2-bytes, ?:, min::2-bytes, ?:, sec::2-bytes, rest::binary>>, calendar) do
with {hour, ""} <- Integer.parse(hour),
{min, ""} <- Integer.parse(min),
{sec, ""} <- Integer.parse(sec),
{microsec, rest} <- Calendar.ISO.parse_microsecond(rest),
{_offset, ""} <- Calendar.ISO.parse_offset(rest) do
{hour, min, sec} = unquote(read_time)
with {:ok, utc_time} <- new(hour, min, sec, microsec, Calendar.ISO) do
convert(utc_time, calendar)
end
with {:ok, utc_time} <- new(hour, min, sec, microsec, Calendar.ISO),
do: convert(utc_time, calendar)
else
_ -> {:error, :invalid_format}
end
end
def from_iso8601(<<_::binary>>, _calendar) do
{:error, :invalid_format}
end
@doc """
Parses the extended "Local time" format described by
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).
@@ -254,7 +251,6 @@ defmodule Time do
~T[23:50:07.123]
iex> Time.from_iso8601!("2015:01:23 23-50-07")
** (ArgumentError) cannot parse "2015:01:23 23-50-07" as time, reason: :invalid_format
"""
@spec from_iso8601!(String.t(), Calendar.calendar()) :: t
def from_iso8601!(string, calendar \\ Calendar.ISO) do
@@ -291,25 +287,17 @@ defmodule Time do
"""
@spec to_iso8601(Calendar.time(), :extended | :basic) :: String.t()
def to_iso8601(time, format \\ :extended)
def to_iso8601(%{calendar: Calendar.ISO} = time, format) when format in [:extended, :basic] do
def to_iso8601(time, format \\ :extended) when format in [:extended, :basic] do
%{
hour: hour,
minute: minute,
second: second,
microsecond: microsecond
} = time
} = convert!(time, Calendar.ISO)
Calendar.ISO.time_to_iso8601(hour, minute, second, microsecond, format)
end
def to_iso8601(%{calendar: _} = time, format) when format in [:extended, :basic] do
time
|> convert!(Calendar.ISO)
|> to_iso8601(format)
end
@doc """
Converts given `time` to an Erlang time tuple.
@@ -391,7 +379,7 @@ defmodule Time do
~T[17:30:00.000000]
iex> Time.add(~T[11:00:00.005], 2400)
~T[11:40:00.005000]
iex> Time.add(~T[00:00:00], 86_399_999, :millisecond)
iex> Time.add(~T[00:00:00], 86399999, :millisecond)
~T[23:59:59.999000]
iex> Time.add(~T[17:10:05], 86400)
~T[17:10:05.000000]
@@ -399,13 +387,15 @@ defmodule Time do
~T[22:59:00.000000]
"""
@doc since: "1.6.0"
@spec add(Calendar.time(), integer, System.time_unit()) :: t
def add(%{calendar: calendar} = time, number, unit \\ :second) when is_integer(number) do
number = System.convert_time_unit(number, unit, :microsecond)
total = time_to_microseconds(time) + number
parts = Integer.mod(total, @parts_per_day)
{hour, minute, second, microsecond} = calendar.time_from_day_fraction({parts, @parts_per_day})
iso_days = {0, to_day_fraction(time)}
total = Calendar.ISO.iso_days_to_unit(iso_days, :microsecond) + number
iso_ppd = 86_400_000_000
parts = Integer.mod(total, iso_ppd)
{hour, minute, second, microsecond} = calendar.time_from_day_fraction({parts, iso_ppd})
%Time{
hour: hour,
@@ -416,21 +406,6 @@ defmodule Time do
}
end
defp time_to_microseconds(%{
calendar: Calendar.ISO,
hour: 0,
minute: 0,
second: 0,
microsecond: {0, _}
}) do
0
end
defp time_to_microseconds(time) do
iso_days = {0, to_day_fraction(time)}
Calendar.ISO.iso_days_to_unit(iso_days, :microsecond)
end
@doc """
Compares two time structs.
@@ -458,7 +433,6 @@ defmodule Time do
:gt
"""
@doc since: "1.4.0"
@spec compare(Calendar.time(), Calendar.time()) :: :lt | :eq | :gt
def compare(%{calendar: calendar} = time1, %{calendar: calendar} = time2) do
%{hour: hour1, minute: minute1, second: second1, microsecond: {microsecond1, _}} = time1
@@ -498,7 +472,6 @@ defmodule Time do
{:ok, %Time{calendar: Calendar.Holocene, hour: 13, minute: 30, second: 15, microsecond: {0, 0}}}
"""
@doc since: "1.5.0"
@spec convert(Calendar.time(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
# Keep it multiline for proper function clause errors.
@@ -554,7 +527,6 @@ defmodule Time do
%Time{calendar: Calendar.Holocene, hour: 13, minute: 30, second: 15, microsecond: {0, 0}}
"""
@doc since: "1.5.0"
@spec convert!(Calendar.time(), Calendar.calendar()) :: t
def convert!(time, calendar) do
case convert(time, calendar) do
@@ -569,7 +541,7 @@ defmodule Time do
end
@doc """
Returns the difference between two times, considering only the hour, minute,
Returns the difference between two times, considering only the hour, minute
second and microsecond.
As with the `compare/2` function both `Time` structs and other structures
@@ -596,7 +568,7 @@ defmodule Time do
# Two `NaiveDateTime` structs could have big differences in the date
# but only the time part is considered.
iex> Time.diff(~N[2017-01-01 00:29:12], ~N[1900-02-03 00:29:10])
iex> Time.diff(~N[2017-01-01 00:29:12], (~N[1900-02-03 00:29:10]))
2
iex> Time.diff(~T[00:29:12], ~T[00:29:10], :microsecond)
@@ -605,35 +577,8 @@ defmodule Time do
-2_000_000
"""
@doc since: "1.5.0"
@spec diff(Calendar.time(), Calendar.time(), System.time_unit()) :: integer
def diff(time1, time2, unit \\ :second)
def diff(
%{
calendar: Calendar.ISO,
hour: hour1,
minute: minute1,
second: second1,
microsecond: {microsecond1, @parts_per_day}
},
%{
calendar: Calendar.ISO,
hour: hour2,
minute: minute2,
second: second2,
microsecond: {microsecond2, @parts_per_day}
},
unit
) do
total =
(hour1 - hour2) * 3_600_000_000 + (minute1 - minute2) * 60_000_000 +
(second1 - second2) * 1_000_000 + (microsecond1 - microsecond2)
System.convert_time_unit(total, :microsecond, unit)
end
def diff(time1, time2, unit) do
def diff(time1, time2, unit \\ :second) do
fraction1 = to_day_fraction(time1)
fraction2 = to_day_fraction(time2)
@@ -645,9 +590,6 @@ defmodule Time do
Returns the given time with the microsecond field truncated to the given
precision (`:microsecond`, `millisecond` or `:second`).
The given time is returned unchanged if it already has lower precision than
the given precision.
## Examples
iex> Time.truncate(~T[01:01:01.123456], :microsecond)
@@ -660,7 +602,6 @@ defmodule Time do
~T[01:01:01]
"""
@doc since: "1.6.0"
@spec truncate(t(), :microsecond | :millisecond | :second) :: t()
def truncate(%Time{microsecond: microsecond} = time, precision) do
%{time | microsecond: Calendar.truncate(microsecond, precision)}
@@ -1,96 +0,0 @@
defmodule Calendar.TimeZoneDatabase do
@moduledoc """
This module defines a behaviour for providing time zone data.
IANA provides time zone data that includes data about different
UTC offsets and standard offsets for time zones.
"""
@typedoc """
A period where a certain combination of UTC offset, standard offset and zone
abbreviation is in effect.
For instance one period could be the summer of 2018 in "Europe/London" where summer time /
daylight saving time is in effect and lasts from spring to autumn. At autumn the `std_offset`
changes along with the `zone_abbr` so a different period is needed during winter.
"""
@type time_zone_period :: %{
optional(any) => any,
utc_offset: Calendar.utc_offset(),
std_offset: Calendar.std_offset(),
zone_abbr: Calendar.zone_abbr()
}
@typedoc """
Limit for when a certain time zone period begins or ends.
A beginning is inclusive. An ending is exclusive. Eg. if a period is from
2015-03-29 01:00:00 and until 2015-10-25 01:00:00, the period includes and
begins from the begining of 2015-03-29 01:00:00 and lasts until just before
2015-10-25 01:00:00.
A beginning or end for certain periods are infinite. For instance the latest
period for time zones without DST or plans to change. However for the purpose
of this behaviour they are only used for gaps in wall time where the needed
period limits are at a certain time.
"""
@type time_zone_period_limit :: Calendar.naive_datetime()
@doc """
Time zone period for a point in time in UTC for a specific time zone.
Takes a time zone name and a point in time for UTC and returns a
`time_zone_period` for that point in time.
"""
@doc since: "1.8.0"
@callback time_zone_period_from_utc_iso_days(Calendar.iso_days(), Calendar.time_zone()) ::
{:ok, time_zone_period}
| {:error, :time_zone_not_found | :utc_only_time_zone_database}
@doc """
Possible time zone periods for a certain time zone and wall clock date and time.
When the provided `datetime` is ambiguous a tuple with `:ambiguous` and two possible
periods. The periods in the list are sorted with the first element being the one that begins first.
When the provided `datetime` is in a gap - for instance during the "spring forward" when going
from winter time to summer time, a tuple with `:gap` and two periods with limits are returned
in a nested tuple. The first nested two-tuple is the period before the gap and a naive datetime
with a limit for when the period ends (wall time). The second nested two-tuple is the period
just after the gap and a datetime (wall time) for when the period begins just after the gap.
If there is only a single possible period for the provided `datetime`, the a tuple with `:single`
and the `time_zone_period` is returned.
"""
@doc since: "1.8.0"
@callback time_zone_periods_from_wall_datetime(Calendar.naive_datetime(), Calendar.time_zone()) ::
{:ok, time_zone_period}
| {:ambiguous, time_zone_period, time_zone_period}
| {:gap, {time_zone_period, time_zone_period_limit},
{time_zone_period, time_zone_period_limit}}
| {:error, :time_zone_not_found | :utc_only_time_zone_database}
end
defmodule Calendar.UTCOnlyTimeZoneDatabase do
@moduledoc """
Built-in time zone database that works only in Etc/UTC.
For all other time zones, it returns `{:error, :utc_only_time_zone_database}`.
"""
@behaviour Calendar.TimeZoneDatabase
@impl true
def time_zone_period_from_utc_iso_days(_, "Etc/UTC"),
do: {:ok, %{std_offset: 0, utc_offset: 0, zone_abbr: "UTC"}}
def time_zone_period_from_utc_iso_days(_, _),
do: {:error, :utc_only_time_zone_database}
@impl true
def time_zone_periods_from_wall_datetime(_, "Etc/UTC"),
do: {:ok, %{std_offset: 0, utc_offset: 0, zone_abbr: "UTC"}}
def time_zone_periods_from_wall_datetime(_, _),
do: {:error, :utc_only_time_zone_database}
end
+167 -292
View File
@@ -5,55 +5,25 @@ defmodule Code do
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.
## Working with files
This module contains three functions for compiling and evaluating files.
Here is a summary of them and their behaviour:
* `require_file/2` - compiles a file and tracks its name. It does not
compile the file again if it has been previously required.
* `compile_file/2` - compiles a file without tracking its name. Compiles the
file multiple times when invoked multiple times.
* `eval_file/2` - evaluates the file contents without tracking its name. It
returns the result of the last expression in the file, instead of the modules
defined in it.
In a nutshell, the first must be used when you want to keep track of the files
handled by the system, to avoid the same file from being compiled multiple
times. This is common in scripts.
`compile_file/2` must be used when you are interested in the modules defined in a
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.
"""
@doc """
Lists all required files.
Lists all loaded files.
## Examples
Code.require_file("../eex/test/eex_test.exs")
List.first(Code.required_files()) =~ "eex_test.exs"
List.first(Code.loaded_files()) =~ "eex_test.exs"
#=> true
"""
@doc since: "1.7.0"
@spec required_files() :: [binary]
def required_files do
:elixir_code_server.call(:required)
end
# TODO: Deprecate me on 1.9
@doc false
@spec loaded_files() :: [binary]
def loaded_files do
required_files()
:elixir_code_server.call(:loaded)
end
@doc """
Removes files from the required files list.
Removes files from the loaded files list.
The modules defined in the file are not removed;
calling this function only removes them from the list,
@@ -61,27 +31,17 @@ defmodule Code do
## Examples
# Require EEx test code
Code.require_file("../eex/test/eex_test.exs")
# Load EEx test code, unload file, check for functions still available
Code.load_file("../eex/test/eex_test.exs")
# Now unrequire all files
Code.unrequire_files(Code.required_files())
# Notice modules are still available
Code.unload_files(Code.loaded_files())
function_exported?(EExTest.Compiled, :before_compile, 0)
#=> true
"""
@doc since: "1.7.0"
@spec unrequire_files([binary]) :: :ok
def unrequire_files(files) do
:elixir_code_server.cast({:unrequire_files, files})
end
# TODO: Deprecate me on 1.9
@doc false
@spec unload_files([binary]) :: :ok
def unload_files(files) do
unrequire_files(files)
:elixir_code_server.cast({:unload_files, files})
end
@doc """
@@ -248,10 +208,7 @@ defmodule Code do
* `:line` - the line the string starts, used for error reporting
* `:line_length` - the line length to aim for when formatting
the document. Defaults to 98. Note this value is used as
reference but it is not enforced by the formatter as sometimes
user intervention is required. See "Running the formatter"
section
the document. Defaults to 98.
* `:locals_without_parens` - a keyword list of name and arity
pairs that should be kept without parens whenever possible.
@@ -286,104 +243,6 @@ defmodule Code do
based on the name, this behaviour should be configurable, such as the
`:locals_without_parens` option.
## Running the formatter
The formatter attempts to fit the most it can on a single line and
introduces line breaks wherever possible when it cannot.
In some cases, this may lead to undesired formatting. Therefore, **some
code generated by the formatter may not be aesthetically pleasing and
may require explicit intervention from the developer**. That's why we
do not recommend to run the formatter blindly in an existing codebase.
Instead you should format and sanity check each formatted file.
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, ...},
arg1,
arg2
) do
...
end
While the code above is completely valid, you may prefer to match on
the struct variables inside the function body in order to keep the
definition on a single line:
def my_function(%User{} = user, arg1, arg2) do
%{name: name, age: age, ...} = user
...
end
In some situations, you can use the fact the formatter does not generate
elegant code as a hint for refactoring. Take this code:
def board?(board_id, %User{} = user, available_permissions, required_permissions) do
Tracker.OrganizationMembers.user_in_organization?(user.id, board.organization_id) and
required_permissions == Enum.to_list(MapSet.intersection(MapSet.new(required_permissions), MapSet.new(available_permissions)))
end
The code above has very long lines and running the formatter is not going
to address this issue. In fact, the formatter may make it more obvious that
you have complex expressions:
def board?(board_id, %User{} = user, available_permissions, required_permissions) do
Tracker.OrganizationMembers.user_in_organization?(user.id, board.organization_id) and
required_permissions ==
Enum.to_list(
MapSet.intersection(
MapSet.new(required_permissions),
MapSet.new(available_permissions)
)
)
end
Take such cases as a suggestion that your code should be refactored:
def board?(board_id, %User{} = user, available_permissions, required_permissions) do
Tracker.OrganizationMembers.user_in_organization?(user.id, board.organization_id) and
matching_permissions?(required_permissions, available_permissions)
end
defp matching_permissions?(required_permissions, available_permissions) do
intersection =
required_permissions
|> MapSet.new()
|> MapSet.intersection(MapSet.new(available_permissions))
|> Enum.to_list()
required_permissions == intersection
end
To sum it up: since the formatter cannot change the semantics of your
code, sometimes it is necessary to tweak or refactor the code to get
optimal formatting. To help better understand how to control the formatter,
we describe in the next sections the cases where the formatter keeps the
user encoding and how to control multiline expressions.
## Keeping user's formatting
The formatter respects the input format in some cases. Those are
@@ -416,7 +275,54 @@ defmodule Code do
rules in the future. The goal of documenting them is to provide better
understanding on what to expect from the formatter.
### Multi-line lists, maps, tuples, etc.
## Adjusting formatted output
The formatter attempts to the fit the most it can on a single line.
When the code does not fit a single line, the formatter introduces
line breaks in the code.
In some rare situations, this may lead to undesired formatting.
For example, 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 `<>`:
"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, ...},
arg1,
arg2
) do
While the code above is completely valid, you may prefer to match on
the struct variables inside the function body in order to keep the
definition on a single line:
def my_function(%User{} = user, arg1, arg2) do
%{name: name, age: age, ...} = user
Since the formatter cannot change the semantics of your code,
sometimes it is necessary to tweak the code to get optimal formatting.
### Multi-line lists, maps, tuples, etc
You can force lists, tuples, bitstrings, maps, structs and function
calls to have one entry per line by adding a newline after the opening
@@ -437,14 +343,14 @@ defmodule Code do
[foo, bar]
You can also force function calls and keywords to be rendered on multiple
lines by having each entry on its own line:
You can also force keywords to be rendered on multiple lines by
having each entry on its own line:
defstruct name: nil,
age: 0
The code above will be kept with one keyword entry per line by the
formatter. To avoid that, just squash everything into a single line.
formatter. To avoid that, just keep everything on a single line.
### Parens and no parens in function calls
@@ -453,8 +359,7 @@ defmodule Code do
1. calls that have do/end blocks
2. local calls without parens where the name and arity of the local
call is also listed under `:locals_without_parens` (except for
calls with arity 0, where the compiler always require parens)
call is also listed under `:locals_without_parens`
The choice of parens and no parens also affects indentation. When a
function call with parens doesn't fit on the same line, the formatter
@@ -482,7 +387,7 @@ defmodule Code do
# code
end)
some_function_without_parens %{
some_funtion_without_parens %{
foo: :bar,
baz: :bat
}
@@ -496,7 +401,7 @@ defmodule Code do
The formatter also extracts all trailing comments to their previous line.
For example, the code below
hello #world
hello # world
will be rewritten to
@@ -531,7 +436,6 @@ defmodule Code do
user formatting). In such cases, the code formatter will always format to
the latter.
"""
@doc since: "1.6.0"
@spec format_string!(binary, keyword) :: iodata
def format_string!(string, opts \\ []) when is_binary(string) and is_list(opts) do
line_length = Keyword.get(opts, :line_length, 98)
@@ -545,7 +449,6 @@ defmodule Code do
See `format_string!/2` for more information on code formatting and
available options.
"""
@doc since: "1.6.0"
@spec format_file!(binary, keyword) :: iodata
def format_file!(file, opts \\ []) when is_binary(file) and is_list(opts) do
string = File.read!(file)
@@ -650,10 +553,6 @@ defmodule Code do
when non-existing atoms are found by the tokenizer.
Defaults to `false`.
* `:warn_on_unnecessary_quotes` - when `false`, does not warn
when atoms, keywords or calls have unnecessary quotes on
them. Defaults to `true`.
## `Macro.to_string/2`
The opposite of converting a string to its quoted form is
@@ -666,12 +565,8 @@ defmodule Code do
file = Keyword.get(opts, :file, "nofile")
line = Keyword.get(opts, :line, 1)
case :elixir.string_to_tokens(to_charlist(string), line, file, opts) do
{:ok, tokens} ->
:elixir.tokens_to_quoted(tokens, file, opts)
{:error, _error_msg} = error ->
error
with {:ok, tokens} <- :elixir.string_to_tokens(to_charlist(string), line, file, opts) do
:elixir.tokens_to_quoted(tokens, file, opts)
end
end
@@ -697,8 +592,8 @@ defmodule Code do
Accepts `relative_to` as an argument to tell where the file is located.
While `require_file/2` and `compile_file/2` return the loaded modules and their
bytecode, `eval_file/2` simply evaluates the file contents and returns the
While `load_file/2` loads a file and returns the loaded modules and their
byte code, `eval_file/2` simply evaluates the file contents and returns the
evaluation result and its bindings (exactly the same return value as `eval_string/3`).
"""
@spec eval_file(binary, nil | binary) :: {term, binding :: list}
@@ -707,13 +602,32 @@ defmodule Code do
eval_string(File.read!(file), [], file: file, line: 1)
end
# TODO: Deprecate me on 1.9
@doc false
@doc """
Loads the given file.
Accepts `relative_to` as an argument to tell where the file is located.
If the file was already required/loaded, loads it again.
It returns a list of tuples `{ModuleName, bytecode}`, one tuple for
each module defined in the file.
Notice that if `load_file/2` is invoked by different processes concurrently,
the target file will be loaded concurrently many times. Check `require_file/2`
if you don't want a file to be loaded concurrently.
## Examples
modules = Code.load_file("eex_test.exs", "../eex/test")
List.first(modules)
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
"""
@spec load_file(binary, nil | binary) :: [{module, binary}]
def load_file(file, relative_to \\ nil) when is_binary(file) do
file = find_file(file, relative_to)
:elixir_code_server.call({:acquire, file})
loaded = :elixir_compiler.file(file)
:elixir_code_server.cast({:required, file})
:elixir_code_server.cast({:loaded, file})
loaded
end
@@ -721,51 +635,47 @@ defmodule Code do
Requires the given `file`.
Accepts `relative_to` as an argument to tell where the file is located.
If the file was already required, `require_file/2` doesn't do anything and
returns `nil`.
The return value is the same as that of `load_file/2`. If the file was already
required or loaded, `require_file/2` doesn't do anything and returns `nil`.
Notice that if `require_file/2` is invoked by different processes concurrently,
the first process to invoke `require_file/2` acquires a lock and the remaining
ones will block until the file is available. This means that if `require_file/2`
is called more than once with a given file, that file will be compiled only once.
The first process to call `require_file/2` will get the list of loaded modules,
others will get `nil`.
ones will block until the file is available. This means that if `require_file/2` is called
more than one times with a given file, that file will be loaded only once. The first process to
call `require_file/2` will get the list of loaded modules, others will get `nil`.
See `compile_file/2` if you would like to compile a file without tracking its
filenames. Finally, if you would like to get the result of evaluating a file rather
than the modules defined in it, see `eval_file/2`.
Check `load_file/2` if you want to load a file multiple times. See also `unload_files/1`.
## Examples
If the file has not been required, it returns the list of modules:
If the code is already loaded, it returns `nil`:
Code.require_file("eex_test.exs", "../eex/test")
#=> nil
If the code is not loaded yet, it returns the same as `load_file/2`:
modules = Code.require_file("eex_test.exs", "../eex/test")
List.first(modules)
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
If the file has been required, it returns `nil`:
Code.require_file("eex_test.exs", "../eex/test")
#=> nil
"""
@spec require_file(binary, nil | binary) :: [{module, binary}] | nil
def require_file(file, relative_to \\ nil) when is_binary(file) do
file = find_file(file, relative_to)
# TODO: Simply block until :required or :proceed once load_file is removed in 2.0
case :elixir_code_server.call({:acquire, file}) do
:required ->
:loaded ->
nil
{:queued, ref} ->
receive do
{:elixir_code_server, ^ref, :required} -> nil
{:elixir_code_server, ^ref, :loaded} -> nil
end
:proceed ->
loaded = :elixir_compiler.file(file)
:elixir_code_server.cast({:required, file})
:elixir_code_server.cast({:loaded, file})
loaded
end
end
@@ -790,11 +700,11 @@ defmodule Code do
@doc """
Returns a list with the available compiler options.
See `compiler_options/1` for more information.
See `compiler_options/1` for more info.
## Examples
iex> Code.available_compiler_options()
iex> Code.available_compiler_options
[:docs, :debug_info, :ignore_module_conflict, :relative_paths, :warnings_as_errors]
"""
@@ -803,27 +713,6 @@ defmodule Code do
[:docs, :debug_info, :ignore_module_conflict, :relative_paths, :warnings_as_errors]
end
@doc """
Purge compiler modules.
The compiler utilizes temporary modules to compile code. For example,
`elixir_compiler_1`, `elixir_compiler_2`, etc. In case the compiled code
stores references to anonymous functions or similar, the Elixir compiler
may be unable to reclaim those modules, keeping an unnecessary amount of
code in memory and eventually leading to modules such as `elixir_compiler_12345`.
This function purges all modules currently kept by the compiler, allowing
old compiler module names to be reused. If there are any processes running
any code from such modules, they will be terminated too.
It returns `{:ok, number_of_modules_purged}`.
"""
@doc since: "1.7.0"
@spec purge_compiler_modules() :: {:ok, non_neg_integer()}
def purge_compiler_modules() do
:elixir_code_server.call(:purge_compiler_modules)
end
@doc """
Sets compilation options.
@@ -885,11 +774,7 @@ defmodule Code do
given as second argument which will be used for reporting warnings
and errors.
**Warning**: `string` can be any Elixir code and code can be executed with
the same privileges as the Erlang VM: this means that such code could
compromise the machine (for example by executing system commands).
Don't use `compile_string/2` with untrusted input (such as strings coming
from the network).
For compiling many files at once, check `Kernel.ParallelCompiler.compile/2`.
"""
@spec compile_string(List.Chars.t(), binary) :: [{module, binary}]
def compile_string(string, file \\ "nofile") when is_binary(file) do
@@ -909,25 +794,6 @@ defmodule Code do
:elixir_compiler.quoted(quoted, file)
end
@doc """
Compiles the given file.
Accepts `relative_to` as an argument to tell where the file is located.
Returns a list of tuples where the first element is the module name and
the second one is its bytecode (as a binary). Opposite to `require_file/2`,
it does not track the filename of the compiled file.
If you would like to get the result of evaluating file rather than the
modules defined in it, see `eval_file/2`.
For compiling many files concurrently, see `Kernel.ParallelCompiler.compile/2`.
"""
@spec compile_file(binary, nil | binary) :: [{module, binary}]
def compile_file(file, relative_to \\ nil) when is_binary(file) do
:elixir_compiler.file(find_file(file, relative_to))
end
@doc """
Ensures the given module is loaded.
@@ -1047,86 +913,95 @@ defmodule Code do
end
@doc ~S"""
Returns the docs for the given module or path to `.beam` file.
Returns the docs for the given module.
When given a module name, it finds its BEAM code and reads the docs from it.
When given a path to a `.beam` file, it will load the docs directly from that
file.
It returns the term stored in the documentation chunk in the format defined by
[EEP 48](http://erlang.org/eep/eeps/eep-0048.html) or `{:error, reason}` if
the chunk is not available.
The return value depends on the `kind` value:
* `:moduledoc` - tuple `{line, doc}` where `line` is the line on
which the module definition starts and `doc` is the string
attached to the module using the `@moduledoc` attribute,
`false` if `@moduledoc false` was used, or `nil` if no `@moduledoc`
was used.
* `:docs` - list of all docstrings attached to functions and macros
using the `@doc` attribute. Each tuple has the form
`{{name, arity}, line, kind, arguments, doc}`. `doc` can be either a
string, `false` if `@doc false` was used, or `nil` if no doc was used.
* `:callback_docs` - list of all docstrings attached to
`@callbacks` using the `@doc` attribute. Each tuple has the form
`{{name, arity}, line, kind, doc}`. `doc` can be either a string or
`nil` if no `@doc` was set.
* `:type_docs` - list of all docstrings attached to `@type` callbacks
using the `@typedoc` attribute. Each tuple has the form
`{{name, arity}, line, kind, doc}`. `doc` can be either a string or
`nil` if no `@typedoc` was used.
* `:all` - a keyword list with `:docs`, `:moduledoc`, `:callback_docs`,
and `:type_docs`.
If the module cannot be found, it returns `nil`.
## Examples
# Module documentation of an existing module
iex> {:docs_v1, _, :elixir, _, %{"en" => module_doc}, _, _} = Code.fetch_docs(Atom)
iex> module_doc |> String.split("\n") |> Enum.at(0)
iex> {_line, text} = Code.get_docs(Atom, :moduledoc)
iex> text |> String.split("\n") |> Enum.at(0)
"Convenience functions for working with atoms."
# A module that doesn't exist
iex> Code.fetch_docs(ModuleNotGood)
{:error, :module_not_found}
iex> Code.get_docs(ModuleNotGood, :all)
nil
"""
@doc since: "1.7.0"
@spec fetch_docs(module | String.t()) ::
{:docs_v1, annotation, beam_language, format, module_doc :: doc_content, metadata,
docs :: [doc_element]}
| {:error, :module_not_found | :chunk_not_found | {:invalid_chunk, binary}}
when annotation: :erl_anno.anno(),
beam_language: :elixir | :erlang | :lfe | :alpaca | atom(),
doc_content: %{binary => binary} | :none | :hidden,
doc_element:
{{kind :: atom, function_name :: atom, arity}, annotation, signature, doc_content,
metadata},
format: binary,
signature: [binary],
metadata: map
def fetch_docs(module_or_path)
@doc_kinds [:docs, :moduledoc, :callback_docs, :type_docs, :all]
def fetch_docs(module) when is_atom(module) do
@spec get_docs(module, :moduledoc) :: {line :: pos_integer, doc :: false | binary} | nil
@spec get_docs(module, :docs) :: [{function, line, kind, list, doc}] | nil
when function: {atom, arity}, line: pos_integer, kind: atom, doc: nil | false | binary
@spec get_docs(module, :callback_docs) :: [{callback, line, kind, doc}] | nil
when callback: {atom, arity}, line: pos_integer, kind: atom, doc: nil | false | binary
@spec get_docs(module, :type_docs) :: [{type, line, kind, doc}] | nil
when type: {atom, arity}, line: pos_integer, kind: atom, doc: nil | false | binary
@spec get_docs(module, :all) :: keyword | nil
def get_docs(module, kind)
def get_docs(module, kind) when is_atom(module) and kind in @doc_kinds do
case :code.get_object_code(module) do
{_module, bin, _beam_path} -> do_fetch_docs(bin)
:error -> {:error, :module_not_found}
{_module, bin, _beam_path} -> do_get_docs(bin, kind)
:error -> nil
end
end
def fetch_docs(path) when is_binary(path) do
do_fetch_docs(String.to_charlist(path))
def get_docs(binpath, kind) when is_binary(binpath) and kind in @doc_kinds do
do_get_docs(String.to_charlist(binpath), kind)
end
@docs_chunk 'Docs'
@docs_chunk 'ExDc'
defp do_fetch_docs(bin_or_path) do
defp do_get_docs(bin_or_path, kind) do
case :beam_lib.chunks(bin_or_path, [@docs_chunk]) do
{:ok, {_module, [{@docs_chunk, bin}]}} ->
try do
:erlang.binary_to_term(bin)
rescue
_ -> {:error, {:invalid_chunk, bin}}
end
lookup_docs(:erlang.binary_to_term(bin), kind)
{:error, :beam_lib, {:missing_chunk, _, @docs_chunk}} ->
{:error, :chunk_not_found}
nil
end
end
@doc ~S"""
Deprecated function to retrieve old documentation format.
defp lookup_docs({:elixir_docs_v1, docs}, kind), do: do_lookup_docs(docs, kind)
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`.
Use `Code.fetch_docs/1` instead.
"""
@deprecated "Code.get_docs/2 always returns nil as its outdated documentation is no longer stored on BEAM files. Use Code.fetch_docs/1 instead"
@spec get_docs(module, :moduledoc | :docs | :callback_docs | :type_docs | :all) :: nil
def get_docs(_module, _kind) do
nil
end
# unsupported chunk version
defp lookup_docs(_, _), do: nil
defp do_lookup_docs(docs, :all), do: docs
defp do_lookup_docs(docs, kind), do: Keyword.get(docs, kind)
## Helpers
+202 -398
View File
@@ -10,7 +10,6 @@ defmodule Code.Formatter do
@min_line 0
@max_line 9_999_999
@empty empty()
@ampersand_prec Code.Identifier.unary_op(:&) |> elem(1)
# Operators that do not have space between operands
@no_space_binary_operators [:..]
@@ -18,8 +17,8 @@ defmodule Code.Formatter do
# Operators that do not have newline between operands (as well as => and keywords)
@no_newline_binary_operators [:\\, :in]
# Left associative operators that start on the next line in case of breaks (always pipes)
@pipeline_operators [:|>, :~>>, :<<~, :~>, :<~, :<~>, :<|>]
# Left associative operators that start on the next line in case of breaks
@left_new_line_before_binary_operators [:|>, :~>>, :<<~, :~>, :<~, :<~>, :<|>]
# Right associative operators that start on the next line in case of breaks
@right_new_line_before_binary_operators [:|, :when]
@@ -194,7 +193,7 @@ defmodule Code.Formatter do
Returns `{:ok, doc}` or `{:error, parser_error}`.
See `Code.format_string!/2` for the list of options.
See `format!/2` for the list of options.
"""
def to_algebra(string, opts \\ []) when is_binary(string) and is_list(opts) do
file = Keyword.get(opts, :file, "nofile")
@@ -202,12 +201,7 @@ defmodule Code.Formatter do
charlist = String.to_charlist(string)
Process.put(:code_formatter_comments, [])
tokenizer_options = [
unescape: false,
preserve_comments: &preserve_comments/5,
warn_on_unnecessary_quotes: false
]
tokenizer_options = [unescape: false, preserve_comments: &preserve_comments/5]
with {:ok, tokens} <- :elixir.string_to_tokens(charlist, line, file, tokenizer_options),
{:ok, forms} <- :elixir.tokens_to_quoted(tokens, file, formatter_metadata: true) do
@@ -229,7 +223,7 @@ defmodule Code.Formatter do
Raises if the `string` cannot be parsed.
See `Code.format_string!/2` for the list of options.
See `format!/2` for the list of options.
"""
def to_algebra!(string, opts \\ []) do
case to_algebra(string, opts) do
@@ -259,7 +253,6 @@ defmodule Code.Formatter do
|> Keyword.get(:locals_without_parens, [])
|> MapSet.new()
|> MapSet.union(@locals_without_parens)
|> MapSet.to_list()
%{
locals_without_parens: locals_without_parens,
@@ -332,12 +325,12 @@ defmodule Code.Formatter do
# Special AST nodes from compiler feedback.
defp quoted_to_algebra({{:special, :clause_args}, _meta, [args]}, _context, state) do
defp quoted_to_algebra({:special, :clause_args, [args]}, _context, state) do
{doc, state} = clause_args_to_algebra(args, state)
{group(doc), state}
end
defp quoted_to_algebra({{:special, :bitstring_segment}, _meta, [arg, last]}, _context, state) do
defp quoted_to_algebra({:special, :bitstring_segment, [arg, last]}, _context, state) do
bitstring_segment_to_algebra({arg, -1}, state, last)
end
@@ -367,24 +360,24 @@ defmodule Code.Formatter do
end
defp quoted_to_algebra(
{{:., _, [List, :to_charlist]}, meta, [entries]} = quoted,
{{:., _, [String, :to_charlist]}, _, [{:<<>>, meta, entries}]} = quoted,
context,
state
) do
cond do
not list_interpolated?(entries) ->
not interpolated?(entries) ->
remote_to_algebra(quoted, context, state)
meta[:format] == :list_heredoc ->
{doc, state} =
entries
|> prepend_heredoc_line()
|> list_interpolation_to_algebra(:heredoc, state, @single_heredoc, @single_heredoc)
|> interpolation_to_algebra(:heredoc, state, @single_heredoc, @single_heredoc)
{force_unfit(doc), state}
true ->
list_interpolation_to_algebra(entries, @single_quote, state, @single_quote, @single_quote)
interpolation_to_algebra(entries, @single_quote, state, @single_quote, @single_quote)
end
end
@@ -425,11 +418,11 @@ defmodule Code.Formatter do
# {}
# {1, 2}
defp quoted_to_algebra({:{}, meta, args}, _context, state) do
tuple_to_algebra(meta, args, :flex_break, state)
tuple_to_algebra(meta, args, :flex_glue, state)
end
defp quoted_to_algebra({:__block__, meta, [{left, right}]}, _context, state) do
tuple_to_algebra(meta, [left, right], :flex_break, state)
tuple_to_algebra(meta, [left, right], :flex_glue, state)
end
defp quoted_to_algebra({:__block__, meta, [list]}, _context, state) when is_list(list) do
@@ -518,7 +511,7 @@ defmodule Code.Formatter do
defp quoted_to_algebra({:not, meta, [{:in, _, [left, right]} = arg]}, context, state) do
%{rename_deprecated_at: since} = state
# TODO: Remove since check on Elixir v2.0 and the OP arrangement is removed.
# TODO: Remove since check on Elixir v2.0 and the OP arrengement is removed.
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
@@ -527,7 +520,7 @@ defmodule Code.Formatter do
end
defp quoted_to_algebra({:fn, meta, [_ | _] = clauses}, _context, state) do
anon_fun_to_algebra(clauses, line(meta), end_line(meta), state, eol?(meta))
anon_fun_to_algebra(clauses, line(meta), end_line(meta), state)
end
defp quoted_to_algebra({fun, meta, args}, context, state) when is_atom(fun) and is_list(args) do
@@ -560,16 +553,7 @@ defmodule Code.Formatter do
{left, state} =
case left_arg do
{:__block__, _, [atom]} when is_atom(atom) ->
key =
case Code.Identifier.classify(atom) do
type when type in [:callable_local, :callable_operator, :not_callable] ->
IO.iodata_to_binary([Atom.to_string(atom), ?:])
_ ->
IO.iodata_to_binary([?", Atom.to_string(atom), ?", ?:])
end
{string(key), state}
{atom |> Code.Identifier.inspect_as_key() |> string(), state}
{{:., _, [:erlang, :binary_to_atom]}, _, [{:<<>>, _, entries}, :utf8]} ->
interpolation_to_algebra(entries, @double_quote, state, "\"", "\":")
@@ -686,82 +670,23 @@ defmodule Code.Formatter do
# strict or flex mode around.
defp binary_op_to_algebra(op, op_string, meta, left_arg, right_arg, context, state) do
%{operand_nesting: nesting} = state
binary_op_to_algebra(op, op_string, meta, left_arg, right_arg, context, state, nesting)
binary_op_to_algebra(op, op_string, meta, left_arg, right_arg, context, state, nil, nesting)
end
defp binary_op_to_algebra(op, op_string, meta, left_arg, right_arg, context, state, _nesting)
when op in @right_new_line_before_binary_operators do
defp binary_op_to_algebra(
op,
op_string,
meta,
left_arg,
right_arg,
context,
state,
parent_info,
nesting
) do
op_info = Code.Identifier.binary_op(op)
op_string = op_string <> " "
left_context = left_op_context(context)
right_context = right_op_context(context)
min_line =
case left_arg do
{_, left_meta, _} -> line(left_meta)
_ -> line(meta)
end
{operands, max_line} =
unwrap_right(right_arg, op, meta, right_context, [{{:root, left_context}, left_arg}])
operand_to_algebra = fn
{{:root, context}, arg}, _args, newlines, state ->
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :left, 2)
{doc, @empty, newlines, state}
{{kind, context}, arg}, _args, newlines, state ->
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, kind, 0)
doc = doc |> nest_by_length(op_string) |> force_keyword(arg)
{concat(op_string, doc), @empty, newlines, state}
end
{doc, state} =
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}
else
{doc, state}
end
end
defp binary_op_to_algebra(op, _, meta, left_arg, right_arg, context, state, _nesting)
when op in @pipeline_operators do
op_info = Code.Identifier.binary_op(op)
left_context = left_op_context(context)
right_context = right_op_context(context)
max_line = line(meta)
{pipes, min_line} =
unwrap_pipes(left_arg, meta, left_context, [{{op, right_context}, right_arg}])
operand_to_algebra = fn
{{:root, context}, arg}, _args, newlines, state ->
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :left, 2)
{doc, @empty, newlines, state}
{{op, context}, arg}, _args, newlines, state ->
op_info = Code.Identifier.binary_op(op)
op_string = Atom.to_string(op) <> " "
{doc, state} = binary_operand_to_algebra(arg, context, state, op, op_info, :right, 0)
{concat(op_string, doc), @empty, newlines, state}
end
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
op_info = Code.Identifier.binary_op(op)
left_context = left_op_context(context)
right_context = right_op_context(context)
left_context = force_many_args_or_operand(context, :parens_arg)
right_context = force_many_args_or_operand(context, :operand)
{left, state} =
binary_operand_to_algebra(left_arg, left_context, state, op, op_info, :left, 2)
@@ -778,17 +703,47 @@ defmodule Code.Formatter do
op_string = " " <> op_string <> " "
concat(concat(group(left), op_string), group(right))
true ->
eol? = eol?(meta)
op in @left_new_line_before_binary_operators ->
op_string = op_string <> " "
# If the parent is of the same type (computed via same precedence),
# we cannot group the left side yet.
left = if op_info == parent_info, do: left, else: group(left)
doc = glue(left, concat(op_string, group(right)))
if Keyword.get(meta, :eol, false), do: force_unfit(doc), else: doc
op in @right_new_line_before_binary_operators ->
op_string = op_string <> " "
# If the parent is of the same type (computed via same precedence),
# we need to nest the left side because of the associativity.
left =
if op_info == parent_info do
nest_by_length(left, op_string)
else
group(left)
end
# If the right side is of the same type, we will keep recursing
# and do the nesting on the left side later on (as written above).
right =
case right_arg do
{^op, _, [_, _]} -> right
_ -> right |> nest_by_length(op_string) |> force_keyword(right_arg) |> group()
end
doc = glue(left, concat(op_string, right))
if Keyword.get(meta, :eol, false), do: force_unfit(doc), else: doc
true ->
next_break_fits? =
op in @next_break_fits_operators and next_break_fits?(right_arg, state) and not eol?
op in @next_break_fits_operators and next_break_fits?(right_arg, state) and
not Keyword.get(meta, :eol, false)
with_next_break_fits(next_break_fits?, right, fn right ->
op_string = " " <> op_string
right = nest(glue(op_string, group(right)), nesting, :break)
right = if eol?, do: force_unfit(right), else: right
concat(group(left), group(right))
concat(group(left), group(nest(glue(op_string, group(right)), nesting, :break)))
end)
end
@@ -814,11 +769,10 @@ defmodule Code.Formatter do
end
defp binary_operand_to_algebra(operand, context, state, parent_op, parent_info, side, nesting) do
{parent_assoc, parent_prec} = parent_info
with {op, meta, [left, right]} <- operand,
op_info = Code.Identifier.binary_op(op),
{_assoc, prec} <- op_info do
{parent_assoc, parent_prec} = parent_info
op_string = Atom.to_string(op)
cond do
@@ -826,7 +780,17 @@ defmodule Code.Formatter do
# the correct side, we respect the nesting rule to avoid multiple
# nestings. This only applies for left associativity or same operator.
parent_prec == prec and parent_assoc == side and (side == :left or op == parent_op) ->
binary_op_to_algebra(op, op_string, meta, left, right, context, state, nesting)
binary_op_to_algebra(
op,
op_string,
meta,
left,
right,
context,
state,
parent_info,
nesting
)
# If the parent requires parens or the precedence is inverted or
# it is in the wrong side, then we *need* parenthesis.
@@ -835,18 +799,16 @@ defmodule Code.Formatter do
parent_op in @required_parens_logical_binary_operands) or parent_prec > prec or
(parent_prec == prec and parent_assoc != side) ->
{operand, state} =
binary_op_to_algebra(op, op_string, meta, left, right, context, state, 2)
binary_op_to_algebra(op, op_string, meta, left, right, context, state, parent_info, 2)
{wrap_in_parens(operand), state}
# Otherwise, we rely on precedence but also nest.
true ->
binary_op_to_algebra(op, op_string, meta, left, right, context, state, 2)
binary_op_to_algebra(op, op_string, meta, left, right, context, state, parent_info, 2)
end
else
{:&, _, [arg]}
when not is_integer(arg) and side == :left
when not is_integer(arg) and parent_assoc == :left and parent_prec > @ampersand_prec ->
{:&, _, [arg]} when not is_integer(arg) and side == :left ->
{doc, state} = quoted_to_algebra(operand, context, state)
{wrap_in_parens(doc), state}
@@ -855,45 +817,6 @@ defmodule Code.Formatter do
end
end
defp unwrap_pipes({op, meta, [left, right]}, _meta, context, acc)
when op in @pipeline_operators do
left_context = left_op_context(context)
right_context = right_op_context(context)
unwrap_pipes(left, meta, left_context, [{{op, right_context}, right} | acc])
end
defp unwrap_pipes(left, meta, context, acc) do
min_line =
case left do
{_, meta, _} -> line(meta)
_ -> line(meta)
end
{[{{:root, context}, left} | acc], min_line}
end
defp unwrap_right({op, meta, [left, right]}, op, _meta, context, acc) do
left_context = left_op_context(context)
right_context = right_op_context(context)
unwrap_right(right, op, meta, right_context, [{{:left, left_context}, left} | acc])
end
defp unwrap_right(right, _op, meta, context, acc) do
acc = [{{:right, context}, right} | acc]
{Enum.reverse(acc), line(meta)}
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, [], min_line, max_line, 1, state, fun)
if comments? or eol?(meta) do
{docs |> Enum.reduce(&line(&2, &1)) |> force_unfit(), state}
else
{docs |> Enum.reduce(&glue(&2, &1)), state}
end
end
## Module attributes
# @Foo
@@ -975,7 +898,7 @@ defmodule Code.Formatter do
# expression.{arguments}
defp remote_to_algebra({{:., _, [target, :{}]}, meta, args}, _context, state) do
{target_doc, state} = remote_target_to_algebra(target, state)
{call_doc, state} = tuple_to_algebra(meta, args, :break, state)
{call_doc, state} = tuple_to_algebra(meta, args, :glue, state)
{concat(concat(target_doc, "."), call_doc), state}
end
@@ -1060,10 +983,6 @@ defmodule Code.Formatter do
# We can only rename functions in the same module because
# introducing a new module may 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
@@ -1106,7 +1025,7 @@ defmodule Code.Formatter do
#
defp call_args_to_algebra([], meta, _context, _parens, _list_to_keyword?, state) do
{args_doc, _join, state} =
args_to_algebra_with_comments([], meta, false, :none, :break, state, &{&1, &2})
args_to_algebra_with_comments([], meta, false, false, :glue, state, &{&1, &2})
{{surround("(", args_doc, ")"), state}, false}
end
@@ -1140,6 +1059,7 @@ defmodule Code.Formatter do
defp call_args_to_algebra_no_blocks(meta, args, skip_parens?, list_to_keyword?, extra, state) do
{left, right} = split_last(args)
generators_count = count_generators(args)
{keyword?, right} = last_arg_to_keyword(right, list_to_keyword?)
context =
@@ -1149,110 +1069,75 @@ defmodule Code.Formatter do
if skip_parens?, do: :no_parens_arg, else: :parens_arg
end
args = if keyword?, do: left ++ right, else: left ++ [right]
many_eol? = match?([_, _ | _], args) and eol?(meta)
no_generators? = no_generators?(args)
if left != [] and keyword? and skip_parens? and generators_count == 0 do
call_args_to_algebra_with_no_parens_keywords(meta, left, right, context, extra, state)
else
next_break_fits? = next_break_fits?(right, state)
force_keyword? = keyword? and force_keyword?(right)
non_empty_eol? = left != [] and not next_break_fits? and Keyword.get(meta, :eol, false)
join = if generators_count > 1 or force_keyword? or non_empty_eol?, do: :line, else: :glue
args = if keyword?, do: left ++ right, else: left ++ [right]
{args_doc, _join, state} =
args_to_algebra_with_comments(
args,
meta,
skip_parens?,
next_break_fits?,
join,
state,
&quoted_to_algebra(&1, context, &2)
)
# If we have a single argument, then we won't have an option to break
# before the "extra" part, so we ungroup it and build it later.
args_doc = ungroup_if_group(args_doc)
doc =
if skip_parens? do
" "
|> concat(nest(args_doc, :cursor, :break))
|> concat(extra)
|> group()
else
glue("(", "", args_doc)
|> nest(2, :break)
|> glue("", ")")
|> concat(extra)
|> group()
end
if next_break_fits? do
{next_break_fits(doc, :disabled), state}
else
{doc, state}
end
end
end
defp call_args_to_algebra_with_no_parens_keywords(meta, left, right, context, extra, state) do
to_algebra_fun = &quoted_to_algebra(&1, context, &2)
{args_doc, next_break_fits?, state} =
if left != [] and keyword? and no_generators? do
join = if force_args?(left) or many_eol?, do: :line, else: :break
{left_doc, _join, state} =
args_to_algebra_with_comments(left, meta, true, false, :glue, state, to_algebra_fun)
{left_doc, _join, state} =
args_to_algebra_with_comments(
left,
Keyword.delete(meta, :end_line),
skip_parens?,
:force_comma,
join,
state,
to_algebra_fun
)
{right_doc, _join, state} =
args_to_algebra_with_comments(right, meta, false, false, :glue, state, to_algebra_fun)
join = if force_args?(right) or force_args?(args) or many_eol?, do: :line, else: :break
{right_doc, _join, state} =
args_to_algebra_with_comments(right, meta, false, :none, join, state, to_algebra_fun)
right_doc = apply(Inspect.Algebra, join, []) |> concat(right_doc)
args_doc =
if skip_parens? do
left_doc
|> concat(next_break_fits(group(right_doc, :inherit), :enabled))
|> nest(:cursor, :break)
else
right_doc =
right_doc
|> nest(2, :break)
|> concat(break(""))
|> group(:inherit)
|> next_break_fits(:enabled)
concat(nest(left_doc, 2, :break), right_doc)
end
{args_doc, true, state}
else
join = if force_args?(args) or many_eol?, do: :line, else: :break
next_break_fits? = join == :break and next_break_fits?(right, state)
last_arg_mode = if next_break_fits?, do: :next_break_fits, else: :none
{args_doc, _join, state} =
args_to_algebra_with_comments(
args,
meta,
skip_parens?,
last_arg_mode,
join,
state,
to_algebra_fun
)
# If we have a single argument, then we won't have an option to break
# before the "extra" part, so we ungroup it and build it later.
args_doc = ungroup_if_group(args_doc)
args_doc =
if skip_parens? do
nest(args_doc, :cursor, :break)
else
nest(args_doc, 2, :break) |> concat(break(""))
end
{args_doc, next_break_fits?, state}
end
right_doc = "," |> glue(right_doc) |> force_keyword(right) |> group(:inherit)
doc =
cond do
left != [] and keyword? and skip_parens? and no_generators? ->
" "
|> concat(args_doc)
|> nest(2)
|> concat(extra)
|> group()
with_next_break_fits(true, right_doc, fn right_doc ->
args_doc = concat(left_doc, right_doc)
skip_parens? ->
" "
|> concat(args_doc)
|> concat(extra)
|> group()
" "
|> concat(nest(args_doc, :cursor, :break))
|> nest(2)
|> concat(extra)
|> group()
end)
true ->
"("
|> concat(break(""))
|> nest(2, :break)
|> concat(args_doc)
|> concat(")")
|> concat(extra)
|> group()
end
if next_break_fits? do
{next_break_fits(doc, :disabled), state}
else
{doc, state}
end
{doc, state}
end
defp local_without_parens?(fun, args, %{locals_without_parens: locals_without_parens}) do
@@ -1264,8 +1149,8 @@ defmodule Code.Formatter do
end)
end
defp no_generators?(args) do
not Enum.any?(args, &match?({:<-, _, [_, _]}, &1))
defp count_generators(args) do
Enum.count(args, &match?({:<-, _, [_, _]}, &1))
end
defp do_end_blocks([{{:__block__, meta, [:do]}, _} | _] = blocks) do
@@ -1306,14 +1191,6 @@ defmodule Code.Formatter do
## Interpolation
defp list_interpolated?(entries) do
Enum.all?(entries, fn
{{:., _, [Kernel, :to_string]}, _, [_]} -> true
entry when is_binary(entry) -> true
_ -> false
end)
end
defp interpolated?(entries) do
Enum.all?(entries, fn
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
@@ -1330,23 +1207,6 @@ defmodule Code.Formatter do
["\n" | entries]
end
defp list_interpolation_to_algebra([entry | entries], escape, state, acc, last)
when is_binary(entry) do
acc = concat(acc, escape_string(entry, escape))
list_interpolation_to_algebra(entries, escape, state, acc, last)
end
defp list_interpolation_to_algebra([entry | entries], escape, state, acc, last) do
{{:., _, [Kernel, :to_string]}, meta, [quoted]} = entry
{doc, state} = block_to_algebra(quoted, line(meta), end_line(meta), state)
doc = surround("\#{", doc, "}")
list_interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
end
defp list_interpolation_to_algebra([], _escape, state, acc, last) do
{concat(acc, last), state}
end
defp interpolation_to_algebra([entry | entries], escape, state, acc, last)
when is_binary(entry) do
acc = concat(acc, escape_string(entry, escape))
@@ -1402,15 +1262,15 @@ defmodule Code.Formatter do
defp bitstring_to_algebra(meta, args, state) do
last = length(args) - 1
join = if eol?(meta), do: :line, else: :flex_break
join = if Keyword.get(meta, :eol, false), do: :line, else: :flex_glue
to_algebra_fun = &bitstring_segment_to_algebra(&1, &2, last)
{args_doc, join, state} =
args
|> Enum.with_index()
|> args_to_algebra_with_comments(meta, false, :none, join, state, to_algebra_fun)
|> args_to_algebra_with_comments(meta, false, false, join, state, to_algebra_fun)
if join == :flex_break do
if join == :flex_glue do
{"<<" |> concat(args_doc) |> nest(2) |> concat(">>") |> group(), state}
else
{surround("<<", args_doc, ">>"), state}
@@ -1418,7 +1278,7 @@ defmodule Code.Formatter do
end
defp bitstring_segment_to_algebra({{:<-, meta, [left, right]}, i}, state, last) do
left = {{:special, :bitstring_segment}, meta, [left, last]}
left = {:special, :bitstring_segment, [left, last]}
{doc, state} = quoted_to_algebra({:<-, meta, [left, right]}, :parens_arg, state)
{bitstring_wrap_parens(doc, i, last), state}
end
@@ -1426,17 +1286,8 @@ defmodule Code.Formatter do
defp bitstring_segment_to_algebra({{:::, _, [segment, spec]}, i}, state, last) do
{doc, state} = quoted_to_algebra(segment, :parens_arg, state)
{spec, state} = bitstring_spec_to_algebra(spec, state)
spec = wrap_in_parens_if_inspected_atom(spec)
spec = if i == last, do: bitstring_wrap_parens(spec, i, last), else: spec
doc =
doc
|> bitstring_wrap_parens(i, -1)
|> concat("::")
|> concat(spec)
{doc, state}
doc = concat(concat(doc, "::"), wrap_in_parens_if_inspected_atom(spec))
{bitstring_wrap_parens(doc, i, last), state}
end
defp bitstring_segment_to_algebra({segment, i}, state, last) do
@@ -1454,38 +1305,40 @@ defmodule Code.Formatter do
quoted_to_algebra_with_parens_if_operator(spec, :parens_arg, state)
end
defp bitstring_wrap_parens(doc, i, last) when i == 0 or i == last do
string = format_to_string(doc)
defp bitstring_wrap_parens(doc, i, last) do
if i == 0 or i == last do
string = format_to_string(doc)
if (i == 0 and String.starts_with?(string, ["~", "<<"])) or
(i == last and String.ends_with?(string, [">>"])) do
wrap_in_parens(doc)
if (i == 0 and String.starts_with?(string, "<<")) or
(i == last and String.ends_with?(string, ">>")) do
wrap_in_parens(doc)
else
doc
end
else
doc
end
end
defp bitstring_wrap_parens(doc, _, _), do: doc
## Literals
defp list_to_algebra(meta, args, state) do
join = if eol?(meta), do: :line, else: :break
join = if Keyword.get(meta, :eol, false), do: :line, else: :glue
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{args_doc, _join, state} =
args_to_algebra_with_comments(args, meta, false, :none, join, state, fun)
args_to_algebra_with_comments(args, meta, false, false, join, state, fun)
{surround("[", args_doc, "]"), state}
end
defp map_to_algebra(meta, name_doc, [{:|, _, [left, right]}], state) do
join = if eol?(meta), do: :line, else: :break
join = if Keyword.get(meta, :eol, false), do: :line, else: :glue
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{left_doc, state} = fun.(left, state)
{right_doc, _join, state} =
args_to_algebra_with_comments(right, meta, false, :none, join, state, fun)
args_to_algebra_with_comments(right, meta, false, false, join, state, fun)
args_doc =
left_doc
@@ -1497,24 +1350,24 @@ defmodule Code.Formatter do
end
defp map_to_algebra(meta, name_doc, args, state) do
join = if eol?(meta), do: :line, else: :break
join = if Keyword.get(meta, :eol, false), do: :line, else: :glue
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{args_doc, _join, state} =
args_to_algebra_with_comments(args, meta, false, :none, join, state, fun)
args_to_algebra_with_comments(args, meta, false, false, join, state, fun)
name_doc = "%" |> concat(name_doc) |> concat("{")
{surround(name_doc, args_doc, "}"), state}
end
defp tuple_to_algebra(meta, args, join, state) do
join = if eol?(meta), do: :line, else: join
join = if Keyword.get(meta, :eol, false), do: :line, else: join
fun = &quoted_to_algebra(&1, :parens_arg, &2)
{args_doc, join, state} =
args_to_algebra_with_comments(args, meta, false, :none, join, state, fun)
args_to_algebra_with_comments(args, meta, false, false, join, state, fun)
if join == :flex_break do
if join == :flex_glue do
{"{" |> concat(args_doc) |> nest(1) |> concat("}") |> group(), state}
else
{surround("{", args_doc, "}"), state}
@@ -1622,7 +1475,7 @@ defmodule Code.Formatter do
defp heredoc_line(["", _ | _]), do: nest(line(), :reset)
defp heredoc_line(_), do: line()
defp args_to_algebra_with_comments(args, meta, skip_parens?, last_arg_mode, join, state, fun) do
defp args_to_algebra_with_comments(args, meta, skip_parens?, next_break_fits?, join, state, fun) do
min_line = line(meta)
max_line = end_line(meta)
@@ -1630,11 +1483,10 @@ defmodule Code.Formatter do
{doc, state} = fun.(arg, state)
doc =
case args do
[_ | _] -> concat_to_last_group(doc, ",")
[] when last_arg_mode == :force_comma -> concat_to_last_group(doc, ",")
[] when last_arg_mode == :next_break_fits -> next_break_fits(doc, :enabled)
[] when last_arg_mode == :none -> doc
cond do
args != [] -> concat(doc, ",")
next_break_fits? -> next_break_fits(doc, :enabled)
true -> doc
end
{doc, @empty, newlines, state}
@@ -1662,24 +1514,18 @@ defmodule Code.Formatter do
join == :line or comments? ->
{args_docs |> Enum.reduce(&line(&2, &1)) |> force_unfit(), :line, state}
join == :break ->
{args_docs |> Enum.reduce(&glue(&2, &1)), :break, state}
join == :glue ->
{args_docs |> Enum.reduce(&glue(&2, &1)), :glue, state}
join == :flex_break ->
{args_docs |> Enum.reduce(&flex_glue(&2, &1)), :flex_break, state}
join == :flex_glue ->
{args_docs |> Enum.reduce(&flex_glue(&2, &1)), :flex_glue, state}
end
end
## Anonymous functions
# fn -> block end
defp anon_fun_to_algebra(
[{:->, meta, [[], body]}] = clauses,
_min_line,
max_line,
state,
_multi_clauses_style
) do
defp anon_fun_to_algebra([{:->, meta, [[], body]}] = clauses, _min_line, max_line, state) do
min_line = line(meta)
{body_doc, state} = block_to_algebra(body, min_line, max_line, state)
@@ -1698,13 +1544,7 @@ defmodule Code.Formatter do
# fn x ->
# y
# end
defp anon_fun_to_algebra(
[{:->, meta, [args, body]}] = clauses,
_min_line,
max_line,
state,
false = _multi_clauses_style
) do
defp anon_fun_to_algebra([{:->, meta, [args, body]}] = clauses, _min_line, max_line, state) do
min_line = line(meta)
{args_doc, state} = clause_args_to_algebra(args, min_line, state)
{body_doc, state} = block_to_algebra(body, min_line, max_line, state)
@@ -1734,7 +1574,7 @@ defmodule Code.Formatter do
# args2 ->
# block2
# end
defp anon_fun_to_algebra(clauses, min_line, max_line, state, _multi_clauses_style) do
defp anon_fun_to_algebra(clauses, min_line, max_line, state) do
{clauses_doc, state} = clauses_to_algebra(clauses, min_line, max_line, state)
{"fn" |> line(clauses_doc) |> nest(2) |> line("end") |> force_unfit(), state}
end
@@ -1791,7 +1631,7 @@ defmodule Code.Formatter do
## Clauses
defp maybe_force_clauses(doc, clauses) do
if Enum.any?(clauses, fn {:->, meta, _} -> eol?(meta) end) do
if Enum.any?(clauses, fn {:->, meta, _} -> Keyword.get(meta, :eol, false) end) do
force_unfit(doc)
else
doc
@@ -1857,25 +1697,19 @@ defmodule Code.Formatter do
end
defp clause_args_to_algebra(args, min_line, state) do
arg_to_algebra = fn arg, _args, newlines, state ->
{doc, state} = clause_args_to_algebra(arg, state)
{doc, @empty, newlines, state}
end
meta = [line: min_line]
fun = &clause_args_to_algebra/2
{args_docs, comments?, state} =
quoted_to_algebra_with_comments([args], [], min_line, @min_line, 1, state, arg_to_algebra)
{args_docs, _join, state} =
args_to_algebra_with_comments([args], meta, false, false, :glue, state, fun)
if comments? do
{Enum.reduce(args_docs, &line(&2, &1)), state}
else
{Enum.reduce(args_docs, &glue(&2, &1)), state}
end
{args_docs, state}
end
# fn a, b, c when d -> e end
defp clause_args_to_algebra([{:when, meta, args}], state) do
{args, right} = split_last(args)
left = {{:special, :clause_args}, meta, [args]}
left = {:special, :clause_args, [args]}
binary_op_to_algebra(:when, "when", meta, left, right, :no_parens_arg, state)
end
@@ -2005,9 +1839,6 @@ defmodule Code.Formatter do
## Quoted helpers
defp left_op_context(context), do: force_many_args_or_operand(context, :parens_arg)
defp right_op_context(context), do: force_many_args_or_operand(context, :operand)
defp force_many_args_or_operand(:no_parens_one_arg, _choice), do: :no_parens_arg
defp force_many_args_or_operand(:parens_one_arg, _choice), do: :parens_arg
defp force_many_args_or_operand(:no_parens_arg, _choice), do: :no_parens_arg
@@ -2127,7 +1958,7 @@ defmodule Code.Formatter do
(not interpolated?(entries) and eol_or_comments?(meta, state))
end
defp next_break_fits?({{:., _, [List, :to_charlist]}, meta, [[_ | _]]}, _state) do
defp next_break_fits?({{:., _, [String, :to_charlist]}, _, [{:<<>>, meta, [_ | _]}]}, _state) do
meta[:format] == :list_heredoc
end
@@ -2161,7 +1992,7 @@ defmodule Code.Formatter do
end
defp eol_or_comments?(meta, %{comments: comments}) do
eol?(meta) or
Keyword.get(meta, :eol, false) or
(
min_line = line(meta)
max_line = end_line(meta)
@@ -2169,47 +2000,38 @@ defmodule Code.Formatter do
)
end
# A literal list is a keyword or (... -> ...)
defp last_arg_to_keyword([_ | _] = arg, _list_to_keyword?) do
{keyword?(arg), arg}
end
# This is a list of tuples, it can be converted to keywords.
defp last_arg_to_keyword({:__block__, _, [[_ | _] = arg]} = block, true) do
if keyword?(arg), do: {true, arg}, else: {false, block}
end
# Otherwise we don't have a keyword.
defp last_arg_to_keyword(arg, _list_to_keyword?) do
{false, arg}
end
defp force_args?(args) do
match?([_, _ | _], args) and force_args?(args, MapSet.new())
defp force_keyword?(keyword) do
match?([_, _ | _], keyword) and force_keyword?(keyword, MapSet.new())
end
defp force_args?([[arg | _] | args], lines) do
force_args?([arg | args], lines)
end
defp force_keyword?([{{_, meta, _}, _} | keyword], lines) do
line = line(meta)
defp force_args?([arg | args], lines) do
line =
case arg do
{{_, meta, _}, _} -> line(meta)
{_, meta, _} -> line(meta)
end
if MapSet.member?(lines, line) do
if line in lines do
false
else
force_args?(args, MapSet.put(lines, line))
force_keyword?(keyword, MapSet.put(lines, line))
end
end
defp force_args?([], _lines), do: true
defp force_keyword?([], _lines) do
true
end
defp force_keyword(doc, arg) do
if force_args?(arg), do: force_unfit(doc), else: doc
if force_keyword?(arg), do: force_unfit(doc), else: doc
end
defp keyword?([{key, _} | list]) do
@@ -2232,10 +2054,6 @@ defmodule Code.Formatter do
false
end
defp eol?(meta) do
Keyword.get(meta, :eol, false)
end
defp line(meta) do
Keyword.get(meta, :line, @max_line)
end
@@ -2246,20 +2064,6 @@ defmodule Code.Formatter do
## Algebra helpers
# Relying on the inner document is brittle and error prone.
# It would be best if we had a mechanism to apply this.
defp concat_to_last_group({:doc_cons, left, right}, concat) do
{:doc_cons, left, concat_to_last_group(right, concat)}
end
defp concat_to_last_group({:doc_group, group, mode}, concat) do
{:doc_group, {:doc_cons, group, concat}, mode}
end
defp concat_to_last_group(other, concat) do
{:doc_cons, other, concat}
end
defp ungroup_if_group({:doc_group, group, _mode}), do: group
defp ungroup_if_group(other), do: other
+11 -11
View File
@@ -55,22 +55,22 @@ defmodule Code.Identifier do
@doc """
Classifies the given atom into one of the following categories:
* `:alias` - a valid Elixir alias, like `Foo`, `Foo.Bar` and so on
* :alias - a valid Elixir alias, like Foo, Foo.Bar and so on
* `:callable_local` - an atom that can be used as a local call;
this category includes identifiers like `:foo`
* :callable_local - an atom that can be used as a local call;
this category includes identifiers like :foo
* `:callable_operators` - all callable operators, such as `:<>`. Note
* :callable_operators - all callable operators, such as `:<>`. Note
operators such as `:..` are not callable because of ambiguity
* `:not_callable` - an atom that cannot be used as a function call after the
`.` operator (for example, `:<<>>` is not callable because `Foo.<<>>` is a
syntax error); this category includes atoms like `:Foo`, since they are
* :not_callable - an atom that cannot be used as a function call after the
. 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"`)
(Foo."Bar")
* `:other` - any other atom (these are usually escaped when inspected, like
`:"foo and bar"`)
* :other - any other atom (these are usually escaped when inspected, like
:"foo and bar")
"""
def classify(atom) when is_atom(atom) do
@@ -87,7 +87,7 @@ defmodule Code.Identifier do
:alias
true ->
case :elixir_config.get(:identifier_tokenizer, String.Tokenizer).tokenize(charlist) do
case :elixir_config.safe_get(:identifier_tokenizer, String.Tokenizer).tokenize(charlist) do
{kind, _acc, [], _, _, special} ->
if kind == :identifier and not :lists.member(?@, special) do
:callable_local
-416
View File
@@ -1,416 +0,0 @@
defmodule Code.Typespec do
@moduledoc false
@doc """
Converts a spec clause back to Elixir quoted expression.
"""
@spec spec_to_quoted(atom, tuple) :: {atom, keyword, [Macro.t()]}
def spec_to_quoted(name, spec)
def spec_to_quoted(name, {:type, line, :fun, [{:type, _, :product, args}, result]})
when is_atom(name) do
meta = [line: line]
body = {name, meta, Enum.map(args, &typespec_to_quoted/1)}
vars =
for type_expr <- args ++ [result],
var <- collect_vars(type_expr),
uniq: true,
do: {var, {:var, meta, nil}}
spec = {:::, meta, [body, typespec_to_quoted(result)]}
if vars == [] do
spec
else
{:when, meta, [spec, vars]}
end
end
def spec_to_quoted(name, {:type, line, :fun, []}) when is_atom(name) do
{:::, [line: line], [{name, [line: line], []}, quote(do: term)]}
end
def spec_to_quoted(name, {:type, line, :bounded_fun, [type, constrs]}) when is_atom(name) do
{:type, _, :fun, [{:type, _, :product, args}, result]} = type
guards =
for {:type, _, :constraint, [{:atom, _, :is_subtype}, [{:var, _, var}, type]]} <- constrs do
{erl_to_ex_var(var), typespec_to_quoted(type)}
end
meta = [line: line]
ignore_vars = Keyword.keys(guards)
vars =
for type_expr <- args ++ [result],
var <- collect_vars(type_expr),
var not in ignore_vars,
uniq: true,
do: {var, {:var, meta, nil}}
args = for arg <- args, do: typespec_to_quoted(arg)
when_args = [
{:::, meta, [{name, [line: line], args}, typespec_to_quoted(result)]},
guards ++ vars
]
{:when, meta, when_args}
end
@doc """
Converts a type clause back to Elixir AST.
"""
def type_to_quoted(type)
def type_to_quoted({{:record, record}, fields, args}) when is_atom(record) do
fields = for field <- fields, do: typespec_to_quoted(field)
args = for arg <- args, do: typespec_to_quoted(arg)
type = {:{}, [], [record | fields]}
quote(do: unquote(record)(unquote_splicing(args)) :: unquote(type))
end
def type_to_quoted({name, type, args}) when is_atom(name) do
args = for arg <- args, do: typespec_to_quoted(arg)
quote(do: unquote(name)(unquote_splicing(args)) :: unquote(typespec_to_quoted(type)))
end
@doc """
Returns all types available from the module's BEAM code.
The result is returned as a list of tuples where the first
element is the type (`:typep`, `:type` and `:opaque`).
The module must have a corresponding BEAM file which can be
located by the runtime system. The types will be in the Erlang
Abstract Format.
"""
@spec fetch_types(module | binary) :: {:ok, [tuple]} | :error
def fetch_types(module) when is_atom(module) or is_binary(module) do
case typespecs_abstract_code(module) do
{:ok, abstract_code} ->
exported_types = for {:attribute, _, :export_type, types} <- abstract_code, do: types
exported_types = List.flatten(exported_types)
types =
for {:attribute, _, kind, {name, _, args} = type} <- abstract_code,
kind in [:opaque, :type] do
cond do
kind == :opaque -> {:opaque, type}
{name, length(args)} in exported_types -> {:type, type}
true -> {:typep, type}
end
end
{:ok, types}
_ ->
:error
end
end
@doc """
Returns all specs available from the module's BEAM code.
The result is returned as a list of tuples where the first
element is spec name and arity and the second is the spec.
The module must have a corresponding BEAM file which can be
located by the runtime system. The types will be in the Erlang
Abstract Format.
"""
@spec fetch_specs(module) :: {:ok, [tuple]} | :error
def fetch_specs(module) when is_atom(module) or is_binary(module) do
case typespecs_abstract_code(module) do
{:ok, abstract_code} ->
{:ok, for({:attribute, _, :spec, value} <- abstract_code, do: value)}
:error ->
:error
end
end
@doc """
Returns all callbacks available from the module's BEAM code.
The result is returned as a list of tuples where the first
element is spec name and arity and the second is the spec.
The module must have a corresponding BEAM file
which can be located by the runtime system. The types will be
in the Erlang Abstract Format.
"""
@spec fetch_callbacks(module) :: {:ok, [tuple]} | :error
def fetch_callbacks(module) when is_atom(module) or is_binary(module) do
case typespecs_abstract_code(module) do
{:ok, abstract_code} ->
{:ok, for({:attribute, _, :callback, value} <- abstract_code, do: value)}
:error ->
:error
end
end
defp typespecs_abstract_code(module) do
with {module, binary} <- get_module_and_beam(module),
{:ok, {_, [debug_info: {:debug_info_v1, backend, data}]}} <-
:beam_lib.chunks(binary, [:debug_info]) do
case data do
{:elixir_v1, %{}, specs} ->
# Fast path to avoid translation to Erlang from Elixir.
{:ok, specs}
_ ->
case backend.debug_info(:erlang_v1, module, data, []) do
{:ok, abstract_code} -> {:ok, abstract_code}
_ -> :error
end
end
else
_ -> :error
end
end
defp get_module_and_beam(module) when is_atom(module) do
case :code.get_object_code(module) do
{^module, beam, _filename} -> {module, beam}
:error -> :error
end
end
defp get_module_and_beam(beam) when is_binary(beam) do
case :beam_lib.info(beam) do
[_ | _] = info -> {info[:module], beam}
_ -> :error
end
end
## To AST conversion
defp collect_vars({:ann_type, _line, args}) when is_list(args) do
[]
end
defp collect_vars({:type, _line, _kind, args}) when is_list(args) do
Enum.flat_map(args, &collect_vars/1)
end
defp collect_vars({:remote_type, _line, args}) when is_list(args) do
Enum.flat_map(args, &collect_vars/1)
end
defp collect_vars({:typed_record_field, _line, type}) do
collect_vars(type)
end
defp collect_vars({:paren_type, _line, [type]}) do
collect_vars(type)
end
defp collect_vars({:var, _line, var}) do
[erl_to_ex_var(var)]
end
defp collect_vars(_) do
[]
end
defp typespec_to_quoted({:user_type, line, name, args}) do
typespec_to_quoted({:type, line, name, args})
end
defp typespec_to_quoted({:type, line, :tuple, :any}) do
{:tuple, [line: line], []}
end
defp typespec_to_quoted({:type, line, :tuple, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
{:{}, [line: line], args}
end
defp typespec_to_quoted({:type, _line, :list, [{:type, _, :union, unions} = arg]}) do
case unpack_typespec_kw(unions, []) do
{:ok, ast} -> ast
:error -> [typespec_to_quoted(arg)]
end
end
defp typespec_to_quoted({:type, line, :list, []}) do
{:list, [line: line], []}
end
defp typespec_to_quoted({:type, _line, :list, [arg]}) do
[typespec_to_quoted(arg)]
end
defp typespec_to_quoted({:type, line, :nonempty_list, []}) do
[{:..., [line: line], nil}]
end
defp typespec_to_quoted({:type, line, :nonempty_list, [arg]}) do
[typespec_to_quoted(arg), {:..., [line: line], nil}]
end
defp typespec_to_quoted({:type, line, :map, :any}) do
{:map, [line: line], []}
end
defp typespec_to_quoted({:type, line, :map, fields}) do
fields =
Enum.map(fields, fn
{:type, _, :map_field_assoc, :any} ->
{{:optional, [], [{:any, [], []}]}, {:any, [], []}}
{:type, _, :map_field_exact, [{:atom, _, k}, v]} ->
{k, typespec_to_quoted(v)}
{:type, _, :map_field_exact, [k, v]} ->
{{:required, [], [typespec_to_quoted(k)]}, typespec_to_quoted(v)}
{:type, _, :map_field_assoc, [k, v]} ->
{{:optional, [], [typespec_to_quoted(k)]}, typespec_to_quoted(v)}
end)
{struct, fields} = Keyword.pop(fields, :__struct__)
map = {:%{}, [line: line], fields}
if struct do
{:%, [line: line], [struct, map]}
else
map
end
end
defp typespec_to_quoted({:type, line, :binary, [arg1, arg2]}) do
[arg1, arg2] = for arg <- [arg1, arg2], do: typespec_to_quoted(arg)
case {typespec_to_quoted(arg1), typespec_to_quoted(arg2)} do
{arg1, 0} ->
quote(line: line, do: <<_::unquote(arg1)>>)
{0, arg2} ->
quote(line: line, do: <<_::_*unquote(arg2)>>)
{arg1, arg2} ->
quote(line: line, do: <<_::unquote(arg1), _::_*unquote(arg2)>>)
end
end
defp typespec_to_quoted({:type, line, :union, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
Enum.reduce(Enum.reverse(args), fn arg, expr -> {:|, [line: line], [arg, expr]} end)
end
defp typespec_to_quoted({:type, line, :fun, [{:type, _, :product, args}, result]}) do
args = for arg <- args, do: typespec_to_quoted(arg)
[{:->, [line: line], [args, typespec_to_quoted(result)]}]
end
defp typespec_to_quoted({:type, line, :fun, [args, result]}) do
[{:->, [line: line], [[typespec_to_quoted(args)], typespec_to_quoted(result)]}]
end
defp typespec_to_quoted({:type, line, :fun, []}) do
typespec_to_quoted({:type, line, :fun, [{:type, line, :any}, {:type, line, :any, []}]})
end
defp typespec_to_quoted({:type, line, :range, [left, right]}) do
{:.., [line: line], [typespec_to_quoted(left), typespec_to_quoted(right)]}
end
defp typespec_to_quoted({:type, _line, nil, []}) do
[]
end
defp typespec_to_quoted({:type, line, name, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
{name, [line: line], args}
end
defp typespec_to_quoted({:var, line, var}) do
{erl_to_ex_var(var), line, nil}
end
defp typespec_to_quoted({:op, line, op, arg}) do
{op, [line: line], [typespec_to_quoted(arg)]}
end
defp typespec_to_quoted({:remote_type, line, [mod, name, args]}) do
remote_type(line, mod, name, args)
end
defp typespec_to_quoted({:ann_type, line, [var, type]}) do
{:::, [line: line], [typespec_to_quoted(var), typespec_to_quoted(type)]}
end
defp typespec_to_quoted(
{:typed_record_field, {:record_field, line, {:atom, line1, name}}, type}
) do
typespec_to_quoted({:ann_type, line, [{:var, line1, name}, type]})
end
defp typespec_to_quoted({:type, _, :any}) do
quote(do: ...)
end
defp typespec_to_quoted({:paren_type, _, [type]}) do
typespec_to_quoted(type)
end
defp typespec_to_quoted({type, _line, atom}) when is_atom(type) do
atom
end
defp typespec_to_quoted(other), do: other
## Helpers
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :charlist}, []) do
typespec_to_quoted({:type, line, :charlist, []})
end
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :nonempty_charlist}, []) do
typespec_to_quoted({:type, line, :nonempty_charlist, []})
end
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :struct}, []) do
typespec_to_quoted({:type, line, :struct, []})
end
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]) do
typespec_to_quoted({:type, line, :as_boolean, [arg]})
end
defp remote_type(line, {:atom, _, :elixir}, {:atom, _, :keyword}, args) do
typespec_to_quoted({:type, line, :keyword, args})
end
defp remote_type(line, mod, name, args) do
args = for arg <- args, do: typespec_to_quoted(arg)
dot = {:., [line: line], [typespec_to_quoted(mod), typespec_to_quoted(name)]}
{dot, [line: line], args}
end
defp erl_to_ex_var(var) do
case Atom.to_string(var) do
<<"_", c::utf8, rest::binary>> ->
String.to_atom("_#{String.downcase(<<c::utf8>>)}#{rest}")
<<c::utf8, rest::binary>> ->
String.to_atom("#{String.downcase(<<c::utf8>>)}#{rest}")
end
end
defp unpack_typespec_kw([{:type, _, :tuple, [{:atom, _, atom}, type]} | t], acc) do
unpack_typespec_kw(t, [{atom, typespec_to_quoted(type)} | acc])
end
defp unpack_typespec_kw([], acc) do
{:ok, Enum.reverse(acc)}
end
defp unpack_typespec_kw(_, _acc) do
:error
end
end
+1 -14
View File
@@ -41,7 +41,7 @@ defprotocol Collectable do
implementation for `MapSet`. In this implementation "collecting" elements
simply means inserting them in the set through `MapSet.put/2`.
defimpl Collectable, for: MapSet do
defimpl Collectable do
def into(original) do
collector_fun = fn
set, {:cont, elem} -> MapSet.put(set, elem)
@@ -79,16 +79,6 @@ end
defimpl Collectable, for: 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 " <>
"when collecting into non-empty lists. If you're collecting into a non-empty keyword " <>
"list, consider using Keyword.merge/2 instead. If you're collecting into a non-empty " <>
"list, consider concatenating the two lists with the ++ operator."
)
end
fun = fn
list, {:cont, x} -> [x | list]
list, :done -> original ++ :lists.reverse(list)
@@ -111,9 +101,6 @@ defimpl Collectable, for: BitString do
acc, {:cont, x} when is_bitstring(x) ->
<<IO.iodata_to_binary(acc)::bitstring, x::bitstring>>
acc, :done when is_bitstring(acc) ->
acc
acc, :done ->
IO.iodata_to_binary(acc)
+3 -54
View File
@@ -1,6 +1,6 @@
defmodule Dict do
@moduledoc ~S"""
Generic API for dictionaries.
WARNING: this module is deprecated.
If you need a general dictionary, use the `Map` module.
If you need to manipulate keyword lists, use `Keyword`.
@@ -9,26 +9,18 @@ defmodule Dict do
`new` function in the respective modules.
"""
@moduledoc deprecated: "Use Map or Keyword modules instead"
@type key :: any
@type value :: any
@type t :: list | map
message =
"Use the Map module for working with maps or the Keyword module for working with keyword lists"
# TODO: Remove by 2.0
# (hard-deprecated in elixir_dispatch)
@deprecated message
defmacro __using__(_) do
# Use this import to guarantee proper code expansion
import Kernel, except: [size: 1]
quote do
message = "Use maps and the Map module instead"
@deprecated message
def get(dict, key, default \\ nil) do
case fetch(dict, key) do
{:ok, value} -> value
@@ -36,7 +28,6 @@ defmodule Dict do
end
end
@deprecated message
def get_lazy(dict, key, fun) when is_function(fun, 0) do
case fetch(dict, key) do
{:ok, value} -> value
@@ -44,14 +35,12 @@ defmodule Dict do
end
end
@deprecated message
def get_and_update(dict, key, fun) do
current_value = get(dict, key)
{get, new_value} = fun.(current_value)
{get, put(dict, key, new_value)}
end
@deprecated message
def fetch!(dict, key) do
case fetch(dict, key) do
{:ok, value} -> value
@@ -59,12 +48,10 @@ defmodule Dict do
end
end
@deprecated message
def has_key?(dict, key) do
match?({:ok, _}, fetch(dict, key))
end
@deprecated message
def put_new(dict, key, value) do
case has_key?(dict, key) do
true -> dict
@@ -72,7 +59,6 @@ defmodule Dict do
end
end
@deprecated message
def put_new_lazy(dict, key, fun) when is_function(fun, 0) do
case has_key?(dict, key) do
true -> dict
@@ -80,12 +66,10 @@ defmodule Dict do
end
end
@deprecated message
def drop(dict, keys) do
Enum.reduce(keys, dict, &delete(&2, &1))
end
@deprecated message
def take(dict, keys) do
Enum.reduce(keys, new(), fn key, acc ->
case fetch(dict, key) do
@@ -95,28 +79,24 @@ defmodule Dict do
end)
end
@deprecated message
def to_list(dict) do
reduce(dict, {:cont, []}, fn kv, acc -> {:cont, [kv | acc]} end)
|> elem(1)
|> :lists.reverse()
end
@deprecated message
def keys(dict) do
reduce(dict, {:cont, []}, fn {k, _}, acc -> {:cont, [k | acc]} end)
|> elem(1)
|> :lists.reverse()
end
@deprecated message
def values(dict) do
reduce(dict, {:cont, []}, fn {_, v}, acc -> {:cont, [v | acc]} end)
|> elem(1)
|> :lists.reverse()
end
@deprecated message
def equal?(dict1, dict2) do
# Use this import to avoid conflicts in the user code
import Kernel, except: [size: 1]
@@ -136,7 +116,6 @@ defmodule Dict do
end
end
@deprecated message
def merge(dict1, dict2, fun \\ fn _k, _v1, v2 -> v2 end) do
# Use this import to avoid conflicts in the user code
import Kernel, except: [size: 1]
@@ -153,7 +132,6 @@ defmodule Dict do
|> elem(1)
end
@deprecated message
def update(dict, key, initial, fun) do
case fetch(dict, key) do
{:ok, value} ->
@@ -164,7 +142,6 @@ defmodule Dict do
end
end
@deprecated message
def update!(dict, key, fun) do
case fetch(dict, key) do
{:ok, value} ->
@@ -175,7 +152,6 @@ defmodule Dict do
end
end
@deprecated message
def pop(dict, key, default \\ nil) do
case fetch(dict, key) do
{:ok, value} ->
@@ -186,7 +162,6 @@ defmodule Dict do
end
end
@deprecated message
def pop_lazy(dict, key, fun) when is_function(fun, 0) do
case fetch(dict, key) do
{:ok, value} ->
@@ -197,7 +172,6 @@ defmodule Dict do
end
end
@deprecated message
def split(dict, keys) do
Enum.reduce(keys, {new(), dict}, fn key, {inc, exc} = acc ->
case fetch(exc, key) do
@@ -246,85 +220,71 @@ defmodule Dict do
end
end
@deprecated message
@spec keys(t) :: [key]
def keys(dict) do
target(dict).keys(dict)
end
@deprecated message
@spec values(t) :: [value]
def values(dict) do
target(dict).values(dict)
end
@deprecated message
@spec size(t) :: non_neg_integer
def size(dict) do
target(dict).size(dict)
end
@deprecated message
@spec has_key?(t, key) :: boolean
def has_key?(dict, key) do
target(dict).has_key?(dict, key)
end
@deprecated message
@spec get(t, key, value) :: value
def get(dict, key, default \\ nil) do
target(dict).get(dict, key, default)
end
@deprecated message
@spec get_lazy(t, key, (() -> value)) :: value
def get_lazy(dict, key, fun) do
target(dict).get_lazy(dict, key, fun)
end
@deprecated message
@spec get_and_update(t, key, (value -> {value, value})) :: {value, t}
def get_and_update(dict, key, fun) do
target(dict).get_and_update(dict, key, fun)
end
@deprecated message
@spec fetch(t, key) :: value
def fetch(dict, key) do
target(dict).fetch(dict, key)
end
@deprecated message
@spec fetch!(t, key) :: value
@spec fetch!(t, key) :: value | no_return
def fetch!(dict, key) do
target(dict).fetch!(dict, key)
end
@deprecated message
@spec put(t, key, value) :: t
def put(dict, key, val) do
target(dict).put(dict, key, val)
end
@deprecated message
@spec put_new(t, key, value) :: t
def put_new(dict, key, val) do
target(dict).put_new(dict, key, val)
end
@deprecated message
@spec put_new_lazy(t, key, (() -> value)) :: t
def put_new_lazy(dict, key, fun) do
target(dict).put_new_lazy(dict, key, fun)
end
@deprecated message
@spec delete(t, key) :: t
def delete(dict, key) do
target(dict).delete(dict, key)
end
@deprecated message
@spec merge(t, t) :: t
def merge(dict1, dict2) do
target1 = target(dict1)
@@ -337,7 +297,6 @@ defmodule Dict do
end
end
@deprecated message
@spec merge(t, t, (key, value, value -> value)) :: t
def merge(dict1, dict2, fun) do
target1 = target(dict1)
@@ -357,55 +316,46 @@ defmodule Dict do
|> elem(1)
end
@deprecated message
@spec pop(t, key, value) :: {value, t}
def pop(dict, key, default \\ nil) do
target(dict).pop(dict, key, default)
end
@deprecated message
@spec pop_lazy(t, key, (() -> value)) :: {value, t}
def pop_lazy(dict, key, fun) do
target(dict).pop_lazy(dict, key, fun)
end
@deprecated message
@spec update!(t, key, (value -> value)) :: t
def update!(dict, key, fun) do
target(dict).update!(dict, key, fun)
end
@deprecated message
@spec update(t, key, value, (value -> value)) :: t
def update(dict, key, initial, fun) do
target(dict).update(dict, key, initial, fun)
end
@deprecated message
@spec split(t, [key]) :: {t, t}
def split(dict, keys) do
target(dict).split(dict, keys)
end
@deprecated message
@spec drop(t, [key]) :: t
def drop(dict, keys) do
target(dict).drop(dict, keys)
end
@deprecated message
@spec take(t, [key]) :: t
def take(dict, keys) do
target(dict).take(dict, keys)
end
@deprecated message
@spec empty(t) :: t
def empty(dict) do
target(dict).empty(dict)
end
@deprecated message
@spec equal?(t, t) :: boolean
def equal?(dict1, dict2) do
target1 = target(dict1)
@@ -429,7 +379,6 @@ defmodule Dict do
end
end
@deprecated message
@spec to_list(t) :: list
def to_list(dict) do
target(dict).to_list(dict)
+58 -105
View File
@@ -6,7 +6,7 @@ defmodule DynamicSupervisor do
that are started in the given order when the supervisor starts. A
`DynamicSupervisor` starts with no children. Instead, children are
started on demand via `start_child/2`. When a dynamic supervisor
terminates, all children are shut down at the same time, with no guarantee
terminates, all children are shutdown at the same time, with no guarantee
of ordering.
## Examples
@@ -19,7 +19,7 @@ defmodule DynamicSupervisor do
{DynamicSupervisor, strategy: :one_for_one, name: MyApp.DynamicSupervisor}
]
Supervisor.start_link(children, strategy: :one_for_one)
Supervisor.start_link(strategy: :one_for_one)
The options given in the child specification are documented in `start_link/1`.
@@ -35,7 +35,7 @@ defmodule DynamicSupervisor do
Agent.get(agent2, & &1)
#=> %{}
DynamicSupervisor.count_children(MyApp.DynamicSupervisor)
DynamicSupervisor.count_children(sup)
#=> %{active: 2, specs: 2, supervisors: 0, workers: 2}
## Module-based supervisors
@@ -51,16 +51,13 @@ defmodule DynamicSupervisor do
DynamicSupervisor.start_link(__MODULE__, arg, name: __MODULE__)
end
@impl true
def init(_arg) do
DynamicSupervisor.init(strategy: :one_for_one)
end
end
See the `Supervisor` docs for a discussion of when you may want to use
module-based supervisors. The `@doc` annotation immediately preceding
`use DymamicSupervisor` will be attached to the generated `child_spec/1`
function.
module-based supervisors.
## Name registration
@@ -87,11 +84,10 @@ defmodule DynamicSupervisor do
Supervisor.start_child(__MODULE__, [foo, bar, baz])
end
@impl true
def init(initial_arg) do
children = [
# Or the deprecated: worker(MyWorker, [initial_arg])
%{id: MyWorker, start: {MyWorker, :start_link, [initial_arg]}}
%{id: MyWorker, start: {MyWorker, :start_link, [initial_arg]})
]
Supervisor.init(children, strategy: :simple_one_for_one)
@@ -114,7 +110,6 @@ defmodule DynamicSupervisor do
DynamicSupervisor.start_child(__MODULE__, spec)
end
@impl true
def init(initial_arg) do
DynamicSupervisor.init(
strategy: :one_for_one,
@@ -137,16 +132,15 @@ defmodule DynamicSupervisor do
Developers typically invoke `DynamicSupervisor.init/1` at the end of
their init callback to return the proper supervision flags.
"""
@callback init(init_arg :: term) :: {:ok, sup_flags()} | :ignore
@callback init(args :: term) :: {:ok, sup_flags()} | :ignore
@typedoc "The supervisor flags returned on init"
@type sup_flags() :: %{
strategy: strategy(),
intensity: non_neg_integer(),
period: pos_integer(),
max_children: non_neg_integer() | :infinity,
extra_arguments: [term()]
}
@opaque sup_flags() :: %{
strategy: strategy(),
intensity: non_neg_integer(),
period: pos_integer(),
max_children: non_neg_integer() | :infinity,
extra_arguments: [term()]
}
@typedoc "Option values used by the `start*` functions"
@type option :: {:name, Supervisor.name()} | init_option()
@@ -165,14 +159,6 @@ defmodule DynamicSupervisor do
@typedoc "Supported strategies"
@type strategy :: :one_for_one
@typedoc "Return values of `start_child` functions"
@type on_start_child ::
{:ok, pid}
| {:ok, pid, info :: term}
| :ignore
| {:error, {:already_started, pid} | :max_children | term}
# In this struct, `args` refers to the arguments passed to init/1 (the `init_arg`).
defstruct [
:args,
:extra_arguments,
@@ -183,6 +169,7 @@ defmodule DynamicSupervisor do
:max_restarts,
:max_seconds,
children: %{},
dynamic: 0,
restarts: []
]
@@ -191,18 +178,11 @@ defmodule DynamicSupervisor do
See `Supervisor`.
"""
@doc since: "1.6.1"
def child_spec(opts) when is_list(opts) do
id =
case Keyword.get(opts, :name, DynamicSupervisor) do
name when is_atom(name) -> name
{:global, name} -> name
{:via, _module, name} -> name
end
@since "1.6.1"
def child_spec(arg) do
%{
id: id,
start: {DynamicSupervisor, :start_link, [opts]},
id: DynamicSupervisor,
start: {DynamicSupervisor, :start_link, [arg]},
type: :supervisor
}
end
@@ -211,14 +191,12 @@ defmodule DynamicSupervisor do
defmacro __using__(opts) do
quote location: :keep, bind_quoted: [opts: opts] do
@behaviour DynamicSupervisor
if Module.get_attribute(__MODULE__, :doc) == nil do
@doc """
Returns a specification to start this module under a supervisor.
See `Supervisor`.
"""
end
@doc """
Returns a specification to start this module under a supervisor.
See `Supervisor`.
"""
def child_spec(arg) do
default = %{
id: __MODULE__,
@@ -230,6 +208,9 @@ defmodule DynamicSupervisor do
end
defoverridable child_spec: 1
@doc false
def init(arg)
end
end
@@ -254,7 +235,6 @@ defmodule DynamicSupervisor do
process and exits not only on crashes but also if the parent process exits
with `:normal` reason.
"""
@doc since: "1.6.0"
@spec start_link(options) :: Supervisor.on_start()
def start_link(options) when is_list(options) do
keys = [:extra_arguments, :max_children, :max_seconds, :max_restarts, :strategy]
@@ -280,18 +260,16 @@ defmodule DynamicSupervisor do
name, the supported values are described in the "Name registration"
section in the `GenServer` module docs.
"""
@doc since: "1.6.0"
@spec start_link(module, term, GenServer.options()) :: Supervisor.on_start()
def start_link(mod, init_arg, opts \\ []) do
GenServer.start_link(__MODULE__, {mod, init_arg, opts[:name]}, opts)
def start_link(mod, args, opts \\ []) do
GenServer.start_link(__MODULE__, {mod, args, opts[:name]}, opts)
end
@doc """
Dynamically adds a child specification to `supervisor` and starts that child.
`child_spec` should be a valid child specification as detailed in the
"child_spec/1" section of the documentation for `Supervisor`. The child
process will be started as defined in the child specification.
`child_spec` should be a valid child specification. The child process will
be started as defined in the child specification.
If the child process start function returns `{:ok, child}` or `{:ok, child,
info}`, then child specification and PID are added to the supervisor and
@@ -309,9 +287,8 @@ defmodule DynamicSupervisor do
of `:max_children` set on the supervisor initialization (see `init/1`), then
this function returns `{:error, :max_children}`.
"""
@doc since: "1.6.0"
@spec start_child(Supervisor.supervisor(), Supervisor.child_spec() | {module, term} | module) ::
on_start_child()
@spec start_child(Supervisor.supervisor(), :supervisor.child_spec() | {module, term} | module) ::
Supervisor.on_start_child()
def start_child(supervisor, {_, _, _, _, _, _} = child_spec) do
validate_and_start_child(supervisor, child_spec)
end
@@ -383,12 +360,11 @@ defmodule DynamicSupervisor do
end
@doc """
Terminates the given child identified by `pid`.
Terminates the given child identified by child id.
If successful, this function returns `:ok`. If there is no process with
the given PID, this function returns `{:error, :not_found}`.
"""
@doc since: "1.6.0"
@spec terminate_child(Supervisor.supervisor(), pid) :: :ok | {:error, :not_found}
def terminate_child(supervisor, pid) when is_pid(pid) do
call(supervisor, {:terminate_child, pid})
@@ -414,7 +390,6 @@ defmodule DynamicSupervisor do
* `modules` - as defined in the child specification
"""
@doc since: "1.6.0"
@spec which_children(Supervisor.supervisor()) :: [
{:undefined, pid | :restarting, :worker | :supervisor, :supervisor.modules()}
]
@@ -427,7 +402,7 @@ defmodule DynamicSupervisor do
The map contains the following keys:
* `:specs` - the number of children processes
* `:specs` - always 1 as dynamic supervisors have a single specification
* `:active` - the count of all actively running child processes managed by
this supervisor
@@ -439,7 +414,6 @@ defmodule DynamicSupervisor do
is still alive
"""
@doc since: "1.6.0"
@spec count_children(Supervisor.supervisor()) :: %{
specs: non_neg_integer,
active: non_neg_integer,
@@ -451,29 +425,13 @@ defmodule DynamicSupervisor do
end
@doc """
Synchronously stops the given supervisor with the given `reason`.
It returns `:ok` if the supervisor terminates with the given
reason. If it terminates with another reason, the call exits.
This function keeps OTP semantics regarding error reporting.
If the reason is any other than `:normal`, `:shutdown` or
`{:shutdown, _}`, an error report is logged.
"""
@doc since: "1.7.0"
@spec stop(Supervisor.supervisor(), reason :: term, timeout) :: :ok
def stop(supervisor, reason \\ :normal, timeout \\ :infinity) do
GenServer.stop(supervisor, reason, timeout)
end
@doc """
Receives a set of `options` that initializes a dynamic supervisor.
Receives a set of options that initializes a dynamic supervisor.
This is typically invoked at the end of the `c:init/1` callback of
module-based supervisors. See the "Module-based supervisors" section
module-based supervisors. See the sections "Module-based supervisors"
in the module documentation for more information.
The `options` received by this function are also supported by `start_link/2`.
The options received by this function are also supported by `start_link/2`.
This function returns a tuple containing the supervisor options.
@@ -498,7 +456,7 @@ defmodule DynamicSupervisor do
* `:max_children` - the maximum amount of children to be running
under this supervisor at the same time. When `:max_children` is
exceeded, `start_child/2` returns `{:error, :max_children}`. Defaults
exceeded, `start_child/2` returns `{:error, :dynamic}`. Defaults
to `:infinity`.
* `:extra_arguments` - arguments that are prepended to the arguments
@@ -506,8 +464,7 @@ defmodule DynamicSupervisor do
an empty list.
"""
@doc since: "1.6.0"
@spec init([init_option]) :: {:ok, sup_flags()}
@spec init([init_option]) :: {:ok, map()}
def init(options) when is_list(options) do
unless strategy = options[:strategy] do
raise ArgumentError, "expected :strategy option to be given"
@@ -532,20 +489,13 @@ defmodule DynamicSupervisor do
## Callbacks
@impl true
def init({mod, init_arg, name}) do
def init({mod, args, name}) do
Process.put(:"$initial_call", {:supervisor, mod, 1})
Process.flag(:trap_exit, true)
case mod.init(init_arg) do
case mod.init(args) do
{:ok, flags} when is_map(flags) ->
name =
cond do
is_nil(name) -> {self(), mod}
is_atom(name) -> {:local, name}
is_tuple(name) -> name
end
state = %DynamicSupervisor{mod: mod, args: init_arg, name: name}
state = %DynamicSupervisor{mod: mod, args: args, name: name || {self(), mod}}
case init(state, flags) do
{:ok, state} -> {:ok, state}
@@ -661,10 +611,10 @@ defmodule DynamicSupervisor do
end
def handle_call({:start_child, child}, _from, state) do
%{children: children, max_children: max_children} = state
%{dynamic: dynamic, max_children: max_children} = state
if map_size(children) < max_children do
handle_start_child(child, state)
if dynamic < max_children do
handle_start_child(child, %{state | dynamic: dynamic + 1})
else
{:reply, {:error, :max_children}, state}
end
@@ -681,7 +631,7 @@ defmodule DynamicSupervisor do
{:reply, reply, save_child(pid, mfa, restart, shutdown, type, modules, state)}
_ ->
{:reply, reply, state}
{:reply, reply, update_in(state.dynamic, &(&1 - 1))}
end
end
@@ -690,7 +640,7 @@ defmodule DynamicSupervisor do
apply(m, f, a)
catch
kind, reason ->
{:error, exit_reason(kind, reason, __STACKTRACE__)}
{:error, exit_reason(kind, reason, System.stacktrace())}
else
{:ok, pid, extra} when is_pid(pid) -> {:ok, pid, extra}
{:ok, pid} when is_pid(pid) -> {:ok, pid}
@@ -700,13 +650,13 @@ defmodule DynamicSupervisor do
end
end
defp save_child(pid, mfa, restart, shutdown, type, modules, state) do
mfa = mfa_for_restart(mfa, restart)
put_in(state.children[pid], {mfa, restart, shutdown, type, modules})
defp save_child(pid, {m, f, _}, :temporary, shutdown, type, modules, state) do
put_in(state.children[pid], {{m, f, :undefined}, :temporary, shutdown, type, modules})
end
defp mfa_for_restart({m, f, _}, :temporary), do: {m, f, :undefined}
defp mfa_for_restart(mfa, _), do: mfa
defp save_child(pid, mfa, restart, shutdown, type, modules, state) do
put_in(state.children[pid], {mfa, restart, shutdown, type, modules})
end
defp exit_reason(:exit, reason, _), do: reason
defp exit_reason(:error, reason, stack), do: {reason, stack}
@@ -750,8 +700,8 @@ defmodule DynamicSupervisor do
end
@impl true
def code_change(_, %{mod: mod, args: init_arg} = state, _) do
case mod.init(init_arg) do
def code_change(_, %{mod: mod, args: args} = state, _) do
case mod.init(args) do
{:ok, flags} when is_map(flags) ->
case init(state, flags) do
{:ok, state} -> {:ok, state}
@@ -924,8 +874,9 @@ defmodule DynamicSupervisor do
{:ok, delete_child(pid, state)}
end
defp delete_child(pid, %{children: children} = state) do
%{state | children: Map.delete(children, pid)}
defp delete_child(pid, state) do
%{children: children, dynamic: dynamic} = state
%{state | children: Map.delete(children, pid), dynamic: dynamic - 1}
end
defp restart_child(pid, child, state) do
@@ -949,6 +900,8 @@ defmodule DynamicSupervisor do
defp add_restart(state) do
%{max_seconds: max_seconds, max_restarts: max_restarts, restarts: restarts} = state
# The below is equivalent to 1 second. We avoid
# :second because of incompatibilties with OTP < 20
now = :erlang.monotonic_time(1)
restarts = add_restart([now | restarts], now, max_seconds)
state = %{state | restarts: restarts}
+232 -298
View File
File diff suppressed because it is too large Load Diff
+121 -286
View File
@@ -2,8 +2,10 @@ defmodule Exception do
@moduledoc """
Functions to format throw/catch/exit and exceptions.
Note that stacktraces in Elixir are only available inside
catch and rescue by using the `__STACKTRACE__/0` variable.
Note that stacktraces in Elixir are updated on throw,
errors and exits. For example, at any given moment,
`System.stacktrace/0` will return the stacktrace for the
last throw/error/exit that occurred in the current process.
Do not rely on the particular format returned by the `format*`
functions in this module. They may be changed in future releases
@@ -80,16 +82,31 @@ defmodule Exception do
normalizes only `:error`, returning the untouched payload
for others.
The third argument is the stacktrace which is used to enrich
a normalized error with more information. It is only used when
the kind is an error.
The third argument, a stacktrace, is optional. If it is
not supplied `System.stacktrace/0` will sometimes be used
to get additional information for the `kind` `:error`. If
the stacktrace is unknown and `System.stacktrace/0` would
not return the stacktrace corresponding to the exception
an empty stacktrace, `[]`, must be used.
"""
@spec normalize(:error, any, stacktrace) :: t
@spec normalize(non_error_kind, payload, stacktrace) :: payload when payload: var
def normalize(kind, payload, stacktrace \\ [])
def normalize(:error, %_{__exception__: true} = payload, _stacktrace), do: payload
def normalize(:error, payload, stacktrace), do: ErlangError.normalize(payload, stacktrace)
def normalize(_kind, payload, _stacktrace), do: payload
# Generating a stacktrace is expensive, default to nil
# to only fetch it when needed.
def normalize(kind, payload, stacktrace \\ nil)
def normalize(:error, exception, stacktrace) do
if exception?(exception) do
exception
else
ErlangError.normalize(exception, stacktrace)
end
end
def normalize(_kind, payload, _stacktrace) do
payload
end
@doc """
Normalizes and formats any throw/error/exit.
@@ -97,12 +114,15 @@ defmodule Exception do
The message is formatted and displayed in the same
format as used by Elixir's CLI.
The third argument is the stacktrace which is used to enrich
a normalized error with more information. It is only used when
the kind is an error.
The third argument, a stacktrace, is optional. If it is
not supplied `System.stacktrace/0` will sometimes be used
to get additional information for the `kind` `:error`. If
the stacktrace is unknown and `System.stacktrace/0` would
not return the stacktrace corresponding to the exception
an empty stacktrace, `[]`, must be used.
"""
@spec format_banner(kind, any, stacktrace) :: String.t()
def format_banner(kind, exception, stacktrace \\ [])
@spec format_banner(kind, any, stacktrace | nil) :: String.t()
def format_banner(kind, exception, stacktrace \\ nil)
def format_banner(:error, exception, stacktrace) do
exception = normalize(:error, exception, stacktrace)
@@ -127,17 +147,18 @@ defmodule Exception do
It relies on `format_banner/3` and `format_stacktrace/1`
to generate the final format.
If `kind` is `{:EXIT, pid}`, it does not generate a stacktrace,
as such exits are retrieved as messages without stacktraces.
Note that `{:EXIT, pid}` do not generate a stacktrace though
(as they are retrieved as messages without stacktraces).
"""
@spec format(kind, any, stacktrace) :: String.t()
def format(kind, payload, stacktrace \\ [])
@spec format(kind, any, stacktrace | nil) :: String.t()
def format(kind, payload, stacktrace \\ nil)
def format({:EXIT, _} = kind, any, _) do
format_banner(kind, any)
end
def format(kind, payload, stacktrace) do
stacktrace = stacktrace || System.stacktrace()
message = format_banner(kind, payload, stacktrace)
case stacktrace do
@@ -150,13 +171,12 @@ defmodule Exception do
Attaches information to exceptions for extra debugging.
This operation is potentially expensive, as it reads data
from the file system, parses beam files, evaluates code and
from the filesystem, parse beam files, evaluates code and
so on.
If the exception module implements the optional `c:blame/2`
callback, it will be invoked to perform the computation.
callbak, it will be invoked to perform the computation.
"""
@doc since: "1.5.0"
@spec blame(:error, any, stacktrace) :: {t, stacktrace}
@spec blame(non_error_kind, payload, stacktrace) :: {payload, stacktrace} when payload: var
def blame(kind, error, stacktrace)
@@ -186,8 +206,9 @@ defmodule Exception do
This function returns either `{:ok, definition, clauses}` or `:error`.
Where `definition` is `:def`, `:defp`, `:defmacro` or `:defmacrop`.
Note this functionality requires Erlang/OTP 20, otherwise `:error`
is always returned.
"""
@doc since: "1.5.0"
@spec blame_mfa(module, function, args :: [term]) ::
{:ok, :def | :defp | :defmacro | :defmacrop, [{args :: [term], guards :: [term]}]}
| :error
@@ -208,7 +229,7 @@ defmodule Exception do
{_, kind, _, clauses} <- List.keyfind(defs, {function, arity}, 0) do
clauses =
for {meta, ex_args, guards, _block} <- clauses do
scope = :elixir_erl.scope(meta)
scope = :elixir_erl.definition_scope(meta, "nofile")
{erl_args, scope} =
:elixir_erl_clauses.match(&:elixir_erl_pass.translate_args/2, ex_args, scope)
@@ -367,8 +388,8 @@ defmodule Exception do
format_exit_reason(reason)
else
mfa ->
# Assume tuple formattable as an mfa is an mfa,
# so exit was caused by failed mfa.
# Assume tuple formattable as an mfa is an mfa, so exit was caused by
# failed mfa.
"exited in: " <>
mfa <> joiner <> "** (EXIT) " <> format_exit(reason2, joiner <> <<" ">>)
end
@@ -592,13 +613,13 @@ defmodule Exception do
## Examples
iex> Exception.format_mfa(Foo, :bar, 1)
iex> Exception.format_mfa Foo, :bar, 1
"Foo.bar/1"
iex> Exception.format_mfa(Foo, :bar, [])
iex> Exception.format_mfa Foo, :bar, []
"Foo.bar()"
iex> Exception.format_mfa(nil, :bar, [])
iex> Exception.format_mfa nil, :bar, []
"nil.bar()"
Anonymous functions are reported as -func/arity-anonfn-count-,
@@ -671,86 +692,15 @@ end
defmodule ArgumentError do
defexception message: "argument error"
@impl true
def blame(
%{message: "argument error"} = exception,
[{:erlang, :apply, [module, function, args], _} | _] = stacktrace
) do
message =
cond do
# Note that args may be an empty list even if they were supplied
not is_atom(module) and is_atom(function) and args == [] ->
"you attempted to apply #{inspect(function)} on #{inspect(module)}. " <>
"If you are using apply/3, make sure the module is an atom. " <>
"If you are using the dot syntax, such as map.field or module.function, " <>
"make sure the left side of the dot is an atom or a map"
not is_atom(module) ->
"you attempted to apply a function on #{inspect(module)}. " <>
"Modules (the first argument of apply) must always be an atom"
not is_atom(function) ->
"you attempted to apply #{inspect(function)} on module #{inspect(module)}. " <>
"Functions (the second argument of apply) must always be an atom"
not is_list(args) ->
"you attempted to apply #{inspect(function)} on module #{inspect(module)} " <>
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a list"
end
{%{exception | message: message}, stacktrace}
end
def blame(exception, stacktrace) do
{exception, stacktrace}
end
end
defmodule ArithmeticError do
defexception message: "bad argument in arithmetic expression"
@unary_ops [:+, :-]
@binary_ops [:+, :-, :*, :/]
@binary_funs [:div, :rem]
@bitwise_binary_funs [:band, :bor, :bxor, :bsl, :bsr]
@impl true
def blame(%{message: message} = exception, [{:erlang, fun, args, _} | _] = stacktrace) do
message =
message <>
case {fun, args} do
{op, [a]} when op in @unary_ops ->
": #{op}(#{inspect(a)})"
{op, [a, b]} when op in @binary_ops ->
": #{inspect(a)} #{op} #{inspect(b)}"
{fun, [a, b]} when fun in @binary_funs ->
": #{fun}(#{inspect(a)}, #{inspect(b)})"
{fun, [a, b]} when fun in @bitwise_binary_funs ->
": Bitwise.#{fun}(#{inspect(a)}, #{inspect(b)})"
{:bnot, [a]} ->
": Bitwise.bnot(#{inspect(a)})"
_ ->
""
end
{%{exception | message: message}, stacktrace}
end
def blame(exception, stacktrace) do
{exception, stacktrace}
end
end
defmodule SystemLimitError do
defexception []
@impl true
def message(_) do
"a system limit has been reached"
end
@@ -759,7 +709,6 @@ end
defmodule SyntaxError do
defexception [:file, :line, description: "syntax error"]
@impl true
def message(exception) do
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
" " <> exception.description
@@ -769,7 +718,6 @@ end
defmodule TokenMissingError do
defexception [:file, :line, description: "expression is incomplete"]
@impl true
def message(%{file: file, line: line, description: description}) do
Exception.format_file_line(file && Path.relative_to_cwd(file), line) <> " " <> description
end
@@ -778,7 +726,6 @@ end
defmodule CompileError do
defexception [:file, :line, description: "compile error"]
@impl true
def message(%{file: file, line: line, description: description}) do
Exception.format_file_line(file && Path.relative_to_cwd(file), line) <> " " <> description
end
@@ -787,11 +734,6 @@ end
defmodule BadFunctionError do
defexception [:term]
@impl true
def message(%{term: term}) when is_function(term) do
"function #{inspect(term)} is invalid, likely because it points to an old version of the code"
end
def message(exception) do
"expected a function, got: #{inspect(exception.term)}"
end
@@ -800,7 +742,6 @@ end
defmodule BadStructError do
defexception [:struct, :term]
@impl true
def message(exception) do
"expected a struct named #{inspect(exception.struct)}, got: #{inspect(exception.term)}"
end
@@ -809,7 +750,6 @@ end
defmodule BadMapError do
defexception [:term]
@impl true
def message(exception) do
"expected a map, got: #{inspect(exception.term)}"
end
@@ -818,7 +758,6 @@ end
defmodule BadBooleanError do
defexception [:term, :operator]
@impl true
def message(exception) do
"expected a boolean on left-side of \"#{exception.operator}\", got: #{inspect(exception.term)}"
end
@@ -827,7 +766,6 @@ end
defmodule MatchError do
defexception [:term]
@impl true
def message(exception) do
"no match of right hand side value: #{inspect(exception.term)}"
end
@@ -836,7 +774,6 @@ end
defmodule CaseClauseError do
defexception [:term]
@impl true
def message(exception) do
"no case clause matching: #{inspect(exception.term)}"
end
@@ -845,7 +782,6 @@ end
defmodule WithClauseError do
defexception [:term]
@impl true
def message(exception) do
"no with clause matching: #{inspect(exception.term)}"
end
@@ -854,16 +790,14 @@ end
defmodule CondClauseError do
defexception []
@impl true
def message(_exception) do
"no cond clause evaluated to a truthy value"
"no cond clause evaluated to a true value"
end
end
defmodule TryClauseError do
defexception [:term]
@impl true
def message(exception) do
"no try clause matching: #{inspect(exception.term)}"
end
@@ -872,12 +806,11 @@ end
defmodule BadArityError do
defexception [:function, :args]
@impl true
def message(exception) do
fun = exception.function
args = exception.args
insp = Enum.map_join(args, ", ", &inspect/1)
{:arity, arity} = Function.info(fun, :arity)
{:arity, arity} = :erlang.fun_info(fun, :arity)
"#{inspect(fun)} with arity #{arity} called with #{count(length(args), insp)}"
end
@@ -887,81 +820,73 @@ defmodule BadArityError do
end
defmodule UndefinedFunctionError do
defexception [:module, :function, :arity, :reason, :message]
defexception [:module, :function, :arity, :reason, :exports]
@impl true
def message(%{message: nil} = exception) do
%{reason: reason, module: module, function: function, arity: arity} = exception
{message, _loaded?} = message(reason, module, function, arity)
message
end
def message(%{message: message}) do
message
end
defp message(nil, module, function, arity) do
def message(%{reason: nil, module: module, function: function, arity: arity} = e) do
cond do
is_nil(function) or is_nil(arity) ->
{"undefined function", false}
"undefined function"
is_nil(module) ->
formatted_fun = Exception.format_mfa(module, function, arity)
{"function #{formatted_fun} is undefined", false}
function_exported?(module, :module_info, 0) ->
message(:"function not exported", module, function, arity)
not is_nil(module) and :code.is_loaded(module) == false ->
message(%{e | reason: :"module could not be loaded"})
true ->
message(:"module could not be loaded", module, function, arity)
message(%{e | reason: :"function not exported"})
end
end
defp message(:"module could not be loaded", module, function, arity) do
def message(%{
reason: :"module could not be loaded",
module: module,
function: function,
arity: arity
}) do
formatted_fun = Exception.format_mfa(module, function, arity)
{"function #{formatted_fun} is undefined (module #{inspect(module)} is not available)", false}
"function #{formatted_fun} is undefined (module #{inspect(module)} is not available)"
end
defp message(:"function not exported", module, function, arity) do
formatted_fun = Exception.format_mfa(module, function, arity)
{"function #{formatted_fun} is undefined or private", true}
def message(%{
reason: :"function not exported",
module: module,
function: function,
arity: arity
}) do
IO.iodata_to_binary(function_not_exported(module, function, arity, nil))
end
defp message(reason, module, function, arity) do
formatted_fun = Exception.format_mfa(module, function, arity)
{"function #{formatted_fun} is undefined (#{reason})", false}
def message(%{
reason: :"function not available",
module: module,
function: function,
arity: arity
}) do
"nil." <> fa = Exception.format_mfa(nil, function, arity)
"function " <>
Exception.format_mfa(module, function, arity) <>
" is undefined (function #{fa} is not available)"
end
@impl true
def blame(exception, stacktrace) do
%{reason: reason, module: module, function: function, arity: arity} = exception
{message, loaded?} = message(reason, module, function, arity)
message = message <> hint(module, function, arity, loaded?)
{%{exception | message: message}, stacktrace}
end
defp hint(nil, _function, 0, _loaded?) do
". If you are using the dot syntax, such as map.field or module.function, " <>
"make sure the left side of the dot is an atom or a map"
end
defp hint(module, function, arity, true) do
behaviour_hint(module, function, arity) <>
hint_for_loaded_module(module, function, arity, nil)
end
defp hint(_module, _function, _arity, _loaded?) do
""
def message(%{reason: reason, module: module, function: function, arity: arity}) do
"function " <> Exception.format_mfa(module, function, arity) <> " is undefined (#{reason})"
end
@doc false
def hint_for_loaded_module(module, function, arity, exports) do
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
def function_not_exported(module, function, arity, exports) do
suffix =
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
did_you_mean(module, function, exports)
end
[
"function ",
Exception.format_mfa(module, function, arity),
" is undefined or private",
suffix
]
end
@function_threshold 0.77
@@ -973,10 +898,9 @@ defmodule UndefinedFunctionError do
result =
case Keyword.take(exports, [function]) do
[] ->
candidates = exports -- deprecated_functions_for(module)
base = Atom.to_string(function)
for {key, val} <- candidates,
for {key, val} <- exports,
dist = String.jaro_distance(base, Atom.to_string(key)),
dist >= @function_threshold,
do: {dist, key, val}
@@ -998,33 +922,6 @@ defmodule UndefinedFunctionError do
[" * ", Code.Identifier.inspect_as_function(fun), ?/, Integer.to_string(arity), ?\n]
end
defp behaviour_hint(module, function, arity) do
case behaviours_for(module) do
[] ->
""
behaviours ->
case Enum.find(behaviours, &expects_callback?(&1, function, arity)) do
nil -> ""
behaviour -> ", but the behaviour #{inspect(behaviour)} expects it to be present"
end
end
rescue
# In case the module was removed while we are computing this
UndefinedFunctionError -> ""
end
defp behaviours_for(module) do
:attributes
|> module.module_info()
|> Keyword.get(:behaviour, [])
end
defp expects_callback?(behaviour, function, arity) do
callbacks = behaviour.behaviour_info(:callbacks)
Enum.member?(callbacks, {function, arity})
end
defp exports_for(module) do
if function_exported?(module, :__info__, 1) do
module.__info__(:macros) ++ module.__info__(:functions)
@@ -1036,24 +933,11 @@ defmodule UndefinedFunctionError do
UndefinedFunctionError ->
[]
end
defp deprecated_functions_for(module) do
if function_exported?(module, :__info__, 1) do
for {name_arity, _message} <- module.__info__(:deprecated), do: name_arity
else
[]
end
rescue
# In case the module was removed while we are computing this
UndefinedFunctionError ->
[]
end
end
defmodule FunctionClauseError do
defexception [:module, :function, :arity, :kind, :args, :clauses]
@impl true
def message(exception) do
case exception do
%{function: nil} ->
@@ -1066,7 +950,6 @@ defmodule FunctionClauseError do
end
end
@impl true
def blame(%{module: module, function: function, arity: arity} = exception, stacktrace) do
case stacktrace do
[{^module, ^function, args, meta} | rest] when length(args) == arity ->
@@ -1146,7 +1029,6 @@ end
defmodule Protocol.UndefinedError do
defexception [:protocol, :value, description: ""]
@impl true
def message(%{protocol: protocol, value: value, description: description}) do
"protocol #{inspect(protocol)} not implemented for #{inspect(value)}" <>
maybe_description(description) <> maybe_available(protocol)
@@ -1170,72 +1052,17 @@ defmodule Protocol.UndefinedError do
end
defmodule KeyError do
defexception [:key, :term, :message]
defexception [:key, :term]
@impl true
def message(exception = %{message: nil}), do: message(exception.key, exception.term)
def message(%{message: message}), do: message
def message(exception) do
msg = "key #{inspect(exception.key)} not found"
defp message(key, term) do
message = "key #{inspect(key)} not found"
if term != nil do
message <> " in: #{inspect(term)}"
if exception.term != nil do
msg <> " in: #{inspect(exception.term)}"
else
message
msg
end
end
@impl true
def blame(exception = %{term: nil}, stacktrace) do
message = message(exception.key, exception.term)
{%{exception | message: message}, stacktrace}
end
def blame(exception, stacktrace) do
%{term: term, key: key} = exception
message = message(key, term)
if is_atom(key) and (map_with_atom_keys_only?(term) or Keyword.keyword?(term)) do
hint = did_you_mean(key, available_keys(term))
message = message <> IO.iodata_to_binary(hint)
{%{exception | message: message}, stacktrace}
else
{%{exception | message: message}, stacktrace}
end
end
defp map_with_atom_keys_only?(term) do
is_map(term) and Enum.all?(Map.to_list(term), fn {k, _} -> is_atom(k) end)
end
defp available_keys(term) when is_map(term), do: Map.keys(term)
defp available_keys(term) when is_list(term), do: Keyword.keys(term)
@threshold 0.77
@max_suggestions 5
defp did_you_mean(missing_key, available_keys) do
stringified_key = Atom.to_string(missing_key)
suggestions =
for key <- available_keys,
distance = String.jaro_distance(stringified_key, Atom.to_string(key)),
distance >= @threshold,
do: {distance, key}
case suggestions do
[] -> []
suggestions -> [". Did you mean one of:\n\n" | format_suggestions(suggestions)]
end
end
defp format_suggestions(suggestions) do
suggestions
|> Enum.sort(&(elem(&1, 0) >= elem(&2, 0)))
|> Enum.take(@max_suggestions)
|> Enum.sort(&(elem(&1, 1) <= elem(&2, 1)))
|> Enum.map(fn {_, key} -> [" * ", inspect(key), ?\n] end)
end
end
defmodule UnicodeConversionError do
@@ -1272,7 +1099,6 @@ end
defmodule File.Error do
defexception [:reason, :path, action: ""]
@impl true
def message(%{action: action, reason: reason, path: path}) do
formatted =
case {action, reason} do
@@ -1290,7 +1116,6 @@ end
defmodule File.CopyError do
defexception [:reason, :source, :destination, on: "", action: ""]
@impl true
def message(exception) do
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
@@ -1308,7 +1133,6 @@ end
defmodule File.LinkError do
defexception [:reason, :existing, :new, action: ""]
@impl true
def message(exception) do
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
@@ -1320,7 +1144,6 @@ end
defmodule ErlangError do
defexception [:original]
@impl true
def message(exception) do
"Erlang error: #{inspect(exception.original)}"
end
@@ -1368,12 +1191,11 @@ defmodule ErlangError do
def normalize({:badkey, key}, stacktrace) do
term =
case stacktrace do
case ensure_stacktrace(stacktrace) do
[{Map, :get_and_update!, [map, _, _], _} | _] -> map
[{Map, :update!, [map, _, _], _} | _] -> map
[{:maps, :update, [_, _, map], _} | _] -> map
[{:maps, :get, [_, map], _} | _] -> map
[{:erlang, :map_get, [_, map], _} | _] -> map
_ -> nil
end
@@ -1397,12 +1219,13 @@ defmodule ErlangError do
end
def normalize(:undef, stacktrace) do
stacktrace = ensure_stacktrace(stacktrace)
{mod, fun, arity} = from_stacktrace(stacktrace)
%UndefinedFunctionError{module: mod, function: fun, arity: arity}
end
def normalize(:function_clause, stacktrace) do
{mod, fun, arity} = from_stacktrace(stacktrace)
{mod, fun, arity} = from_stacktrace(ensure_stacktrace(stacktrace))
%FunctionClauseError{module: mod, function: fun, arity: arity}
end
@@ -1414,6 +1237,18 @@ defmodule ErlangError do
%ErlangError{original: other}
end
defp ensure_stacktrace(nil) do
try do
:erlang.get_stacktrace()
rescue
_ -> []
end
end
defp ensure_stacktrace(stacktrace) do
stacktrace
end
defp from_stacktrace([{module, function, args, _} | _]) when is_list(args) do
{module, function, length(args)}
end
+103 -196
View File
@@ -90,13 +90,8 @@ defmodule File do
| :read
| :read_ahead
| :sync
| :utf8
| :write
| {:read_ahead, pos_integer}
| {:delayed_write, non_neg_integer, non_neg_integer}
| encoding_mode()
@type encoding_mode ::
:utf8
| {
:encoding,
:latin1
@@ -107,42 +102,23 @@ defmodule File do
| {:utf16, :big | :little}
| {:utf32, :big | :little}
}
@type stream_mode ::
encoding_mode()
| :trim_bom
| {:read_ahead, pos_integer | false}
| {:read_ahead, pos_integer}
| {:delayed_write, non_neg_integer, non_neg_integer}
@type erlang_time ::
{{year :: non_neg_integer(), month :: 1..12, day :: 1..31},
{hour :: 0..23, minute :: 0..59, second :: 0..59}}
@type posix_time :: integer()
@doc """
Returns `true` if the path is a regular file.
This function follows symbolic links, so if a symbolic link points to a
regular file, `true` is returned.
## Options
The supported options are:
* `:raw` - a single atom to bypass the file server and only check
for the file locally
## Examples
File.regular?(__ENV__.file)
#=> true
File.regular? __ENV__.file #=> true
"""
@spec regular?(Path.t(), [regular_option]) :: boolean
when regular_option: :raw
def regular?(path, opts \\ []) do
:elixir_utils.read_file_type(IO.chardata_to_string(path), opts) == {:ok, :regular}
@spec regular?(Path.t()) :: boolean
def regular?(path) do
:elixir_utils.read_file_type(IO.chardata_to_string(path)) == {:ok, :regular}
end
@doc """
@@ -151,13 +127,6 @@ defmodule File do
This function follows symbolic links, so if a symbolic link points to a
directory, `true` is returned.
## Options
The supported options are:
* `:raw` - a single atom to bypass the file server and only check
for the file locally
## Examples
File.dir?("./test")
@@ -172,29 +141,21 @@ defmodule File do
File.dir?("~/Downloads")
#=> false
"~/Downloads" |> Path.expand() |> File.dir?()
"~/Downloads" |> Path.expand |> File.dir?
#=> true
"""
@spec dir?(Path.t(), [dir_option]) :: boolean
when dir_option: :raw
def dir?(path, opts \\ []) do
:elixir_utils.read_file_type(IO.chardata_to_string(path), opts) == {:ok, :directory}
@spec dir?(Path.t()) :: boolean
def dir?(path) do
:elixir_utils.read_file_type(IO.chardata_to_string(path)) == {:ok, :directory}
end
@doc """
Returns `true` if the given path exists.
It can be a regular file, directory, socket, symbolic link, named pipe, or device file.
It can be regular file, directory, socket, symbolic link, named pipe or device file.
Returns `false` for symbolic links pointing to non-existing targets.
## Options
The supported options are:
* `:raw` - a single atom to bypass the file server and only check
for the file locally
## Examples
File.exists?("test/")
@@ -207,11 +168,9 @@ defmodule File do
#=> true
"""
@spec exists?(Path.t(), [exists_option]) :: boolean
when exists_option: :raw
def exists?(path, opts \\ []) do
opts = [{:time, :posix}] ++ opts
match?({:ok, _}, :file.read_file_info(IO.chardata_to_string(path), opts))
@spec exists?(Path.t()) :: boolean
def exists?(path) do
match?({:ok, _}, :file.read_file_info(IO.chardata_to_string(path)))
end
@doc """
@@ -237,10 +196,9 @@ defmodule File do
end
@doc """
Same as `mkdir/1`, but raises a `File.Error` exception in case of failure.
Otherwise `:ok`.
Same as `mkdir/1`, but raises an exception in case of failure. Otherwise `:ok`.
"""
@spec mkdir!(Path.t()) :: :ok
@spec mkdir!(Path.t()) :: :ok | no_return
def mkdir!(path) do
case mkdir(path) do
:ok ->
@@ -301,10 +259,9 @@ defmodule File do
end
@doc """
Same as `mkdir_p/1`, but raises a `File.Error` exception in case of failure.
Otherwise `:ok`.
Same as `mkdir_p/1`, but raises an exception in case of failure. Otherwise `:ok`.
"""
@spec mkdir_p!(Path.t()) :: :ok
@spec mkdir_p!(Path.t()) :: :ok | no_return
def mkdir_p!(path) do
case mkdir_p(path) do
:ok ->
@@ -340,10 +297,10 @@ defmodule File do
end
@doc """
Returns a binary with the contents of the given filename,
or raises a `File.Error` exception if an error occurs.
Returns a binary with the contents of the given filename or raises
`File.Error` if an error occurs.
"""
@spec read!(Path.t()) :: binary
@spec read!(Path.t()) :: binary | no_return
def read!(path) do
case read(path) do
{:ok, binary} ->
@@ -388,10 +345,10 @@ defmodule File do
end
@doc """
Same as `stat/2` but returns the `File.Stat` directly,
or raises a `File.Error` exception if an error is returned.
Same as `stat/2` but returns the `File.Stat` directly, or
throws `File.Error` if an error is returned.
"""
@spec stat!(Path.t(), stat_options) :: File.Stat.t()
@spec stat!(Path.t(), stat_options) :: File.Stat.t() | no_return
def stat!(path, opts \\ []) do
case stat(path, opts) do
{:ok, info} ->
@@ -439,10 +396,10 @@ defmodule File do
end
@doc """
Same as `lstat/2` but returns the `File.Stat` struct directly,
or raises a `File.Error` exception if an error is returned.
Same as `lstat/2` but returns the `File.Stat` struct directly, or
throws `File.Error` if an error is returned.
"""
@spec lstat!(Path.t(), stat_options) :: File.Stat.t()
@spec lstat!(Path.t(), stat_options) :: File.Stat.t() | no_return
def lstat!(path, opts \\ []) do
case lstat(path, opts) do
{:ok, info} ->
@@ -471,7 +428,6 @@ defmodule File do
* `:enotsup` - symbolic links are not supported on the current platform
"""
@doc since: "1.5.0"
@spec read_link(Path.t()) :: {:ok, binary} | {:error, posix}
def read_link(path) do
case path |> IO.chardata_to_string() |> :file.read_link() do
@@ -481,11 +437,10 @@ defmodule File do
end
@doc """
Same as `read_link/1` but returns the target directly,
or raises a `File.Error` exception if an error is returned.
Same as `read_link/1` but returns the target directly or throws `File.Error` if an error is
returned.
"""
@doc since: "1.5.0"
@spec read_link!(Path.t()) :: binary
@spec read_link!(Path.t()) :: binary | no_return
def read_link!(path) do
case read_link(path) do
{:ok, resolved} ->
@@ -497,7 +452,7 @@ defmodule File do
end
@doc """
Writes the given `File.Stat` back to the file system at the given
Writes the given `File.Stat` back to the filesystem at the given
path. Returns `:ok` or `{:error, reason}`.
"""
@spec write_stat(Path.t(), File.Stat.t(), stat_options) :: :ok | {:error, posix}
@@ -507,10 +462,10 @@ defmodule File do
end
@doc """
Same as `write_stat/3` but raises a `File.Error` exception if it fails.
Same as `write_stat/3` but raises an exception if it fails.
Returns `:ok` otherwise.
"""
@spec write_stat!(Path.t(), File.Stat.t(), stat_options) :: :ok
@spec write_stat!(Path.t(), File.Stat.t(), stat_options) :: :ok | no_return
def write_stat!(path, stat, opts \\ []) do
case write_stat(path, stat, opts) do
:ok ->
@@ -528,60 +483,32 @@ defmodule File do
Updates modification time (mtime) and access time (atime) of
the given file.
The file is created if it doesn't exist. Requires datetime in UTC
(as returned by `:erlang.universaltime()`) or an integer
representing the POSIX timestamp (as returned by `System.os_time(:second)`).
## Examples
File.touch("/tmp/a.txt", {{2018, 1, 30}, {13, 59, 59}})
#=> :ok
File.touch("/fakedir/b.txt", {{2018, 1, 30}, {13, 59, 59}})
{:error, :enoent}
File.touch("/tmp/a.txt", 1544519753)
#=> :ok
The file is created if it doesn’t exist. Requires datetime in UTC.
"""
@spec touch(Path.t(), erlang_time() | posix_time()) :: :ok | {:error, posix}
def touch(path, time \\ System.os_time(:second))
def touch(path, time) when is_tuple(time) do
@spec touch(Path.t(), :calendar.datetime()) :: :ok | {:error, posix}
def touch(path, time \\ :calendar.universal_time()) do
path = IO.chardata_to_string(path)
with {:error, :enoent} <- :elixir_utils.change_universal_time(path, time),
:ok <- write(path, "", [:append]),
do: :elixir_utils.change_universal_time(path, time)
case :elixir_utils.change_universal_time(path, time) do
{:error, :enoent} -> touch_new(path, time)
other -> other
end
end
def touch(path, time) when is_integer(time) do
path = IO.chardata_to_string(path)
with {:error, :enoent} <- :elixir_utils.change_posix_time(path, time),
:ok <- write(path, "", [:append]),
do: :elixir_utils.change_posix_time(path, time)
defp touch_new(path, time) do
case write(path, "", [:append]) do
:ok -> :elixir_utils.change_universal_time(path, time)
{:error, _reason} = error -> error
end
end
@doc """
Same as `touch/2` but raises a `File.Error` exception if it fails.
Returns `:ok` otherwise.
The file is created if it doesn't exist. Requires datetime in UTC
(as returned by `:erlang.universaltime()`) or an integer
representing the POSIX timestamp (as returned by `System.os_time(:second)`).
## Examples
File.touch!("/tmp/a.txt", {{2018, 1, 30}, {13, 59, 59}})
#=> :ok
File.touch!("/fakedir/b.txt", {{2018, 1, 30}, {13, 59, 59}})
#=> ** (File.Error) could not touch "/fakedir/b.txt": no such file or directory
File.touch!("/tmp/a.txt", 1544519753)
Same as `touch/2` but raises an exception if it fails.
Returns `:ok` otherwise. Requires datetime in UTC.
"""
@spec touch!(Path.t(), erlang_time() | posix_time()) :: :ok
def touch!(path, time \\ System.os_time(:second)) do
@spec touch!(Path.t(), :calendar.datetime()) :: :ok | no_return
def touch!(path, time \\ :calendar.universal_time()) do
case touch(path, time) do
:ok ->
:ok
@@ -598,18 +525,15 @@ defmodule File do
If the operating system does not support hard links, returns
`{:error, :enotsup}`.
"""
@doc since: "1.5.0"
@spec ln(Path.t(), Path.t()) :: :ok | {:error, posix}
def ln(existing, new) do
:file.make_link(IO.chardata_to_string(existing), IO.chardata_to_string(new))
end
@doc """
Same as `ln/2` but raises a `File.LinkError` exception if it fails.
Returns `:ok` otherwise.
Same as `ln/2` but raises an exception if it fails.
Returns `:ok` otherwise
"""
@doc since: "1.5.0"
@spec ln!(Path.t(), Path.t()) :: :ok
def ln!(existing, new) do
case ln(existing, new) do
:ok ->
@@ -631,17 +555,15 @@ defmodule File do
If the operating system does not support symlinks, returns
`{:error, :enotsup}`.
"""
@doc since: "1.5.0"
@spec ln_s(Path.t(), Path.t()) :: :ok | {:error, posix}
def ln_s(existing, new) do
:file.make_symlink(IO.chardata_to_string(existing), IO.chardata_to_string(new))
end
@doc """
Same as `ln_s/2` but raises a `File.LinkError` exception if it fails.
Returns `:ok` otherwise.
Same as `ln_s/2` but raises an exception if it fails.
Returns `:ok` otherwise
"""
@spec ln_s!(Path.t(), Path.t()) :: :ok
def ln_s!(existing, new) do
case ln_s(existing, new) do
:ok ->
@@ -685,11 +607,11 @@ defmodule File do
end
@doc """
The same as `copy/3` but raises a `File.CopyError` exception if it fails.
The same as `copy/3` but raises an `File.CopyError` if it fails.
Returns the `bytes_copied` otherwise.
"""
@spec copy!(Path.t() | io_device, Path.t() | io_device, pos_integer | :infinity) ::
non_neg_integer
non_neg_integer | no_return
def copy!(source, destination, bytes_count \\ :infinity) do
case copy(source, destination, bytes_count) do
{:ok, bytes_count} ->
@@ -719,11 +641,10 @@ defmodule File do
## Examples
# Rename file "a.txt" to "b.txt"
File.rename("a.txt", "b.txt")
File.rename "a.txt", "b.txt"
# Rename directory "samples" to "tmp"
File.rename("samples", "tmp")
File.rename "samples", "tmp"
"""
@spec rename(Path.t(), Path.t()) :: :ok | {:error, posix}
def rename(source, destination) do
@@ -767,10 +688,10 @@ defmodule File do
end
@doc """
The same as `cp/3`, but raises a `File.CopyError` exception if it fails.
The same as `cp/3`, but raises `File.CopyError` if it fails.
Returns `:ok` otherwise.
"""
@spec cp!(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) :: :ok
@spec cp!(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) :: :ok | no_return
def cp!(source, destination, callback \\ fn _, _ -> true end) do
case cp(source, destination, callback) do
:ok ->
@@ -816,15 +737,15 @@ defmodule File do
## Examples
# Copies file "a.txt" to "b.txt"
File.cp_r("a.txt", "b.txt")
File.cp_r "a.txt", "b.txt"
# Copies all files in "samples" to "tmp"
File.cp_r("samples", "tmp")
File.cp_r "samples", "tmp"
# Same as before, but asks the user how to proceed in case of conflicts
File.cp_r("samples", "tmp", fn source, destination ->
File.cp_r "samples", "tmp", fn source, destination ->
IO.gets("Overwriting #{destination} by #{source}. Type y to confirm. ") == "y\n"
end)
end
"""
@spec cp_r(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) ::
@@ -847,10 +768,10 @@ defmodule File do
end
@doc """
The same as `cp_r/3`, but raises a `File.CopyError` exception if it fails.
The same as `cp_r/3`, but raises `File.CopyError` if it fails.
Returns the list of copied files otherwise.
"""
@spec cp_r!(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) :: [binary]
@spec cp_r!(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) :: [binary] | no_return
def cp_r!(source, destination, callback \\ fn _, _ -> true end) do
case cp_r(source, destination, callback) do
{:ok, files} ->
@@ -999,10 +920,9 @@ defmodule File do
end
@doc """
Same as `write/3` but raises a `File.Error` exception if it fails.
Returns `:ok` otherwise.
Same as `write/3` but raises an exception if it fails, returns `:ok` otherwise.
"""
@spec write!(Path.t(), iodata, [mode]) :: :ok
@spec write!(Path.t(), iodata, [mode]) :: :ok | no_return
def write!(path, content, modes \\ []) do
modes = normalize_modes(modes, false)
@@ -1079,10 +999,9 @@ defmodule File do
end
@doc """
Same as `rm/1`, but raises a `File.Error` exception in case of failure.
Otherwise `:ok`.
Same as `rm/1`, but raises an exception in case of failure. Otherwise `:ok`.
"""
@spec rm!(Path.t()) :: :ok
@spec rm!(Path.t()) :: :ok | no_return
def rm!(path) do
case rm(path) do
:ok ->
@@ -1095,19 +1014,14 @@ defmodule File do
@doc """
Tries to delete the dir at `path`.
Returns `:ok` if successful, or `{:error, reason}` if an error occurs.
It returns `{:error, :eexist}` if the directory is not empty.
## Examples
File.rmdir("tmp_dir")
File.rmdir('tmp_dir')
#=> :ok
File.rmdir("non_empty_dir")
#=> {:error, :eexist}
File.rmdir("file.txt")
File.rmdir('file.txt')
#=> {:error, :enotdir}
"""
@@ -1117,8 +1031,7 @@ defmodule File do
end
@doc """
Same as `rmdir/1`, but raises a `File.Error` exception in case of failure.
Otherwise `:ok`.
Same as `rmdir/1`, but raises an exception in case of failure. Otherwise `:ok`.
"""
@spec rmdir!(Path.t()) :: :ok | {:error, posix}
def rmdir!(path) do
@@ -1145,10 +1058,10 @@ defmodule File do
## Examples
File.rm_rf("samples")
File.rm_rf "samples"
#=> {:ok, ["samples", "samples/1.txt"]}
File.rm_rf("unknown")
File.rm_rf "unknown"
#=> {:ok, []}
"""
@@ -1208,7 +1121,7 @@ defmodule File do
# On Windows, symlinks are treated as directory and must be removed
# with rmdir/1. But on Unix, we remove them via rm/1. So we first try
# to remove it as a directory and, if we get :enotdir, we fall back to
# to remove it as a directory and, if we get :enotdir, we fallback to
# a file removal.
defp do_rm_directory(path, {:ok, acc} = entry) do
case rmdir(path) do
@@ -1239,10 +1152,10 @@ defmodule File do
end
@doc """
Same as `rm_rf/1` but raises a `File.Error` exception in case of failures,
Same as `rm_rf/1` but raises `File.Error` in case of failures,
otherwise the list of files or directories removed.
"""
@spec rm_rf!(Path.t()) :: [binary]
@spec rm_rf!(Path.t()) :: [binary] | no_return
def rm_rf!(path) do
case rm_rf(path) do
{:ok, files} ->
@@ -1364,7 +1277,7 @@ defmodule File do
## Examples
File.open("file.txt", [:read, :write], fn file ->
File.open("file.txt", [:read, :write], fn(file) ->
IO.read(file, :line)
end)
@@ -1387,13 +1300,14 @@ defmodule File do
end
@doc """
Similar to `open/2` but raises a `File.Error` exception if the file
could not be opened. Returns the IO device otherwise.
Similar to `open/2` but raises an error if file could not be opened.
Returns the IO device otherwise.
See `open/2` for the list of available modes.
"""
@spec open!(Path.t(), [mode | :ram]) :: io_device
@spec open!(Path.t(), (io_device -> res)) :: res when res: var
@spec open!(Path.t(), [mode | :ram]) :: io_device | no_return
@spec open!(Path.t(), (io_device -> res)) :: res | no_return when res: var
def open!(path, modes_or_function \\ []) do
case open(path, modes_or_function) do
{:ok, io_device_or_function_result} ->
@@ -1405,14 +1319,13 @@ defmodule File do
end
@doc """
Similar to `open/3` but raises a `File.Error` exception if the file
could not be opened.
Similar to `open/3` but raises an error if file could not be opened.
If it succeeds opening the file, it returns the `function` result on the IO device.
See `open/2` for the list of available `modes`.
"""
@spec open!(Path.t(), [mode | :ram], (io_device -> res)) :: res when res: var
@spec open!(Path.t(), [mode | :ram], (io_device -> res)) :: res | no_return when res: var
def open!(path, modes, function) do
case open(path, modes, function) do
{:ok, function_result} ->
@@ -1449,9 +1362,9 @@ defmodule File do
defp fix_drive_letter(original), do: original
@doc """
The same as `cwd/0`, but raises a `File.Error` exception if it fails.
The same as `cwd/0`, but raises an exception if it fails.
"""
@spec cwd!() :: binary
@spec cwd!() :: binary | no_return
def cwd!() do
case cwd() do
{:ok, cwd} ->
@@ -1473,9 +1386,9 @@ defmodule File do
end
@doc """
The same as `cd/1`, but raises a `File.Error` exception if it fails.
The same as `cd/1`, but raises an exception if it fails.
"""
@spec cd!(Path.t()) :: :ok
@spec cd!(Path.t()) :: :ok | no_return
def cd!(path) do
case cd(path) do
:ok ->
@@ -1524,9 +1437,10 @@ defmodule File do
end
@doc """
The same as `ls/1` but raises a `File.Error` exception in case of an error.
The same as `ls/1` but raises `File.Error`
in case of an error.
"""
@spec ls!(Path.t()) :: [binary]
@spec ls!(Path.t()) :: [binary] | no_return
def ls!(path \\ ".") do
case ls(path) do
{:ok, value} ->
@@ -1574,8 +1488,7 @@ defmodule File do
in raw mode for performance reasons. Therefore, Elixir **will** open
streams in `:raw` mode with the `:read_ahead` option unless an encoding
is specified. This means any data streamed into the file must be
converted to `t:iodata/0` type. If you pass e.g. `[encoding: :utf8]`
or `[encoding: {:utf16, :little}]` in the modes parameter,
converted to `t:iodata/0` type. If you pass `[:utf8]` in the modes parameter,
the underlying stream will use `IO.write/2` and the `String.Chars` protocol
to convert the data. See `IO.binwrite/2` and `IO.write/2` .
@@ -1587,9 +1500,6 @@ defmodule File do
If you pass `:trim_bom` in the modes parameter, the stream will
trim UTF-8, UTF-16 and UTF-32 byte order marks when reading from file.
Note that this function does not try to discover the file encoding basing
on BOM.
## Examples
# Read in 2048 byte chunks rather than lines
@@ -1598,8 +1508,8 @@ defmodule File do
#=> path: "./test/test.data", raw: true}
See `Stream.run/1` for an example of streaming into a file.
"""
@spec stream!(Path.t(), stream_mode, :line | pos_integer) :: File.Stream.t()
def stream!(path, modes \\ [], line_or_bytes \\ :line) do
modes = normalize_modes(modes, true)
File.Stream.__build__(IO.chardata_to_string(path), modes, line_or_bytes)
@@ -1612,7 +1522,7 @@ defmodule File do
## Permissions
File permissions are specified by adding together the following octal modes:
File permissions are specified by adding together the following octal flags:
* `0o400` - read permission: owner
* `0o200` - write permission: owner
@@ -1637,10 +1547,9 @@ defmodule File do
end
@doc """
Same as `chmod/2`, but raises a `File.Error` exception in case of failure.
Otherwise `:ok`.
Same as `chmod/2`, but raises an exception in case of failure. Otherwise `:ok`.
"""
@spec chmod!(Path.t(), non_neg_integer) :: :ok
@spec chmod!(Path.t(), non_neg_integer) :: :ok | no_return
def chmod!(path, mode) do
case chmod(path, mode) do
:ok ->
@@ -1665,10 +1574,9 @@ defmodule File do
end
@doc """
Same as `chgrp/2`, but raises a `File.Error` exception in case of failure.
Otherwise `:ok`.
Same as `chgrp/2`, but raises an exception in case of failure. Otherwise `:ok`.
"""
@spec chgrp!(Path.t(), non_neg_integer) :: :ok
@spec chgrp!(Path.t(), non_neg_integer) :: :ok | no_return
def chgrp!(path, gid) do
case chgrp(path, gid) do
:ok ->
@@ -1693,10 +1601,9 @@ defmodule File do
end
@doc """
Same as `chown/2`, but raises a `File.Error` exception in case of failure.
Otherwise `:ok`.
Same as `chown/2`, but raises an exception in case of failure. Otherwise `:ok`.
"""
@spec chown!(Path.t(), non_neg_integer) :: :ok
@spec chown!(Path.t(), non_neg_integer) :: :ok | no_return
def chown!(path, uid) do
case chown(path, uid) do
:ok ->
+2 -17
View File
@@ -12,7 +12,7 @@ defmodule File.Stat do
* `size` - size of file in bytes.
* `type` - `:device | :directory | :regular | :other | :symlink`; the type of the
* `type` - `:device | :directory | :regular | :other`; the type of the
file.
* `access` - `:read | :write | :read_write | :none`; the current system
@@ -58,22 +58,7 @@ defmodule File.Stat do
pairs = :lists.zip(keys, vals)
defstruct keys
@type t :: %__MODULE__{
size: non_neg_integer(),
type: :device | :directory | :regular | :other | :symlink,
access: :read | :write | :read_write | :none,
atime: :calendar.datetime(),
mtime: :calendar.datetime(),
ctime: :calendar.datetime(),
mode: non_neg_integer(),
links: non_neg_integer(),
major_device: non_neg_integer(),
minor_device: non_neg_integer(),
inode: non_neg_integer(),
uid: non_neg_integer(),
gid: non_neg_integer()
}
@type t :: %__MODULE__{}
@doc """
Converts a `File.Stat` struct to a `:file_info` record.
+11 -36
View File
@@ -22,10 +22,10 @@ defmodule File.Stream do
modes =
case raw do
true ->
case :lists.keyfind(:read_ahead, 1, modes) do
{:read_ahead, false} -> [:raw | :lists.keydelete(:read_ahead, 1, modes)]
{:read_ahead, _} -> [:raw | modes]
false -> [:raw, :read_ahead | modes]
if :lists.keyfind(:read_ahead, 1, modes) == {:read_ahead, false} do
[:raw | modes]
else
[:raw, :read_ahead | modes]
end
false ->
@@ -77,7 +77,7 @@ defmodule File.Stream do
start_fun = fn ->
case :file.open(path, read_modes(modes)) do
{:ok, device} ->
if :trim_bom in modes, do: trim_bom(device, raw) |> elem(0), else: device
if :trim_bom in modes, do: trim_bom(device), else: device
{:error, reason} ->
raise File.Error, reason: reason, action: "stream", path: path
@@ -106,24 +106,19 @@ defmodule File.Stream do
end
end
def count(%{path: path, line_or_bytes: bytes, raw: true, modes: modes}) do
def count(%{path: path, line_or_bytes: bytes}) do
case File.stat(path) do
{:ok, %{size: 0}} ->
{:error, __MODULE__}
{:ok, %{size: size}} ->
remainder = if rem(size, bytes) == 0, do: 0, else: 1
{:ok, div(size, bytes) + remainder - count_raw_bom(path, modes)}
{:ok, div(size, bytes) + if(rem(size, bytes) == 0, do: 0, else: 1)}
{:error, reason} ->
raise File.Error, reason: reason, action: "stream", path: path
end
end
def count(_stream) do
{:error, __MODULE__}
end
def member?(_stream, _term) do
{:error, __MODULE__}
end
@@ -132,30 +127,10 @@ defmodule File.Stream do
{:error, __MODULE__}
end
defp count_raw_bom(path, modes) do
if :trim_bom in modes do
File.open!(path, read_modes(modes), &(&1 |> trim_bom(true) |> elem(1)))
else
0
end
end
defp trim_bom(device, true) do
bom_length = device |> IO.binread(4) |> bom_length()
{:ok, new_pos} = :file.position(device, bom_length)
{device, new_pos}
end
defp trim_bom(device, false) do
# Or we read the bom in the correct amount or it isn't there
case bom_length(IO.read(device, 1)) do
0 ->
{:ok, _} = :file.position(device, 0)
{device, 0}
_ ->
{device, 1}
end
defp trim_bom(device) do
header = IO.binread(device, 4)
{:ok, _new_pos} = :file.position(device, bom_length(header))
device
end
defp bom_length(<<239, 187, 191, _rest::binary>>), do: 3
+27 -56
View File
@@ -16,9 +16,7 @@ defmodule Float do
There are some very well known problems with floating-point numbers
and arithmetics due to the fact most decimal fractions cannot be
represented by a floating-point binary and most operations are not exact,
but operate on approximations. Those issues are not specific
to Elixir, they are a property of floating point representation itself.
represented by a floating-point binary.
For example, the numbers 0.1 and 0.01 are two of them, what means the result
of squaring 0.1 does not give 0.01 neither the closest representable. Here is
@@ -32,12 +30,6 @@ defmodule Float do
There are also other known problems like flooring or rounding numbers. See
`round/2` and `floor/2` for more details about them.
To learn more about floating-point arithmetic visit:
* [0.30000000000000004.com](http://0.30000000000000004.com/)
* [What Every Programmer Should Know About Floating-Point Arithmetic](http://floating-point-gui.de/)
"""
import Bitwise
@@ -149,10 +141,6 @@ defmodule Float do
@spec floor(float, precision_range) :: float
def floor(number, precision \\ 0)
def floor(number, 0) when is_float(number) do
:math.floor(number)
end
def floor(number, precision) when is_float(number) and precision in @precision_range do
round(number, precision, :floor)
end
@@ -196,10 +184,6 @@ defmodule Float do
@spec ceil(float, precision_range) :: float
def ceil(number, precision \\ 0)
def ceil(number, 0) when is_float(number) do
:math.ceil(number)
end
def ceil(number, precision) when is_float(number) and precision in @precision_range do
round(number, precision, :ceil)
end
@@ -256,26 +240,25 @@ defmodule Float do
# and could be implemented in the future.
def round(float, precision \\ 0)
def round(float, 0) when is_float(float) do
float |> :erlang.round() |> :erlang.float()
end
def round(float, precision) when is_float(float) and precision in @precision_range do
round(float, precision, :half_up)
end
def round(float, precision) when is_float(float) do
def round(number, precision) when is_float(number) do
raise ArgumentError, invalid_precision_message(precision)
end
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)
count = count - exp + 1023
cond do
# There is no decimal precision
# zero or minus zero
count <= 0 or (0 == exp and <<0::52>> == significant) ->
float
# Precision beyond 15 digits
count >= 104 ->
case rounding do
@@ -382,8 +365,6 @@ defmodule Float do
## Examples
iex> Float.ratio(0.0)
{0, 1}
iex> Float.ratio(3.14)
{7070651414971679, 2251799813685248}
iex> Float.ratio(-3.14)
@@ -398,36 +379,26 @@ defmodule Float do
{-16, 1}
"""
@doc since: "1.4.0"
@spec ratio(float) :: {integer, pos_integer}
def ratio(0.0), do: {0, 1}
def ratio(float) when is_float(float) do
case <<float::float>> do
<<sign::1, 0::11, significant::52-bitstring>> ->
{num, _, den} = decompose(significant, 0)
{sign(sign, num), shift_left(den, 1022)}
<<sign::1, exp::11, significant::52-bitstring>> = <<float::float>>
{num, _, den} = decompose(significant)
num = sign(sign, num)
<<sign::1, exp::11, significant::52-bitstring>> ->
{num, _, den} = decompose(significant, 1)
num = sign(sign, num)
case exp - 1023 do
exp when exp > 0 ->
{den, exp} = shift_right(den, exp)
{shift_left(num, exp), den}
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)}
exp when exp < 0 ->
{num, shift_left(den, -exp)}
0 ->
{num, den}
end
0 ->
{num, den}
end
end
defp decompose(significant, initial) do
decompose(significant, 1, 0, 2, 1, initial)
defp decompose(significant) do
decompose(significant, 1, 0, 2, 1, 1)
end
defp decompose(<<1::1, bits::bitstring>>, count, last_count, power, _last_power, acc) do
@@ -442,11 +413,11 @@ defmodule Float 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_left(num, 0), do: num
defp shift_left(num, times), do: shift_left(num <<< 1, times - 1)
defp shift_right(num, 0), do: {num, 0}
defp shift_right(1, times), do: {1, times}
@@ -492,21 +463,21 @@ defmodule Float do
IO.iodata_to_binary(:io_lib_format.fwrite_g(float))
end
@doc false
# TODO: Remove by 2.0
@deprecated "Use Float.to_charlist/1 instead"
# (hard-deprecated in elixir_dispatch)
@doc false
def to_char_list(float), do: Float.to_charlist(float)
@doc false
# TODO: Remove by 2.0
@deprecated "Use :erlang.float_to_list/2 instead"
# (hard-deprecated in elixir_dispatch)
def to_char_list(float, options) do
:erlang.float_to_list(float, expand_compact(options))
end
@doc false
# TODO: Remove by 2.0
@deprecated "Use :erlang.float_to_binary/2 instead"
# (hard-deprecated in elixir_dispatch)
def to_string(float, options) do
:erlang.float_to_binary(float, expand_compact(options))
end
-137
View File
@@ -1,137 +0,0 @@
defmodule Function do
@moduledoc """
A set of functions for working with functions.
There are two types of captured functions: **external** and **local**.
External functions are functions residing in modules that are captured
with `&/1`, such as `&String.length/1`. Local functions are anonymous functions
defined with `fn/1` or with the capture operator `&/1` using `&1`, `&2`,
and so on as replacements.
"""
@type information ::
:arity
| :env
| :index
| :module
| :name
| :new_index
| :new_uniq
| :pid
| :type
| :uniq
@doc """
Captures the given function.
Inlined by the compiler.
## Examples
iex> Function.capture(String, :length, 1)
&String.length/1
"""
@doc since: "1.7.0"
@spec capture(module, atom, arity) :: fun
def capture(module, function_name, arity) do
:erlang.make_fun(module, function_name, arity)
end
@doc """
Returns a keyword list with information about a function.
The returned keys (with the corresponding possible values) for
all types of functions (local and external) are the following:
* `:type` - `:local` (for anonymous functions) or `:external` (for
named functions).
* `:module` - an atom which is the module where the function is defined when
anonymous or the module which the function refers to when it's a named function.
* `:arity` - (integer) the number of arguments the function is to be called with.
* `:name` - (atom) the name of the function.
* `:env` - a list of the environment or free variables. For named
functions, the returned list is always empty.
When `fun` is an anonymous function (that is, the type is `:local`), the following
additional keys are returned:
* `:pid` - PID of the process that originally created the function.
* `:index` - (integer) an index into the module function table.
* `:new_index` - (integer) an index into the module function table.
* `:new_uniq` - (binary) a unique value for this function. It's
calculated from the compiled code for the entire module.
* `:uniq` - (integer) a unique value for this function. This integer is
calculated from the compiled code for the entire module.
**Note**: this function must be used only for debugging purposes.
Inlined by the compiler.
## Examples
iex> fun = fn x -> x end
iex> info = Function.info(fun)
iex> Keyword.get(info, :arity)
1
iex> Keyword.get(info, :type)
:local
iex> fun = &String.length/1
iex> info = Function.info(fun)
iex> Keyword.get(info, :type)
:external
iex> Keyword.get(info, :name)
:length
"""
@doc since: "1.7.0"
@spec info(fun) :: [{information, term}]
def info(fun), do: :erlang.fun_info(fun)
@doc """
Returns a specific information about the function.
The returned information is a two-element tuple in the shape of
`{info, value}`.
For any function, the information asked for can be any of the atoms
`:module`, `:name`, `:arity`, `:env`, or `:type`.
For anonymous functions, there is also information about any of the
atoms `:index`, `:new_index`, `:new_uniq`, `:uniq`, and `:pid`.
For a named function, the value of any of these items is always the
atom `:undefined`.
For more information on each of the possible returned values, see
`info/1`.
Inlined by the compiler.
## Examples
iex> f = fn x -> x end
iex> Function.info(f, :arity)
{:arity, 1}
iex> Function.info(f, :type)
{:type, :local}
iex> fun = &String.length/1
iex> Function.info(fun, :name)
{:name, :length}
iex> Function.info(fun, :pid)
{:pid, :undefined}
"""
@doc since: "1.7.0"
@spec info(fun, item) :: {item, term} when item: information
def info(fun, item), do: :erlang.fun_info(fun, item)
end
+3 -22
View File
@@ -4,7 +4,7 @@ defmodule GenEvent do
# Functions from this module are deprecated in elixir_dispatch.
@moduledoc """
A event manager with event handlers behaviour.
WARNING: this module is deprecated.
If you are interested in implementing an event manager, please read the
"Alternatives" section below. If you have to implement an event handler to
@@ -43,8 +43,6 @@ defmodule GenEvent do
[`:gen_event`](http://erlang.org/doc/man/gen_event.html) Erlang module.
"""
@moduledoc deprecated: "Use Erlang/OTP's :gen_event module instead"
@callback init(args :: term) ::
{:ok, state}
| {:ok, state, :hibernate}
@@ -85,9 +83,6 @@ defmodule GenEvent do
@type handler :: atom | {atom, term}
message = "Use one of the alternatives described in the documentation for the GenEvent module"
@deprecated message
@doc false
defmacro __using__(_) do
%{file: file, line: line} = __CALLER__
@@ -153,14 +148,12 @@ defmodule GenEvent do
end
@doc false
@deprecated message
@spec start_link(options) :: on_start
def start_link(options \\ []) when is_list(options) do
do_start(:link, options)
end
@doc false
@deprecated message
@spec start(options) :: on_start
def start(options \\ []) when is_list(options) do
do_start(:nolink, options)
@@ -184,7 +177,7 @@ defmodule GenEvent do
other ->
raise ArgumentError, """
expected :name option to be one of the following:
expected :name option to be one of:
* nil
* atom
@@ -197,28 +190,24 @@ defmodule GenEvent do
end
@doc false
@deprecated message
@spec stream(manager, keyword) :: GenEvent.Stream.t()
def stream(manager, options \\ []) do
%GenEvent.Stream{manager: manager, timeout: Keyword.get(options, :timeout, :infinity)}
end
@doc false
@deprecated message
@spec add_handler(manager, handler, term) :: :ok | {:error, term}
def add_handler(manager, handler, args) do
rpc(manager, {:add_handler, handler, args})
end
@doc false
@deprecated message
@spec add_mon_handler(manager, handler, term) :: :ok | {:error, term}
def add_mon_handler(manager, handler, args) do
rpc(manager, {:add_mon_handler, handler, args, self()})
end
@doc false
@deprecated message
@spec notify(manager, term) :: :ok
def notify(manager, event)
@@ -249,21 +238,18 @@ defmodule GenEvent do
end
@doc false
@deprecated message
@spec sync_notify(manager, term) :: :ok
def sync_notify(manager, event) do
rpc(manager, {:sync_notify, event})
end
@doc false
@deprecated message
@spec ack_notify(manager, term) :: :ok
def ack_notify(manager, event) do
rpc(manager, {:ack_notify, event})
end
@doc false
@deprecated message
@spec call(manager, handler, term, timeout) :: term | {:error, term}
def call(manager, handler, request, timeout \\ 5000) do
try do
@@ -277,35 +263,30 @@ defmodule GenEvent do
end
@doc false
@deprecated message
@spec remove_handler(manager, handler, term) :: term | {:error, term}
def remove_handler(manager, handler, args) do
rpc(manager, {:delete_handler, handler, args})
end
@doc false
@deprecated message
@spec swap_handler(manager, handler, term, handler, term) :: :ok | {:error, term}
def swap_handler(manager, handler1, args1, handler2, args2) do
rpc(manager, {:swap_handler, handler1, args1, handler2, args2})
end
@doc false
@deprecated message
@spec swap_mon_handler(manager, handler, term, handler, term) :: :ok | {:error, term}
def swap_mon_handler(manager, handler1, args1, handler2, args2) do
rpc(manager, {:swap_mon_handler, handler1, args1, handler2, args2, self()})
end
@doc false
@deprecated message
@spec which_handlers(manager) :: [handler]
def which_handlers(manager) do
rpc(manager, :which_handlers)
end
@doc false
@deprecated message
@spec stop(manager, reason :: term, timeout) :: :ok
def stop(manager, reason \\ :normal, timeout \\ :infinity) do
:gen.stop(manager, reason, timeout)
@@ -848,7 +829,7 @@ defmodule GenEvent do
apply(mod, fun, args)
catch
:throw, val -> {:ok, val}
:error, val -> {:error, {val, __STACKTRACE__}}
:error, val -> {:error, {val, System.stacktrace()}}
:exit, val -> {:error, val}
else
res -> {:ok, res}
+176 -329
View File
@@ -23,17 +23,10 @@ defmodule GenServer do
# Callbacks
@impl true
def init(stack) do
{:ok, stack}
def handle_call(:pop, _from, [h | t]) do
{:reply, h, t}
end
@impl true
def handle_call(:pop, _from, [head | tail]) do
{:reply, head, tail}
end
@impl true
def handle_cast({:push, item}, state) do
{:noreply, [item | state]}
end
@@ -52,7 +45,7 @@ defmodule GenServer do
GenServer.call(pid, :pop)
#=> :world
We start our `Stack` by calling `start_link/2`, passing the module
We start our `Stack` by calling `start_link/3`, passing the module
with the server implementation and its initial argument (a list
representing the stack containing the item `:hello`). We can primarily
interact with the server by sending two types of messages. **call**
@@ -61,100 +54,31 @@ 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 7 possible
callbacks to be implemented when you use a `GenServer`. The only required
callback is `init/1`.
A `cast/2` message must be handled by `c:handle_cast/2`.
## Client / Server APIs
## use GenServer and callbacks
Although in the example above we have used `GenServer.start_link/3` and
friends to directly start and communicate with the server, most of the
time we don't call the `GenServer` functions directly. Instead, we wrap
the calls in new functions representing the public API of the server.
There are 6 callbacks required to be implemented in a `GenServer`. By
adding `use GenServer` to your module, Elixir will automatically define
all 6 callbacks for you, leaving it up to you to implement the ones
you want to customize.
Here is a better implementation of our Stack module:
`use GenServer` also defines a `child_spec/1` function, allowing the
defined module to be put under a supervision tree. The generated
`child_spec/1` can be customized with the following options:
defmodule Stack do
use GenServer
# Client
def start_link(default) when is_list(default) do
GenServer.start_link(__MODULE__, default)
end
def push(pid, item) do
GenServer.cast(pid, {:push, item})
end
def pop(pid) do
GenServer.call(pid, :pop)
end
# Server (callbacks)
@impl true
def init(stack) do
{:ok, stack}
end
@impl true
def handle_call(:pop, _from, [head | tail]) do
{:reply, head, tail}
end
@impl true
def handle_cast({:push, item}, state) do
{:noreply, [item | state]}
end
end
In practice, it is common to have both server and client functions in
the same module. If the server and/or client implementations are growing
complex, you may want to have them in different modules.
## How to supervise
A `GenServer` is most commonly started under a supervision tree.
When we invoke `use GenServer`, it automatically defines a `child_spec/1`
function that allows us to start the `Stack` directly under a supervisor.
To start a default stack of `[:hello]` under a supervisor, one may do:
children = [
{Stack, [:hello]}
]
Supervisor.start_link(children, strategy: :one_for_all)
Note you can also start it simply as `Stack`, which is the same as
`{Stack, []}`:
children = [
Stack # The same as {Stack, []}
]
Supervisor.start_link(children, strategy: :one_for_all)
In both cases, `Stack.start_link/1` is always invoked.
`use GenServer` also accepts a list of options which configures the
child specification and therefore how it runs under a supervisor.
The generated `child_spec/1` can be customized with the following options:
* `:id` - the child specification identifier, defaults to the current module
* `:id` - the child specification id, defaults to the current module
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
* `:restart` - when the child should be restarted, defaults to `:permanent`
* `:shutdown` - how to shut down the child, either immediately or by giving it time to shut down
* `:shutdown` - how to shut down the child
For example:
use GenServer, restart: :transient, shutdown: 10_000
See the "Child specification" section in the `Supervisor` module for more
detailed information. The `@doc` annotation immediately preceding
`use GenServer` will be attached to the generated `child_spec/1` function.
See the `Supervisor` docs for more information.
## Name registration
## Name Registration
Both `start_link/3` and `start/3` support the `GenServer` to register
a name on start via the `:name` option. Registered names are also
@@ -163,7 +87,7 @@ defmodule GenServer do
* an atom - the GenServer is registered locally with the given name
using `Process.register/2`.
* `{:global, term}` - the GenServer is registered globally with the given
* `{:global, term}`- the GenServer is registered globally with the given
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
@@ -181,34 +105,83 @@ defmodule GenServer do
{:ok, _} = GenServer.start_link(Stack, [:hello], name: MyStack)
# Now messages can be sent directly to MyStack
GenServer.call(MyStack, :pop)
#=> :hello
GenServer.call(MyStack, :pop) #=> :hello
Once the server is started, the remaining functions in this module (`call/3`,
`cast/2`, and friends) will also accept an atom, or any `{:global, ...}` or
`{:via, ...}` tuples. In general, the following formats are supported:
`cast/2`, and friends) will also accept an atom, or any `:global` or `:via`
tuples. In general, the following formats are supported:
* a PID
* an atom if the server is locally registered
* a `pid`
* an `atom` if the server is locally registered
* `{atom, node}` if the server is locally registered at another node
* `{:global, term}` if the server is globally registered
* `{:via, module, name}` if the server is registered through an alternative
registry
If there is an interest to register dynamic names locally, do not use
atoms, as atoms are never garbage-collected and therefore dynamically
generated atoms won't be garbage-collected. For such cases, you can
atoms, as atoms are never garbage collected and therefore dynamically
generated atoms won't be garbage collected. For such cases, you can
set up your own local registry by using the `Registry` module.
## Client / Server APIs
Although in the example above we have used `GenServer.start_link/3` and
friends to directly start and communicate with the server, most of the
time we don't call the `GenServer` functions directly. Instead, we wrap
the calls in new functions representing the public API of the server.
Here is a better implementation of our Stack module:
defmodule Stack do
use GenServer
# Client
def start_link(default) do
GenServer.start_link(__MODULE__, default)
end
def push(pid, item) do
GenServer.cast(pid, {:push, item})
end
def pop(pid) do
GenServer.call(pid, :pop)
end
# Server (callbacks)
def handle_call(:pop, _from, [h | t]) do
{:reply, h, t}
end
def handle_call(request, from, state) do
# Call the default implementation from GenServer
super(request, from, state)
end
def handle_cast({:push, item}, state) do
{:noreply, [item | state]}
end
def handle_cast(request, state) do
super(request, state)
end
end
In practice, it is common to have both server and client functions in
the same module. If the server and/or client implementations are growing
complex, you may want to have them in different modules.
## Receiving "regular" messages
The goal of a `GenServer` is to abstract the "receive" loop for developers,
automatically handling system messages, supporting code change, synchronous
automatically handling system messages, support code change, synchronous
calls and more. Therefore, you should never call your own "receive" inside
the GenServer callbacks as doing so will cause the GenServer to misbehave.
Besides the synchronous and asynchronous communication provided by `call/3`
and `cast/2`, "regular" messages sent by functions such as `Kernel.send/2`,
and `cast/2`, "regular" messages sent by functions such `Kernel.send/2`,
`Process.send_after/4` and similar, can be handled inside the `c:handle_info/2`
callback.
@@ -219,84 +192,33 @@ defmodule GenServer do
defmodule MyApp.Periodically do
use GenServer
def start_link(_) do
def start_link do
GenServer.start_link(__MODULE__, %{})
end
@impl true
def init(state) do
# Schedule work to be performed on start
schedule_work()
schedule_work() # Schedule work to be performed on start
{:ok, state}
end
@impl true
def handle_info(:work, state) do
# Do the desired work here
...
# Reschedule once more
schedule_work()
schedule_work() # Reschedule once more
{:noreply, state}
end
defp schedule_work do
# In 2 hours
Process.send_after(self(), :work, 2 * 60 * 60 * 1000)
defp schedule_work() do
Process.send_after(self(), :work, 2 * 60 * 60 * 1000) # In 2 hours
end
end
## When (not) to use a GenServer
So far, we have learned that a `GenServer` can be used as a supervised process
that handles sync and async calls. It can also handle system messages, such as
periodic messages and monitoring events. GenServer processes may also be named.
A GenServer, or a process in general, must be used to model runtime characteristics
of your system. A GenServer must never be used for code organization purposes.
In Elixir, code organization is done by modules and functions, processes are not
necessary. For example, imagine you are implementing a calculator and you decide
to put all the calculator operations behind a GenServer:
def add(a, b) do
GenServer.call(__MODULE__, {:add, a, b})
end
def handle_call({:add, a, b}, _from, state) do
{:reply, a + b, state}
end
def handle_call({:subtract, a, b}, _from, state) do
{:reply, a - b, state}
end
This is an anti-pattern not only because it convolutes the calculator logic but
also because you put the calculator logic behind a single process that will
potentially become a bottleneck in your system, especially as the number of
calls grow. Instead just define the functions directly:
def add(a, b) do
a + b
end
def subtract(a, b) do
a - b
end
If you don't need a process, then you don't need a process. Use processes only to
model runtime properties, such as mutable state, concurrency and failures, never
for code organization.
## Debugging with the :sys module
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.
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](http://www.erlang.org/doc/man/sys.html) used for debugging:
@@ -331,67 +253,47 @@ defmodule GenServer do
*DBG* <0.122.0> got cast {push,1}
*DBG* <0.122.0> new state [1]
:ok
iex> :sys.get_state(pid)
[1]
iex> Stack.pop(pid)
*DBG* <0.122.0> got call pop from <0.80.0>
*DBG* <0.122.0> sent 1 to <0.80.0>, new state []
1
iex> :sys.statistics(pid, :get)
{:ok,
[
start_time: {{2016, 7, 16}, {12, 29, 41}},
current_time: {{2016, 7, 16}, {12, 29, 50}},
reductions: 117,
messages_in: 2,
messages_out: 0
]}
[start_time: {{2016, 7, 16}, {12, 29, 41}},
current_time: {{2016, 7, 16}, {12, 29, 50}},
reductions: 117, messages_in: 2, messages_out: 0]}
iex> :sys.no_debug(pid) # turn off all debug handlers
:ok
iex> :sys.get_status(pid)
{:status, #PID<0.122.0>, {:module, :gen_server},
[
[
"$initial_call": {Stack, :init, 1}, # process dictionary
"$ancestors": [#PID<0.80.0>, #PID<0.51.0>]
],
:running, # :running | :suspended
#PID<0.80.0>, # parent
[], # debugger state
[
header: 'Status for generic server <0.122.0>', # module status
data: [
{'Status', :running},
{'Parent', #PID<0.80.0>},
{'Logged events', []}
],
data: [{'State', [1]}]
]
]}
[["$initial_call": {Stack, :init, 1}, # pdict
"$ancestors": [#PID<0.80.0>, #PID<0.51.0>]],
:running, # :running | :suspended
#PID<0.80.0>, # parent
[], # debugger state
[header: 'Status for generic server <0.122.0>', # module status
data: [{'Status', :running}, {'Parent', #PID<0.80.0>},
{'Logged events', []}], data: [{'State', [1]}]]]}
## Learn more
If you wish to find out more about GenServers, the Elixir Getting Started
If you wish to find out more about gen servers, the Elixir Getting Started
guide provides a tutorial-like introduction. The documentation and links
in Erlang can also provide extra insight.
* [GenServer - Elixir's Getting Started Guide](https://elixir-lang.org/getting-started/mix-otp/genserver.html)
* [GenServer – Elixir's Getting Started Guide](http://elixir-lang.org/getting-started/mix-otp/genserver.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)
* [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)
"""
@doc """
Invoked when the server is started. `start_link/3` or `start/3` will
block until it returns.
`init_arg` is the argument term (second argument) passed to `start_link/3`.
`args` is the argument term (second argument) passed to `start_link/3`.
Returning `{:ok, state}` will cause `start_link/3` to return
`{:ok, pid}` and the process to enter its loop.
@@ -400,24 +302,18 @@ defmodule GenServer do
except `handle_info(:timeout, state)` will be called after `timeout`
milliseconds if no messages are received within the timeout.
Returning `{:ok, state, :hibernate}` is similar to `{:ok, state}`
except the process is hibernated before entering the loop. See
Returning `{:ok, state, :hibernate}` is similar to
`{:ok, state}` except the process is hibernated before entering the loop. See
`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
the `c:handle_continue/2` callback will be invoked with the value
`continue` as first argument.
Returning `:ignore` will cause `start_link/3` to return `:ignore` and
the process will exit normally without entering the loop or calling
`c:terminate/2`. If used when part of a supervision tree the parent
supervisor will not fail to start nor immediately try to restart the
`GenServer`. The remainder of the supervision tree will be started
and so the `GenServer` should not be required by other processes.
It can be started later with `Supervisor.restart_child/2` as the child
specification is saved in the parent supervisor. The main use cases for
this are:
Returning `:ignore` will cause `start_link/3` to return `:ignore` and the
process will exit normally without entering the loop or calling `c:terminate/2`.
If used when part of a supervision tree the parent supervisor will not fail
to start nor immediately try to restart the `GenServer`. The remainder of the
supervision tree will be (re)started and so the `GenServer` should not be
required by other processes. It can be started later with
`Supervisor.restart_child/2` as the child specification is saved in the parent
supervisor. The main use cases for this are:
* The `GenServer` is disabled by configuration but might be enabled later.
* An error occurred and it will be handled by a different mechanism than the
@@ -428,9 +324,9 @@ defmodule GenServer do
`{:error, reason}` and the process to exit with reason `reason` without
entering the loop or calling `c:terminate/2`.
"""
@callback init(init_arg :: term) ::
@callback init(args :: term) ::
{:ok, state}
| {:ok, state, timeout | :hibernate | {:continue, term}}
| {:ok, state, timeout | :hibernate}
| :ignore
| {:stop, reason :: any}
when state: any
@@ -457,10 +353,6 @@ defmodule GenServer do
`GenServer` causes garbage collection and leaves a continuous heap that
minimises the memory used by the process.
Returning `{:reply, reply, new_state, {:continue, continue}}` is similar to
`{:reply, reply, new_state}` except `c:handle_continue/2` will be invoked
immediately after with the value `continue` as first argument.
Hibernating should not be used aggressively as too much time could be spent
garbage collecting. Normally it should only be used when a message is not
expected soon and minimising the memory of the process is shown to be
@@ -482,9 +374,9 @@ defmodule GenServer do
process exits without replying as the caller will be blocking awaiting a
reply.
Returning `{:noreply, new_state, timeout | :hibernate | {:continue, continue}}`
is similar to `{:noreply, new_state}` except a timeout, hibernation or continue
occurs as with a `:reply` tuple.
Returning `{:noreply, new_state, timeout | :hibernate}` is similar to
`{:noreply, new_state}` except a timeout or hibernation 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
@@ -493,14 +385,15 @@ defmodule GenServer do
Returning `{:stop, reason, new_state}` is similar to
`{:stop, reason, reply, new_state}` except a reply is not sent.
This callback is optional. If one is not implemented, the server will fail
if a call is performed against it.
If this callback is not implemented, the default implementation by
`use GenServer` will fail with a `RuntimeError` exception with a message:
attempted to call `GenServer` but no `handle_call/3` clause was provided.
"""
@callback handle_call(request :: term, from, state :: term) ::
{:reply, reply, new_state}
| {:reply, reply, new_state, timeout | :hibernate | {:continue, term}}
| {:reply, reply, new_state, timeout | :hibernate}
| {:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:noreply, new_state, timeout | :hibernate}
| {:stop, reason, reply, new_state}
| {:stop, reason, new_state}
when reply: term, new_state: term, reason: term
@@ -521,20 +414,17 @@ defmodule GenServer do
`{:noreply, new_state}` except the process is hibernated before continuing the
loop. See `c:handle_call/3` for more information.
Returning `{:noreply, new_state, {:continue, continue}}` is similar to
`{:noreply, new_state}` except `c:handle_continue/2` will be invoked
immediately after with the value `continue` as first argument.
Returning `{:stop, reason, new_state}` stops the loop and `c:terminate/2` is
called with the reason `reason` and state `new_state`. The process exits with
reason `reason`.
This callback is optional. If one is not implemented, the server will fail
if a cast is performed against it.
If this callback is not implemented, the default implementation by
`use GenServer` will fail with a `RuntimeError` exception with a message:
attempted to call `GenServer` but no `handle_cast/2` clause was provided.
"""
@callback handle_cast(request :: term, state :: term) ::
{:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:noreply, new_state, timeout | :hibernate}
| {:stop, reason :: term, new_state}
when new_state: term
@@ -546,31 +436,12 @@ defmodule GenServer do
Return values are the same as `c:handle_cast/2`.
This callback is optional. If one is not implemented, the received message
will be logged.
If this callback is not implemented, the default implementation by
`use GenServer` will return `{:noreply, state}`.
"""
@callback handle_info(msg :: :timeout | term, state :: term) ::
{:noreply, new_state}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:stop, reason :: term, new_state}
when new_state: term
@doc """
Invoked to handle `continue` instructions.
It is useful for performing work after initialization or for splitting the work
in a callback in multiple steps, updating the process state along the way.
Return values are the same as `c:handle_cast/2`.
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}
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
| {:noreply, new_state, timeout | :hibernate}
| {:stop, reason :: term, new_state}
when new_state: term
@@ -590,20 +461,13 @@ defmodule GenServer do
* the `GenServer` traps exits (using `Process.flag/2`) *and* the parent
process sends an exit signal
If part of a supervision tree, a `GenServer`'s will receive an exit
signal when the tree is shutting down. The exit signal is based on
the shutdown strategy in the child's specification, where we this
value can be:
* `:brutal_kill`: the `GenServer` is killed and so `c:terminate/2` is not called.
* a timeout value, where the supervisor will send the exit signal `:shutdown` and
the `GenServer` will have the duration of the timeout to terminate.
If after duration of this timeout the process is still alive, it will be killed
immediately.
For a more in-depth explanation, please read the "Shutdown values (:shutdown)"
section in the `Supervisor` module.
If part of a supervision tree, a `GenServer`'s `Supervisor` will send an exit
signal when shutting it down. The exit signal is based on the shutdown
strategy in the child's specification. If it is `:brutal_kill` the `GenServer`
is killed and so `c:terminate/2` is not called. However if it is a timeout the
`Supervisor` will send the exit signal `:shutdown` and the `GenServer` will
have the duration of the timeout to call `c:terminate/2` - if the process is
still alive after the timeout it is killed.
If the `GenServer` receives an exit signal (that is not `:normal`) from any
process when it is not trapping exits it will exit abruptly with the same
@@ -614,15 +478,12 @@ defmodule GenServer do
Therefore it is not guaranteed that `c:terminate/2` is called when a `GenServer`
exits. For such reasons, we usually recommend important clean-up rules to
happen in separated processes either by use of monitoring or by links
themselves. There is no cleanup needed when the `GenServer` controls a `port` (e.g.
`:gen_tcp.socket`) or `t:File.io_device/0`, because these will be closed on
receiving a `GenServer`'s exit signal and do not need to be closed manually
in `c:terminate/2`.
themselves. For example if the `GenServer` controls a `port` (e.g.
`:gen_tcp.socket`) or `t:File.io_device/0`, they will be closed on receiving a
`GenServer`'s exit signal and do not need to be closed in `c:terminate/2`.
If `reason` is neither `:normal`, `:shutdown`, nor `{:shutdown, term}` an error is
If `reason` is not `:normal`, `:shutdown`, nor `{:shutdown, term}` an error is
logged.
This callback is optional.
"""
@callback terminate(reason, state :: term) :: term
when reason: :normal | :shutdown | {:shutdown, term}
@@ -645,8 +506,6 @@ defmodule GenServer do
If `c:code_change/3` raises the code change fails and the loop will continue
with its previous state. Therefore this callback does not usually contain side effects.
This callback is optional.
"""
@callback code_change(old_vsn, state :: term, extra :: term) ::
{:ok, new_state :: term}
@@ -674,13 +533,7 @@ defmodule GenServer do
@callback format_status(reason, pdict_and_state :: list) :: term
when reason: :normal | :terminate
@optional_callbacks code_change: 3,
terminate: 2,
handle_info: 2,
handle_cast: 2,
handle_call: 3,
format_status: 2,
handle_continue: 2
@optional_callbacks format_status: 2
@typedoc "Return values of `start*` functions"
@type on_start :: {:ok, pid} | :ignore | {:error, {:already_started, pid} | term}
@@ -697,7 +550,6 @@ defmodule GenServer do
| {:name, name}
| {:timeout, timeout}
| {:spawn_opt, Process.spawn_opt()}
| {:hibernate_after, timeout}
@typedoc "Debug options supported by the `start*` functions"
@type debug :: [:trace | :log | :statistics | {:log_to_file, Path.t()}]
@@ -718,18 +570,15 @@ defmodule GenServer do
quote location: :keep, bind_quoted: [opts: opts] do
@behaviour GenServer
if Module.get_attribute(__MODULE__, :doc) == nil do
@doc """
Returns a specification to start this module under a supervisor.
@doc """
Returns a specification to start this module under a supervisor.
See `Supervisor`.
"""
end
def child_spec(init_arg) do
See `Supervisor`.
"""
def child_spec(arg) do
default = %{
id: __MODULE__,
start: {__MODULE__, :start_link, [init_arg]}
start: {__MODULE__, :start_link, [arg]}
}
Supervisor.child_spec(default, unquote(Macro.escape(opts)))
@@ -799,7 +648,7 @@ defmodule GenServer do
{:ok, state}
end
defoverridable code_change: 3, terminate: 2, handle_info: 2, handle_cast: 2, handle_call: 3
defoverridable GenServer
end
end
@@ -811,20 +660,20 @@ defmodule GenServer do
We will inject a default implementation for now:
def init(init_arg) do
{:ok, init_arg}
def init(args) do
{:ok, args}
end
You can copy the implementation above or define your own that converts \
the arguments given to GenServer.start_link/3 to the server state.
But you want to define your own implementation that converts the \
arguments given to GenServer.start_link/3 to the server state
"""
:elixir_errors.warn(env.line, env.file, message)
quote do
@doc false
def init(init_arg) do
{:ok, init_arg}
def init(args) do
{:ok, args}
end
defoverridable init: 1
@@ -838,7 +687,7 @@ defmodule GenServer do
This is often used to start the `GenServer` as part of a supervision tree.
Once the server is started, the `c:init/1` function of the given `module` is
called with `init_arg` as its argument to initialize the server. To ensure a
called with `args` as its arguments to initialize the server. To ensure a
synchronized start-up procedure, this function does not return until `c:init/1`
has returned.
@@ -850,7 +699,7 @@ defmodule GenServer do
## Options
* `:name` - used for name registration as described in the "Name
registration" section in the documentation for `GenServer`
registration" section of the module documentation
* `:timeout` - if present, the server is allowed to spend the given number of
milliseconds initializing or it will be terminated and the start function
@@ -861,10 +710,6 @@ defmodule GenServer do
* `:spawn_opt` - if present, its value is passed as options to the
underlying process as in `Process.spawn/4`
* `:hibernate_after` - if present, the GenServer process awaits any message for
the given number of milliseconds and if no message is received, the process goes
into hibernation automatically (by calling `:proc_lib.hibernate/3`).
## Return values
If the server is successfully created and initialized, this function returns
@@ -878,8 +723,8 @@ defmodule GenServer do
`{:error, reason}` or `:ignore`, respectively.
"""
@spec start_link(module, any, options) :: on_start
def start_link(module, init_arg, options \\ []) when is_atom(module) and is_list(options) do
do_start(:link, module, init_arg, options)
def start_link(module, args, options \\ []) when is_atom(module) and is_list(options) do
do_start(:link, module, args, options)
end
@doc """
@@ -888,27 +733,27 @@ defmodule GenServer do
See `start_link/3` for more information.
"""
@spec start(module, any, options) :: on_start
def start(module, init_arg, options \\ []) when is_atom(module) and is_list(options) do
do_start(:nolink, module, init_arg, options)
def start(module, args, options \\ []) when is_atom(module) and is_list(options) do
do_start(:nolink, module, args, options)
end
defp do_start(link, module, init_arg, options) do
defp do_start(link, module, args, options) do
case Keyword.pop(options, :name) do
{nil, opts} ->
:gen.start(:gen_server, link, module, init_arg, opts)
:gen.start(:gen_server, link, module, args, opts)
{atom, opts} when is_atom(atom) ->
:gen.start(:gen_server, link, {:local, atom}, module, init_arg, opts)
:gen.start(:gen_server, link, {:local, atom}, module, args, opts)
{{:global, _term} = tuple, opts} ->
:gen.start(:gen_server, link, tuple, module, init_arg, opts)
:gen.start(:gen_server, link, tuple, module, args, opts)
{{:via, via_module, _term} = tuple, opts} when is_atom(via_module) ->
:gen.start(:gen_server, link, tuple, module, init_arg, opts)
:gen.start(:gen_server, link, tuple, module, args, opts)
{other, _} ->
raise ArgumentError, """
expected :name option to be one of the following:
expected :name option to be one of:
* nil
* atom
@@ -1003,13 +848,11 @@ defmodule GenServer do
`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.
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.
not yet connected to the caller one, the call is going to
block until a connection happens. This is different than
the behaviour in OTP's `:gen_server` where the message
is sent by another process in this case, which could cause
messages to other nodes to arrive out of order.
"""
@spec cast(server, term) :: :ok
def cast(server, request)
@@ -1135,8 +978,12 @@ defmodule GenServer do
def reply(client, reply)
def reply({to, tag}, reply) when is_pid(to) do
send(to, {tag, reply})
:ok
try do
send(to, {tag, reply})
:ok
catch
_, _ -> :ok
end
end
@doc """
+3 -15
View File
@@ -1,13 +1,12 @@
defmodule HashDict do
@moduledoc """
Tuple-based HashDict implementation.
WARNING: this module is deprecated.
This module is deprecated. Use the `Map` module instead.
Use the `Map` module instead.
"""
@moduledoc deprecated: "Use Map instead"
# TODO: Remove by 2.0
# (hard-deprecated in elixir_dispatch)
use Dict
@@ -24,24 +23,19 @@ defmodule HashDict do
@compile :inline_list_funcs
@compile {:inline, key_hash: 1, key_mask: 1, key_shift: 1}
message = "Use maps and the Map module instead"
@doc """
Creates a new empty dict.
"""
@spec new :: Dict.t()
@deprecated message
def new do
%HashDict{}
end
@deprecated message
def put(%HashDict{root: root, size: size}, key, value) do
{root, counter} = do_put(root, key, value, key_hash(key))
%HashDict{root: root, size: size + counter}
end
@deprecated message
def update!(%HashDict{root: root, size: size} = dict, key, fun) when is_function(fun, 1) do
{root, counter} =
do_update(root, key, fn -> raise KeyError, key: key, term: dict end, fun, key_hash(key))
@@ -49,18 +43,15 @@ defmodule HashDict do
%HashDict{root: root, size: size + counter}
end
@deprecated message
def update(%HashDict{root: root, size: size}, key, initial, fun) when is_function(fun, 1) do
{root, counter} = do_update(root, key, fn -> initial end, fun, key_hash(key))
%HashDict{root: root, size: size + counter}
end
@deprecated message
def fetch(%HashDict{root: root}, key) do
do_fetch(root, key, key_hash(key))
end
@deprecated message
def delete(dict, key) do
case dict_delete(dict, key) do
{dict, _value} -> dict
@@ -68,7 +59,6 @@ defmodule HashDict do
end
end
@deprecated message
def pop(dict, key, default \\ nil) do
case dict_delete(dict, key) do
{dict, value} -> {value, dict}
@@ -76,13 +66,11 @@ defmodule HashDict do
end
end
@deprecated message
def size(%HashDict{size: size}) do
size
end
@doc false
@deprecated message
def reduce(%HashDict{root: root}, acc, fun) do
do_reduce(root, acc, fun, @node_size, fn
{:suspend, acc} -> {:suspended, acc, &{:done, elem(&1, 1)}}
+4 -18
View File
@@ -1,19 +1,18 @@
defmodule HashSet do
@moduledoc """
Tuple-based HashSet implementation.
WARNING: this module is deprecated.
This module is deprecated. Use the `MapSet` module instead.
Use the `MapSet` module instead.
"""
@moduledoc deprecated: "Use MapSet instead"
# TODO: Remove by 2.0
# (hard-deprecated in elixir_dispatch)
@node_bitmap 0b111
@node_shift 3
@node_size 8
@node_template :erlang.make_tuple(@node_size, [])
message = "Use the MapSet module instead"
@opaque t :: %__MODULE__{size: non_neg_integer, root: term}
@doc false
defstruct size: 0, root: @node_template
@@ -22,40 +21,33 @@ defmodule HashSet do
@compile :inline_list_funcs
@compile {:inline, key_hash: 1, key_mask: 1, key_shift: 1}
@deprecated message
@spec new :: Set.t()
def new do
%HashSet{}
end
@deprecated message
def union(%HashSet{size: size1} = set1, %HashSet{size: size2} = set2) when size1 <= size2 do
set_fold(set1, set2, fn v, acc -> put(acc, v) end)
end
@deprecated message
def union(%HashSet{} = set1, %HashSet{} = set2) do
set_fold(set2, set1, fn v, acc -> put(acc, v) end)
end
@deprecated message
def intersection(%HashSet{} = set1, %HashSet{} = set2) do
set_fold(set1, %HashSet{}, fn v, acc ->
if member?(set2, v), do: put(acc, v), else: acc
end)
end
@deprecated message
def difference(%HashSet{} = set1, %HashSet{} = set2) do
set_fold(set2, set1, fn v, acc -> delete(acc, v) end)
end
@deprecated message
def to_list(set) do
set_fold(set, [], &[&1 | &2]) |> :lists.reverse()
end
@deprecated message
def equal?(%HashSet{size: size1} = set1, %HashSet{size: size2} = set2) do
case size1 do
^size2 -> subset?(set1, set2)
@@ -63,7 +55,6 @@ defmodule HashSet do
end
end
@deprecated message
def subset?(%HashSet{} = set1, %HashSet{} = set2) do
reduce(set1, {:cont, true}, fn member, acc ->
case member?(set2, member) do
@@ -74,7 +65,6 @@ defmodule HashSet do
|> elem(1)
end
@deprecated message
def disjoint?(%HashSet{} = set1, %HashSet{} = set2) do
reduce(set2, {:cont, true}, fn member, acc ->
case member?(set1, member) do
@@ -85,18 +75,15 @@ defmodule HashSet do
|> elem(1)
end
@deprecated message
def member?(%HashSet{root: root}, term) do
do_member?(root, term, key_hash(term))
end
@deprecated message
def put(%HashSet{root: root, size: size}, term) do
{root, counter} = do_put(root, term, key_hash(term))
%HashSet{root: root, size: size + counter}
end
@deprecated message
def delete(%HashSet{root: root, size: size} = set, term) do
case do_delete(root, term, key_hash(term)) do
{:ok, root} -> %HashSet{root: root, size: size - 1}
@@ -113,7 +100,6 @@ defmodule HashSet do
end)
end
@deprecated message
def size(%HashSet{size: size}) do
size
end
+26 -91
View File
@@ -5,22 +5,20 @@ alias Code.Identifier
defprotocol Inspect do
@moduledoc """
The `Inspect` protocol converts an Elixir data structure into an
algebra document.
This documentation refers to implementing the `Inspect` protocol
for your own data structures. To learn more about using inspect,
see `Kernel.inspect/2` and `IO.inspect/2`.
The `Inspect` protocol is responsible for converting any Elixir
data structure into an algebra document. This document is then
formatted, either in pretty printing format or a regular one.
The `inspect/2` function receives the entity to be inspected
followed by the inspecting options, represented by the struct
`Inspect.Opts`. Building of the algebra document is done with
`Inspect.Algebra`.
`Inspect.Opts`.
Inspection is done using the functions available in `Inspect.Algebra`.
## Examples
Many times, inspecting a structure can be implemented in function
of existing entities. For example, here is `MapSet`'s `inspect/2`
of existing entities. For example, here is `MapSet`'s `inspect`
implementation:
defimpl Inspect, for: MapSet do
@@ -39,8 +37,8 @@ defprotocol Inspect do
other string `">"`.
Since regular strings are valid entities in an algebra document,
an implementation of the `Inspect` protocol may simply return a
string, although that will devoid it of any pretty-printing.
an implementation of inspect may simply return a string,
although that will devoid it of any pretty-printing.
## Error handling
@@ -48,30 +46,12 @@ defprotocol Inspect do
Elixir will raise an `ArgumentError` error and will automatically fall back
to a raw representation for printing the structure.
You can however access the underlying error by invoking the `Inspect`
implementation directly. For example, to test `Inspect.MapSet` above,
You can however access the underlying error by invoking the Inspect
implementation directly. For example, to test Inspect.MapSet above,
you can invoke it as:
Inspect.MapSet.inspect(MapSet.new(), %Inspect.Opts{})
## Deriving
The `Inspect` protocol can be derived to hide certain fields from
structs, so they don't show up in logs, inspects and similar. This
is especially useful for fields containing private information.
The options `:only` and `:except` can be used with `@derive` to
specify which fields should and should not appear in the
algebra document:
defmodule User do
@derive {Inspect, only: [:id, :name]}
defstruct [:id, :name, :address]
end
inspect(%User{id: 1, name: "Homer", address: "742 Evergreen Terrace"})
#=> #User<id: 1, name: "Homer", ...>
"""
# Handle structs in Any
@@ -323,25 +303,26 @@ end
defimpl Inspect, for: Function do
def inspect(function, _opts) do
fun_info = Function.info(function)
fun_info = :erlang.fun_info(function)
mod = fun_info[:module]
name = fun_info[:name]
cond do
fun_info[:type] == :external and fun_info[:env] == [] ->
inspected_as_atom = Identifier.inspect_as_atom(mod)
inspected_as_function = Identifier.inspect_as_function(name)
"&#{inspected_as_atom}.#{inspected_as_function}/#{fun_info[:arity]}"
if fun_info[:type] == :external and fun_info[:env] == [] do
inspected_as_atom = Identifier.inspect_as_atom(mod)
inspected_as_function = Identifier.inspect_as_function(name)
"&#{inspected_as_atom}.#{inspected_as_function}/#{fun_info[:arity]}"
else
case Atom.to_charlist(mod) do
'elixir_compiler_' ++ _ ->
if function_exported?(mod, :__RELATIVE__, 0) do
"#Function<#{uniq(fun_info)} in file:#{mod.__RELATIVE__}>"
else
default_inspect(mod, fun_info)
end
match?('elixir_compiler_' ++ _, Atom.to_charlist(mod)) ->
if function_exported?(mod, :__RELATIVE__, 0) do
"#Function<#{uniq(fun_info)} in file:#{mod.__RELATIVE__}>"
else
_ ->
default_inspect(mod, fun_info)
end
true ->
default_inspect(mod, fun_info)
end
end
end
@@ -390,39 +371,6 @@ defimpl Inspect, for: Reference do
end
defimpl Inspect, for: Any do
defmacro __deriving__(module, struct, options) do
fields =
struct
|> Map.drop([:__exception__, :__struct__])
|> Map.keys()
only = Keyword.get(options, :only, fields)
except = Keyword.get(options, :except, [])
filtered_fields =
fields
|> Enum.reject(&(&1 in except))
|> Enum.filter(&(&1 in only))
inspect_module =
if fields == only and except == [] do
quote(do: Inspect.Map)
else
quote(do: Inspect.Any)
end
quote do
defimpl Inspect, for: unquote(module) do
def inspect(struct, opts) do
map = Map.take(struct, unquote(filtered_fields))
colorless_opts = %{opts | syntax_colors: []}
name = Inspect.Atom.inspect(unquote(module), colorless_opts)
unquote(inspect_module).inspect(map, name, opts)
end
end
end
end
def inspect(%module{} = struct, opts) do
try do
module.__struct__
@@ -439,17 +387,4 @@ defimpl Inspect, for: Any do
end
end
end
def inspect(map, name, opts) do
# 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 = :maps.to_list(map) ++ ["..."]
open = color("#" <> name <> "<", :map, opts)
sep = color(",", :map, opts)
close = color(">", :map, opts)
container_doc(open, map, close, opts, &Inspect.List.keyword/2, separator: sep, break: :strict)
end
end
+73 -78
View File
@@ -1,6 +1,6 @@
defmodule Inspect.Opts do
@moduledoc """
Defines the options used by the `Inspect` protocol.
Defines the Inspect.Opts used by the Inspect protocol.
The following fields are available:
@@ -15,8 +15,8 @@ defmodule Inspect.Opts do
When the default `:infer`, the binary will be printed as a string if it
is printable, otherwise in bit syntax.
* `:charlists` - when `:as_charlists` all lists will be printed as charlists,
non-printable elements will be escaped.
* `:charlists` - when `:as_charlists` all lists will be printed as char
lists, non-printable elements will be escaped.
When `:as_lists` all lists will be printed as lists.
@@ -25,19 +25,16 @@ defmodule Inspect.Opts do
* `:limit` - limits the number of items that are printed for tuples,
bitstrings, maps, lists and any other collection of items. It does not
apply to strings nor charlists and defaults to 50. If you don't want to limit
the number of items to a particular number, use `:infinity`.
apply to strings nor charlists and defaults to 50.
* `:printable_limit` - limits the number of bytes that are printed for strings
and charlists. Defaults to 4096. If you don't want to limit the number of items
to a particular number, use `:infinity`.
and char lists. Defaults to 4096.
* `:pretty` - if set to `true` enables pretty printing, defaults to `false`.
* `:width` - defaults to 80 characters, used when pretty is `true` or when
printing to IO devices. Set to 0 to force each item to be printed on its
own line. If you don't want to limit the number of items to a particular
number, use `:infinity`.
own line.
* `:base` - prints integers as `:binary`, `:octal`, `:decimal`, or `:hex`,
defaults to `:decimal`. When inspecting binaries any `:base` other than
@@ -104,7 +101,7 @@ defmodule Inspect.Algebra do
additions, like support for binary nodes and a break mode that
maximises use of horizontal space.
iex> Inspect.Algebra.empty()
iex> Inspect.Algebra.empty
:doc_nil
iex> "foo"
@@ -113,7 +110,7 @@ defmodule Inspect.Algebra do
With the functions in this module, we can concatenate different
elements together and render them:
iex> doc = Inspect.Algebra.concat(Inspect.Algebra.empty(), "foo")
iex> doc = Inspect.Algebra.concat(Inspect.Algebra.empty, "foo")
iex> Inspect.Algebra.format(doc, 80)
["foo"]
@@ -148,7 +145,7 @@ defmodule Inspect.Algebra do
## Implementation details
The implementation of `Inspect.Algebra` is based on the Strictly Pretty
The implementation of Inspect.Algebra is based on the Strictly Pretty
paper by [Lindig][0] which builds on top of previous pretty printing
algorithms but is tailored to strict languages, such as Elixir.
The core idea in the paper is the use of explicit document groups which
@@ -232,6 +229,19 @@ defmodule Inspect.Algebra do
quote do: {:doc_color, unquote(doc), unquote(color)}
end
defmacrop is_doc(doc) do
if Macro.Env.in_guard?(__CALLER__) do
do_is_doc(doc)
else
var = quote(do: doc)
quote do
unquote(var) = unquote(doc)
unquote(do_is_doc(var))
end
end
end
@docs [
:doc_string,
:doc_cons,
@@ -244,9 +254,12 @@ defmodule Inspect.Algebra do
:doc_collapse
]
defguard is_doc(doc)
when is_binary(doc) or doc in [:doc_nil, :doc_line] or
(is_tuple(doc) and elem(doc, 0) in @docs)
defp do_is_doc(doc) do
quote do
is_binary(unquote(doc)) or unquote(doc) in [:doc_nil, :doc_line] or
(is_tuple(unquote(doc)) and elem(unquote(doc), 0) in unquote(@docs))
end
end
# Elixir + Inspect.Opts conveniences
@@ -263,6 +276,8 @@ defmodule Inspect.Algebra do
Inspect.inspect(struct, opts)
rescue
caught_exception ->
stacktrace = System.stacktrace()
# Because we try to raise a nice error message in case
# we can't inspect a struct, there is a chance the error
# message itself relies on the struct being printed, so
@@ -287,7 +302,7 @@ defmodule Inspect.Algebra do
if opts.safe do
Inspect.inspect(exception, opts)
else
reraise(exception, __STACKTRACE__)
reraise(exception, stacktrace)
end
after
Process.delete(:inspect_trap)
@@ -312,7 +327,7 @@ defmodule Inspect.Algebra do
attempts to put as much as possible on the same line. If they are not simple,
only one entry is shown per line if they do not fit.
The limit in the given `inspect_opts` is respected and when reached this
The limit in the given `Inspect.Opts` is respected and when reached this
function stops processing and outputs `"..."` instead.
## Options
@@ -324,30 +339,25 @@ defmodule Inspect.Algebra do
## Examples
iex> inspect_opts = %Inspect.Opts{limit: :infinity}
iex> fun = fn i, _opts -> to_string(i) end
iex> doc = Inspect.Algebra.container_doc("[", Enum.to_list(1..5), "]", inspect_opts, fun)
iex> Inspect.Algebra.format(doc, 5) |> IO.iodata_to_binary()
iex> doc = Inspect.Algebra.container_doc("[", Enum.to_list(1..5), "]",
...> %Inspect.Opts{limit: :infinity}, fn i, _opts -> to_string(i) end)
iex> Inspect.Algebra.format(doc, 5) |> IO.iodata_to_binary
"[1,\n 2,\n 3,\n 4,\n 5]"
iex> inspect_opts = %Inspect.Opts{limit: 3}
iex> fun = fn i, _opts -> to_string(i) end
iex> doc = Inspect.Algebra.container_doc("[", Enum.to_list(1..5), "]", inspect_opts, fun)
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary()
iex> doc = Inspect.Algebra.container_doc("[", Enum.to_list(1..5), "]",
...> %Inspect.Opts{limit: 3}, fn i, _opts -> to_string(i) end)
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary
"[1, 2, 3, ...]"
iex> inspect_opts = %Inspect.Opts{limit: 3}
iex> fun = fn i, _opts -> to_string(i) end
iex> opts = [separator: "!"]
iex> doc = Inspect.Algebra.container_doc("[", Enum.to_list(1..5), "]", inspect_opts, fun, opts)
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary()
iex> doc = Inspect.Algebra.container_doc("[", Enum.to_list(1..5), "]",
...> %Inspect.Opts{limit: 3}, fn i, _opts -> to_string(i) end, separator: "!")
iex> Inspect.Algebra.format(doc, 20) |> IO.iodata_to_binary
"[1! 2! 3! ...]"
"""
@doc since: "1.6.0"
@spec container_doc(t, [any], t, Inspect.Opts.t(), (term, Inspect.Opts.t() -> t), keyword()) ::
t
def container_doc(left, collection, right, inspect_opts, fun, opts \\ [])
def container_doc(left, collection, right, inspect, fun, opts \\ [])
when is_doc(left) and is_list(collection) and is_doc(right) and is_function(fun, 2) and
is_list(opts) do
case collection do
@@ -359,7 +369,7 @@ defmodule Inspect.Algebra do
separator = Keyword.get(opts, :separator, @container_separator)
{docs, simple?} =
container_each(collection, inspect_opts.limit, inspect_opts, fun, [], break == :maybe)
container_each(collection, inspect.limit, inspect, fun, [], break == :maybe)
flex? = simple? or break == :flex
docs = fold_doc(docs, &join(&1, &2, flex?, separator))
@@ -410,16 +420,14 @@ defmodule Inspect.Algebra do
defp simple?(:doc_nil), do: true
defp simple?(other), do: is_binary(other)
# TODO: Remove on 2.0
@doc false
@deprecated "Use a combination of concat/2 and nest/2 instead"
# TODO: Deprecate on Elixir v1.8
def surround(left, doc, right) when is_doc(left) and is_doc(doc) and is_doc(right) do
concat(concat(left, nest(doc, 1)), right)
end
# TODO: Remove on 2.0
@doc false
@deprecated "Use Inspect.Algebra.container_doc/6 instead"
# TODO: Deprecate on Elixir v1.8
def surround_many(
left,
docs,
@@ -439,7 +447,7 @@ defmodule Inspect.Algebra do
## Examples
iex> Inspect.Algebra.empty()
iex> Inspect.Algebra.empty
:doc_nil
"""
@@ -474,7 +482,6 @@ defmodule Inspect.Algebra do
["olá", " ", "mundo"]
"""
@doc since: "1.6.0"
@spec string(String.t()) :: doc_string
def string(string) when is_binary(string) do
doc_string(string, String.length(string))
@@ -513,7 +520,6 @@ defmodule Inspect.Algebra do
@doc ~S"""
Colors a document if the `color_key` has a color in the options.
"""
@doc since: "1.4.0"
@spec color(t, Inspect.Opts.color_key(), Inspect.Opts.t()) :: doc_color
def color(doc, color_key, %Inspect.Opts{syntax_colors: syntax_colors}) when is_doc(doc) do
if precolor = Keyword.get(syntax_colors, color_key) do
@@ -544,7 +550,7 @@ defmodule Inspect.Algebra do
["hello", "\n ", "world"]
"""
@spec nest(t, non_neg_integer | :cursor | :reset, :always | :break) :: doc_nest
@spec nest(t, non_neg_integer) :: doc_nest
def nest(doc, level, mode \\ :always)
def nest(doc, :cursor, mode) when is_doc(doc) and mode in [:always, :break] do
@@ -598,7 +604,6 @@ defmodule Inspect.Algebra do
Collapse any new lines and whitespace following this
node, emitting up to `max` new lines.
"""
@doc since: "1.6.0"
@spec collapse_lines(pos_integer) :: doc_collapse
def collapse_lines(max) when is_integer(max) and max > 0 do
doc_collapse(max)
@@ -645,8 +650,7 @@ defmodule Inspect.Algebra do
})
"""
@doc since: "1.6.0"
@spec next_break_fits(t, :enabled | :disabled) :: doc_fits
@spec next_break_fits(t) :: doc_fits
def next_break_fits(doc, mode \\ @next_break_fits)
when is_doc(doc) and mode in [:enabled, :disabled] do
doc_fits(doc, mode)
@@ -655,7 +659,6 @@ defmodule Inspect.Algebra do
@doc """
Forces the current group to be unfit.
"""
@doc since: "1.6.0"
@spec force_unfit(t) :: doc_force
def force_unfit(doc) when is_doc(doc) do
doc_force(doc)
@@ -689,7 +692,6 @@ defmodule Inspect.Algebra do
This function is used by `container_doc/4` and friends to the
maximum number of entries on the same line.
"""
@doc since: "1.6.0"
@spec flex_break(binary) :: doc_break
def flex_break(string \\ " ") when is_binary(string) do
doc_break(string, :flex)
@@ -702,7 +704,6 @@ defmodule Inspect.Algebra do
This function is used by `container_doc/6` and friends
to the maximum number of entries on the same line.
"""
@doc since: "1.6.0"
@spec flex_glue(t, binary, t) :: t
def flex_glue(doc1, break_string \\ " ", doc2) when is_binary(break_string) do
concat(doc1, concat(flex_break(break_string), doc2))
@@ -741,31 +742,29 @@ defmodule Inspect.Algebra do
## Examples
iex> doc =
...> Inspect.Algebra.group(
...> Inspect.Algebra.concat(
...> Inspect.Algebra.group(
...> Inspect.Algebra.concat(
...> "Hello,",
...> Inspect.Algebra.concat(
...> Inspect.Algebra.break(),
...> "A"
...> )
...> )
...> ),
iex> doc = Inspect.Algebra.group(
...> Inspect.Algebra.concat(
...> Inspect.Algebra.group(
...> Inspect.Algebra.concat(
...> Inspect.Algebra.break(),
...> "B"
...> "Hello,",
...> Inspect.Algebra.concat(
...> Inspect.Algebra.break,
...> "A"
...> )
...> )
...> ),
...> Inspect.Algebra.concat(
...> Inspect.Algebra.break,
...> "B"
...> )
...> )
...> ))
iex> Inspect.Algebra.format(doc, 80)
["Hello,", " ", "A", " ", "B"]
iex> Inspect.Algebra.format(doc, 6)
["Hello,", "\n", "A", "\n", "B"]
"""
@spec group(t, :self | :inherit) :: doc_group
@spec group(t) :: doc_group
def group(doc, mode \\ :self) when is_doc(doc) do
doc_group(doc, mode)
end
@@ -790,19 +789,16 @@ defmodule Inspect.Algebra do
## Examples
iex> doc =
...> Inspect.Algebra.concat(
...> Inspect.Algebra.concat(
...> "Hughes",
...> Inspect.Algebra.line()
...> ),
...> "Wadler"
...> )
iex> Inspect.Algebra.format(doc, 80)
["Hughes", "\n", "Wadler"]
iex> doc = Inspect.Algebra.concat(
...> Inspect.Algebra.concat(
...> "Hughes",
...> Inspect.Algebra.line()
...> ), "Wadler"
...> )
iex> Inspect.Algebra.format(doc, 80)
["Hughes", "\n", "Wadler"]
"""
@doc since: "1.6.0"
@spec line() :: t
def line(), do: :doc_line
@@ -831,10 +827,9 @@ defmodule Inspect.Algebra do
## Examples
iex> docs = ["A", "B", "C"]
iex> docs =
...> Inspect.Algebra.fold_doc(docs, fn doc, acc ->
...> Inspect.Algebra.concat([doc, "!", acc])
...> end)
iex> docs = Inspect.Algebra.fold_doc(docs, fn(doc, acc) ->
...> Inspect.Algebra.concat([doc, "!", acc])
...> end)
iex> Inspect.Algebra.format(docs, 80)
["A", "!", "B", "!", "C"]
+16 -19
View File
@@ -1,15 +1,6 @@
defmodule Integer do
@moduledoc """
Functions for working with integers.
Some functions that work on integers are found in `Kernel`:
* `abs/1`
* `div/2`
* `max/2`
* `min/2`
* `rem/2`
"""
import Bitwise
@@ -81,7 +72,6 @@ defmodule Integer do
-2
"""
@doc since: "1.4.0"
@spec mod(integer, neg_integer | pos_integer) :: integer
def mod(dividend, divisor) do
remainder = rem(dividend, divisor)
@@ -115,7 +105,6 @@ defmodule Integer do
-50
"""
@doc since: "1.4.0"
@spec floor_div(integer, neg_integer | pos_integer) :: integer
def floor_div(dividend, divisor) do
if dividend * divisor < 0 and rem(dividend, divisor) != 0 do
@@ -149,7 +138,10 @@ defmodule Integer do
do_digits(integer, base, [])
end
defp do_digits(integer, base, acc) when abs(integer) < base, do: [integer | acc]
defp do_digits(digit, base, []) when abs(digit) < base, do: [digit]
defp do_digits(digit, base, []) when digit == -base, do: [-1, 0]
defp do_digits(base, base, []), do: [1, 0]
defp do_digits(0, _base, acc), do: acc
defp do_digits(integer, base, acc),
do: do_digits(div(integer, base), base, [rem(integer, base) | acc])
@@ -158,7 +150,7 @@ defmodule Integer do
Returns the integer represented by the ordered `digits`.
An optional `base` value may be provided representing the radix for the `digits`.
Base has to be an integer greater than or equal to `2`.
Base has to be an integer greater or equal than `2`.
## Examples
@@ -177,6 +169,10 @@ defmodule Integer do
do_undigits(digits, base, 0)
end
defp do_undigits([], _base, 0), do: 0
defp do_undigits([digit], base, 0) when is_integer(digit) and digit < base, do: digit
defp do_undigits([1, 0], base, 0), do: base
defp do_undigits([0 | tail], base, 0), do: do_undigits(tail, base, 0)
defp do_undigits([], _base, acc), do: acc
defp do_undigits([digit | _], base, _) when is_integer(digit) and digit >= base,
@@ -196,7 +192,7 @@ defmodule Integer do
Raises an error if `base` is less than 2 or more than 36.
If you want to convert a string-formatted integer directly to an integer,
If you want to convert a string-formatted integer directly to a integer,
`String.to_integer/1` or `String.to_integer/2` can be used instead.
## Examples
@@ -311,7 +307,7 @@ defmodule Integer do
iex> Integer.to_string(-100, 16)
"-64"
iex> Integer.to_string(882_681_651, 36)
iex> Integer.to_string(882681651, 36)
"ELIXIR"
"""
@@ -360,7 +356,7 @@ defmodule Integer do
iex> Integer.to_charlist(-100, 16)
'-64'
iex> Integer.to_charlist(882_681_651, 36)
iex> Integer.to_charlist(882681651, 36)
'ELIXIR'
"""
@@ -398,7 +394,6 @@ defmodule Integer do
0
"""
@doc since: "1.5.0"
@spec gcd(0, 0) :: 0
@spec gcd(integer, integer) :: pos_integer
def gcd(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
@@ -410,12 +405,14 @@ defmodule Integer do
defp gcd_positive(integer1, integer2), do: gcd_positive(integer2, rem(integer1, integer2))
# TODO: Remove by 2.0
# (hard-deprecated in elixir_dispatch)
@doc false
@deprecated "Use Integer.to_charlist/1 instead"
@spec to_char_list(integer) :: charlist
def to_char_list(integer), do: Integer.to_charlist(integer)
# TODO: Remove by 2.0
# (hard-deprecated in elixir_dispatch)
@doc false
@deprecated "Use Integer.to_charlist/2 instead"
@spec to_char_list(integer, 2..36) :: charlist
def to_char_list(integer, base), do: Integer.to_charlist(integer, base)
end
+13 -13
View File
@@ -36,7 +36,7 @@ defmodule IO do
@type device :: atom | pid
@type nodata :: {:error, term} | :eof
@type chardata :: String.t() | maybe_improper_list(char | chardata, String.t() | [])
@type chardata() :: :unicode.chardata()
defmacrop is_iodata(data) do
quote do
@@ -147,10 +147,10 @@ defmodule IO do
## Examples
IO.write("sample")
IO.write "sample"
#=> sample
IO.write(:stderr, "error")
IO.write :stderr, "error"
#=> error
"""
@@ -183,10 +183,10 @@ defmodule IO do
## Examples
IO.puts("Hello World!")
IO.puts "Hello World!"
#=> Hello World!
IO.puts(:stderr, "error")
IO.puts :stderr, "error"
#=> error
"""
@@ -207,7 +207,7 @@ defmodule IO do
## Examples
stacktrace = [{MyApp, :main, 1, [file: 'my_app.ex', line: 4]}]
IO.warn("variable bar is unused", stacktrace)
IO.warn "variable bar is unused", stacktrace
#=> warning: variable bar is unused
#=> my_app.ex:4: MyApp.main/1
@@ -232,7 +232,7 @@ defmodule IO do
## Examples
IO.warn("variable bar is unused")
IO.warn "variable bar is unused"
#=> warning: variable bar is unused
#=> (iex) evaluator.ex:108: IEx.Evaluator.eval/4
@@ -263,7 +263,7 @@ defmodule IO do
## Examples
IO.inspect(<<0, 1, 2>>, width: 40)
IO.inspect <<0, 1, 2>>, width: 40
Prints:
@@ -271,7 +271,7 @@ defmodule IO do
We can use the `:label` option to decorate the output:
IO.inspect(1..100, label: "a wonderful range")
IO.inspect 1..100, label: "a wonderful range"
Prints:
@@ -283,7 +283,7 @@ defmodule IO do
|> IO.inspect(label: "before")
|> Enum.map(&(&1 * 2))
|> IO.inspect(label: "after")
|> Enum.sum()
|> Enum.sum
Prints:
@@ -374,7 +374,7 @@ defmodule IO do
To display "What is your name?" as a prompt and await user input:
IO.gets("What is your name?\n")
IO.gets "What is your name?\n"
"""
@spec gets(device, chardata | String.Chars.t()) :: chardata | nodata
@@ -403,7 +403,7 @@ defmodule IO do
Here is an example on how we mimic an echo server
from the command line:
Enum.each(IO.stream(:stdio, :line), &IO.write(&1))
Enum.each IO.stream(:stdio, :line), &IO.write(&1)
"""
@spec stream(device, :line | pos_integer) :: Enumerable.t()
@@ -457,7 +457,7 @@ defmodule IO do
"string"
"""
@spec chardata_to_string(chardata) :: String.t()
@spec chardata_to_string(chardata) :: String.t() | no_return
def chardata_to_string(string) when is_binary(string) do
string
end
-27
View File
@@ -170,33 +170,6 @@ defmodule IO.ANSI do
@doc "Sends cursor home."
defsequence(:home, "", "H")
@doc """
Sends cursor to the absolute position specified by `line` and `column`.
Line `0` and column `0` would mean the top left corner.
"""
@spec cursor(non_neg_integer, non_neg_integer) :: String.t()
def cursor(line, column)
when is_integer(line) and line >= 0 and is_integer(column) and column >= 0 do
"\e[#{line};#{column}H"
end
@doc "Sends cursor `lines` up."
@spec cursor_up(pos_integer) :: String.t()
def cursor_up(lines \\ 1) when is_integer(lines) and lines >= 1, do: "\e[#{lines}A"
@doc "Sends cursor `lines` down."
@spec cursor_down(pos_integer) :: String.t()
def cursor_down(lines \\ 1) when is_integer(lines) and lines >= 1, do: "\e[#{lines}B"
@doc "Sends cursor `columns` to the right."
@spec cursor_right(pos_integer) :: String.t()
def cursor_right(columns \\ 1) when is_integer(columns) and columns >= 1, do: "\e[#{columns}C"
@doc "Sends cursor `columns` to the left."
@spec cursor_left(pos_integer) :: String.t()
def cursor_left(columns \\ 1) when is_integer(columns) and columns >= 1, do: "\e[#{columns}D"
@doc "Clears screen."
defsequence(:clear, "2", "J")
+13 -64
View File
@@ -7,15 +7,14 @@ defmodule IO.ANSI.Docs do
@doc """
The default options used by this module.
The supported keys are:
The supported values are:
* `:enabled` - toggles coloring on and off (true)
* `:doc_bold` - bold text (bright)
* `:doc_code` - code blocks (cyan)
* `:doc_headings` - h1, h2, h3, h4, h5, h6 headings (yellow)
* `:doc_metadata` - documentation metadata keys (yellow)
* `:doc_inline_code` - inline code (cyan)
* `:doc_table_heading` - the style for table headings
* `:doc_table_heading` - style for table headings
* `:doc_title` - top level heading (reverse, yellow)
* `:doc_underline` - underlined text (underline)
* `:width` - the width to format the text (80)
@@ -23,14 +22,12 @@ defmodule IO.ANSI.Docs do
Values for the color settings are strings with
comma-separated ANSI values.
"""
@spec default_options() :: keyword
def default_options do
[
enabled: true,
doc_bold: [:bright],
doc_code: [:cyan],
doc_headings: [:yellow],
doc_metadata: [:yellow],
doc_inline_code: [:cyan],
doc_table_heading: [:reverse],
doc_title: [:reverse, :yellow],
@@ -44,7 +41,6 @@ defmodule IO.ANSI.Docs do
See `default_options/0` for docs on the supported options.
"""
@spec print_heading(String.t(), keyword) :: :ok
def print_heading(heading, options \\ []) do
IO.puts(IO.ANSI.reset())
options = Keyword.merge(default_options(), options)
@@ -55,53 +51,12 @@ defmodule IO.ANSI.Docs do
newline_after_block()
end
@doc """
Prints documentation metadata (only `delegate_to`, `deprecated`, `guard`, and `since` for now).
See `default_options/0` for docs on the supported options.
"""
@spec print_metadata(map, keyword) :: :ok
def print_metadata(metadata, options \\ []) when is_map(metadata) do
options = Keyword.merge(default_options(), options)
print_each_metadata(metadata, options) && IO.write("\n")
end
@metadata_filter [:deprecated, :guard, :since]
defp print_each_metadata(metadata, options) do
Enum.reduce(metadata, false, fn
{key, value}, _printed when is_binary(value) and key in @metadata_filter ->
label = metadata_label(key, options)
indent = String.duplicate(" ", length_without_escape(label, 0) + 1)
write_with_wrap([label | String.split(value, @spaces)], options[:width], indent, true)
{key, value}, _printed when is_boolean(value) and key in @metadata_filter ->
IO.puts([metadata_label(key, options), ' ', to_string(value)])
{:delegate_to, {m, f, a}}, _printed ->
label = metadata_label(:delegate_to, options)
IO.puts([label, ' ', Exception.format_mfa(m, f, a)])
_metadata, printed ->
printed
end)
end
defp metadata_label(key, options) do
if options[:enabled] do
"#{color(:doc_metadata, options)}#{key}:#{IO.ANSI.reset()}"
else
"#{key}:"
end
end
@doc """
Prints the documentation body.
In addition to the printing string, takes a set of `options`
defined in `default_options/0`.
In addition to the printing string, takes a set of options
defined in `default_options/1`.
"""
@spec print(String.t(), keyword) :: :ok
def print(doc, options \\ []) do
options = Keyword.merge(default_options(), options)
@@ -431,22 +386,17 @@ defmodule IO.ANSI.Docs do
end
defp generate_table_cell({{{col, length}, width}, :center}) do
ansi_diff = byte_size(col) - length
width = width + ansi_diff
col
|> String.pad_leading(div(width, 2) - div(length, 2) + length)
|> String.pad_trailing(width + 1 - rem(width, 2))
end
defp generate_table_cell({{{col, length}, width}, :right}) do
ansi_diff = byte_size(col) - length
String.pad_leading(col, width + ansi_diff)
defp generate_table_cell({{{col, _length}, width}, :right}) do
String.pad_leading(col, width)
end
defp generate_table_cell({{{col, length}, width}, :left}) do
ansi_diff = byte_size(col) - length
String.pad_trailing(col, width + ansi_diff)
defp generate_table_cell({{{col, _length}, width}, :left}) do
String.pad_trailing(col, width)
end
defp table_line?(line) do
@@ -524,8 +474,7 @@ defmodule IO.ANSI.Docs do
end
defp escape_underlines_in_link(text) do
# Regular expression adapted from https://tools.ietf.org/html/rfc3986#appendix-B
~r{[a-z][a-z0-9\+\-\.]*://\S*}i
~r{https?\S*}
|> Regex.recompile!()
|> Regex.replace(text, &String.replace(&1, "_", "\\_"))
end
@@ -539,9 +488,8 @@ defmodule IO.ANSI.Docs do
# We have four entries: **, *, _ and `.
#
# The first three behave the same while the last one is simpler
# when it comes to delimiters as it ignores spaces and escape
# characters. But, since the first has two characters, we need to
# handle 3 cases:
# when it comes to delimiters. But, since the first has two
# characters, we need to handle 3 cases:
#
# 1. **
# 2. _ and *
@@ -603,7 +551,8 @@ defmodule IO.ANSI.Docs do
end
# An escape is not valid inside `
defp handle_inline(<<?\\, mark, rest::binary>>, limit, buffer, acc, options) when limit != ?` do
defp handle_inline(<<?\\, mark, rest::binary>>, limit, buffer, acc, options)
when not (mark == limit and mark == ?`) do
handle_inline(rest, limit, [mark | buffer], acc, options)
end
-1
View File
@@ -1,7 +1,6 @@
defmodule IO.StreamError do
defexception [:reason, :message]
@impl true
def exception(opts) do
reason = opts[:reason]
formatted = IO.iodata_to_binary(:file.format_error(reason))
+321 -579
View File
File diff suppressed because it is too large Load Diff
+4 -3
View File
@@ -116,9 +116,10 @@ defmodule Kernel.CLI do
exit(reason)
kind, reason ->
print_error(kind, reason, __STACKTRACE__)
stack = System.stacktrace()
print_error(kind, reason, stack)
send(parent, {self(), {:shutdown, 1}})
exit(to_exit(kind, reason, __STACKTRACE__))
exit(to_exit(kind, reason, stack))
else
_ ->
send(parent, {self(), res})
@@ -175,7 +176,7 @@ defmodule Kernel.CLI do
" " <> String.replace(string, "\n", "\n ")
end
@elixir_internals [:elixir, :elixir_aliases, :elixir_expand, :elixir_compiler, :elixir_module] ++
@elixir_internals [:elixir, :elixir_expand, :elixir_compiler, :elixir_module] ++
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map] ++
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_pass, Kernel.ErrorHandler]
+20 -6
View File
@@ -23,19 +23,28 @@ defmodule Kernel.LexicalTracker do
:gen_server.call(to_pid(arg), :remote_dispatches, @timeout)
end
@doc """
Gets the destination the lexical scope is meant to
compile to.
"""
def dest(arg) do
:gen_server.call(to_pid(arg), :dest, @timeout)
end
defp to_pid(pid) when is_pid(pid), do: pid
defp to_pid(mod) when is_atom(mod) do
{set, _} = :elixir_module.data_tables(mod)
:ets.lookup_element(set, {:elixir, :lexical_tracker}, 2)
table = :elixir_module.data_table(mod)
[{_, val}] = :ets.lookup(table, {:elixir, :lexical_tracker})
val
end
# Internal API
# Starts the tracker and returns its PID.
@doc false
def start_link() do
:gen_server.start_link(__MODULE__, :ok, [])
def start_link(dest) do
:gen_server.start_link(__MODULE__, dest, [])
end
@doc false
@@ -116,13 +125,14 @@ defmodule Kernel.LexicalTracker do
# Callbacks
def init(:ok) do
def init(dest) do
state = %{
directives: %{},
references: %{},
compile: %{},
runtime: %{},
structs: %{},
dest: dest,
cache: %{},
file: nil
}
@@ -149,6 +159,10 @@ defmodule Kernel.LexicalTracker do
{:reply, {state.compile, state.runtime}, state}
end
def handle_call(:dest, _from, state) do
{:reply, state.dest, state}
end
def handle_call({:read_cache, key}, _from, %{cache: cache} = state) do
{:reply, :maps.get(key, cache), state}
end
@@ -162,7 +176,7 @@ defmodule Kernel.LexicalTracker do
end
def handle_cast({:remote_struct, module, line}, state) do
state = add_remote_dispatch(state, module, {:__struct__, 0}, line, :compile)
state = add_remote_dispatch(state, module, {:__struct__, 1}, line, :compile)
structs = :maps.put(module, true, state.structs)
{:noreply, %{state | structs: structs}}
end
+22 -68
View File
@@ -14,17 +14,14 @@ defmodule Kernel.ParallelCompiler do
See `Task.async/1` for more information. The task spawned must be
always awaited on by calling `Task.await/1`
"""
@doc since: "1.6.0"
def async(fun) when is_function(fun) do
if parent = :erlang.get(:elixir_compiler_pid) do
file = :erlang.get(:elixir_compiler_file)
dest = :erlang.get(:elixir_compiler_dest)
{:error_handler, error_handler} = :erlang.process_info(self(), :error_handler)
Task.async(fn ->
:erlang.put(:elixir_compiler_pid, parent)
:erlang.put(:elixir_compiler_file, file)
dest != :undefined and :erlang.put(:elixir_compiler_dest, dest)
:erlang.process_flag(:error_handler, error_handler)
fun.()
end)
@@ -45,7 +42,7 @@ defmodule Kernel.ParallelCompiler do
It returns `{:ok, modules, warnings}` or `{:error, errors, warnings}`.
Both errors and warnings are a list of three-element tuples containing
Both errors and warnings are a list of three element tuples containing
the file, line and the formatted error/warning.
## Options
@@ -72,12 +69,10 @@ defmodule Kernel.ParallelCompiler do
`dest`, use `compile_to_path/3` instead.
"""
@doc since: "1.6.0"
def compile(files, options \\ []) when is_list(options) do
spawn_workers(files, :compile, options)
end
@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)
end
@@ -90,7 +85,7 @@ defmodule Kernel.ParallelCompiler do
It returns `{:ok, modules, warnings}` or `{:error, errors, warnings}`.
Both errors and warnings are a list of three-element tuples containing
Both errors and warnings are a list of three element tuples containing
the file, line and the formatted error/warning.
## Options
@@ -102,14 +97,12 @@ defmodule Kernel.ParallelCompiler do
the file, module and the module bytecode
"""
@doc since: "1.6.0"
def require(files, options \\ []) when is_list(options) do
spawn_workers(files, :require, options)
end
# TODO: Remove on 2.0
# TODO: Deprecate on Elixir v1.8
@doc false
@deprecated "Use Kernel.ParallelCompiler.compile/2 instead"
def files(files, options \\ []) when is_list(options) do
case spawn_workers(files, :compile, options) do
{:ok, modules, _} -> modules
@@ -117,9 +110,8 @@ defmodule Kernel.ParallelCompiler do
end
end
# TODO: Remove on 2.0
# TODO: Deprecate on Elixir v1.8
@doc false
@deprecated "Use Kernel.ParallelCompiler.compile_to_path/2 instead"
def files_to_path(files, path, options \\ []) when is_binary(path) and is_list(options) do
case spawn_workers(files, {:compile, path}, options) do
{:ok, modules, _} -> modules
@@ -128,7 +120,7 @@ defmodule Kernel.ParallelCompiler do
end
defp spawn_workers(files, output, options) do
{:module, _} = :code.ensure_loaded(Kernel.ErrorHandler)
true = Code.ensure_loaded?(Kernel.ErrorHandler)
compiler_pid = self()
:elixir_code_server.cast({:reset_warnings, compiler_pid})
schedulers = max(:erlang.system_info(:schedulers_online), 2)
@@ -199,13 +191,11 @@ defmodule Kernel.ParallelCompiler do
case output do
{:compile, path} ->
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
:erlang.put(:elixir_compiler_dest, path)
:elixir_compiler.file_to_path(Path.expand(file), path)
:elixir_compiler.file_to_path(file, path)
:compile ->
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
:erlang.put(:elixir_compiler_dest, dest)
Code.compile_file(file)
:elixir_compiler.file(file, dest)
:require ->
Code.require_file(file)
@@ -214,7 +204,7 @@ defmodule Kernel.ParallelCompiler do
:ok
catch
kind, reason ->
{kind, reason, __STACKTRACE__}
{kind, reason, System.stacktrace()}
end
send(parent, {:file_done, self(), file, result})
@@ -240,35 +230,19 @@ defmodule Kernel.ParallelCompiler do
end
# Queued x, waiting for x: POSSIBLE ERROR! Release processes so we get the failures
# Single entry, just release it.
defp spawn_workers([], [_] = waiting, [_] = queued, result, warnings, state) do
[{_, _, ref, _, _}] = waiting
spawn_workers([{ref, :not_found}], waiting, queued, result, warnings, state)
end
# Multiple entries, try to release modules.
defp spawn_workers([], waiting, queued, result, warnings, state)
when length(waiting) == length(queued) do
# The goal of this function is to find leaves in the dependency graph,
# i.e. to find code that depends on code that we know is not being defined.
without_definition =
pending =
for {pid, _, _, _} <- queued,
entry = waiting_on_without_definition(waiting, pid),
do: entry
# Note we only release modules because those can be rescued. A missing
# struct is a guaranteed compile error, so we never release it and treat
# it exclusively a missing entry/deadlock.
pending =
for {:module, _, ref, on, _} <- without_definition,
{_, _, ref, on, _} = entry,
do: {on, {ref, :not_found}}
# Instead of releasing all files at once, we release them in groups
# based on the module they are waiting on. We pick the module being
# depended on with less edges, as it is the mostly likely source of
# error (for example, someone made a typo). This may not always be
# true though. For example, if there is a macro injecting code into
# true though: for example, if there is a macro injecting code into
# multiple modules and such code becomes faulty, now multiple modules
# are waiting on the same module required by the faulty code. However,
# since we need to pick something to be first, the one with fewer edges
@@ -281,16 +255,8 @@ defmodule Kernel.ParallelCompiler do
spawn_workers(refs, waiting, queued, result, warnings, state)
[] ->
# There is a deadlock. Instead of printing a deadlock, let's release
# structs, as a missing struct error is clearer than a deadlock one.
structs = for {:struct, _, ref, _, _} <- without_definition, do: {ref, :not_found}
if structs != [] do
spawn_workers(structs, waiting, queued, result, warnings, state)
else
errors = handle_deadlock(waiting, queued)
{:error, errors, warnings}
end
errors = handle_deadlock(waiting, queued)
{:error, errors, warnings}
end
end
@@ -390,12 +356,7 @@ defmodule Kernel.ParallelCompiler do
{:file_done, child_pid, file, {kind, reason, stack}} ->
discard_down(child_pid)
print_error(file, kind, reason, stack)
cancel_waiting_timer(queued, child_pid)
queued
|> List.keydelete(child_pid, 0)
|> terminate()
terminate(queued)
{:error, [to_error(file, kind, reason, stack)], warnings}
{:DOWN, ref, :process, _pid, reason} ->
@@ -418,13 +379,9 @@ defmodule Kernel.ParallelCompiler do
defp handle_down(queued, ref, reason) do
case List.keyfind(queued, ref, 1) do
{child_pid, ^ref, file, _timer_ref} ->
{_child, ^ref, file, _timer_ref} ->
print_error(file, :exit, reason, [])
queued
|> List.keydelete(child_pid, 0)
|> terminate()
terminate(queued)
{:error, [to_error(file, :exit, reason, [])]}
_ ->
@@ -438,8 +395,8 @@ defmodule Kernel.ParallelCompiler do
{:current_stacktrace, stacktrace} = Process.info(pid, :current_stacktrace)
Process.exit(pid, :kill)
{kind, ^pid, _, on, _} = List.keyfind(waiting, pid, 1)
description = "deadlocked waiting on #{kind} #{inspect(on)}"
{_kind, ^pid, _, on, _} = List.keyfind(waiting, pid, 1)
description = "deadlocked waiting on module #{inspect(on)}"
error = CompileError.exception(description: description, file: nil, line: nil)
print_error(file, :error, error, stacktrace)
@@ -461,18 +418,15 @@ defmodule Kernel.ParallelCompiler do
IO.puts([" ", String.pad_leading(file, max), " => " | inspect(mod)])
end
IO.puts(
"\nEnsure there are no compile-time dependencies between those files " <>
"and that the modules they reference exist and are correctly named\n"
)
IO.puts("")
for {file, _, description} <- deadlock, do: {Path.absname(file), nil, description}
end
defp terminate(queued) do
for {pid, _, _, _} <- queued, do: Process.exit(pid, :kill)
for {pid, _, _, _} <- queued, do: discard_down(pid)
:ok
for {pid, _, _, _} <- queued do
Process.exit(pid, :kill)
end
end
defp print_error(file, kind, reason, stack) do
+1 -2
View File
@@ -1,8 +1,7 @@
defmodule Kernel.ParallelRequire do
# TODO: Remove on 2.0
# TODO: Deprecate on Elixir v1.8
@moduledoc false
@deprecated "Use Kernel.ParallelCompiler.require/2 instead"
def files(files, callbacks \\ [])
def files(files, callback) when is_function(callback, 1) do
+91 -234
View File
@@ -4,7 +4,7 @@ defmodule Kernel.SpecialForms do
cannot be overridden by the developer.
We define them in this module. Some of these forms are lexical (like
`alias/2`, `case/2`, etc.). The macros `{}/1` and `<<>>/1` are also special
`alias/2`, `case/2`, etc). The macros `{}/1` and `<<>>/1` are also special
forms used to define tuple and binary data structures respectively.
This module also documents macros that return information about Elixir's
@@ -75,7 +75,7 @@ defmodule Kernel.SpecialForms do
defmacro unquote(:%{})(args), do: error!([args])
@doc """
Matches on or builds a struct.
Creates a struct.
A struct is a tagged map that allows developers to provide
default values for keys, tags to be used in polymorphic
@@ -96,25 +96,16 @@ defmodule Kernel.SpecialForms do
%User{} == %{__struct__: User, name: "john", age: 27}
The struct fields can be given when building the struct:
A struct also validates that the given keys are part of the defined
struct. The example below will fail because there is no key
`:full_name` in the `User` struct:
%User{age: 31}
#=> %{__struct__: User, name: "john", age: 31}
Or also on pattern matching to extract values out:
%User{age: age} = user
%User{full_name: "john doe"}
An update operation specific for structs is also available:
%User{user | age: 28}
The advantage of structs is that they validate that the given
keys are part of the defined struct. The example below will fail
because there is no key `:full_name` in the `User` struct:
%User{full_name: "john doe"}
The syntax above will guarantee the given keys are valid at
compilation time and it will guarantee at runtime the given
argument is a struct, failing with `BadStructError` otherwise.
@@ -125,24 +116,6 @@ defmodule Kernel.SpecialForms do
can be used with protocols for polymorphic dispatch. Also
see `Kernel.struct/2` and `Kernel.struct!/2` for examples on
how to create and update structs dynamically.
## Pattern matching on struct names
Besides allowing pattern matching on struct fields, such as:
%User{age: age} = user
Structs also allow pattern matching on the struct name:
%struct_name{} = user
struct_name #=> User
You can also assign the struct name to `_` when you want to
check if something is a struct but you are not interested in
its name:
%_{} = user
"""
defmacro unquote(:%)(struct, map), do: error!([struct, map])
@@ -239,24 +212,6 @@ defmodule Kernel.SpecialForms do
iex> {name, species}
{"Frank", "Walrus"}
The size can be a variable:
iex> name_size = 5
iex> <<name::binary-size(name_size), " the ", species::binary>> = <<"Frank the Walrus">>
iex> {name, species}
{"Frank", "Walrus"}
And the variable can be defined in the match itself (prior to its use):
iex> <<name_size::size(8), name::binary-size(name_size), " the ", species::binary>> = <<5, "Frank the Walrus">>
iex> {name, species}
{"Frank", "Walrus"}
However, the size cannot be defined in the match outside the binary/bitstring match:
{name_size, <<name::binary-size(name_size), _rest::binary>>} = {5, <<"Frank the Walrus">>}
** (CompileError): undefined variable "name_size" in bitstring segment
Failing to specify the size for the non-last causes compilation to fail:
<<name::binary, " the ", species::binary>> = <<"Frank the Walrus">>
@@ -339,7 +294,7 @@ defmodule Kernel.SpecialForms do
Or as a part of function definitions to pattern match:
defmodule ImageTyper do
defmodule ImageTyper
@png_signature <<137::size(8), 80::size(8), 78::size(8), 71::size(8),
13::size(8), 10::size(8), 26::size(8), 10::size(8)>>
@jpg_signature <<255::size(8), 216::size(8)>>
@@ -376,7 +331,7 @@ defmodule Kernel.SpecialForms do
The dot may be used to invoke anonymous functions too:
iex> (fn n -> n end).(7)
iex> (fn(n) -> n end).(7)
7
in which case there is a function on the left hand side.
@@ -391,7 +346,7 @@ defmodule Kernel.SpecialForms do
## Syntax
The right side of `.` may be a word starting with an uppercase letter, which represents
The right side of `.` may be a word starting in upcase, which represents
an alias, a word starting with lowercase or underscore, any valid language
operator or any name wrapped in single- or double-quotes. Those are all valid
examples:
@@ -405,18 +360,21 @@ defmodule Kernel.SpecialForms do
iex> Kernel.+(1, 2)
3
iex> Kernel."+"(1, 2)
iex> Kernel."length"([1, 2, 3])
3
Wrapping the function name in single- or double-quotes is always a
remote call. Therefore `Kernel."Foo"` will attempt to call the function "Foo"
and not return the alias `Kernel.Foo`. This is done by design as module names
are more strict than function names.
iex> Kernel.'+'(1, 2)
3
Note that `Kernel."FUNCTION_NAME"` will be treated as a remote call and not an alias.
This choice was done so every time single- or double-quotes are used, we have
a remote call regardless of the quote contents. This decision is also reflected
in the quoted expressions discussed below.
When the dot is used to invoke an anonymous function there is only one
operand, but it is still written using a postfix notation:
iex> negate = fn n -> -n end
iex> negate = fn(n) -> -n end
iex> negate.(7)
-7
@@ -440,7 +398,15 @@ defmodule Kernel.SpecialForms do
with the name as first argument, some keyword list as metadata as second,
and the list of arguments as third. In this case, the arguments are the
alias `String` and the atom `:downcase`. The second argument in a remote call
is **always** an atom.
is **always** an atom regardless of the literal used in the call:
iex> quote do
...> String."downcase"("FOO")
...> end
{{:., [], [{:__aliases__, [alias: false], [:String]}, :downcase]}, [], ["FOO"]}
The tuple containing `:.` is wrapped in another tuple, which actually
represents the function call, and has `"FOO"` as argument.
In the case of calls to anonymous functions, the inner tuple with the dot
special form has only one argument, reflecting the fact that the operator is
@@ -506,7 +472,7 @@ defmodule Kernel.SpecialForms do
Keyword.values #=> uses MyKeyword.values
Elixir.Keyword.values #=> uses Keyword.values
Notice that calling `alias` without the `:as` option automatically
Notice that calling `alias` without the `as:` option automatically
sets an alias based on the last part of the module. For example:
alias Foo.Bar.Baz
@@ -566,7 +532,7 @@ defmodule Kernel.SpecialForms do
## Alias shortcut
`require/2` also accepts `:as` as an option so it automatically sets
`require/2` also accepts `as:` as an option so it automatically sets
up an alias. Please check `alias/2` for more information.
"""
@@ -704,17 +670,6 @@ defmodule Kernel.SpecialForms do
"""
defmacro __CALLER__, do: error!([])
@doc """
Returns the stacktrace for the currently handled exception.
It is available only in the `catch` and `rescue` clauses of `try/1`
expressions.
To retrieve the stacktrace of the current process, use
`Process.info(self(), :current_stacktrace)` instead.
"""
defmacro __STACKTRACE__, do: error!([])
@doc """
Accesses an already bound variable in match clauses. Also known as the pin operator.
@@ -783,7 +738,7 @@ defmodule Kernel.SpecialForms do
...> end
{:sum, [], [1, 2, 3]}
## Elixir's AST (Abstract Syntax Tree)
## Explanation
Any Elixir code can be represented using Elixir data structures.
The building block of Elixir macros is a tuple with three elements,
@@ -803,35 +758,10 @@ defmodule Kernel.SpecialForms do
function call. The third argument may be an atom, which is
usually a variable (or a local call).
Besides the tuple described above, Elixir has a few literals that
are also part of its AST. Those literals return themselves when
quoted. They are:
:sum #=> Atoms
1 #=> Integers
2.0 #=> Floats
[1, 2] #=> Lists
"strings" #=> Strings
{key, value} #=> Tuples with two elements
Any other value, such as a map or a four-element tuple, must be escaped
(`Macro.escape/1`) before being introduced into an AST.
## Options
* `:unquote` - when `false`, disables unquoting. This means any `unquote`
call will be kept as is in the AST, instead of replaced by the `unquote`
arguments. For example:
iex> quote do
...> unquote("hello")
...> end
"hello"
iex> quote unquote: false do
...> unquote("hello")
...> end
{:unquote, [], ["hello"]}
* `:unquote` - when `false`, disables unquoting. Useful when you have a quote
inside another quote and want to control what quote is able to unquote.
* `:location` - when set to `:keep`, keeps the current line and file from
quote. Read the Stacktrace information section below for more
@@ -848,10 +778,22 @@ defmodule Kernel.SpecialForms do
* `:bind_quoted` - passes a binding to the macro. Whenever a binding is
given, `unquote/1` is automatically disabled.
## Quote literals
Besides the tuple described above, Elixir has a few literals that
when quoted return themselves. They are:
:sum #=> Atoms
1 #=> Integers
2.0 #=> Floats
[1, 2] #=> Lists
"strings" #=> Strings
{key, value} #=> Tuples with two elements
## Quote and macros
`quote/2` is commonly used with macros for code generation. As an exercise,
let's define a macro that multiplies a number by itself (squared). In practice,
let's define a macro that multiplies a number by itself (squared). Note
there is no reason to define such as a macro (and it would actually be
seen as a bad practice), but it is simple enough that it allows us to focus
on the important aspects of quotes and macros:
@@ -914,7 +856,7 @@ defmodule Kernel.SpecialForms do
end
end
Now invoking `squared(my_number.())` as before will print the value just
Now invoking `square(my_number.())` as before will print the value just
once.
In fact, this pattern is so common that most of the times you will want
@@ -990,8 +932,8 @@ defmodule Kernel.SpecialForms do
NoHygiene.interference
a #=> 1
You cannot even access variables defined in the same module unless
you explicitly give it a context:
Note that you cannot even access variables defined in the same
module unless you explicitly give it a context:
defmodule Hygiene do
defmacro write do
@@ -1189,7 +1131,7 @@ defmodule Kernel.SpecialForms do
When using `location: :keep` and invalid arguments are given to
`Sample.add/2`, the stacktrace information will point to the file
and line inside the quote. Without `location: :keep`, the error is
reported to where `defadd` was invoked. `location: :keep` affects
reported to where `defadd` was invoked. Note `location: :keep` affects
only definitions inside the quote.
## Binding and unquote fragments
@@ -1268,61 +1210,37 @@ defmodule Kernel.SpecialForms do
defmacro quote(opts, block), do: error!([opts, block])
@doc """
Unquotes the given expression inside a quoted expression.
This function expects a valid Elixir AST, also known as
quoted expression, as argument. If you would like to `unquote`
any value, such as a map or a four-element tuple, you should
call `Macro.escape/1` before unquoting.
Unquotes the given expression from inside a macro.
## Examples
Imagine the situation you have a quoted expression and
Imagine the situation you have a variable `value` and
you want to inject it inside some quote. The first attempt
would be:
value =
quote do
13
end
value = 13
quote do
sum(1, value, 3)
end
Which would then return:
{:sum, [], [1, {:value, [], Elixir}, 3]}
{:sum, [], [1, {:value, [], quoted}, 3]}
Which is not the expected result. For this, we use `unquote`:
Which is not the expected result. For this, we use unquote:
iex> value =
...> quote do
...> 13
...> end
iex> value = 13
iex> quote do
...> sum(1, unquote(value), 3)
...> end
{:sum, [], [1, 13, 3]}
If you want to unquote a value that is not a quoted expression,
such as a map, you need to call `Macro.escape/1` before:
iex> value = %{foo: :bar}
iex> quote do
...> process_map(unquote(Macro.escape(value)))
...> end
{:process_map, [], [{:%{}, [], [foo: :bar]}]}
If you forget to escape it, Elixir will raise an error
when compiling the code.
"""
defmacro unquote(:unquote)(expr), do: error!([expr])
@doc """
Unquotes the given list expanding its arguments.
Similar to `unquote/1`.
Unquotes the given list expanding its arguments. Similar
to `unquote/1`.
## Examples
@@ -1361,7 +1279,7 @@ defmodule Kernel.SpecialForms do
iex> for n <- [1, 2, 3, 4, 5, 6], rem(n, 2) == 0, do: n
[2, 4, 6]
Generators can also be used to filter as it removes any value
Note generators can also be used to filter as it removes any value
that doesn't match the pattern on the left side of `<-`:
iex> users = [user: "john", admin: "meg", guest: "barbara"]
@@ -1381,7 +1299,7 @@ defmodule Kernel.SpecialForms do
filters or inside the block, are not reflected outside of the
comprehension.
## The `:into` and `:uniq` options
## Into
In the examples above, the result returned by the comprehension was
always a list. The returned result can be configured by passing an
@@ -1401,57 +1319,18 @@ defmodule Kernel.SpecialForms do
String.upcase(line)
end
Similarly, `uniq: true` can also be given to comprehensions to guarantee
the results are only added to the collection if they were not returned
## Uniq
`uniq: true` can also be given to comprehensions to guarantee that
that results are only added to the collection if they were not returned
before. For example:
iex> for x <- [1, 1, 2, 3], uniq: true, do: x * 2
iex> for(x <- [1, 1, 2, 3], uniq: true, do: x * 2)
[2, 4, 6]
iex> for <<x <- "abcabc">>, uniq: true, into: "", do: <<x - 32>>
iex> for(<<x <- "abcabc">>, uniq: true, into: "", do: <<x-32>>)
"ABC"
## The `:reduce` option
While the `:into` option allows us to customize the comprehension behaviour
to a given data type, such as putting all of the values inside a map or inside
a binary, it is not always enough.
For example, imagine that you have a binary with letters where you want to
count how many times each lowercase letter happens, ignoring all uppercase
ones. For instance, for the string `"AbCabCABc"`, we want to return the map
`%{"a" => 1, "b" => 2, "c" => 1}`.
If we were to use `:into`, we would need a data type that computes the
frequency of each element it holds. While there is no such data type in
Elixir, you could implement one yourself.
A simpler option would be to use comprehensions for the mapping and
filtering of letters, and then we invoke `Enum.reduce/3` to build a map,
for example:
iex> letters = for <<x <- "AbCabCABc">>, x in ?a..?z, do: <<x>>
iex> Enum.reduce(letters, %{}, fn x, acc -> Map.update(acc, x, 1, & &1 + 1) end)
%{"a" => 1, "b" => 2, "c" => 1}
While the above is straight-forward, it has the downside of traversing the
data at least twice. If you are expecting long strings as inputs, this can
be quite expensive.
Luckily, comprehensions also support the `:reduce` option, which would allow
us to fuse both steps above into a single step:
iex> for <<x <- "AbCabCABc">>, x in ?a..?z, reduce: %{} do
...> acc -> Map.update(acc, <<x>>, 1, & &1 + 1)
...> end
%{"a" => 1, "b" => 2, "c" => 1}
When the `:reduce` key is given, its value is used as the initial accumulator
and the `do` block must be changed to use `->` clauses, where the left side
of `->` receives the accumulated value of the previous iteration and the
expression on the right side must return the new accumulator value. Once there are no more
elements, the final accumulated value is returned. If there are no elements
at all, then the initial accumulator value is returned.
"""
defmacro for(args), do: error!([args])
@@ -1462,9 +1341,8 @@ defmodule Kernel.SpecialForms do
iex> opts = %{width: 10, height: 15}
iex> with {:ok, width} <- Map.fetch(opts, :width),
...> {:ok, height} <- Map.fetch(opts, :height) do
...> {:ok, width * height}
...> end
...> {:ok, height} <- Map.fetch(opts, :height),
...> do: {:ok, width * height}
{:ok, 150}
If all clauses match, the `do` block is executed, returning its result.
@@ -1472,55 +1350,36 @@ defmodule Kernel.SpecialForms do
iex> opts = %{width: 10}
iex> with {:ok, width} <- Map.fetch(opts, :width),
...> {:ok, height} <- Map.fetch(opts, :height) do
...> {:ok, width * height}
...> end
...> {:ok, height} <- Map.fetch(opts, :height),
...> do: {:ok, width * height}
:error
Guards can be used in patterns as well:
iex> users = %{"melany" => "guest", "bob" => :admin}
iex> with {:ok, role} when not is_binary(role) <- Map.fetch(users, "bob") do
...> {:ok, to_string(role)}
...> end
iex> with {:ok, role} when not is_binary(role) <- Map.fetch(users, "bob"),
...> do: {:ok, to_string(role)}
{:ok, "admin"}
As in `for/1`, variables bound inside `with/1` won't leak.
Expressions without `<-` may also be used in clauses. For instance,
you can perform regular matches with the `=` operator:
As in `for/1`, variables bound inside `with/1` won't leak;
"bare expressions" may also be inserted between the clauses:
iex> width = nil
iex> opts = %{width: 10, height: 15}
iex> with {:ok, width} <- Map.fetch(opts, :width),
...> double_width = width * 2,
...> {:ok, height} <- Map.fetch(opts, :height) do
...> {:ok, double_width * height}
...> end
...> {:ok, height} <- Map.fetch(opts, :height),
...> do: {:ok, double_width * height}
{:ok, 300}
iex> width
nil
The behaviour of any expression in a clause is the same as outside.
For example, `=` will raise a `MatchError` instead of returning the
non-matched value:
Note that if a "bare expression" fails to match, it will raise a `MatchError`
instead of returning the non-matched value:
with :foo = :bar, do: :ok
#=> ** (MatchError) no match of right hand side value: :bar
As with any other function or macro call in Elixir, explicit parens can
also be used around the arguments before the `do`/`end` block:
iex> opts = %{width: 10, height: 15}
iex> with(
...> {:ok, width} <- Map.fetch(opts, :width),
...> {:ok, height} <- Map.fetch(opts, :height)
...> ) do
...> {:ok, width * height}
...> end
{:ok, 150}
The choice between parens and no parens is a matter of preference.
An `else` option can be given to modify what is being returned from
`with` in the case of a failed match:
@@ -1534,8 +1393,7 @@ defmodule Kernel.SpecialForms do
...> end
{:error, :wrong_data}
If an `else` block is used and there are no matching clauses, a `WithClauseError`
exception is raised.
If there is no matching `else` condition, then a `WithClauseError` exception is raised.
"""
defmacro with(args), do: error!([args])
@@ -1824,9 +1682,8 @@ defmodule Kernel.SpecialForms do
the expression. `catch`, `rescue`, and `else` clauses work based on
pattern matching (similar to the `case` special form).
Calls inside `try/1` are not tail recursive since the VM needs to keep
the stacktrace in case an exception happens. To retrieve the stacktrace,
access `__STACKTRACE__/0` inside the `rescue` or `catch` clause.
Note that calls inside `try/1` are not tail recursive since the VM
needs to keep the stacktrace in case an exception happens.
## `rescue` clauses
@@ -2096,10 +1953,10 @@ defmodule Kernel.SpecialForms do
## Examples
receive do
{:selector, number, name} when is_integer(number) ->
name
name when is_atom(name) ->
name
{:selector, i, value} when is_integer(i) ->
value
value when is_atom(value) ->
value
_ ->
IO.puts :stderr, "Unexpected message received"
end
@@ -2108,10 +1965,10 @@ defmodule Kernel.SpecialForms do
received after the given timeout period, specified in milliseconds:
receive do
{:selector, number, name} when is_integer(number) ->
name
name when is_atom(name) ->
name
{:selector, i, value} when is_integer(i) ->
value
value when is_atom(value) ->
value
_ ->
IO.puts :stderr, "Unexpected message received"
after
@@ -2124,13 +1981,13 @@ defmodule Kernel.SpecialForms do
one of the allowed values:
* `:infinity` - the process should wait indefinitely for a matching
message, this is the same as not using the after clause
message, this is the same as not using a timeout
* `0` - if there is no matching message in the mailbox, the timeout
will occur immediately
* positive integer smaller than or equal to `4_294_967_295` (`0xFFFFFFFF`
in hexadecimal notation) - it should be possible to represent the timeout
* positive integer smaller than `4_294_967_295` (`0xFFFFFFFF`
in hex notation) - it should be possible to represent the timeout
value as an unsigned 32-bit integer.
## Variables handling
File diff suppressed because it is too large Load Diff
+25 -49
View File
@@ -125,8 +125,8 @@ defmodule Kernel.Utils do
RuntimeError.exception(msg)
end
def raise(module) when is_atom(module) do
module.exception([])
def raise(atom) when is_atom(atom) do
atom.exception([])
end
def raise(%_{__exception__: true} = exception) do
@@ -157,7 +157,7 @@ defmodule Kernel.Utils do
macro.
Secondly, if the expression is being used outside of a guard, we want to unquote
`value`, but only once, and then re-use the unquoted form throughout the expression.
`value`––but only once, and then re-use the unquoted form throughout the expression.
This helper does exactly that: takes the AST for an expression and a list of
variable references it should be aware of, and rewrites it into a new expression
@@ -170,17 +170,14 @@ defmodule Kernel.Utils do
> variable_references = [value: Elixir]
> Kernel.Utils.defguard(expression, variable_references) |> Macro.to_string |> IO.puts
case Macro.Env.in_guard?(__CALLER__) do
true ->
quote do
is_integer(unquote(value)) and rem(unquote(value), 2) == 0
end
false ->
quote do
value = unquote(value)
is_integer(value) and rem(value, 2) == 0
end
case Macro.Env.in_guard? __CALLER__ do
true -> quote do
is_integer(unquote(value)) and rem(unquote(value), 2) == 0
end
false -> quote do
value = unquote(value)
is_integer(value) and rem(value, 2) == 0
end
end
"""
@@ -191,8 +188,7 @@ defmodule Kernel.Utils do
@spec defguard([Macro.t()], Macro.t(), Macro.Env.t()) :: Macro.t()
def defguard(args, expr, env) do
{^args, vars} = extract_refs_from_args(args)
env = :elixir_env.with_vars(%{env | context: :guard}, vars)
{expr, _scope} = :elixir_expand.expand(expr, env)
{expr, _scope} = :elixir_expand.expand(expr, %{env | context: :guard, vars: vars})
quote do
case Macro.Env.in_guard?(__CALLER__) do
@@ -204,8 +200,8 @@ defmodule Kernel.Utils do
defp extract_refs_from_args(args) do
Macro.postwalk(args, [], fn
{ref, meta, context} = var, acc when is_atom(ref) and is_atom(context) ->
{var, [{ref, var_context(meta, context)} | acc]}
{ref, _meta, context} = var, acc when is_atom(ref) and is_atom(context) ->
{var, [{ref, context} | acc]}
node, acc ->
{node, acc}
@@ -215,8 +211,8 @@ defmodule Kernel.Utils do
# Finds every reference to `refs` in `guard` and wraps them in an unquote.
defp unquote_every_ref(guard, refs) do
Macro.postwalk(guard, fn
{ref, meta, context} = var when is_atom(ref) and is_atom(context) ->
case {ref, var_context(meta, context)} in refs do
{ref, _meta, context} = var when is_atom(ref) and is_atom(context) ->
case {ref, context} in refs do
true -> literal_unquote(var)
false -> var
end
@@ -228,36 +224,23 @@ defmodule Kernel.Utils do
# Prefaces `guard` with unquoted versions of `refs`.
defp unquote_refs_once(guard, refs) do
{guard, used_refs} =
Macro.postwalk(guard, %{}, fn
{ref, meta, context} = var, acc when is_atom(ref) and is_atom(context) ->
pair = {ref, var_context(meta, context)}
case pair in refs do
true ->
case acc do
%{^pair => {new_var, _}} ->
{new_var, acc}
%{} ->
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
false ->
{var, acc}
{_, used_refs} =
Macro.postwalk(guard, [], fn
{ref, _meta, context} = var, acc when is_atom(ref) and is_atom(context) ->
case {ref, context} in refs and {ref, context} not in acc do
true -> {var, [{ref, context} | acc]}
false -> {var, acc}
end
node, acc ->
{node, acc}
end)
all_used = for ref <- :lists.reverse(refs), used = :maps.get(ref, used_refs, nil), do: used
{vars, exprs} = :lists.unzip(all_used)
vars = for {ref, context} <- :lists.reverse(used_refs), do: {ref, [], context}
exprs = for var <- vars, do: literal_unquote(var)
quote do
{unquote_splicing(vars)} = {unquote_splicing(Enum.map(exprs, &literal_unquote/1))}
{unquote_splicing(vars)} = {unquote_splicing(exprs)}
unquote(guard)
end
end
@@ -269,11 +252,4 @@ defmodule Kernel.Utils do
defp literal_unquote(ast) do
{:unquote, [], List.wrap(ast)}
end
defp var_context(meta, kind) do
case :lists.keyfind(:counter, 1, meta) do
{:counter, counter} -> counter
false -> kind
end
end
end
+45 -82
View File
@@ -20,22 +20,10 @@ defmodule Keyword do
iex> [{:active, :once}]
[active: :once]
The two syntaxes are completely equivalent. Like atoms, keywords
must be composed of Unicode characters such as letters, numbers,
underscore, and `@`. If the keyword has a character that does not
belong to the category above, such as spaces, you can wrap it in
quotes:
iex> ["exit on close": true]
["exit on close": true]
Wrapping a keyword in quotes does not make it a string. Keywords are
always atoms. If you use quotes when all characters are a valid part
of a keyword without quotes, Elixir will warn.
Note that when keyword lists are passed as the last argument to a function,
if the short-hand syntax is used then the square brackets around the keyword list
can be omitted as well. For example, the following:
The two syntaxes are completely equivalent. Note that when keyword
lists are passed as the last argument to a function, if the short-hand
syntax is used then the square brackets around the keyword list can
be omitted as well. For example, the following:
String.split("1-0", "-", trim: true, parts: 2)
@@ -84,7 +72,7 @@ defmodule Keyword do
iex> Keyword.keyword?([])
true
iex> Keyword.keyword?(a: 1)
iex> Keyword.keyword?([a: 1])
true
iex> Keyword.keyword?([{Foo, 1}])
true
@@ -145,7 +133,7 @@ defmodule Keyword do
## Examples
iex> Keyword.new([:a, :b], fn x -> {x, x} end)
iex> Keyword.new([:a, :b], fn(x) -> {x, x} end)
[a: :a, b: :b]
"""
@@ -322,7 +310,7 @@ defmodule Keyword do
{1, []}
"""
@spec get_and_update!(t, key, (value -> {get, value})) :: {get, t} when get: term
@spec get_and_update!(t, key, (value -> {get, value})) :: {get, t} | no_return when get: term
def get_and_update!(keywords, key, fun) do
get_and_update!(keywords, key, fun, [])
end
@@ -382,7 +370,7 @@ defmodule Keyword do
** (KeyError) key :b not found in: [a: 1]
"""
@spec fetch!(t, key) :: value
@spec fetch!(t, key) :: value | no_return
def fetch!(keywords, key) when is_list(keywords) and is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{^key, value} -> value
@@ -420,9 +408,9 @@ defmodule Keyword do
## Examples
iex> Keyword.keys(a: 1, b: 2)
iex> Keyword.keys([a: 1, b: 2])
[:a, :b]
iex> Keyword.keys(a: 1, b: 2, a: 3)
iex> Keyword.keys([a: 1, b: 2, a: 3])
[:a, :b, :a]
"""
@@ -438,9 +426,9 @@ defmodule Keyword do
## Examples
iex> Keyword.values(a: 1, b: 2)
iex> Keyword.values([a: 1, b: 2])
[1, 2]
iex> Keyword.values(a: 1, b: 2, a: 3)
iex> Keyword.values([a: 1, b: 2, a: 3])
[1, 2, 3]
"""
@@ -468,23 +456,19 @@ defmodule Keyword do
"""
@spec delete(t, key, value) :: t
def delete(keywords, key, value) when is_list(keywords) and is_atom(key) do
case :lists.keymember(key, 1, keywords) do
true -> delete_key_value(keywords, key, value)
_ -> keywords
end
delete_key_value(keywords, key, value, _deleted? = false)
catch
:not_deleted -> keywords
end
defp delete_key_value([{key, value} | tail], key, value) do
delete_key_value(tail, key, value)
end
defp delete_key_value([{key, value} | rest], key, value, _deleted?),
do: delete_key_value(rest, key, value, true)
defp delete_key_value([{_, _} = pair | tail], key, value) do
[pair | delete_key_value(tail, key, value)]
end
defp delete_key_value([{_, _} = pair | rest], key, value, deleted?),
do: [pair | delete_key_value(rest, key, value, deleted?)]
defp delete_key_value([], _key, _value) do
[]
end
defp delete_key_value([], _key, _value, _deleted? = true), do: []
defp delete_key_value([], _key, _value, _deleted? = false), do: throw(:not_deleted)
@doc """
Deletes the entries in the keyword list for a specific `key`.
@@ -506,23 +490,18 @@ defmodule Keyword do
@spec delete(t, key) :: t
@compile {:inline, delete: 2}
def delete(keywords, key) when is_list(keywords) and is_atom(key) do
case :lists.keymember(key, 1, keywords) do
true -> delete_key(keywords, key)
_ -> keywords
end
delete_key(keywords, key, _deleted? = false)
catch
:not_deleted -> keywords
end
defp delete_key([{key, _} | tail], key) do
delete_key(tail, key)
end
defp delete_key([{key, _} | rest], key, _deleted?), do: delete_key(rest, key, true)
defp delete_key([{_, _} = pair | tail], key) do
[pair | delete_key(tail, key)]
end
defp delete_key([{_, _} = pair | rest], key, deleted?),
do: [pair | delete_key(rest, key, deleted?)]
defp delete_key([], _key) do
[]
end
defp delete_key([], _key, _deleted? = true), do: []
defp delete_key([], _key, _deleted? = false), do: throw(:not_deleted)
@doc """
Deletes the first entry in the keyword list for a specific `key`.
@@ -539,23 +518,14 @@ defmodule Keyword do
"""
@spec delete_first(t, key) :: t
def delete_first(keywords, key) when is_list(keywords) and is_atom(key) do
case :lists.keymember(key, 1, keywords) do
true -> delete_first_key(keywords, key)
_ -> keywords
end
delete_first_key(keywords, key)
catch
:not_deleted -> keywords
end
defp delete_first_key([{key, _} | tail], key) do
tail
end
defp delete_first_key([{_, _} = pair | tail], key) do
[pair | delete_first_key(tail, key)]
end
defp delete_first_key([], _key) do
[]
end
defp delete_first_key([{key, _} | rest], key), do: rest
defp delete_first_key([{_, _} = pair | rest], key), do: [pair | delete_first_key(rest, key)]
defp delete_first_key([], _key), do: throw(:not_deleted)
@doc """
Puts the given `value` under `key`.
@@ -628,7 +598,6 @@ defmodule Keyword do
end
@doc false
@deprecated "Use Keyword.fetch/2 + Keyword.put/3 instead"
def replace(keywords, key, value) when is_list(keywords) and is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{^key, _} -> [{key, value} | delete(keywords, key)]
@@ -651,7 +620,6 @@ defmodule Keyword do
** (KeyError) key :b not found in: [a: 1]
"""
@doc since: "1.5.0"
@spec replace!(t, key, value) :: t
def replace!(keywords, key, value) when is_list(keywords) and is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
@@ -702,11 +670,6 @@ defmodule Keyword do
"""
@spec merge(t, t) :: t
def merge(keywords1, keywords2)
def merge(keywords1, []) when is_list(keywords1), do: keywords1
def merge([], keywords2) when is_list(keywords2), do: keywords2
def merge(keywords1, keywords2) when is_list(keywords1) and is_list(keywords2) do
if keyword?(keywords2) do
fun = fn
@@ -715,13 +678,13 @@ defmodule Keyword do
_ ->
raise ArgumentError,
"expected a keyword list as the first argument, got: #{inspect(keywords1)}"
message: "expected a keyword list as the first argument, got: #{inspect(keywords1)}"
end
:lists.filter(fun, keywords1) ++ keywords2
else
raise ArgumentError,
"expected a keyword list as the second argument, got: #{inspect(keywords2)}"
message: "expected a keyword list as the second argument, got: #{inspect(keywords2)}"
end
end
@@ -744,17 +707,17 @@ defmodule Keyword do
[b: 2, a: 4, d: 4]
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4, a: 5], fn :a, v1, v2 ->
...> v1 + v2
...> v1 + v2
...> end)
[b: 2, a: 4, d: 4, a: 5]
iex> Keyword.merge([a: 1, b: 2, a: 3], [a: 3, d: 4, a: 5], fn :a, v1, v2 ->
...> v1 + v2
...> v1 + v2
...> end)
[b: 2, a: 4, d: 4, a: 8]
iex> Keyword.merge([a: 1, b: 2], [:a, :b], fn :a, v1, v2 ->
...> v1 + v2
...> v1 + v2
...> end)
** (ArgumentError) expected a keyword list as the second argument, got: [:a, :b]
@@ -766,7 +729,7 @@ defmodule Keyword do
do_merge(keywords2, [], keywords1, keywords1, fun, keywords2)
else
raise ArgumentError,
"expected a keyword list as the first argument, got: #{inspect(keywords1)}"
message: "expected a keyword list as the first argument, got: #{inspect(keywords1)}"
end
end
@@ -788,7 +751,7 @@ defmodule Keyword do
defp do_merge(_other, _acc, _rest, _original, _fun, keywords2) do
raise ArgumentError,
"expected a keyword list as the second argument, got: #{inspect(keywords2)}"
message: "expected a keyword list as the second argument, got: #{inspect(keywords2)}"
end
@doc """
@@ -826,7 +789,7 @@ defmodule Keyword do
** (KeyError) key :b not found in: [a: 1]
"""
@spec update!(t, key, (value -> value)) :: t
@spec update!(t, key, (value -> value)) :: t | no_return
def update!(keywords, key, fun) do
update!(keywords, key, fun, keywords)
end
@@ -1038,7 +1001,7 @@ defmodule Keyword do
## Examples
iex> Keyword.to_list(a: 1)
iex> Keyword.to_list([a: 1])
[a: 1]
"""
@@ -1049,7 +1012,7 @@ defmodule Keyword do
@doc false
# TODO: Remove on 2.0
@deprecated "Use Kernel.length/1 instead"
# (hard-deprecated in elixir_dispatch)
def size(keyword) do
length(keyword)
end
+126 -280
View File
@@ -2,19 +2,6 @@ defmodule List do
@moduledoc """
Functions that work on (linked) lists.
Many of the functions provided for lists, which implement
the `Enumerable` protocol, are found in the `Enum` module.
Additionally, the following functions and operators for lists are
found in `Kernel`:
* `++/2`
* `--/2`
* `hd/1`
* `tl/1`
* `in/2`
* `length/1`
Lists in Elixir are specified between square brackets:
iex> [1, "two", 3, :four]
@@ -58,15 +45,17 @@ defmodule List do
slower as the list grows in size (linear time):
iex> list = [1, 2, 3]
iex> [0 | list] # fast
iex> [0 | list] # fast
[0, 1, 2, 3]
iex> list ++ [4] # slow
iex> list ++ [4] # slow
[1, 2, 3, 4]
Additionally, getting a list's length and accessing it by index are
linear time operations. Negative indexes are also supported but
they imply the list will be iterated twice, once to calculate the
proper index and another time to perform the operation.
The `Kernel` module contains many functions to manipulate lists
and that are allowed in guards. For example, `Kernel.hd/1` to
retrieve the head, `Kernel.tl/1` to fetch the tail and
`Kernel.length/1` for calculating the length. Keep in mind that,
similar to appending to a list, calculating the length needs to
traverse the whole list.
## Charlists
@@ -87,17 +76,28 @@ defmodule List do
instead of Elixir strings. One example of such functions
is `Application.loaded_applications/0`:
Application.loaded_applications()
#=> [
#=> {:stdlib, 'ERTS CXC 138 10', '2.6'},
#=> {:compiler, 'ERTS CXC 138 10', '6.0.1'},
#=> {:elixir, 'elixir', '1.0.0'},
#=> {:kernel, 'ERTS CXC 138 10', '4.1'},
#=> {:logger, 'logger', '1.0.0'}
#=> ]
Application.loaded_applications
#=> [{:stdlib, 'ERTS CXC 138 10', '2.6'},
#=> {:compiler, 'ERTS CXC 138 10', '6.0.1'},
#=> {:elixir, 'elixir', '1.0.0'},
#=> {:kernel, 'ERTS CXC 138 10', '4.1'},
#=> {:logger, 'logger', '1.0.0'}]
A list can be checked if it is made of printable ASCII
A list can be checked if it is made of printable ascii
codepoints with `ascii_printable?/2`.
## List and Enum modules
This module aims to provide operations that are specific
to lists, like conversion between data types, updates,
deletions and key lookups (for lists of tuples). For traversing
lists in general, developers should use the functions in the
`Enum` module that work across a variety of data types.
In both `Enum` and `List` modules, any kind of index access
on a list is linear. Negative indexes are also supported but
they imply the list will be iterated twice, one to calculate
the proper index and another to perform the operation.
"""
@compile :inline_list_funcs
@@ -177,10 +177,10 @@ defmodule List do
## Examples
iex> List.foldl([5, 5], 10, fn x, acc -> x + acc end)
iex> List.foldl([5, 5], 10, fn(x, acc) -> x + acc end)
20
iex> List.foldl([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
iex> List.foldl([1, 2, 3, 4], 0, fn(x, acc) -> x - acc end)
2
"""
@@ -195,7 +195,7 @@ defmodule List do
## Examples
iex> List.foldr([1, 2, 3, 4], 0, fn x, acc -> x - acc end)
iex> List.foldr([1, 2, 3, 4], 0, fn(x, acc) -> x - acc end)
-2
"""
@@ -288,17 +288,14 @@ defmodule List do
end
@doc """
Receives a list of tuples and if the identified item by `key` at `position`
exists, it is replaced with `new_tuple`.
Receives a list of tuples and replaces the item
identified by `key` at `position` if it exists.
## Examples
iex> List.keyreplace([a: 1, b: 2], :a, 0, {:a, 3})
[a: 3, b: 2]
iex> List.keyreplace([a: 1, b: 2], :a, 1, {:a, 3})
[a: 1, b: 2]
"""
@spec keyreplace([tuple], any, non_neg_integer, tuple) :: [tuple]
def keyreplace(list, key, position, new_tuple) do
@@ -325,7 +322,7 @@ defmodule List do
@doc """
Receives a `list` of tuples and replaces the item
identified by `key` at `position` with `new_tuple`.
identified by `key` at `position`.
If the item does not exist, it is added to the end of the `list`.
@@ -393,10 +390,10 @@ defmodule List do
end
@doc """
Wraps `term` in a list if this is not list.
Wraps the argument in a list.
If `term` is already a list, it returns the list.
If `term` is `nil`, it returns an empty list.
If the argument is already a list, returns the list.
If the argument is `nil`, returns an empty list.
## Examples
@@ -410,11 +407,7 @@ defmodule List do
[]
"""
@spec wrap(nil) :: []
@spec wrap(list) :: list when list: maybe_improper_list()
@spec wrap(term) :: nonempty_list(term) when term: any()
def wrap(term)
@spec wrap(list | any) :: list
def wrap(list) when is_list(list) do
list
end
@@ -448,28 +441,14 @@ defmodule List do
do_zip(list_of_lists, [])
end
@doc ~S"""
Checks if `list` is a charlist made only of printable ASCII characters.
@doc """
Checks if a list is a charlist made only of printable ASCII characters.
A printable charlist in Elixir contains only ASCII characters.
Takes an optional `limit` as a second argument. `ascii_printable?/2` only
checks the printability of the list up to the `limit`.
A printable charlist in Elixir contains only the printable characters in the
standard seven-bit ASCII character encoding, which are characters ranging from
32 to 126 in decimal notation, plus the following control characters:
* `?\a` - Bell
* `?\b` - Backspace
* `?\t` - Horizontal tab
* `?\n` - Line feed
* `?\v` - Vertical tab
* `?\f` - Form feed
* `?\r` - Carriage return
* `?\e` - Escape
For more information read the [Character groups](https://en.wikipedia.org/wiki/ASCII#Character_groups)
section in the Wikipedia article of the [ASCII](https://en.wikipedia.org/wiki/ASCII) standard.
## Examples
iex> List.ascii_printable?('abc')
@@ -481,85 +460,62 @@ defmodule List do
iex> List.ascii_printable?('abc' ++ [0], 2)
true
Improper lists are not printable, even if made only of ASCII characters:
Improper lists are not printable, even if made only of ascii characters:
iex> List.ascii_printable?('abc' ++ ?d)
false
"""
@doc since: "1.6.0"
@spec ascii_printable?(list, limit) :: boolean
when limit: :infinity | non_neg_integer
def ascii_printable?(list, limit \\ :infinity)
when is_list(list) and (limit == :infinity or (is_integer(limit) and limit >= 0)) do
ascii_printable_guarded?(list, limit)
end
def ascii_printable?(list, counter \\ :infinity)
defp ascii_printable_guarded?(_, 0) do
def ascii_printable?(_, 0) do
true
end
defp ascii_printable_guarded?([char | rest], counter)
when is_integer(char) and char >= 32 and char <= 126 do
ascii_printable_guarded?(rest, decrement(counter))
def ascii_printable?([char | rest], counter)
when is_integer(char) and char >= 32 and char <= 126 do
ascii_printable?(rest, decrement(counter))
end
defp ascii_printable_guarded?([?\n | rest], counter) do
ascii_printable_guarded?(rest, decrement(counter))
def ascii_printable?([?\n | rest], counter) do
ascii_printable?(rest, decrement(counter))
end
defp ascii_printable_guarded?([?\r | rest], counter) do
ascii_printable_guarded?(rest, decrement(counter))
def ascii_printable?([?\r | rest], counter) do
ascii_printable?(rest, decrement(counter))
end
defp ascii_printable_guarded?([?\t | rest], counter) do
ascii_printable_guarded?(rest, decrement(counter))
def ascii_printable?([?\t | rest], counter) do
ascii_printable?(rest, decrement(counter))
end
defp ascii_printable_guarded?([?\v | rest], counter) do
ascii_printable_guarded?(rest, decrement(counter))
def ascii_printable?([?\v | rest], counter) do
ascii_printable?(rest, decrement(counter))
end
defp ascii_printable_guarded?([?\b | rest], counter) do
ascii_printable_guarded?(rest, decrement(counter))
def ascii_printable?([?\b | rest], counter) do
ascii_printable?(rest, decrement(counter))
end
defp ascii_printable_guarded?([?\f | rest], counter) do
ascii_printable_guarded?(rest, decrement(counter))
def ascii_printable?([?\f | rest], counter) do
ascii_printable?(rest, decrement(counter))
end
defp ascii_printable_guarded?([?\e | rest], counter) do
ascii_printable_guarded?(rest, decrement(counter))
def ascii_printable?([?\e | rest], counter) do
ascii_printable?(rest, decrement(counter))
end
defp ascii_printable_guarded?([?\a | rest], counter) do
ascii_printable_guarded?(rest, decrement(counter))
def ascii_printable?([?\a | rest], counter) do
ascii_printable?(rest, decrement(counter))
end
defp ascii_printable_guarded?([], _counter), do: true
defp ascii_printable_guarded?(_, _counter), do: false
def ascii_printable?([], _counter), do: true
def ascii_printable?(_, _counter), do: false
@compile {:inline, decrement: 1}
defp decrement(:infinity), do: :infinity
defp decrement(counter), do: counter - 1
@doc """
Returns `true` if `list` is an improper list. Otherwise returns `false`.
## Examples
iex> List.improper?([1, 2 | 3])
true
iex> List.improper?([1, 2, 3])
false
"""
@doc since: "1.8.0"
@spec improper?(maybe_improper_list) :: boolean
def improper?(list) when is_list(list) and length(list) >= 0, do: false
def improper?(list) when is_list(list), do: true
@doc """
Returns a list with `value` inserted at the specified `index`.
@@ -582,19 +538,11 @@ defmodule List do
"""
@spec insert_at(list, integer, any) :: list
def insert_at(list, index, value) when is_list(list) and is_integer(index) do
case index do
-1 ->
list ++ [value]
_ when index < 0 ->
case length(list) + index + 1 do
index when index < 0 -> [value | list]
index -> do_insert_at(list, index, value)
end
_ ->
do_insert_at(list, index, value)
def insert_at(list, index, value) when is_integer(index) do
if index < 0 do
do_insert_at(list, length(list) + index + 1, value)
else
do_insert_at(list, index, value)
end
end
@@ -620,12 +568,9 @@ defmodule List do
"""
@spec replace_at(list, integer, any) :: list
def replace_at(list, index, value) when is_list(list) and is_integer(index) do
def replace_at(list, index, value) when is_integer(index) do
if index < 0 do
case length(list) + index do
index when index < 0 -> list
index -> do_replace_at(list, index, value)
end
do_replace_at(list, length(list) + index, value)
else
do_replace_at(list, index, value)
end
@@ -653,12 +598,9 @@ defmodule List do
"""
@spec update_at([elem], integer, (elem -> any)) :: list when elem: var
def update_at(list, index, fun) when is_list(list) and is_function(fun) and is_integer(index) do
def update_at(list, index, fun) when is_function(fun, 1) and is_integer(index) do
if index < 0 do
case length(list) + index do
index when index < 0 -> list
index -> do_update_at(list, index, fun)
end
do_update_at(list, length(list) + index, fun)
else
do_update_at(list, index, fun)
end
@@ -705,7 +647,6 @@ defmodule List do
{3, [1, 2]}
"""
@doc since: "1.4.0"
@spec pop_at(list, integer, any) :: {any, list}
def pop_at(list, index, default \\ nil) when is_integer(index) do
if index < 0 do
@@ -735,7 +676,6 @@ defmodule List do
false
"""
@doc since: "1.5.0"
@spec starts_with?(list, list) :: boolean
@spec starts_with?(list, []) :: true
@spec starts_with?([], nonempty_list) :: false
@@ -748,18 +688,15 @@ defmodule List do
@doc """
Converts a charlist to an atom.
Elixir supports conversions from charlists which contains any Unicode
codepoint.
Currently Elixir does not support conversions from charlists
which contains Unicode codepoints greater than 0xFF.
Inlined by the compiler.
## Examples
iex> List.to_atom('Elixir')
:Elixir
iex> List.to_atom('🌢 Elixir')
:"🌢 Elixir"
iex> List.to_atom('elixir')
:elixir
"""
@spec to_atom(charlist) :: atom
@@ -771,8 +708,8 @@ defmodule List do
Converts a charlist to an existing atom. Raises an `ArgumentError`
if the atom does not exist.
Elixir supports conversions from charlists which contains any Unicode
codepoint.
Currently Elixir does not support conversions from charlists
which contains Unicode codepoints greater than 0xFF.
Inlined by the compiler.
@@ -782,10 +719,6 @@ defmodule List do
iex> List.to_existing_atom('my_atom')
:my_atom
iex> _ = :"🌢 Elixir"
iex> List.to_existing_atom('🌢 Elixir')
:"🌢 Elixir"
iex> List.to_existing_atom('this_atom_will_never_exist')
** (ArgumentError) argument error
@@ -910,58 +843,6 @@ defmodule List do
end
end
@doc """
Converts a list of integers representing codepoints, lists or
strings into a charlist.
Notice that this function expects a list of integers representing
UTF-8 codepoints. If you have a list of bytes, you must instead use
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
## Examples
iex> List.to_charlist([0x00E6, 0x00DF])
'æß'
iex> List.to_charlist([0x0061, "bc"])
'abc'
iex> List.to_charlist([0x0064, "ee", ['p']])
'deep'
"""
@doc since: "1.8.0"
@spec to_charlist(:unicode.charlist()) :: charlist()
def to_charlist(list) when is_list(list) do
try do
:unicode.characters_to_list(list)
rescue
ArgumentError ->
raise ArgumentError, """
cannot convert the given list to a charlist.
To be converted to a charlist, a list must contain only:
* strings
* integers representing Unicode codepoints
* or a list containing one of these three elements
Please check the given list or call inspect/1 to get the list representation, got:
#{inspect(list)}
"""
else
result when is_list(result) ->
result
{:error, encoded, rest} ->
raise UnicodeConversionError, encoded: encoded, rest: rest, kind: :invalid
{:incomplete, encoded, rest} ->
raise UnicodeConversionError, encoded: encoded, rest: rest, kind: :incomplete
end
end
@doc """
Returns a keyword list that represents an *edit script*.
@@ -975,60 +856,31 @@ defmodule List do
corresponding key is `:del`), or left alone (if the corresponding key is
`:eq`) in `list1` in order to be closer to `list2`.
See `myers_difference/3` if you want to handle nesting in the diff scripts.
## Examples
iex> List.myers_difference([1, 4, 2, 3], [1, 2, 3, 4])
[eq: [1], del: [4], eq: [2, 3], ins: [4]]
"""
@doc since: "1.4.0"
@spec myers_difference(list, list) :: [{:eq | :ins | :del, list}]
@spec myers_difference(list, list) :: [{:eq | :ins | :del, list}] | nil
def myers_difference(list1, list2) when is_list(list1) and is_list(list2) do
myers_difference_with_diff_script(list1, list2, nil)
path = {0, 0, list1, list2, []}
find_script(0, length(list1) + length(list2), [path])
end
@doc """
Returns a keyword list that represents an *edit script* with nested diffs.
This is an extension of `myers_difference/2` where a `diff_script` function
can be given in case it is desired to compute nested differences. The function
may return a list with the inner edit script or `nil` in case there is no
such script. The returned inner edit script will be under the `:diff` key.
## Examples
iex> List.myers_difference(["a", "db", "c"], ["a", "bc"], &String.myers_difference/2)
[eq: ["a"], diff: [del: "d", eq: "b", ins: "c"], del: ["c"]]
"""
@doc since: "1.8.0"
@spec myers_difference(list, list, (term, term -> script | nil)) :: script
when script: [{:eq | :ins | :del | :diff, list}]
def myers_difference(list1, list2, diff_script)
when is_list(list1) and is_list(list2) and is_function(diff_script) do
myers_difference_with_diff_script(list1, list2, diff_script)
defp find_script(envelope, max, _paths) when envelope > max do
nil
end
defp myers_difference_with_diff_script(list1, list2, diff_script) do
path = {0, list1, list2, []}
find_script(0, length(list1) + length(list2), [path], diff_script)
end
defp find_script(envelope, max, paths, diff_script) do
case each_diagonal(-envelope, envelope, paths, [], diff_script) do
defp find_script(envelope, max, paths) do
case each_diagonal(-envelope, envelope, paths, []) do
{:done, edits} -> compact_reverse(edits, [])
{:next, paths} -> find_script(envelope + 1, max, paths, diff_script)
{:next, paths} -> find_script(envelope + 1, max, paths)
end
end
defp compact_reverse([], acc), do: acc
defp compact_reverse([{:diff, _} = fragment | rest], acc) do
compact_reverse(rest, [fragment | acc])
end
defp compact_reverse([{kind, elem} | rest], [{kind, result} | acc]) do
compact_reverse(rest, [{kind, [elem | result]} | acc])
end
@@ -1041,76 +893,58 @@ defmodule List do
compact_reverse(rest, [{kind, [elem]} | acc])
end
defp each_diagonal(diag, limit, _paths, next_paths, _diff_script) when diag > limit do
defp each_diagonal(diag, limit, _paths, next_paths) when diag > limit do
{:next, :lists.reverse(next_paths)}
end
defp each_diagonal(diag, limit, paths, next_paths, diff_script) do
{path, rest} = proceed_path(diag, limit, paths, diff_script)
defp each_diagonal(diag, limit, paths, next_paths) do
{path, rest} = proceed_path(diag, limit, paths)
case follow_snake(path) do
{:cont, path} -> each_diagonal(diag + 2, limit, rest, [path | next_paths], diff_script)
{:cont, path} -> each_diagonal(diag + 2, limit, rest, [path | next_paths])
{:done, edits} -> {:done, edits}
end
end
defp proceed_path(0, 0, [path], _diff_script), do: {path, []}
defp proceed_path(0, 0, [path]), do: {path, []}
defp proceed_path(diag, limit, [path | _] = paths, diff_script) when diag == -limit do
{move_down(path, diff_script), paths}
defp proceed_path(diag, limit, [path | _] = paths) when diag == -limit do
{move_down(path), paths}
end
defp proceed_path(diag, limit, [path], diff_script) when diag == limit do
{move_right(path, diff_script), []}
defp proceed_path(diag, limit, [path]) when diag == limit do
{move_right(path), []}
end
defp proceed_path(_diag, _limit, [path1, path2 | rest], diff_script) do
if elem(path1, 0) > elem(path2, 0) do
{move_right(path1, diff_script), [path2 | rest]}
defp proceed_path(_diag, _limit, [path1, path2 | rest]) do
if elem(path1, 1) > elem(path2, 1) do
{move_right(path1), [path2 | rest]}
else
{move_down(path2, diff_script), [path2 | rest]}
{move_down(path2), [path2 | rest]}
end
end
defp move_right({y, [elem1 | rest1] = list1, [elem2 | rest2], edits}, diff_script)
when diff_script != nil do
if diff = diff_script.(elem1, elem2) do
{y + 1, rest1, rest2, [{:diff, diff} | edits]}
else
{y, list1, rest2, [{:ins, elem2} | edits]}
end
defp move_right({x, y, list1, [elem | rest], edits}) do
{x + 1, y, list1, rest, [{:ins, elem} | edits]}
end
defp move_right({y, list1, [elem | rest], edits}, _diff_script) do
{y, list1, rest, [{:ins, elem} | edits]}
defp move_right({x, y, list1, [], edits}) do
{x + 1, y, list1, [], edits}
end
defp move_right({y, list1, [], edits}, _diff_script) do
{y, list1, [], edits}
defp move_down({x, y, [elem | rest], list2, edits}) do
{x, y + 1, rest, list2, [{:del, elem} | edits]}
end
defp move_down({y, [elem1 | rest1], [elem2 | rest2] = list2, edits}, diff_script)
when diff_script != nil do
if diff = diff_script.(elem1, elem2) do
{y + 1, rest1, rest2, [{:diff, diff} | edits]}
else
{y + 1, rest1, list2, [{:del, elem1} | edits]}
end
defp move_down({x, y, [], list2, edits}) do
{x, y + 1, [], list2, edits}
end
defp move_down({y, [elem | rest], list2, edits}, _diff_script) do
{y + 1, rest, list2, [{:del, elem} | edits]}
defp follow_snake({x, y, [elem | rest1], [elem | rest2], edits}) do
follow_snake({x + 1, y + 1, rest1, rest2, [{:eq, elem} | edits]})
end
defp move_down({y, [], list2, edits}, _diff_script) do
{y + 1, [], list2, edits}
end
defp follow_snake({y, [elem | rest1], [elem | rest2], edits}) do
follow_snake({y + 1, rest1, rest2, [{:eq, elem} | edits]})
end
defp follow_snake({_y, [], [], edits}) do
defp follow_snake({_x, _y, [], [], edits}) do
{:done, edits}
end
@@ -1126,6 +960,10 @@ defmodule List do
[]
end
defp do_replace_at(list, index, _value) when index < 0 do
list
end
defp do_replace_at([_old | rest], 0, value) do
[value | rest]
end
@@ -1140,7 +978,7 @@ defmodule List do
[value]
end
defp do_insert_at(list, 0, value) do
defp do_insert_at(list, index, value) when index <= 0 do
[value | list]
end
@@ -1154,6 +992,10 @@ defmodule List do
[fun.(value) | list]
end
defp do_update_at(list, index, _fun) when index < 0 do
list
end
defp do_update_at([head | tail], index, fun) do
[head | do_update_at(tail, index - 1, fun)]
end
@@ -1168,6 +1010,10 @@ defmodule List do
{default, :lists.reverse(acc)}
end
defp do_pop_at(list, index, default, []) when index < 0 do
{default, list}
end
defp do_pop_at([head | tail], 0, _default, acc) do
{head, :lists.reverse(acc, tail)}
end
+1 -1
View File
@@ -18,7 +18,7 @@ defprotocol List.Chars do
@doc false
# TODO: Remove by 2.0
@deprecated "Use List.Chars.to_charlist/1 instead"
# (hard-deprecated in elixir_dispatch)
Kernel.def to_char_list(term) do
__MODULE__.to_charlist(term)
end
+53 -207
View File
@@ -95,7 +95,7 @@ defmodule Macro do
`div/2` function, so that the AST for that function will become `{:div, [],
[100, 5]}` (`div(100, 5)`).
"""
@spec unpipe(t()) :: [t()]
@spec unpipe(Macro.t()) :: [Macro.t()]
def unpipe(expr) do
:lists.reverse(unpipe(expr, []))
end
@@ -111,7 +111,7 @@ defmodule Macro do
@doc """
Pipes `expr` into the `call_args` at the given `position`.
"""
@spec pipe(t(), t(), integer) :: t()
@spec pipe(Macro.t(), Macro.t(), integer) :: Macro.t() | no_return
def pipe(expr, call_args, position)
def pipe(expr, {:&, _, _} = call_args, _integer) do
@@ -127,21 +127,13 @@ defmodule Macro do
raise ArgumentError, bad_pipe(expr, call_args)
end
def pipe(expr, {:<<>>, _, _} = call_args, _integer) do
raise ArgumentError, bad_pipe(expr, call_args)
end
def pipe(expr, {unquote, _, []}, _integer) when unquote in [:unquote, :unquote_splicing] do
raise ArgumentError,
"cannot pipe #{to_string(expr)} into the special form #{unquote}/1 " <>
"since #{unquote}/1 is used to build the Elixir AST itself"
end
# {:fn, _, _} is what we get when we pipe into an anonymous function without
# calling it, e.g., `:foo |> (fn x -> x end)`.
def pipe(expr, {:fn, _, _}, _integer) do
expr_str = to_string(expr)
raise ArgumentError,
"cannot pipe #{to_string(expr)} into an anonymous function without" <>
"cannot pipe #{expr_str} into an anonymous function without" <>
" calling the function; use something like (fn ... end).() or" <>
" define the anonymous function as a regular private function"
end
@@ -166,7 +158,7 @@ defmodule Macro do
defp bad_pipe(expr, call_args) do
"cannot pipe #{to_string(expr)} into #{to_string(call_args)}, " <>
"can only pipe into local calls foo(), remote calls Foo.bar() or anonymous function calls foo.()"
"can only pipe into local calls foo(), remote calls Foo.bar() or anonymous functions calls foo.()"
end
@doc """
@@ -202,15 +194,14 @@ defmodule Macro do
## Examples
iex> Macro.generate_arguments(2, __MODULE__)
[{:arg1, [], __MODULE__}, {:arg2, [], __MODULE__}]
[{:var1, [], __MODULE__}, {:var2, [], __MODULE__}]
"""
@doc since: "1.5.0"
def generate_arguments(0, _), do: []
def generate_arguments(amount, context)
when is_integer(amount) and amount > 0 and is_atom(context) do
for id <- 1..amount, do: var(String.to_atom("arg" <> Integer.to_string(id)), context)
for id <- 1..amount, do: Macro.var(String.to_atom("var" <> Integer.to_string(id)), context)
end
@doc """
@@ -346,7 +337,7 @@ defmodule Macro do
:error
"""
@spec decompose_call(t()) :: {atom, [t()]} | {t(), atom, [t()]} | :error
@spec decompose_call(Macro.t()) :: {atom, [Macro.t()]} | {Macro.t(), atom, [Macro.t()]} | :error
def decompose_call(ast)
def decompose_call({{:., _, [remote, function]}, _, args})
@@ -360,7 +351,12 @@ defmodule Macro do
def decompose_call(_), do: :error
@doc """
Recursively escapes a value so it can be inserted into a syntax tree.
Recursively escapes a value so it can be inserted
into a syntax tree.
One may pass `unquote: true` to `escape/2`
which leaves `unquote/1` statements unescaped, effectively
unquoting the contents on escape.
## Examples
@@ -373,75 +369,10 @@ defmodule Macro do
iex> Macro.escape({:unquote, [], [1]}, unquote: true)
1
## Options
* `:unquote` - when true, this function leaves `unquote/1` and
`unquote_splicing/1` statements unescaped, effectively unquoting
the contents on escape. This option is useful only when escaping
ASTs which may have quoted fragments in them. Defaults to false.
* `:prune_metadata` - when true, removes metadata from escaped AST
nodes. Note this option changes the semantics of escaped code and
it should only be used when escaping ASTs, never values. Defaults
to false.
As an example, `ExUnit` stores the AST of every assertion, so when
an assertion fails we can show code snippets to users. Without this
option, each time the test module is compiled, we get a different
MD5 of the module byte code, because the AST contains metadata,
such as counters, specific to the compilation environment. By pruning
the metadata, we ensure that the module is deterministic and reduce
the amount of data `ExUnit` needs to keep around.
## Comparison to `Kernel.SpecialForms.quote/2`
The `escape/2` function is sometimes confused with `Kernel.SpecialForms.quote/2`,
because the above examples behave the same with both. The key difference is
best illustrated when the value to escape is stored in a variable.
iex> Macro.escape({:a, :b, :c})
{:{}, [], [:a, :b, :c]}
iex> quote do: {:a, :b, :c}
{:{}, [], [:a, :b, :c]}
iex> value = {:a, :b, :c}
iex> Macro.escape(value)
{:{}, [], [:a, :b, :c]}
iex> quote do: value
{:value, [], __MODULE__}
iex> value = {:a, :b, :c}
iex> quote do: unquote(value)
{:a, :b, :c}
`escape/2` is used to escape *values* (either directly passed or variable
bound), while `Kernel.SpecialForms.quote/2` produces syntax trees for
expressions.
"""
@spec escape(term, keyword) :: t()
@spec escape(term, keyword) :: Macro.t()
def escape(expr, opts \\ []) do
unquote = Keyword.get(opts, :unquote, false)
kind = if Keyword.get(opts, :prune_metadata, false), do: :prune_metadata, else: :default
:elixir_quote.escape(expr, kind, unquote)
end
@doc """
Expands the struct given by `module` in the given `env`.
This is useful when a struct needs to be expanded at
compilation time and the struct being expanded may or may
not have been compiled. This function is even capable of
expanding structs defined under the module being compiled.
It will raise `CompileError` if the struct is not available.
"""
@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)
elem(:elixir_quote.escape(expr, Keyword.get(opts, :unquote, false)), 0)
end
@doc """
@@ -485,8 +416,8 @@ defmodule Macro do
defp find_invalid(bin) when is_binary(bin), do: nil
defp find_invalid(fun) when is_function(fun) do
unless Function.info(fun, :env) == {:env, []} and
Function.info(fun, :type) == {:type, :external} do
unless :erlang.fun_info(fun, :env) == {:env, []} and
:erlang.fun_info(fun, :type) == {:type, :external} do
{:error, fun}
end
end
@@ -547,7 +478,7 @@ defmodule Macro do
def unescape_map(?s), do: ?\s
def unescape_map(?t), do: ?\t
def unescape_map(?v), do: ?\v
def unescape_map(e), do: e
def unescape_map(e), do: e
If the `unescape_map/1` function returns `false`, the char is
not escaped and the backslash is kept in the string.
@@ -560,7 +491,7 @@ defmodule Macro do
Using the `unescape_map/1` function defined above is easy:
Macro.unescape_string("example\\n", &unescape_map(&1))
Macro.unescape_string "example\\n", &unescape_map(&1)
"""
@spec unescape_string(String.t(), (non_neg_integer -> non_neg_integer | false)) :: String.t()
@@ -569,21 +500,13 @@ defmodule Macro do
end
@doc false
@deprecated "Traverse over the arguments using Enum.map/2 instead"
def unescape_tokens(tokens) do
case :elixir_interpolation.unescape_tokens(tokens) do
{:ok, unescaped_tokens} -> unescaped_tokens
{:error, reason} -> raise ArgumentError, to_string(reason)
end
:elixir_interpolation.unescape_tokens(tokens)
end
@doc false
@deprecated "Traverse over the arguments using Enum.map/2 instead"
def unescape_tokens(tokens, map) do
case :elixir_interpolation.unescape_tokens(tokens, map) do
{:ok, unescaped_tokens} -> unescaped_tokens
{:error, reason} -> raise ArgumentError, to_string(reason)
end
:elixir_interpolation.unescape_tokens(tokens, map)
end
@doc """
@@ -609,7 +532,7 @@ defmodule Macro do
"one + two"
"""
@spec to_string(t(), (t(), String.t() -> String.t())) :: String.t()
@spec to_string(Macro.t(), (Macro.t(), String.t() -> String.t())) :: String.t()
def to_string(tree, fun \\ fn _ast, string -> string end)
# Variables
@@ -758,13 +681,9 @@ defmodule Macro do
{list, last} = split_last(args)
result =
if kw_blocks?(last) do
case list do
[] -> call_to_string(target, fun) <> kw_blocks_to_string(last, fun)
_ -> call_to_string_with_args(target, list, fun) <> kw_blocks_to_string(last, fun)
end
else
call_to_string_with_args(target, args, fun)
case kw_blocks?(last) do
true -> call_to_string_with_args(target, list, fun) <> kw_blocks_to_string(last, fun)
false -> call_to_string_with_args(target, args, fun)
end
fun.(ast, result)
@@ -800,13 +719,7 @@ defmodule Macro do
end
# All other structures
def to_string(other, fun) do
fun.(other, inspect_no_limit(other))
end
defp inspect_no_limit(value) do
Kernel.inspect(value, limit: :infinity, printable_limit: :infinity)
end
def to_string(other, fun), do: fun.(other, inspect(other, []))
defp bitpart_to_string({:::, _, [left, right]} = ast, fun) do
result =
@@ -860,15 +773,15 @@ defmodule Macro do
"\#{" <> to_string(arg, fun) <> "}"
binary when is_binary(binary) ->
binary = inspect_no_limit(binary)
binary_part(binary, 1, byte_size(binary) - 2)
binary = inspect(binary, [])
:binary.part(binary, 1, byte_size(binary) - 2)
end)
<<?", parts::binary, ?">>
end
defp module_to_string(atom, _fun) when is_atom(atom) do
inspect_no_limit(atom)
inspect(atom, [])
end
defp module_to_string({:&, _, [val]} = expr, fun) when not is_integer(val) do
@@ -926,25 +839,11 @@ defmodule Macro do
:error
end
defp sigil_call({sigil, _, [{:<<>>, _, _} = parts, args]} = ast, fun)
defp sigil_call({sigil, _, [{:<<>>, _, _} = bin, args]} = ast, fun)
when is_atom(sigil) and is_list(args) do
case Atom.to_string(sigil) do
<<"sigil_", name>> when name >= ?A and name <= ?Z ->
{:<<>>, _, [binary]} = parts
formatted =
if :binary.last(binary) == ?\n do
binary = String.replace(binary, ~s["""], ~s["\\""])
<<?~, name, ~s["""\n], binary::binary, ~s["""], sigil_args(args, fun)::binary>>
else
{left, right} = select_sigil_container(binary)
<<?~, name, left, binary::binary, right, sigil_args(args, fun)::binary>>
end
{:ok, fun.(ast, formatted)}
<<"sigil_", name>> when name >= ?a and name <= ?z ->
{:ok, fun.(ast, "~" <> <<name>> <> interpolate(parts, fun) <> sigil_args(args, fun))}
<<"sigil_", name>> ->
{:ok, fun.(ast, "~" <> <<name>> <> interpolate(bin, fun) <> sigil_args(args, fun))}
_ ->
:error
@@ -955,18 +854,6 @@ defmodule Macro do
:error
end
defp select_sigil_container(binary) do
cond do
:binary.match(binary, ["\""]) == :nomatch -> {?", ?"}
:binary.match(binary, ["\'"]) == :nomatch -> {?', ?'}
:binary.match(binary, ["(", ")"]) == :nomatch -> {?(, ?)}
:binary.match(binary, ["[", "]"]) == :nomatch -> {?[, ?]}
:binary.match(binary, ["{", "}"]) == :nomatch -> {?{, ?}}
:binary.match(binary, ["<", ">"]) == :nomatch -> {?<, ?>}
true -> {?/, ?/}
end
end
defp sigil_args([], _fun), do: ""
defp sigil_args(args, fun), do: fun.(args, List.to_string(args))
@@ -1065,9 +952,8 @@ defmodule Macro do
end
defp map_list_to_string(list, fun) do
Enum.map_join(list, ", ", fn
{key, value} -> to_string(key, fun) <> " => " <> to_string(value, fun)
other -> to_string(other, fun)
Enum.map_join(list, ", ", fn {key, value} ->
to_string(key, fun) <> " => " <> to_string(value, fun)
end)
end
@@ -1133,7 +1019,7 @@ defmodule Macro do
* Macros (local or remote)
* Aliases are expanded (if possible) and return atoms
* Compilation environment macros (`__CALLER__/0`, `__DIR__/0`, `__ENV__/0` and `__MODULE__/0`)
* Compilation environment macros (`__ENV__/0`, `__MODULE__/0` and `__DIR__/0`)
* Module attributes reader (`@foo`)
If the expression cannot be expanded, it returns the expression
@@ -1168,7 +1054,7 @@ defmodule Macro do
end
The compilation will fail because `My.Module` when quoted
is not an atom, but a syntax tree as follows:
is not an atom, but a syntax tree as follow:
{:__aliases__, [], [:My, :Module]}
@@ -1191,7 +1077,7 @@ defmodule Macro do
defmacro defmodule_with_length(name, do: block) do
expanded = Macro.expand(name, __CALLER__)
length = length(Atom.to_charlist(expanded))
length = length(Atom.to_charlist(expanded))
quote do
defmodule unquote(name) do
@@ -1248,7 +1134,7 @@ defmodule Macro do
# Expand possible macro import invocation
defp do_expand_once({atom, meta, context} = original, env)
when is_atom(atom) and is_list(meta) and is_atom(context) do
if Macro.Env.has_var?(env, {atom, Keyword.get(meta, :counter, context)}) do
if :lists.member({atom, Keyword.get(meta, :counter, context)}, env.vars) do
{original, false}
else
case do_expand_once({atom, meta, []}, env) do
@@ -1262,7 +1148,7 @@ defmodule Macro do
when is_atom(atom) and is_list(args) and is_list(meta) do
arity = length(args)
if special_form?(atom, arity) do
if :elixir_import.special_form(atom, arity) do
{original, false}
else
module = env.module
@@ -1278,15 +1164,9 @@ defmodule Macro do
case expand do
{:ok, receiver, quoted} ->
next = :elixir_module.next_counter(module)
next = :erlang.unique_integer()
{:elixir_quote.linify_with_context_counter(0, {receiver, next}, quoted), true}
{:ok, Kernel, op, [arg]} when op in [:+, :-] ->
case expand_once(arg, env) do
integer when is_integer(integer) -> {apply(Kernel, op, [integer]), true}
_ -> {original, false}
end
{:ok, _receiver, _name, _args} ->
{original, false}
@@ -1309,7 +1189,7 @@ defmodule Macro do
case expand do
{:ok, receiver, quoted} ->
next = :elixir_module.next_counter(env.module)
next = :erlang.unique_integer()
{:elixir_quote.linify_with_context_counter(0, {receiver, next}, quoted), true}
:error ->
@@ -1321,39 +1201,6 @@ defmodule Macro do
# Anything else is just returned
defp do_expand_once(other, _env), do: {other, false}
@doc """
Returns `true` if the given name and arity is a special form.
"""
@doc since: "1.7.0"
@spec special_form?(name :: atom(), arity()) :: boolean()
def special_form?(name, arity) when is_atom(name) and is_integer(arity) do
:elixir_import.special_form(name, arity)
end
@doc """
Returns `true` if the given name and arity is an operator.
"""
@doc since: "1.7.0"
@spec operator?(name :: atom(), arity()) :: boolean()
def operator?(name, 2) when is_atom(name), do: Identifier.binary_op(name) != :error
def operator?(name, 1) when is_atom(name), do: Identifier.unary_op(name) != :error
def operator?(name, arity) when is_atom(name) and is_integer(arity), do: false
@doc """
Returns `true` if the given quoted expression is an AST literal.
"""
@doc since: "1.7.0"
@spec quoted_literal?(literal) :: true
@spec quoted_literal?(expr) :: false
def quoted_literal?(term)
def quoted_literal?({left, right}), do: quoted_literal?(left) and quoted_literal?(right)
def quoted_literal?(list) when is_list(list), do: Enum.all?(list, &quoted_literal?/1)
def quoted_literal?(term) do
is_atom(term) or is_number(term) or is_binary(term) or is_function(term)
end
@doc """
Receives an AST node and expands it until it can no longer
be expanded.
@@ -1386,29 +1233,28 @@ defmodule Macro do
## Examples
iex> Macro.underscore("FooBar")
iex> Macro.underscore "FooBar"
"foo_bar"
iex> Macro.underscore("Foo.Bar")
iex> Macro.underscore "Foo.Bar"
"foo/bar"
iex> Macro.underscore(Foo.Bar)
iex> Macro.underscore Foo.Bar
"foo/bar"
In general, `underscore` can be thought of as the reverse of
`camelize`, however, in some cases formatting may be lost:
iex> Macro.underscore("SAPExample")
iex> Macro.underscore "SAPExample"
"sap_example"
iex> Macro.camelize("sap_example")
iex> Macro.camelize "sap_example"
"SapExample"
iex> Macro.camelize("hello_10")
iex> Macro.camelize "hello_10"
"Hello10"
"""
@spec underscore(atom | String.t()) :: String.t()
def underscore(atom) when is_atom(atom) do
"Elixir." <> rest = Atom.to_string(atom)
underscore(rest)
@@ -1454,15 +1300,15 @@ defmodule Macro do
## Examples
iex> Macro.camelize("foo_bar")
iex> Macro.camelize "foo_bar"
"FooBar"
If uppercase characters are present, they are not modified in any way
If uppercase characters are present, they are not modified in anyway
as a mechanism to preserve acronyms:
iex> Macro.camelize("API.V1")
iex> Macro.camelize "API.V1"
"API.V1"
iex> Macro.camelize("API_SPEC")
iex> Macro.camelize "API_SPEC"
"API_SPEC"
"""
+25 -59
View File
@@ -28,7 +28,7 @@ defmodule Macro.Env do
element is the function name and the second its arity; returns
`nil` if not inside a function
* `context` - the context of the environment; it can be `nil`
(default context), `:guard` (inside a guard) or `:match` (inside a match)
(default context), inside a guard or inside a match
* `aliases` - a list of two-element tuples, where the first
element is the aliased name and the second one the actual name
* `requires` - the list of required modules
@@ -38,41 +38,37 @@ defmodule Macro.Env do
* `context_modules` - a list of modules defined in the current context
* `lexical_tracker` - PID of the lexical tracker which is responsible for
keeping user info
The following fields pertain to variable handling and must not be accessed or
relied on. To get a list of all variables, see `vars/1`:
* `current_vars`
* `unused_vars`
* `prematch_vars`
* `contextual_vars`
The following fields are deprecated and must not be accessed or relied on:
* `vars` - a list keeping all defined variables as `{var, context}`
The following fields are private and must not be accessed or relied on:
* `export_vars` - a list keeping all variables to be exported in a
construct (may be `nil`)
* `match_vars` - controls how "new" variables are handled. Inside a
match it is a list with all variables in a match. Outside of a match
is either `:warn` or `:apply`
* `prematch_vars` - a list of variables defined before a match (is
`nil` when not inside a match)
"""
@type name_arity :: {atom, arity}
@type file :: binary
@type line :: non_neg_integer
@type aliases :: [{module, module}]
@type macro_aliases :: [{module, {term, module}}]
@type macro_aliases :: [{module, {integer, module}}]
@type context :: :match | :guard | nil
@type requires :: [module]
@type functions :: [{module, [name_arity]}]
@type macros :: [{module, [name_arity]}]
@type context_modules :: [module]
@type vars :: [{atom, atom | non_neg_integer}]
@type lexical_tracker :: pid | nil
@type variable :: {atom, atom | term}
@type local :: atom | nil
@typep vars :: [variable]
@typep var_type :: :term
@typep var_version :: non_neg_integer
@typep unused_vars :: %{{variable, var_version} => non_neg_integer | false}
@typep current_vars :: %{variable => {var_version, var_type}}
@typep prematch_vars :: current_vars | :warn | :raise | :pin | :apply
@typep contextual_vars :: [atom]
@opaque export_vars :: vars | nil
@opaque match_vars :: vars | :warn | :apply
@opaque prematch_vars :: vars | nil
@type t :: %{
__struct__: __MODULE__,
@@ -88,14 +84,12 @@ defmodule Macro.Env do
macro_aliases: aliases,
context_modules: context_modules,
vars: vars,
unused_vars: unused_vars,
current_vars: current_vars,
export_vars: export_vars,
match_vars: match_vars,
prematch_vars: prematch_vars,
lexical_tracker: lexical_tracker,
contextual_vars: contextual_vars
lexical_tracker: lexical_tracker
}
# TODO: Remove :vars field on v2.0
def __struct__ do
%{
__struct__: __MODULE__,
@@ -111,11 +105,10 @@ defmodule Macro.Env do
macro_aliases: [],
context_modules: [],
vars: [],
unused_vars: %{},
current_vars: %{},
prematch_vars: :warn,
lexical_tracker: nil,
contextual_vars: []
export_vars: nil,
match_vars: :warn,
prematch_vars: nil
}
end
@@ -123,33 +116,6 @@ defmodule Macro.Env do
Enum.reduce(kv, __struct__(), fn {k, v}, acc -> :maps.update(k, v, acc) end)
end
@doc """
Returns a list of variables in the current environment.
Each variable is identified by a tuple of two elements,
where the first element is the variable name as an atom
and the second element is its context, which may be an
atom or an integer.
"""
@doc since: "1.7.0"
@spec vars(t) :: [variable]
def vars(env)
def vars(%{__struct__: Macro.Env, current_vars: current_vars}) do
Map.keys(current_vars)
end
@doc """
Checks if a variable belongs to the environment.
"""
@doc since: "1.7.0"
@spec has_var?(t, variable) :: boolean()
def has_var?(env, var)
def has_var?(%{__struct__: Macro.Env, current_vars: current_vars}, var) do
Map.has_key?(current_vars, var)
end
@doc """
Returns a keyword list containing the file and line
information as keys.
@@ -169,8 +135,8 @@ defmodule Macro.Env do
env
end
def to_match(%{__struct__: Macro.Env, current_vars: vars} = env) do
%{env | context: :match, prematch_vars: vars}
def to_match(%{__struct__: Macro.Env, prematch_vars: nil, vars: vars} = env) do
%{env | context: :match, match_vars: [], prematch_vars: vars}
end
@doc """
+29 -47
View File
@@ -2,12 +2,6 @@ defmodule Map do
@moduledoc """
A set of functions for working with maps.
Many functions for maps, which implement the `Enumerable` protocol,
are found in the `Enum` module. Additionally, the following functions
for maps are found in `Kernel`:
* `map_size/1`
Maps are the "go to" key-value data structure in Elixir. Maps can be created
with the `%{}` syntax, and key-value pairs can be expressed as `key => value`:
@@ -21,7 +15,7 @@ defmodule Map do
Maps do not impose any restriction on the key type: anything can be a key in a
map. As a key-value structure, maps do not allow duplicated keys. Keys are
compared using the exact-equality operator (`===/2`). If colliding keys are defined
compared using the exact-equality operator (`===`). If colliding keys are defined
in a map literal, the last one prevails.
When the key in a key-value pair is an atom, the `key: value` shorthand syntax
@@ -32,8 +26,8 @@ defmodule Map do
%{:a => 1, :b => 2, "hello" => "world"}
Keys in maps can be accessed through some of the functions in this module
(such as `Map.get/3` or `Map.fetch/2`) or through the `map[]` syntax provided
by the `Access` module:
(such as `Map.get/3` or `Map.fetch/2`) or through the `[]` syntax provided by
the `Access` module:
iex> map = %{a: 1, b: 2}
iex> Map.fetch(map, :a)
@@ -43,9 +37,10 @@ defmodule Map do
iex> map["non_existing_key"]
nil
For accessing atom keys, one may also `map.key`. Note that while `map[key]` will
return `nil` if `map` doesn't contain `key`, `map.key` will raise if `map` doesn't
contain the key `:key`.
The alternative access syntax `map.key` is provided alongside `[]` when the
map has a `:key` key; note that while `map[key]` will return `nil` if `map`
doesn't contain `key`, `map.key` will raise if `map` doesn't contain
the key `:key`.
iex> map = %{foo: "bar", baz: "bong"}
iex> map.foo
@@ -53,13 +48,7 @@ defmodule Map do
iex> map.non_existing_key
** (KeyError) key :non_existing_key not found in: %{baz: "bong", foo: "bar"}
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.
`map.key` is used with maps that hold a predetermined set of atoms keys, which are
expected to always be present. Structs, defined via `defstruct/1`, are one example
of such "static maps", where the keys can also be checked during compile time.
Maps can be pattern matched on. When a map is on the left-hand side of a
Maps can be pattern matched on; when a map is on the left-hand side of a
pattern match, it will match if the map on the right-hand side contains the
keys on the left-hand side and their values match the ones on the left-hand
side. This means that an empty map matches every map.
@@ -91,6 +80,16 @@ defmodule Map do
iex> %{map | three: 3}
** (KeyError) key :three not found
## Modules to work with maps
This module aims to provide functions that perform operations specific to maps
(like accessing keys, updating values, and so on). For traversing maps as
collections, developers should use the `Enum` module that works across a
variety of data types.
The `Kernel` module also provides a few functions to work with maps: for
example, `Kernel.map_size/1` to know the number of key-value pairs in a map or
`Kernel.is_map/1` to know if a term is a map.
"""
@type key :: any
@@ -100,8 +99,6 @@ defmodule Map do
@doc """
Returns all keys from `map`.
Inlined by the compiler.
## Examples
iex> Map.keys(%{a: 1, b: 2})
@@ -114,8 +111,6 @@ defmodule Map do
@doc """
Returns all values from `map`.
Inlined by the compiler.
## Examples
iex> Map.values(%{a: 1, b: 2})
@@ -131,8 +126,6 @@ defmodule Map do
Each key-value pair in the map is converted to a two-element tuple `{key,
value}` in the resulting list.
Inlined by the compiler.
## Examples
iex> Map.to_list(%{a: 1})
@@ -149,7 +142,7 @@ defmodule Map do
## Examples
iex> Map.new()
iex> Map.new
%{}
"""
@@ -165,7 +158,7 @@ defmodule Map do
iex> Map.new([{:b, 1}, {:a, 2}])
%{a: 2, b: 1}
iex> Map.new(a: 1, a: 2, a: 3)
iex> Map.new([a: 1, a: 2, a: 3])
%{a: 3}
"""
@@ -213,8 +206,6 @@ defmodule Map do
@doc """
Returns whether the given `key` exists in the given `map`.
Inlined by the compiler.
## Examples
iex> Map.has_key?(%{a: 1}, :a)
@@ -222,6 +213,7 @@ defmodule Map do
iex> Map.has_key?(%{a: 1}, :b)
false
Inlined by the compiler.
"""
@spec has_key?(map, key) :: boolean
def has_key?(map, key), do: :maps.is_key(key, map)
@@ -232,8 +224,6 @@ defmodule Map do
If `map` contains the given `key` with value `value`, then `{:ok, value}` is
returned. If `map` doesn't contain `key`, `:error` is returned.
Inlined by the compiler.
## Examples
iex> Map.fetch(%{a: 1}, :a)
@@ -241,6 +231,7 @@ defmodule Map do
iex> Map.fetch(%{a: 1}, :b)
:error
Inlined by the compiler.
"""
@spec fetch(map, key) :: {:ok, value} | :error
def fetch(map, key), do: :maps.find(key, map)
@@ -252,8 +243,6 @@ defmodule Map do
If `map` contains the given `key`, the corresponding value is returned. If
`map` doesn't contain `key`, a `KeyError` exception is raised.
Inlined by the compiler.
## Examples
iex> Map.fetch!(%{a: 1}, :a)
@@ -262,7 +251,7 @@ defmodule Map do
** (KeyError) key :b not found in: %{a: 1}
"""
@spec fetch!(map, key) :: value
@spec fetch!(map, key) :: value | no_return
def fetch!(map, key) do
:maps.get(key, map)
end
@@ -294,7 +283,6 @@ defmodule Map do
end
@doc false
@deprecated "Use Map.fetch/2 + Map.put/3 instead"
def replace(map, key, value) do
case map do
%{^key => _value} ->
@@ -314,8 +302,6 @@ defmodule Map do
If `key` is not present in `map`, a `KeyError` exception is raised.
Inlined by the compiler.
## Examples
iex> Map.replace!(%{a: 1, b: 2}, :a, 3)
@@ -324,8 +310,8 @@ defmodule Map do
iex> Map.replace!(%{a: 1}, :b, 2)
** (KeyError) key :b not found in: %{a: 1}
Inlined by the compiler.
"""
@doc since: "1.5.0"
@spec replace!(map, key, value) :: map
def replace!(map, key, value) do
:maps.update(key, value, map)
@@ -477,8 +463,6 @@ defmodule Map do
@doc """
Puts the given `value` under `key` in `map`.
Inlined by the compiler.
## Examples
iex> Map.put(%{a: 1}, :b, 2)
@@ -486,6 +470,7 @@ defmodule Map do
iex> Map.put(%{a: 1, b: 2}, :a, 3)
%{a: 3, b: 2}
Inlined by the compiler.
"""
@spec put(map, key, value) :: map
def put(map, key, value) do
@@ -497,8 +482,6 @@ defmodule Map do
If the `key` does not exist, returns `map` unchanged.
Inlined by the compiler.
## Examples
iex> Map.delete(%{a: 1, b: 2}, :a)
@@ -506,6 +489,7 @@ defmodule Map do
iex> Map.delete(%{b: 2}, :a)
%{b: 2}
Inlined by the compiler.
"""
@spec delete(map, key) :: map
def delete(map, key), do: :maps.remove(key, map)
@@ -521,8 +505,6 @@ defmodule Map do
side into the struct, even if the key is not part of the struct. Instead,
use `Kernel.struct/2`.
Inlined by the compiler.
## Examples
iex> Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4})
@@ -760,7 +742,7 @@ defmodule Map do
(the retrieved value, which can be operated on before being returned) and the
new value to be stored under `key` in the resulting new map. `fun` may also
return `:pop`, which means the current value shall be removed from `map` and
returned (making this function behave like `Map.pop(map, key)`).
returned (making this function behave like `Map.pop(map, key)`.
The returned value is a tuple with the "get" value returned by
`fun` and a new map with the updated value under `key`.
@@ -824,7 +806,7 @@ defmodule Map do
{1, %{}}
"""
@spec get_and_update!(map, key, (value -> {get, value} | :pop)) :: {get, map}
@spec get_and_update!(map, key, (value -> {get, value} | :pop)) :: {get, map} | no_return
when get: term
def get_and_update!(map, key, fun) when is_function(fun, 1) do
value = fetch!(map, key)
@@ -893,7 +875,7 @@ defmodule Map do
@doc false
# TODO: Remove on 2.0
@deprecated "Use Kernel.map_size/1 instead"
# (hard-deprecated in elixir_dispatch)
def size(map) do
map_size(map)
end
+4 -5
View File
@@ -5,7 +5,7 @@ defmodule MapSet do
`MapSet` is the "go to" set data structure in Elixir. A set can be constructed
using `MapSet.new/0`:
iex> MapSet.new()
iex> MapSet.new
#MapSet<[]>
A set can contain any kind of elements, and elements in a set don't have to be
@@ -13,7 +13,7 @@ defmodule MapSet do
inserting an element in a set where it's already present, the insertion is
simply a no-op.
iex> map_set = MapSet.new()
iex> map_set = MapSet.new
iex> MapSet.put(map_set, "foo")
#MapSet<["foo"]>
iex> map_set |> MapSet.put("foo") |> MapSet.put("foo")
@@ -49,7 +49,7 @@ defmodule MapSet do
## Examples
iex> MapSet.new()
iex> MapSet.new
#MapSet<[]>
"""
@@ -210,7 +210,7 @@ defmodule MapSet do
@doc """
Checks if two sets are equal.
The comparison between elements must be done using `===/2`.
The comparison between elements must be done using `===`.
## Examples
@@ -398,7 +398,6 @@ defmodule MapSet do
import Inspect.Algebra
def inspect(map_set, opts) do
opts = %Inspect.Opts{opts | charlists: :as_lists}
concat(["#MapSet<", Inspect.List.inspect(MapSet.to_list(map_set), opts), ">"])
end
end
+324 -534
View File
File diff suppressed because it is too large Load Diff
+73 -65
View File
@@ -4,23 +4,48 @@
#
# ## Implementation
#
# The implementation uses ETS to track all dependencies
# resembling a graph. The keys and what they point to are:
# The implementation uses ets to track all dependencies
# resembling a graph. The graph has the following vertices:
#
# * `:reattach` points to `{name, arity}`
# * `{:local, {name, arity}}` points to `{name, arity}`
# * `{:import, {name, arity}}` points to `Module`
# * `Module` - a module that was invoked via an import
# * `{name, arity}` - a local function/arity pair
# * `{:import, name, arity}` - an invoked function/arity import
# * `:reattach` - points to reattached functions
#
# Those vertices can associate to other vertices as described
# below:
#
# * `{name, arity}`
# * in neighbours: `:reattach`, `{name, arity}`
# * out neighbours: `{:import, name, arity}`
#
# * `{:import, name, arity}`
# * in neighbours: `{name, arity}`
# * out neighbours: `Module`
#
# This is built on top of the internal module tables.
defmodule Module.LocalsTracker do
@moduledoc false
@doc """
Starts the tracker table.
"""
def init do
:ets.new(__MODULE__, [:bag, :public])
end
@doc """
Deletes the tracker table.
"""
def delete(d) do
:ets.delete(d)
end
@doc """
Adds and tracks defaults for a definition into the tracker.
"""
def add_defaults({_set, bag}, _kind, {name, arity} = pair, defaults, meta) do
def add_defaults(d, _kind, {name, arity}, defaults) do
for i <- :lists.seq(arity - defaults, arity - 1) do
put_edge(bag, {:local, {name, i}}, {pair, get_line(meta)})
put_edge(d, {name, i}, {name, arity})
end
:ok
@@ -29,53 +54,58 @@ defmodule Module.LocalsTracker do
@doc """
Adds a local dispatch from-to the given target.
"""
def add_local({_set, bag}, from, to, meta) when is_tuple(from) and is_tuple(to) do
if from != to do
put_edge(bag, {:local, from}, {to, get_line(meta)})
end
:ok
def add_local(d, from, to) when is_tuple(from) and is_tuple(to) do
put_edge(d, from, to)
end
@doc """
Adds an import dispatch to the given target.
"""
def add_import({set, _bag}, function, module, imported)
def add_import(d, function, module, {name, arity})
when is_tuple(function) and is_atom(module) do
put_edge(set, {:import, imported}, module)
tuple = {:import, name, arity}
put_edge(d, tuple, module)
put_edge(d, function, tuple)
:ok
end
@doc """
Yanks a local node. Returns its in and out vertices in a tuple.
"""
def yank({_set, bag}, local) do
:lists.usort(take_out_neighbours(bag, {:local, local}))
def yank(d, local) do
{[], take_out_neighbours(d, local)}
end
@doc """
Reattach a previously yanked node.
"""
def reattach({_set, bag}, tuple, _kind, function, out_neighbours, meta) do
for out_neighbour <- out_neighbours do
put_edge(bag, {:local, function}, out_neighbour)
def reattach(d, tuple, _kind, function, {in_neigh, out_neigh}) do
# Reattach the old function
for from <- in_neigh do
put_edge(d, from, function)
end
for to <- out_neigh do
put_edge(d, function, to)
end
# Make a call from the old function to the new one
if function != tuple do
put_edge(bag, {:local, function}, {tuple, get_line(meta)})
put_edge(d, function, tuple)
end
# Finally marked the new one as reattached
put_edge(bag, :reattach, tuple)
put_edge(d, :reattach, tuple)
:ok
end
# Collecting all conflicting imports with the given functions
@doc false
def collect_imports_conflicts({set, _bag}, all_defined) do
for {pair, _, meta, _} <- all_defined, n = out_neighbour(set, {:import, pair}) do
{meta, {n, pair}}
def collect_imports_conflicts(d, all_defined) do
for {{name, arity}, _, meta, _} <- all_defined,
n = out_neighbours(d, {:import, name, arity}),
n != [] do
{meta, {n, name, arity}}
end
end
@@ -84,33 +114,20 @@ defmodule Module.LocalsTracker do
given, also accounting the expected number of default
clauses a private function have.
"""
def collect_unused_locals({_set, bag}, all_defined, private) do
def collect_unused_locals(d, all_defined, private) do
reachable =
Enum.reduce(all_defined, %{}, fn {pair, kind, _, _}, acc ->
if kind in [:def, :defmacro] do
reachable_from(bag, pair, acc)
reachable_from(d, pair, acc)
else
acc
end
end)
reattached = :lists.usort(out_neighbours(bag, :reattach))
reattached = out_neighbours(d, :reattach)
{unreachable(reachable, reattached, private), collect_warnings(reachable, private)}
end
@doc """
Collect undefined functions based on local calls and existing definitions
"""
def collect_undefined_locals({set, bag}, all_defined) do
undefined =
for {pair, _, _, _} <- all_defined,
{local, line} <- out_neighbours(bag, {:local, pair}),
not :ets.member(set, {:def, local}),
do: {build_meta(line), local}
:lists.usort(undefined)
end
defp unreachable(reachable, reattached, private) do
for {tuple, kind, _, _} <- private,
not reachable?(tuple, kind, reachable, reattached),
@@ -174,42 +191,33 @@ defmodule Module.LocalsTracker do
A private function is only reachable if it has
a public function that it invokes directly.
"""
def reachable_from({_, bag}, local) do
bag
|> reachable_from(local, %{})
def reachable_from(d, vertex) do
d
|> reachable_from(vertex, %{})
|> Map.keys()
end
defp reachable_from(bag, local, vertices) do
vertices = Map.put(vertices, local, true)
defp reachable_from(d, vertex, vertices) do
vertices = Map.put(vertices, vertex, true)
Enum.reduce(out_neighbours(bag, {:local, local}), vertices, fn {local, _line}, acc ->
case acc do
%{^local => true} -> acc
_ -> reachable_from(bag, local, acc)
end
Enum.reduce(out_neighbours(d, vertex), vertices, fn
{_, _} = local, acc ->
case acc do
%{^local => true} -> acc
_ -> reachable_from(d, local, acc)
end
_, acc ->
acc
end)
end
defp get_line(meta), do: Keyword.get(meta, :line)
defp build_meta(nil), do: []
defp build_meta(line), do: [line: line]
## Lightweight digraph implementation
defp put_edge(d, from, to) do
:ets.insert(d, {from, to})
end
defp out_neighbour(d, from) do
try do
:ets.lookup_element(d, from, 2)
catch
:error, :badarg -> nil
end
end
defp out_neighbours(d, from) do
try do
:ets.lookup_element(d, from, 2)
-4
View File
@@ -59,8 +59,6 @@ defmodule Node do
the local node.
Same as `list(:visible)`.
Inlined by the compiler.
"""
@spec list :: [t]
def list do
@@ -74,8 +72,6 @@ defmodule Node do
satisfying the disjunction(s) of the list elements.
For more information, see `:erlang.nodes/1`.
Inlined by the compiler.
"""
@type state :: :visible | :hidden | :connected | :this | :known
@spec list(state | [state]) :: [t]
+127 -160
View File
@@ -1,23 +1,6 @@
defmodule OptionParser do
@moduledoc """
Functions for parsing command line options.
The main function in this module is `parse/2`, which allows
developers to parse a list of arguments into options:
iex> OptionParser.parse(["--debug"], strict: [debug: :boolean])
{[debug: true], [], []}
`OptionParser` provides some conveniences out of the box,
such as aliases and automatic handling of negation switches.
The `parse_head/2` function is an alternative to `parse/2`
which stops parsing as soon as it finds a value that is not
a switch nor a value for a previous switch.
This module also provides low-level functions, such as `next/2`,
for parsing switches manually, as well as `split/1` and `to_argv/1`
for parsing from and converting switches to strings.
This module contains functions to parse command line options.
"""
@type argv :: [String.t()]
@@ -51,16 +34,14 @@ defmodule OptionParser do
When parsing, it is common to list switches and their expected types:
iex> OptionParser.parse(["--debug"], strict: [debug: :boolean])
iex> OptionParser.parse(["--debug"], switches: [debug: :boolean])
{[debug: true], [], []}
iex> OptionParser.parse(["--source", "lib"], strict: [source: :string])
iex> OptionParser.parse(["--source", "lib"], switches: [source: :string])
{[source: "lib"], [], []}
iex> OptionParser.parse(
...> ["--source-path", "lib", "test/enum_test.exs", "--verbose"],
...> strict: [source_path: :string, verbose: :boolean]
...> )
iex> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--verbose"],
...> switches: [source_path: :string, verbose: :boolean])
{[source_path: "lib", verbose: true], ["test/enum_test.exs"], []}
We will explore the valid switches and operation modes of option parser below.
@@ -70,23 +51,24 @@ defmodule OptionParser do
The following options are supported:
* `:switches` or `:strict` - see the "Switch definitions" section below
* `:allow_nonexistent_atoms` - see the "Parsing unknown switches" section below
* `:allow_nonexistent_atoms` - see the "Parsing dynamic switches" section below
* `:aliases` - see the "Aliases" section below
## Switch definitions
Switches can be specified via one of two options:
* `:strict` - defines strict switches. Any switch in `argv` that is not
specified in the list is returned in the invalid options list.
* `:switches` - defines some switches and their types. This function
still attempts to parse switches that are not in this list.
* `:strict` - defines strict switches. Any switch in `argv` that is not
specified in the list is returned in the invalid options list.
Both these options accept a keyword list where the key is an atom
defining the name of the switch and value is the `type` of the
switch (see the "Types" section below for more information).
Both these options accept a keyword list of `{name, type}` tuples where `name`
is an atom defining the name of the switch and `type` is an atom that
specifies the type for the value of this switch (see the "Types" section below
for the possible types and more information about type casting).
Note that you should only supply the `:switches` or the `:strict` option.
Note that you should only supply the `:switches` or the`:strict` option.
If you supply both, an `ArgumentError` exception will be raised.
### Types
@@ -128,52 +110,50 @@ defmodule OptionParser do
iex> OptionParser.parse(["--no-op", "path/to/file"], switches: [op: :boolean])
{[op: false], ["path/to/file"], []}
### Parsing unknown switches
### Parsing dynamic switches
When the `:switches` option is given, `OptionParser` will attempt to parse
unknown switches:
`OptionParser` also includes a dynamic mode where it will attempt to parse
switches dynamically. Such can be done by not specifying the `:switches` or
`:strict` option.
iex> OptionParser.parse(["--debug"], switches: [key: :string])
iex> OptionParser.parse(["--debug"])
{[debug: true], [], []}
Even though we haven't specified `--debug` in the list of switches, it is part
of the returned options. This would also work:
iex> OptionParser.parse(["--debug", "value"], switches: [key: :string])
{[debug: "value"], [], []}
Switches followed by a value will be assigned the value, as a string. Switches
without an argument will be set automatically to `true`. Since we cannot assert
the type of the switch value, it is preferred to use the `:strict` option that
accepts only known switches and always verify their types.
without an argument, like `--debug` in the examples above, will automatically be
set to `true`.
If you do want to parse unknown switches, remember that Elixir converts switches
to atoms. Since atoms are not garbage-collected, OptionParser will only parse
switches that translate to atoms used by the runtime to avoid leaking atoms.
For instance, the code below will discard the `--option-parser-example` switch
because the `:option_parser_example` atom is never used anywhere:
Since Elixir converts switches to atoms, the dynamic mode will only parse
switches that translate to atoms used by the runtime. Therefore, the code below
likely won't parse the given option since the `:option_parser_example` atom is
never used anywhere:
OptionParser.parse(["--option-parser-example"], switches: [debug: :boolean])
OptionParser.parse(["--option-parser-example"])
# The :option_parser_example atom is not used anywhere below
However, the code below would work as long as `:option_parser_example` atom is
used at some point later (or earlier) **in the same module**:
However, the code below does since the `:option_parser_example` atom is used
at some point later (or earlier) on:
{opts, _, _} = OptionParser.parse(["--option-parser-example"], switches: [debug: :boolean])
{opts, _, _} = OptionParser.parse(["--option-parser-example"])
opts[:option_parser_example]
In other words, Elixir will do the correct thing and only parse options that are
used by the runtime, ignoring all others. If you would like to parse all switches,
regardless if they exist or not, you can force creation of atoms by passing
`allow_nonexistent_atoms: true` as option. Use this option with care. It is only
useful when you are building command-line applications that receive
dynamically-named arguments and must be avoided in long-running systems.
In other words, when using dynamic mode, Elixir will do the correct thing and
only parse options that are used by the runtime, ignoring all others. If you
would like to parse all switches, regardless if they exist or not, you can
force creation of atoms by passing `allow_nonexistent_atoms: true` as option.
Such option is useful when you are building command-line applications that
receive dynamically-named arguments but must be used with care on long-running
systems.
Switches followed by a value will be assigned the value, as a string.
Switches without an argument, like `--debug` in the examples above, will
automatically be set to `true`.
## Aliases
A set of aliases can be specified in the `:aliases` option:
iex> OptionParser.parse(["-d"], aliases: [d: :debug], strict: [debug: :boolean])
iex> OptionParser.parse(["-d"], aliases: [d: :debug])
{[debug: true], [], []}
## Examples
@@ -183,10 +163,8 @@ defmodule OptionParser do
iex> OptionParser.parse(["--unlock", "path/to/file"], strict: [unlock: :boolean])
{[unlock: true], ["path/to/file"], []}
iex> OptionParser.parse(
...> ["--unlock", "--limit", "0", "path/to/file"],
...> strict: [unlock: :boolean, limit: :integer]
...> )
iex> OptionParser.parse(["--unlock", "--limit", "0", "path/to/file"],
...> strict: [unlock: :boolean, limit: :integer])
{[unlock: true, limit: 0], ["path/to/file"], []}
iex> OptionParser.parse(["--limit", "3"], strict: [limit: :integer])
@@ -204,16 +182,11 @@ defmodule OptionParser do
iex> OptionParser.parse(["--unknown", "xyz"], strict: [])
{[], ["xyz"], [{"--unknown", nil}]}
iex> OptionParser.parse(
...> ["--limit", "3", "--unknown", "xyz"],
...> switches: [limit: :integer]
...> )
iex> OptionParser.parse(["--limit", "3", "--unknown", "xyz"],
...> switches: [limit: :integer])
{[limit: 3, unknown: "xyz"], [], []}
iex> OptionParser.parse(
...> ["--unlock", "path/to/file", "--unlock", "path/to/another/file"],
...> strict: [unlock: :keep]
...> )
iex> OptionParser.parse(["--unlock", "path/to/file", "--unlock", "path/to/another/file"], strict: [unlock: :keep])
{[unlock: "path/to/file", unlock: "path/to/another/file"], [], []}
"""
@@ -244,17 +217,14 @@ defmodule OptionParser do
** (OptionParser.ParseError) 1 error found!
--unknown : Unknown option
iex> OptionParser.parse!(
...> ["-l", "xyz", "-f", "bar"],
...> switches: [limit: :integer, foo: :integer],
...> aliases: [l: :limit, f: :foo]
...> )
iex> OptionParser.parse!(["-l", "xyz", "-f", "bar"],
...> switches: [limit: :integer, foo: :integer], aliases: [l: :limit, f: :foo])
** (OptionParser.ParseError) 2 errors found!
-l : Expected type integer, got "xyz"
-f : Expected type integer, got "bar"
"""
@spec parse!(argv, options) :: {parsed, argv}
@spec parse!(argv, options) :: {parsed, argv} | no_return
def parse!(argv, opts \\ []) when is_list(argv) and is_list(opts) do
case parse(argv, opts) do
{parsed, args, []} -> {parsed, args}
@@ -270,16 +240,12 @@ defmodule OptionParser do
## Example
iex> OptionParser.parse_head(
...> ["--source", "lib", "test/enum_test.exs", "--verbose"],
...> switches: [source: :string, verbose: :boolean]
...> )
iex> OptionParser.parse_head(["--source", "lib", "test/enum_test.exs", "--verbose"],
...> switches: [source: :string, verbose: :boolean])
{[source: "lib"], ["test/enum_test.exs", "--verbose"], []}
iex> OptionParser.parse_head(
...> ["--verbose", "--source", "lib", "test/enum_test.exs", "--unlock"],
...> switches: [source: :string, verbose: :boolean, unlock: :boolean]
...> )
iex> OptionParser.parse_head(["--verbose", "--source", "lib", "test/enum_test.exs", "--unlock"],
...> switches: [source: :string, verbose: :boolean, unlock: :boolean])
{[verbose: true, source: "lib"], ["test/enum_test.exs", "--unlock"], []}
"""
@@ -299,29 +265,22 @@ defmodule OptionParser do
## Examples
iex> OptionParser.parse_head!(
...> ["--source", "lib", "path/to/file", "--verbose"],
...> switches: [source: :string, verbose: :boolean]
...> )
iex> OptionParser.parse_head!(["--source", "lib", "path/to/file", "--verbose"],
...> switches: [source: :string, verbose: :boolean])
{[source: "lib"], ["path/to/file", "--verbose"]}
iex> OptionParser.parse_head!(
...> ["--number", "lib", "test/enum_test.exs", "--verbose"],
...> strict: [number: :integer]
...> )
iex> OptionParser.parse_head!(["--number", "lib", "test/enum_test.exs", "--verbose"],
...> strict: [number: :integer])
** (OptionParser.ParseError) 1 error found!
--number : Expected type integer, got "lib"
iex> OptionParser.parse_head!(
...> ["--verbose", "--source", "lib", "test/enum_test.exs", "--unlock"],
...> strict: [verbose: :integer, source: :integer]
...> )
iex> OptionParser.parse_head!(["--verbose", "--source", "lib", "test/enum_test.exs", "--unlock"],
...> strict: [verbose: :integer, source: :integer])
** (OptionParser.ParseError) 2 errors found!
--verbose : Missing argument of type integer
--source : Expected type integer, got "lib"
"""
@spec parse_head!(argv, options) :: {parsed, argv}
@spec parse_head!(argv, options) :: {parsed, argv} | no_return
def parse_head!(argv, opts \\ []) when is_list(argv) and is_list(opts) do
case parse_head(argv, opts) do
{parsed, args, []} -> {parsed, args}
@@ -346,8 +305,8 @@ defmodule OptionParser do
do_parse(rest, config, opts, args, [{option, value} | invalid], all?)
{:undefined, option, _value, rest} ->
invalid = if config.strict?, do: [{option, nil} | invalid], else: invalid
do_parse(rest, config, opts, args, invalid, all?)
# the option does not exist (for strict cases)
do_parse(rest, config, opts, args, [{option, nil} | invalid], all?)
{:error, ["--" | rest]} ->
{Enum.reverse(opts), Enum.reverse(args, rest), Enum.reverse(invalid)}
@@ -376,7 +335,7 @@ defmodule OptionParser do
(returned when the value cannot be parsed according to the switch type)
* `{:undefined, key, value, rest}` - the option `key` is undefined
(returned in strict mode when the switch is unknown or on nonexistent atoms)
(returned in strict mode when the switch is unknown)
* `{:error, rest}` - there are no switches at the head of the given `argv`
@@ -410,21 +369,15 @@ defmodule OptionParser do
# Handles --foo or --foo=bar
defp next_with_config(["--" <> option | rest], config) do
{option, value} = split_option(option)
if String.contains?(option, ["_"]) do
{:undefined, "--" <> option, value, rest}
else
tagged = tag_option(option, config)
next_tagged(tagged, value, "--" <> option, rest, config)
end
tagged = tag_option(option, config)
next_tagged(tagged, value, "--" <> option, rest, config)
end
# Handles -a, -abc, -abc=something
defp next_with_config(["-" <> option | rest] = argv, config) do
%{allow_nonexistent_atoms?: allow_nonexistent_atoms?} = config
%{aliases: aliases, allow_nonexistent_atoms?: allow_nonexistent_atoms?} = config
{option, value} = split_option(option)
original = "-" <> option
letters = String.graphemes(option)
cond do
is_nil(value) and negative_number?(original) ->
@@ -433,22 +386,21 @@ defmodule OptionParser do
String.contains?(option, ["-", "_"]) ->
{:undefined, original, value, rest}
tl(letters) == [] ->
# We have a regular one-letter alias here
tagged = tag_oneletter_alias(option, config)
next_tagged(tagged, value, original, rest, config)
true ->
String.length(option) > 1 ->
key = get_option_key(option, allow_nonexistent_atoms?)
option_key = config.aliases[key]
option_key = aliases[key]
if key && option_key do
# TODO: Remove this in Elixir v2.0
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(letters, value) ++ rest, config)
next_with_config(expand_multiletter_alias(option, value) ++ rest, config)
end
true ->
# We have a regular one-letter alias here
tagged = tag_oneletter_alias(option, config)
next_tagged(tagged, value, original, rest, config)
end
end
@@ -456,17 +408,12 @@ defmodule OptionParser do
{:error, argv}
end
defp next_tagged(:unknown, value, original, rest, _) do
{value, _kinds, rest} = normalize_value(value, [], rest)
{:undefined, original, value, rest}
end
defp next_tagged({tag, option}, value, original, rest, %{switches: switches, strict?: strict?}) do
if strict? and not Keyword.has_key?(switches, option) do
defp next_tagged(tagged, value, original, rest, %{switches: switches, strict?: strict?}) do
if strict? and not option_defined?(tagged, switches) do
{:undefined, original, value, rest}
else
{kinds, value} = normalize_tag(tag, option, value, switches)
{value, kinds, rest} = normalize_value(value, kinds, rest)
{option, kinds, value} = normalize_option(tagged, value, switches)
{value, kinds, rest} = normalize_value(value, kinds, rest, strict?)
case validate_option(value, kinds) do
{:ok, new_value} -> {:ok, option, new_value, rest}
@@ -485,17 +432,17 @@ defmodule OptionParser do
It is advised to pass to `to_argv/2` the same set of `options`
given to `parse/2`. Some switches can only be reconstructed
correctly with the `:switches` information in hand.
correctly with the `switches` information in hand.
## Examples
iex> OptionParser.to_argv(foo_bar: "baz")
iex> OptionParser.to_argv([foo_bar: "baz"])
["--foo-bar", "baz"]
iex> OptionParser.to_argv(bool: true, bool: false, discarded: nil)
iex> OptionParser.to_argv([bool: true, bool: false, discarded: nil])
["--bool", "--no-bool"]
Some switches will output different values based on the switches
types:
flag:
iex> OptionParser.to_argv([number: 2], switches: [])
["--number", "2"]
@@ -504,8 +451,8 @@ defmodule OptionParser do
"""
@spec to_argv(Enumerable.t(), options) :: argv
def to_argv(enum, options \\ []) do
switches = Keyword.get(options, :switches, [])
def to_argv(enum, opts \\ []) do
switches = Keyword.get(opts, :switches, [])
Enum.flat_map(enum, fn
{_key, nil} -> []
@@ -596,8 +543,6 @@ defmodule OptionParser do
{strict, true}
true ->
# TODO: Remove this in Elixir v2.0
IO.warn("not passing the :switches or :strict option to OptionParser is deprecated")
{[], false}
end
@@ -624,7 +569,7 @@ defmodule OptionParser do
:count in kinds ->
case value do
nil -> {false, 1}
1 -> {false, value}
_ -> {true, value}
end
@@ -664,9 +609,10 @@ defmodule OptionParser do
end
end
defp tag_option("no-" <> option = original, config) do
%{switches: switches, allow_nonexistent_atoms?: allow_nonexistent_atoms?} = config
defp tag_option("no-" <> option = original, %{
switches: switches,
allow_nonexistent_atoms?: allow_nonexistent_atoms?
}) do
cond do
(negated = get_option_key(option, allow_nonexistent_atoms?)) &&
:boolean in List.wrap(switches[negated]) ->
@@ -680,9 +626,7 @@ defmodule OptionParser do
end
end
defp tag_option(option, config) do
%{allow_nonexistent_atoms?: allow_nonexistent_atoms?} = config
defp tag_option(option, %{allow_nonexistent_atoms?: allow_nonexistent_atoms?}) do
if option_key = get_option_key(option, allow_nonexistent_atoms?) do
{:default, option_key}
else
@@ -690,9 +634,11 @@ defmodule OptionParser do
end
end
defp tag_oneletter_alias(alias, config) when is_binary(alias) do
%{aliases: aliases, allow_nonexistent_atoms?: allow_nonexistent_atoms?} = config
defp tag_oneletter_alias(alias, %{
aliases: aliases,
allow_nonexistent_atoms?: allow_nonexistent_atoms?
})
when is_binary(alias) do
if option_key = aliases[to_existing_key(alias, allow_nonexistent_atoms?)] do
{:default, option_key}
else
@@ -700,39 +646,59 @@ defmodule OptionParser do
end
end
defp expand_multiletter_alias(letters, value) do
defp expand_multiletter_alias(letters, value) when is_binary(letters) do
{last, expanded} =
letters
|> String.codepoints()
|> Enum.map(&("-" <> &1))
|> List.pop_at(-1)
expanded ++ [last <> if(value, do: "=" <> value, else: "")]
end
defp normalize_tag(:negated, option, value, switches) do
defp option_defined?(:unknown, _switches) do
false
end
defp option_defined?({:negated, option}, switches) do
Keyword.has_key?(switches, option)
end
defp option_defined?({:default, option}, switches) do
Keyword.has_key?(switches, option)
end
defp normalize_option(:unknown, value, _switches) do
{nil, [:invalid], value}
end
defp normalize_option({:negated, option}, value, switches) do
if value do
{[:invalid], value}
{option, [:invalid], value}
else
{List.wrap(switches[option]), false}
{option, List.wrap(switches[option]), false}
end
end
defp normalize_tag(:default, option, value, switches) do
{List.wrap(switches[option]), value}
defp normalize_option({:default, option}, value, switches) do
{option, List.wrap(switches[option]), value}
end
defp normalize_value(nil, kinds, t) do
defp normalize_value(nil, kinds, t, strict?) do
cond do
:boolean in kinds ->
{true, kinds, t}
:count in kinds ->
{nil, kinds, t}
{1, kinds, t}
value_in_tail?(t) ->
[h | t] = t
{h, kinds, t}
kinds == [] and strict? ->
{nil, kinds, t}
kinds == [] ->
{true, kinds, t}
@@ -741,7 +707,7 @@ defmodule OptionParser do
end
end
defp normalize_value(value, kinds, t) do
defp normalize_value(value, kinds, t, _strict?) do
{value, kinds, t}
end
@@ -759,14 +725,15 @@ defmodule OptionParser do
end
defp to_underscore(option), do: to_underscore(option, <<>>)
defp to_underscore("_" <> _rest, _acc), do: nil
defp to_underscore("-" <> rest, acc), do: to_underscore(rest, acc <> "_")
defp to_underscore(<<c>> <> rest, acc), do: to_underscore(rest, <<acc::binary, c>>)
defp to_underscore(<<>>, acc), do: acc
defp get_option_key(option, allow_nonexistent_atoms?) do
option
|> to_underscore()
|> to_existing_key(allow_nonexistent_atoms?)
if string = to_underscore(option) do
to_existing_key(string, allow_nonexistent_atoms?)
end
end
defp to_existing_key(option, true), do: String.to_atom(option)
+16 -28
View File
@@ -12,7 +12,7 @@ defmodule Path do
that require it (like `wildcard/2` and `expand/1`).
"""
@type t :: IO.chardata()
@type t :: :unicode.chardata()
@doc """
Converts the given path to an absolute one. Unlike
@@ -30,16 +30,15 @@ defmodule Path do
### Windows
Path.absname("foo")
Path.absname("foo").
#=> "D:/usr/local/foo"
Path.absname("../x")
Path.absname("../x").
#=> "D:/usr/local/../x"
"""
@spec absname(t) :: binary
def absname(path) do
absname(path, File.cwd!())
absname(path, System.cwd!())
end
@doc """
@@ -151,7 +150,7 @@ defmodule Path do
"""
@spec expand(t) :: binary
def expand(path) do
expand_dot(absname(expand_home(path), File.cwd!()))
expand_dot(absname(expand_home(path), System.cwd!()))
end
@doc """
@@ -172,15 +171,14 @@ defmodule Path do
#=> "/quux/baz/foo/bar"
Path.expand("foo/bar/../bar", "/baz")
#=> "/baz/foo/bar"
"/baz/foo/bar"
Path.expand("/foo/bar/../bar", "/baz")
#=> "/foo/bar"
"/foo/bar"
"""
@spec expand(t, t) :: binary
def expand(path, relative_to) do
expand_dot(absname(absname(expand_home(path), expand_home(relative_to)), File.cwd!()))
expand_dot(absname(absname(expand_home(path), expand_home(relative_to)), System.cwd!()))
end
@doc """
@@ -594,23 +592,24 @@ defmodule Path do
Traverses paths according to the given `glob` expression and returns a
list of matches.
The wildcard looks like an ordinary path, except that the following
"wildcard characters" are interpreted in a special way:
The wildcard looks like an ordinary path, except that certain
"wildcard characters" are interpreted in a special way. The
following characters are special:
* `?` - matches one character.
* `?` - matches one character
* `*` - matches any number of characters up to the end of the filename, the
next dot, or the next slash.
next dot, or the next slash
* `**` - two adjacent `*`'s used as a single pattern will match all
files and zero or more directories and subdirectories.
files and zero or more directories and subdirectories
* `[char1,char2,...]` - matches any of the characters listed; two
characters separated by a hyphen will match a range of characters.
Do not add spaces before and after the comma as it would then match
paths containing the space character itself.
* `{item1,item2,...}` - matches one of the alternatives.
* `{item1,item2,...}` - matches one of the alternatives
Do not add spaces before and after the comma as it would then match
paths containing the space character itself.
@@ -618,19 +617,8 @@ defmodule Path do
exactly the same character in the same position will match. Note
that matching is case-sensitive: `"a"` will not match `"A"`.
Directory separators must always be written as `/`, even on Windows.
You may call `Path.expand/1` to normalize the path before invoking
this function.
By default, the patterns `*` and `?` do not match files starting
with a dot `.`. See the `:match_dot` option in the "Options" section
below.
## Options
* `:match_dot` - (boolean) if `false`, the special wildcard characters `*` and `?`
will not match files starting with a dot (`.`). If `true`, files starting with
a `.` will not be treated specially. Defaults to `false`.
with a dot `.` unless `match_dot: true` is given in `opts`.
## Examples
+17 -24
View File
@@ -8,12 +8,12 @@ defmodule Port do
## Example
iex> port = Port.open({:spawn, "cat"}, [:binary])
iex> send(port, {self(), {:command, "hello"}})
iex> send(port, {self(), {:command, "world"}})
iex> send port, {self(), {:command, "hello"}}
iex> send port, {self(), {:command, "world"}}
iex> flush()
{#Port<0.1444>, {:data, "hello"}}
{#Port<0.1444>, {:data, "world"}}
iex> send(port, {self(), :close})
iex> send port, {self(), :close}
:ok
iex> flush()
{#Port<0.1464>, :closed}
@@ -117,7 +117,7 @@ defmodule Port do
reimplementing core part of the Runtime System, such as the `:user` and
`:shell` processes.
## Zombie OS processes
## Zombie processes
A port can be closed via the `close/1` function or by sending a `{pid, :close}`
message. However, if the VM crashes, a long-running program started by the port
@@ -127,11 +127,11 @@ defmodule Port do
While most UNIX command line tools will exit once its communication channels
are closed, not all command line applications will do so. While we encourage
graceful termination by detecting if stdin/stdout has been closed, we do not
always have control over how third-party software terminates. In those cases,
always have control over how 3rd party software terminates. In those cases,
you can wrap the application in a script that checks for stdin. Here is such
script in Bash:
script in bash:
#!/bin/bash
#!/bin/sh
"$@" &
pid=$!
while read line ; do
@@ -141,17 +141,13 @@ defmodule Port do
Now instead of:
Port.open(
{:spawn_executable, "/path/to/program"},
args: ["a", "b", "c"]
)
Port.open({:spawn_executable, "/path/to/program"},
[args: ["a", "b", "c"]])
You may invoke:
Port.open(
{:spawn_executable, "/path/to/wrapper"},
args: ["/path/to/program", "a", "b", "c"]
)
Port.open({:spawn_executable, "/path/to/wrapper"},
[args: ["/path/to/program", "a", "b", "c"]])
"""
@@ -181,8 +177,8 @@ defmodule Port do
Inlined by the compiler.
"""
@spec open(name, list) :: port
def open(name, options) do
:erlang.open_port(name, options)
def open(name, settings) do
:erlang.open_port(name, settings)
end
@doc """
@@ -240,9 +236,8 @@ defmodule Port do
def info(port, :registered_name) do
case :erlang.port_info(port, :registered_name) do
:undefined -> nil
[] -> {:registered_name, []}
other -> other
other -> nillify(other)
end
end
@@ -265,12 +260,11 @@ defmodule Port do
or `{name, node}` (when monitoring by a port name);
* `reason` is the exit reason.
See `:erlang.monitor/2` for more information.
See `:erlang.monitor/2` for more info.
Inlined by the compiler.
"""
@doc since: "1.6.0"
@spec monitor(port | {name, node} | name) :: reference when name: atom
@spec monitor(port | {name :: atom, node :: atom} | name :: atom) :: reference
def monitor(port) do
:erlang.monitor(:port, port)
end
@@ -282,11 +276,10 @@ defmodule Port do
obtained by calling `monitor/1`, that monitoring is turned off.
If the monitoring is already turned off, nothing happens.
See `:erlang.demonitor/2` for more information.
See `:erlang.demonitor/2` for more info.
Inlined by the compiler.
"""
@doc since: "1.6.0"
@spec demonitor(reference, options :: [:flush | :info]) :: boolean
defdelegate demonitor(monitor_ref, options \\ []), to: :erlang
+44 -165
View File
@@ -14,27 +14,19 @@ defmodule Process do
While this module provides low-level conveniences to work with processes,
developers typically use abstractions such as `Agent`, `GenServer`,
`Registry`, `Supervisor` and `Task` for building their systems and
`Registry`, `Supervisor` and `Task` for building their systems and
resort to this module for gathering information, trapping exits, links
and monitoring.
"""
@typedoc """
A process destination.
A remote or local PID, a local port, a locally registered name, or a tuple in
the form of `{registered_name, node}` for a registered name at another node.
"""
@type dest :: pid | port | (registered_name :: atom) | {registered_name :: atom, node}
@doc """
Tells whether the given process is alive on the local node.
Tells whether the given process is alive.
If the process identified by `pid` is alive (that is, it's not exiting and has
not exited yet) than this function returns `true`. Otherwise, it returns
`false`.
`pid` must refer to a process running on the local node or `ArgumentError` is raised.
`pid` must refer to a process running on the local node.
Inlined by the compiler.
"""
@@ -52,17 +44,6 @@ defmodule Process do
@doc """
Returns the value for the given `key` in the process dictionary,
or `default` if `key` is not set.
## Examples
# Assuming :locale was not set
iex> Process.get(:locale, "pt")
"pt"
iex> Process.put(:locale, "fr")
nil
iex> Process.get(:locale, "pt")
"fr"
"""
@spec get(term, default :: term) :: term
def get(key, default \\ nil) do
@@ -76,17 +57,6 @@ defmodule Process do
Returns all keys in the process dictionary.
Inlined by the compiler.
## Examples
# Assuming :locale was not set
iex> :locale in Process.get_keys()
false
iex> Process.put(:locale, "pt")
nil
iex> :locale in Process.get_keys()
true
"""
@spec get_keys() :: [term]
defdelegate get_keys(), to: :erlang
@@ -103,15 +73,15 @@ defmodule Process do
Stores the given `key`-`value` pair in the process dictionary.
The return value of this function is the value that was previously stored
under `key`, or `nil` in case no value was stored under it.
under `key`, or `nil` in case no value was stored under `key`.
## Examples
# Assuming :locale was not set
iex> Process.put(:locale, "en")
nil
iex> Process.put(:locale, "fr")
"en"
Process.put(:locale, "en")
#=> nil
Process.put(:locale, "fr")
#=> "en"
"""
@spec put(term, term) :: term | nil
@@ -127,11 +97,11 @@ defmodule Process do
## Examples
iex> Process.put(:comments, ["comment", "other comment"])
iex> Process.delete(:comments)
["comment", "other comment"]
iex> Process.delete(:comments)
nil
Process.put(:comments, ["comment", "other comment"])
Process.delete(:comments)
#=> ["comment", "other comment"]
Process.delete(:comments)
#=> nil
"""
@spec delete(term) :: term | nil
@@ -191,10 +161,10 @@ defmodule Process do
In other words, **do not**:
Task.start_link(fn ->
Task.start_link fn ->
do_something()
...
end)
end
# Wait until work is done
Process.sleep(2000)
@@ -202,18 +172,16 @@ defmodule Process do
But **do**:
parent = self()
Task.start_link(fn ->
Task.start_link fn ->
do_something()
send(parent, :work_is_done)
send parent, :work_is_done
...
end)
end
receive do
:work_is_done -> :ok
after
# Optional timeout
30_000 -> :timeout
30_000 -> :timeout # Optional timeout
end
For cases like the one above, `Task.async/1` and `Task.await/2` are
@@ -222,9 +190,9 @@ defmodule Process do
Similarly, if you are waiting for a process to terminate,
monitor that process instead of sleeping. **Do not**:
Task.start_link(fn ->
Task.start_link fn ->
...
end)
end
# Wait until task terminates
Process.sleep(2000)
@@ -232,17 +200,15 @@ defmodule Process do
Instead **do**:
{:ok, pid} =
Task.start_link(fn ->
Task.start_link fn ->
...
end)
end
ref = Process.monitor(pid)
receive do
{:DOWN, ^ref, _, _, _} -> :task_is_down
after
# Optional timeout
30_000 -> :timeout
30_000 -> :timeout # Optional timeout
end
"""
@@ -254,13 +220,7 @@ defmodule Process do
end
@doc """
Sends a message to the given `dest`.
`dest` may be a remote or local PID, a local port, a locally
registered name, or a tuple in the form of `{registered_name, node}` for a
registered name at another node.
Inlined by the compiler.
Sends a message to the given process.
## Options
@@ -278,9 +238,10 @@ defmodule Process do
iex> Process.send({:name, :node_that_does_not_exist}, :hi, [:noconnect])
:noconnect
Inlined by the compiler.
"""
@spec send(dest, msg, [option]) :: :ok | :noconnect | :nosuspend
when dest: dest(),
when dest: pid | port | atom | {atom, node},
msg: any,
option: :noconnect | :nosuspend
defdelegate send(dest, msg, options), to: :erlang
@@ -293,9 +254,6 @@ defmodule Process do
which is looked up at the time of delivery. No error is produced if the name does
not refer to a process.
The message is not sent immediately. Therefore, `dest` can receive other messages
in-between even when `time` is `0`.
This function returns a timer reference, which can be read with `read_timer/1`
or canceled with `cancel_timer/1`.
@@ -338,8 +296,6 @@ defmodule Process do
Even if the timer had expired and the message was sent, this function does not
tell you if the timeout message has arrived at its destination yet.
Inlined by the compiler.
## Options
* `:async` - (boolean) when `false`, the request for cancellation is
@@ -357,6 +313,7 @@ defmodule Process do
cancellation has been performed. If `:async` is `true` and `:info` is
`false`, no message is sent. Defaults to `true`.
Inlined by the compiler.
"""
@spec cancel_timer(reference, options) :: non_neg_integer | false | :ok
when options: [async: boolean, info: boolean]
@@ -401,14 +358,6 @@ defmodule Process do
check `:erlang.spawn_opt/4`.
Inlined by the compiler.
## Examples
Process.spawn(fn -> 1 + 2 end, [:monitor])
#=> {#PID<0.93.0>, #Reference<0.18808174.1939079169.202418>}
Process.spawn(fn -> 1 + 2 end, [:link])
#=> #PID<0.95.0>
"""
@spec spawn((() -> any), spawn_opts) :: pid | {pid, reference}
defdelegate spawn(fun, opts), to: :erlang, as: :spawn_opt
@@ -445,29 +394,12 @@ defmodule Process do
a PID) or `{name, node}` (if monitoring a remote or local name);
* `reason` is the exit reason.
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)
for an example. See `:erlang.monitor/2` for more information.
See [the need for monitoring](http://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
for an example. See `:erlang.monitor/2` for more info.
Inlined by the compiler.
## Examples
pid = spawn(fn -> 1 + 2 end)
#=> #PID<0.118.0>
Process.monitor(pid)
#=> #Reference<0.906660723.3006791681.40191>
Process.exit(pid, :kill)
#=> true
receive do
msg -> msg
end
#=> {:DOWN, #Reference<0.906660723.3006791681.40191>, :process, #PID<0.118.0>, :noproc}
"""
@spec monitor(pid | {name, node} | name) :: reference when name: atom
@spec monitor(pid | {name :: atom, node :: atom} | name :: atom) :: reference
def monitor(item) do
:erlang.monitor(:process, item)
end
@@ -479,17 +411,9 @@ defmodule Process do
obtained by calling `monitor/1`, that monitoring is turned off.
If the monitoring is already turned off, nothing happens.
See `:erlang.demonitor/2` for more information.
See `:erlang.demonitor/2` for more info.
Inlined by the compiler.
## Examples
pid = spawn(fn -> 1 + 2 end)
ref = Process.monitor(pid)
Process.demonitor(ref)
#=> true
"""
@spec demonitor(reference, options :: [:flush | :info]) :: boolean
defdelegate demonitor(monitor_ref, options \\ []), to: :erlang
@@ -502,15 +426,9 @@ defmodule Process do
alive. This means that for such process, `alive?/1` will return `false` but
its PID will be part of the list of PIDs returned by this function.
See `:erlang.processes/0` for more information.
See `:erlang.processes/0` for more info.
Inlined by the compiler.
## Examples
Process.list()
#=> [#PID<0.0.0>, #PID<0.1.0>, #PID<0.2.0>, #PID<0.3.0>, ...]
"""
@spec list() :: [pid]
defdelegate list(), to: :erlang, as: :processes
@@ -533,7 +451,7 @@ defmodule Process do
emit an exit signal to all its other linked processes. The behaviour when
`pid1` is trapping exits is described in `exit/2`.
See `:erlang.link/1` for more information.
See `:erlang.link/1` for more info.
Inlined by the compiler.
"""
@@ -549,7 +467,7 @@ defmodule Process do
The return value of this function is always `true`.
See `:erlang.unlink/1` for more information.
See `:erlang.unlink/1` for more info.
Inlined by the compiler.
"""
@@ -576,15 +494,6 @@ defmodule Process do
* `true`
* `:undefined`
## Examples
Process.register(self(), :test)
#=> true
send(:test, :hello)
#=> :hello
send(:wrong_name, :hello)
#=> ** (ArgumentError) argument error
"""
@spec register(pid | port, atom) :: true
def register(pid_or_port, name)
@@ -612,16 +521,6 @@ defmodule Process do
to any PID or port.
Inlined by the compiler.
## Examples
Process.register(self(), :test)
#=> true
Process.unregister(:test)
#=> true
Process.unregister(:wrong_name)
#=> ** (ArgumentError) argument error
"""
@spec unregister(atom) :: true
defdelegate unregister(name), to: :erlang
@@ -630,16 +529,7 @@ defmodule Process do
Returns the PID or port identifier registered under `name` or `nil` if the
name is not registered.
See `:erlang.whereis/1` for more information.
## Examples
Process.register(self(), :test)
Process.whereis(:test)
#=> #PID<0.84.0>
Process.whereis(:wrong_name)
#=> nil
See `:erlang.whereis/1` for more info.
"""
@spec whereis(atom) :: pid | port | nil
def whereis(name) do
@@ -650,12 +540,6 @@ defmodule Process do
Returns the PID of the group leader for the calling process.
Inlined by the compiler.
## Examples
Process.group_leader()
#=> #PID<0.53.0>
"""
@spec group_leader() :: pid
defdelegate group_leader(), to: :erlang
@@ -677,13 +561,6 @@ defmodule Process do
Returns a list of names which have been registered using `register/2`.
Inlined by the compiler.
## Examples
Process.register(self(), :test)
Process.registered()
#=> [:test, :elixir_config, :inet_db, ...]
"""
@spec registered() :: [atom]
defdelegate registered(), to: :erlang
@@ -699,7 +576,9 @@ defmodule Process do
Returns the old value of `flag`.
See `:erlang.process_flag/2` for more information.
See `:erlang.process_flag/2` for more info.
Note that `flag` values `:max_heap_size` and `:message_queue_data` are only available since OTP 19.
Inlined by the compiler.
"""
@@ -726,7 +605,7 @@ defmodule Process do
The allowed values for `flag` are only a subset of those allowed in `flag/2`,
namely `:save_calls`.
See `:erlang.process_flag/3` for more information.
See `:erlang.process_flag/3` for more info.
Inlined by the compiler.
"""
@@ -739,9 +618,9 @@ defmodule Process do
Use this only for debugging information.
See `:erlang.process_info/1` for more information.
See `:erlang.process_info/1` for more info.
"""
@spec info(pid) :: keyword | nil
@spec info(pid) :: keyword
def info(pid) do
nillify(:erlang.process_info(pid))
end
@@ -750,7 +629,7 @@ defmodule Process do
Returns information about the process identified by `pid`,
or returns `nil` if the process is not alive.
See `:erlang.process_info/2` for more information.
See `:erlang.process_info/2` for more info.
"""
@spec info(pid, atom | [atom]) :: {atom, term} | [{atom, term}] | nil
def info(pid, spec)
@@ -775,7 +654,7 @@ defmodule Process do
which is useful if the process does not expect to receive any messages
in the near future.
See `:erlang.hibernate/3` for more information.
See `:erlang.hibernate/3` for more info.
Inlined by the compiler.
"""
+173 -82
View File
@@ -22,7 +22,7 @@ defmodule Protocol do
type_args = :lists.map(fn _ -> quote(do: term) end, :lists.seq(2, arity))
type_args = [quote(do: t) | type_args]
varify = fn pos -> Macro.var(String.to_atom("arg" <> Integer.to_string(pos)), __MODULE__) end
varify = fn pos -> Macro.var(String.to_atom("var" <> Integer.to_string(pos)), __MODULE__) end
call_args = :lists.map(varify, :lists.seq(2, arity))
call_args = [quote(do: term) | call_args]
@@ -44,7 +44,7 @@ defmodule Protocol do
# Convert the spec to callback if possible,
# otherwise generate a dummy callback
Module.spec_to_callback(__MODULE__, {name, arity}) ||
Protocol.__spec__?(__MODULE__, name, arity) ||
@callback unquote(name)(unquote_splicing(type_args)) :: term
end
end
@@ -58,7 +58,7 @@ defmodule Protocol do
Returns `:ok` if so, otherwise raises `ArgumentError`.
"""
@spec assert_protocol!(module) :: :ok
@spec assert_protocol!(module) :: :ok | no_return
def assert_protocol!(module) do
assert_protocol!(module, "")
end
@@ -85,7 +85,7 @@ defmodule Protocol do
Returns `:ok` if so, otherwise raises `ArgumentError`.
"""
@spec assert_impl!(module, module) :: :ok
@spec assert_impl!(module, module) :: :ok | no_return
def assert_impl!(protocol, base) do
assert_impl!(protocol, base, "")
end
@@ -168,7 +168,8 @@ defmodule Protocol do
"""
defmacro derive(protocol, module, options \\ []) do
quote do
Protocol.__derive__([{unquote(protocol), unquote(options)}], unquote(module), __ENV__)
module = unquote(module)
Protocol.__derive__([{unquote(protocol), unquote(options)}], module, __ENV__)
end
end
@@ -185,7 +186,7 @@ defmodule Protocol do
## Examples
# Get Elixir's ebin directory path and retrieve all protocols
# Get Elixir's ebin and retrieve all protocols
iex> path = :code.lib_dir(:elixir, :ebin)
iex> mods = Protocol.extract_protocols([path])
iex> Enumerable in mods
@@ -214,7 +215,7 @@ defmodule Protocol do
## Examples
# Get Elixir's ebin directory path and retrieve all protocols
# Get Elixir's ebin and retrieve all protocols
iex> path = :code.lib_dir(:elixir, :ebin)
iex> mods = Protocol.extract_impls(Enumerable, [path])
iex> List in mods
@@ -283,15 +284,15 @@ defmodule Protocol do
are retrieved with the help of `extract_impls/2`.
It returns the updated version of the protocol bytecode.
If the first element of the tuple is `:ok`, it means
the protocol was consolidated.
A given bytecode or protocol implementation can be checked
to be consolidated or not by analyzing the protocol
attribute:
Protocol.consolidated?(Enumerable)
If the first element of the tuple is `true`, it means
the protocol was consolidated.
This function does not load the protocol at any point
nor loads the new bytecode for the compiled module.
However each implementation must be available and
@@ -302,24 +303,30 @@ defmodule Protocol do
| {:error, :not_a_protocol}
| {:error, :no_beam_info}
def consolidate(protocol, types) when is_atom(protocol) do
with {:ok, ast_info, specs, compile_info} <- beam_protocol(protocol),
{:ok, definitions} <- change_debug_info(protocol, ast_info, types),
do: compile(definitions, specs, compile_info)
with {:ok, ast_info, chunks_info} <- beam_protocol(protocol),
{:ok, code} <- change_debug_info(ast_info, types),
do: compile(protocol, code, chunks_info)
end
defp beam_protocol(protocol) do
chunk_ids = [:debug_info, 'Docs', 'ExDp']
chunk_ids = [:abstract_code, :attributes, :compile_info, 'ExDc', 'ExDp']
opts = [:allow_missing_chunks]
case :beam_lib.chunks(beam_file(protocol), chunk_ids, opts) do
{:ok, {^protocol, [{:debug_info, debug_info} | chunks]}} ->
{:debug_info_v1, _backend, {:elixir_v1, info, specs}} = debug_info
%{attributes: attributes, definitions: definitions} = info
chunks = :lists.map(fn {name, value} -> {List.to_string(name), value} end, chunks)
{:ok, {^protocol, entries}} ->
[
{:abstract_code, {_raw, abstract_code}},
{:attributes, attributes},
{:compile_info, compile_info} | extra_chunks
] = entries
extra_chunks =
for {name, contents} when is_binary(contents) <- extra_chunks,
do: {List.to_string(name), contents}
case attributes[:protocol] do
[fallback_to_any: any] ->
{:ok, {any, definitions}, specs, {info, chunks}}
{:ok, {protocol, any, abstract_code}, {compile_info, extra_chunks}}
_ ->
{:error, :not_a_protocol}
@@ -339,83 +346,148 @@ defmodule Protocol do
# Change the debug information to the optimized
# impl_for/1 dispatch version.
defp change_debug_info(protocol, {any, definitions}, types) do
defp change_debug_info({protocol, any, code}, types) do
types = if any, do: types, else: List.delete(types, Any)
all = [Any] ++ for {_guard, mod} <- __built_in__(), do: mod
all = [Any] ++ for {_guard, mod} <- __builtin__(), do: mod
structs = types -- all
case List.keytake(definitions, {:__protocol__, 1}, 0) do
{protocol_def, definitions} ->
{impl_for, definitions} = List.keytake(definitions, {:impl_for, 1}, 0)
{struct_impl_for, definitions} = List.keytake(definitions, {:struct_impl_for, 1}, 0)
protocol_def = change_protocol(protocol_def, types)
impl_for = change_impl_for(impl_for, protocol, types)
struct_impl_for = change_struct_impl_for(struct_impl_for, protocol, types, structs)
{:ok, [protocol_def, impl_for, struct_impl_for] ++ definitions}
nil ->
{:error, :not_a_protocol}
case change_impl_for(code, protocol, types, structs, false, []) do
{:ok, ret} -> {:ok, ret}
other -> other
end
end
defp change_protocol({_name, _kind, meta, clauses}, types) do
defp change_impl_for(
[{:function, line, :__protocol__, 1, clauses} | tail],
protocol,
types,
structs,
_,
acc
) do
abstract_types = :erl_parse.abstract(:lists.usort(types))
clauses =
Enum.map(clauses, fn
{meta, [:consolidated?], [], _} -> {meta, [:consolidated?], [], true}
{meta, [:impls], [], _} -> {meta, [:impls], [], {:consolidated, types}}
clause -> clause
{:clause, l, [{:atom, _, :consolidated?}], [], [{:atom, _, _}]} ->
{:clause, l, [{:atom, 0, :consolidated?}], [], [{:atom, 0, true}]}
{:clause, l, [{:atom, _, :impls}], [], [{:atom, _, _}]} ->
tuple = {:tuple, 0, [{:atom, 0, :consolidated}, abstract_types]}
{:clause, l, [{:atom, 0, :impls}], [], [tuple]}
{:clause, _, _, _, _} = c ->
c
end)
{{:__protocol__, 1}, :def, meta, clauses}
acc = [{:function, line, :__protocol__, 1, clauses} | acc]
change_impl_for(tail, protocol, types, structs, true, acc)
end
defp change_impl_for({_name, _kind, meta, _clauses}, protocol, types) do
defp change_impl_for(
[{:function, line, :impl_for, 1, _} | tail],
protocol,
types,
structs,
protocol?,
acc
) do
fallback = if Any in types, do: load_impl(protocol, Any)
line = meta[:line]
clauses =
for {guard, mod} <- __built_in__(),
for {guard, mod} <- __builtin__(),
mod in types,
do: built_in_clause_for(mod, guard, protocol, meta, line)
do: builtin_clause_for(mod, guard, protocol, line)
struct_clause = struct_clause_for(meta, line)
fallback_clause = fallback_clause_for(fallback, protocol, meta)
clauses = [struct_clause] ++ clauses ++ [fallback_clause]
clauses =
[struct_clause_for(line) | clauses] ++ [fallback_clause_for(fallback, protocol, line)]
{{:impl_for, 1}, :def, meta, clauses}
acc = [{:function, line, :impl_for, 1, clauses} | acc]
change_impl_for(tail, protocol, types, structs, protocol?, acc)
end
defp change_struct_impl_for({_name, _kind, meta, _clauses}, protocol, types, structs) do
defp change_impl_for(
[{:function, line, :struct_impl_for, 1, _} | tail],
protocol,
types,
structs,
protocol?,
acc
) do
fallback = if Any in types, do: load_impl(protocol, Any)
clauses = for struct <- structs, do: each_struct_clause_for(struct, protocol, meta)
clauses = clauses ++ [fallback_clause_for(fallback, protocol, meta)]
clauses = for struct <- structs, do: each_struct_clause_for(struct, protocol, line)
clauses = clauses ++ [fallback_clause_for(fallback, protocol, line)]
{{:struct_impl_for, 1}, :defp, meta, clauses}
acc = [{:function, line, :struct_impl_for, 1, clauses} | acc]
change_impl_for(tail, protocol, types, structs, protocol?, acc)
end
defp built_in_clause_for(mod, guard, protocol, meta, line) do
x = quote(line: line, do: x)
guard = quote(line: line, do: :erlang.unquote(guard)(unquote(x)))
body = load_impl(protocol, mod)
{meta, [x], [guard], body}
defp change_impl_for(
[{:attribute, line, :spec, {{:__protocol__, 1}, funspecs}} | tail],
protocol,
types,
structs,
protocol?,
acc
) do
new_specs =
for spec <- funspecs do
case spec do
{:type, line, :fun, [{:type, _, :product, [{:atom, _, :consolidated?}]}, _]} ->
product = {:type, line, :product, [{:atom, 0, :consolidated?}]}
{:type, line, :fun, [product, {:atom, 0, true}]}
{:type, line, :fun, [{:type, _, :product, [{:atom, _, :impls}]}, _]} ->
impls = for mod <- types, do: {:atom, 0, mod}
list = {:type, 0, :list, [{:type, 0, :union, impls}]}
tuple = {:type, 0, :tuple, [{:atom, 0, :consolidated}, list]}
{:type, line, :fun, [{:type, line, :product, [{:atom, 0, :impls}]}, tuple]}
other ->
other
end
end
acc = [{:attribute, line, :spec, {{:__protocol__, 1}, new_specs}} | acc]
change_impl_for(tail, protocol, types, structs, protocol?, acc)
end
defp struct_clause_for(meta, line) do
x = quote(line: line, do: x)
head = quote(line: line, do: %{__struct__: unquote(x)})
guard = quote(line: line, do: :erlang.is_atom(unquote(x)))
body = quote(line: line, do: struct_impl_for(unquote(x)))
{meta, [head], [guard], body}
defp change_impl_for([head | tail], protocol, info, types, protocol?, acc) do
change_impl_for(tail, protocol, info, types, protocol?, [head | acc])
end
defp each_struct_clause_for(struct, protocol, meta) do
{meta, [struct], [], load_impl(protocol, struct)}
defp change_impl_for([], _protocol, _info, _types, protocol?, acc) do
if protocol? do
{:ok, Enum.reverse(acc)}
else
{:error, :not_a_protocol}
end
end
defp fallback_clause_for(value, _protocol, meta) do
{meta, [quote(do: _)], [], value}
defp builtin_clause_for(mod, guard, protocol, line) do
remote = {:remote, line, {:atom, line, :erlang}, {:atom, line, guard}}
guard = {:call, line, remote, [{:var, line, :x}]}
body = {:atom, line, load_impl(protocol, mod)}
{:clause, line, [{:var, line, :x}], [[guard]], [body]}
end
defp struct_clause_for(line) do
map_field_exact = {:map_field_exact, line, {:atom, line, :__struct__}, {:var, line, :x}}
arg = {:map, line, [map_field_exact]}
is_atom = {:remote, line, {:atom, line, :erlang}, {:atom, line, :is_atom}}
guard = {:call, line, is_atom, [{:var, line, :x}]}
body = {:call, line, {:atom, line, :struct_impl_for}, [{:var, line, :x}]}
{:clause, line, [arg], [[guard]], [body]}
end
defp each_struct_clause_for(struct, protocol, line) do
{:clause, line, [{:atom, line, struct}], [], [{:atom, line, load_impl(protocol, struct)}]}
end
defp fallback_clause_for(value, _protocol, line) do
{:clause, line, [{:var, line, :_}], [], [{:atom, line, value}]}
end
defp load_impl(protocol, for) do
@@ -423,9 +495,11 @@ defmodule Protocol do
end
# Finally compile the module and emit its bytecode.
defp compile(definitions, specs, {info, chunks}) do
info = %{info | definitions: definitions}
{:ok, :elixir_erl.consolidate(info, specs, chunks)}
defp compile(protocol, code, {compile_info, extra_chunks}) do
opts = Keyword.take(compile_info, [:source])
opts = if Code.compiler_options()[:debug_info], do: [:debug_info | opts], else: opts
{:ok, ^protocol, binary, _warnings} = :compile.forms(code, [:return | opts])
{:ok, :elixir_erl.add_beam_chunks(binary, extra_chunks)}
end
## Definition callbacks
@@ -467,7 +541,7 @@ defmodule Protocol do
end
defp after_defprotocol do
quote bind_quoted: [built_in: __built_in__()] do
quote bind_quoted: [builtin: __builtin__()] do
any_impl_for =
if @fallback_to_any do
quote do: unquote(__MODULE__.Any).__impl__(:target)
@@ -475,10 +549,6 @@ defmodule Protocol do
nil
end
# Disable dialyzer checks - before and after consolidation
# the types could be more strict
@dialyzer {:nowarn_function, __protocol__: 1, impl_for: 1, impl_for!: 1}
@doc false
@spec impl_for(term) :: atom | nil
Kernel.def(impl_for(data))
@@ -487,7 +557,7 @@ defmodule Protocol do
#
# It simply delegates to struct_impl_for which is then
# optimized during protocol consolidation.
Kernel.def impl_for(%struct{}) do
Kernel.def impl_for(%{__struct__: struct}) when :erlang.is_atom(struct) do
struct_impl_for(struct)
end
@@ -504,7 +574,7 @@ defmodule Protocol do
end
end
end,
built_in
builtin
)
# Define a catch-all impl_for/1 clause to pacify Dialyzer (since
@@ -542,7 +612,7 @@ defmodule Protocol do
# Inline struct implementation for performance
@compile {:inline, struct_impl_for: 1}
unless Module.defines_type?(__MODULE__, {:t, 0}) do
unless Kernel.Typespec.defines_type?(__MODULE__, :t, 0) do
@type t :: term
end
@@ -554,8 +624,8 @@ defmodule Protocol do
@doc false
@spec __protocol__(:module) :: __MODULE__
@spec __protocol__(:functions) :: unquote(Protocol.__functions_spec__(@functions))
@spec __protocol__(:consolidated?) :: boolean
@spec __protocol__(:impls) :: :not_consolidated | {:consolidated, [module]}
@spec __protocol__(:consolidated?) :: false
@spec __protocol__(:impls) :: :not_consolidated
Kernel.def(__protocol__(:module), do: __MODULE__)
Kernel.def(__protocol__(:functions), do: unquote(:lists.sort(@functions)))
Kernel.def(__protocol__(:consolidated?), do: false)
@@ -618,7 +688,12 @@ defmodule Protocol do
@doc false
def __derive__(derives, for, %Macro.Env{} = env) when is_atom(for) do
struct = Macro.struct!(for, env)
struct =
if for == env.module do
Module.get_attribute(for, :struct) || raise "struct is not defined for #{inspect(for)}"
else
for.__struct__
end
foreach = fn
proto when is_atom(proto) ->
@@ -640,7 +715,7 @@ defmodule Protocol do
assert_impl!(protocol, Any, extra)
# Clean up variables from eval context
env = :elixir_env.reset_vars(env)
env = %{env | vars: [], export_vars: nil}
args = [for, struct, opts]
impl = Module.concat(protocol, Any)
@@ -682,10 +757,26 @@ defmodule Protocol do
:ok
end
@doc false
def __spec__?(module, name, arity) do
signature = {name, arity}
mapper = fn {:spec, expr, pos} ->
if Kernel.Typespec.spec_to_signature(expr) == signature do
Module.store_typespec(module, :callback, {:callback, expr, pos})
true
end
end
specs = Module.get_attribute(module, :spec)
found = :lists.map(mapper, specs)
:lists.any(&(&1 == true), found)
end
## Helpers
@doc false
def __built_in__ do
def __builtin__ do
[
is_tuple: Tuple,
is_atom: Atom,
+12 -43
View File
@@ -2,8 +2,8 @@ defmodule Range do
@moduledoc """
Defines a range.
A range represents a sequence of one or many,
ascending or descending, consecutive integers.
A range represents a discrete number of values where
the first and last values are integers.
Ranges can be either increasing (`first <= last`) or
decreasing (`first > last`). Ranges are also always
@@ -36,16 +36,12 @@ defmodule Range do
iex> Enum.member?(range, 8)
true
Such function calls are efficient memory-wise no matter the
size of the range. The implementation of the `Enumerable`
protocol uses logic based solely on the endpoints and does
not materialize the whole list of integers.
"""
defstruct first: nil, last: nil
@type t :: %__MODULE__{first: integer, last: integer}
@type t(first, last) :: %__MODULE__{first: first, last: last}
@type t :: %Range{first: integer, last: integer}
@type t(first, last) :: %Range{first: first, last: last}
@doc """
Creates a new range.
@@ -61,36 +57,9 @@ defmodule Range do
"got: #{inspect(first)}..#{inspect(last)}"
end
@doc """
Checks if two ranges are disjoint.
## Examples
iex> Range.disjoint?(1..5, 6..9)
true
iex> Range.disjoint?(5..1, 6..9)
true
iex> Range.disjoint?(1..5, 5..9)
false
iex> Range.disjoint?(1..5, 2..7)
false
"""
@doc since: "1.8.0"
@spec disjoint?(t, t) :: boolean
def disjoint?(first1..last1, first2..last2) do
{first1, last1} = normalize(first1, last1)
{first2, last2} = normalize(first2, last2)
last2 < first1 or last1 < first2
end
@compile inline: [normalize: 2]
defp normalize(first, last) when first > last, do: {last, first}
defp normalize(first, last), do: {first, last}
# TODO: Remove by 2.0
@doc false
@deprecated "Pattern match on first..last instead"
@spec range?(term) :: boolean
def range?(term)
def range?(first..last) when is_integer(first) and is_integer(last), do: true
def range?(_), do: false
@@ -101,20 +70,20 @@ defimpl Enumerable, for: Range do
reduce(first, last, acc, fun, _up? = last >= first)
end
defp reduce(_first, _last, {:halt, acc}, _fun, _up?) do
defp reduce(_x, _y, {:halt, acc}, _fun, _up?) do
{:halted, acc}
end
defp reduce(first, last, {:suspend, acc}, fun, up?) do
{:suspended, acc, &reduce(first, last, &1, fun, up?)}
defp reduce(x, y, {:suspend, acc}, fun, up?) do
{:suspended, acc, &reduce(x, y, &1, fun, up?)}
end
defp reduce(first, last, {:cont, acc}, fun, _up? = true) when first <= last do
reduce(first + 1, last, fun.(first, acc), fun, _up? = true)
defp reduce(x, y, {:cont, acc}, fun, _up? = true) when x <= y do
reduce(x + 1, y, fun.(x, 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)
defp reduce(x, y, {:cont, acc}, fun, _up? = false) when x >= y do
reduce(x - 1, y, fun.(x, acc), fun, _up? = false)
end
defp reduce(_, _, {:cont, acc}, _fun, _up) do
+66 -109
View File
@@ -4,7 +4,7 @@ defmodule Record do
Records are simply tuples where the first element is an atom:
iex> Record.is_record({User, "john", 27})
iex> Record.is_record {User, "john", 27}
true
This module provides conveniences for working with records at
@@ -28,10 +28,10 @@ defmodule Record do
defmodule MyModule do
require Record
Record.defrecord(:user, name: "john", age: 25)
Record.defrecord :user, name: "john", age: 25
@type user :: record(:user, name: String.t(), age: integer)
# expands to: "@type user :: {:user, String.t(), integer}"
@type user :: record(:user, name: String.t, age: integer)
# expands to: "@type user :: {:user, String.t, integer}"
end
"""
@@ -62,11 +62,11 @@ defmodule Record do
* `:includes` - (a list of directories as binaries) if the record being
extracted depends on relative includes, this option allows developers
to specify the directory where those relative includes exist.
to specify the directory those relative includes exist
* `:macros` - (keyword list of macro names and values) if the record
being extracted depends on the values of macros, this option allows
the value of those macros to be set.
being extract depends on the values of macros, this option allows
the value of those macros to be set
These options are expected to be literals (including the binary values) at
compile time.
@@ -74,21 +74,10 @@ defmodule Record do
## Examples
iex> Record.extract(:file_info, from_lib: "kernel/include/file.hrl")
[
size: :undefined,
type: :undefined,
access: :undefined,
atime: :undefined,
mtime: :undefined,
ctime: :undefined,
mode: :undefined,
links: :undefined,
major_device: :undefined,
minor_device: :undefined,
inode: :undefined,
uid: :undefined,
gid: :undefined
]
[size: :undefined, type: :undefined, access: :undefined, atime: :undefined,
mtime: :undefined, ctime: :undefined, mode: :undefined, links: :undefined,
major_device: :undefined, minor_device: :undefined, inode: :undefined,
uid: :undefined, gid: :undefined]
"""
@spec extract(name :: atom, keyword) :: keyword
@@ -112,7 +101,6 @@ defmodule Record do
that contains the record definitions to extract; with this option, this
function uses the same path lookup used by the `-include` attribute used in
Erlang modules.
* `:from_lib` - (binary representing a path to a file) path to the Erlang
file that contains the record definitions to extract; with this option,
this function uses the same path lookup used by the `-include_lib`
@@ -187,7 +175,7 @@ defmodule Record do
defmodule User do
require Record
Record.defrecord(:user, name: "meg", age: "25")
Record.defrecord :user, [name: "meg", age: "25"]
end
In the example above, a set of macros named `user` but with different
@@ -227,7 +215,7 @@ defmodule Record do
defmodule User do
require Record
Record.defrecord(:user, Customer, name: nil)
Record.defrecord :user, Customer, name: nil
end
require User
@@ -240,7 +228,7 @@ defmodule Record do
a record after extracting it from an Erlang library that uses anonymous
functions for defaults.
Record.defrecord(:my_rec, Record.extract(...))
Record.defrecord :my_rec, Record.extract(...)
#=> ** (ArgumentError) invalid value for record field fun_field,
#=> cannot escape #Function<12.90072148/2 in :erl_eval.expr/5>.
@@ -249,25 +237,14 @@ defmodule Record do
defmodule MyRec do
require Record
Record.defrecord(:my_rec, Record.extract(...) |> Keyword.merge(fun_field: &__MODULE__.foo/2))
Record.defrecord :my_rec, Record.extract(...) |> Keyword.merge(fun_field: &__MODULE__.foo/2)
def foo(bar, baz), do: IO.inspect({bar, baz})
end
"""
defmacro defrecord(name, tag \\ nil, kv) do
quote bind_quoted: [name: name, tag: tag, kv: kv] do
defined_arity =
Enum.find(0..2, fn arity ->
Module.defines?(__MODULE__, {name, arity})
end)
if defined_arity do
raise ArgumentError,
"cannot define record #{inspect(name)} because a definition #{name}/#{defined_arity} already exists"
end
tag = tag || name
fields = Record.__fields__(:defrecord, kv)
defmacro unquote(name)(args \\ []) do
@@ -285,18 +262,7 @@ defmodule Record do
"""
defmacro defrecordp(name, tag \\ nil, kv) do
quote bind_quoted: [name: name, tag: tag, kv: kv] do
defined_arity =
Enum.find(0..2, fn arity ->
Module.defines?(__MODULE__, {name, arity})
end)
if defined_arity do
raise ArgumentError,
"cannot define record #{inspect(name)} because a definition #{name}/#{defined_arity} already exists"
end
tag = tag || name
fields = Record.__fields__(:defrecordp, kv)
defmacrop unquote(name)(args \\ []) do
@@ -379,30 +345,38 @@ defmodule Record do
# Gets the index of field.
defp index(tag, fields, field) do
find_index(fields, field, 1) ||
if index = find_index(fields, field, 0) do
# Convert to Elixir index
index - 1
else
raise ArgumentError, "record #{inspect(tag)} does not have the key: #{inspect(field)}"
end
end
# Creates a new record with the given default fields and keyword values.
defp create(tag, fields, keyword, caller) do
# Using {} here is safe, since it's not valid AST
default = if Macro.Env.in_match?(caller), do: {:_, [], nil}, else: {}
{default, keyword} = Keyword.pop(keyword, :_, default)
in_match = Macro.Env.in_match?(caller)
keyword = apply_underscore(fields, keyword)
{elements, remaining} =
Enum.map_reduce(fields, keyword, fn {key, field_default}, remaining ->
case Keyword.pop(remaining, key, default) do
{{}, remaining} -> {Macro.escape(field_default), remaining}
{default, remaining} -> {default, remaining}
end
{match, remaining} =
Enum.map_reduce(fields, keyword, fn {field, default}, each_keyword ->
new_fields =
case Keyword.fetch(each_keyword, field) do
{:ok, value} -> value
:error when in_match -> {:_, [], nil}
:error -> Macro.escape(default)
end
{new_fields, Keyword.delete(each_keyword, field)}
end)
case remaining do
[] ->
quote(do: {unquote(tag), unquote_splicing(elements)})
{:{}, [], [tag | match]}
[{key, _} | _] ->
raise ArgumentError, "record #{inspect(tag)} does not have the key: #{inspect(key)}"
_ ->
keys = for {key, _} <- remaining, do: key
raise ArgumentError, "record #{inspect(tag)} does not have the key: #{inspect(hd(keys))}"
end
end
@@ -412,65 +386,35 @@ defmodule Record do
raise ArgumentError, "cannot invoke update style macro inside match"
end
if Keyword.has_key?(keyword, :_) do
message = "updating a record with a default (:_) is equivalent to creating a new record"
IO.warn(message, Macro.Env.stacktrace(caller))
create(tag, fields, keyword, caller)
else
case build_updates(keyword, fields, [], [], []) do
{updates, [], []} ->
build_update(updates, var)
keyword = apply_underscore(fields, keyword)
{updates, vars, exprs} ->
quote do
{unquote_splicing(:lists.reverse(vars))} = {unquote_splicing(:lists.reverse(exprs))}
unquote(build_update(updates, var))
end
Enum.reduce(keyword, var, fn {key, value}, acc ->
index = find_index(fields, key, 0)
{:error, key} ->
raise ArgumentError, "record #{inspect(tag)} does not have the key: #{inspect(key)}"
if index do
quote do
:erlang.setelement(unquote(index), unquote(acc), unquote(value))
end
else
raise ArgumentError, "record #{inspect(tag)} does not have the key: #{inspect(key)}"
end
end
end
defp build_update(updates, initial) do
updates
|> Enum.sort(fn {left, _}, {right, _} -> right <= left end)
|> Enum.reduce(initial, fn {key, value}, acc ->
quote(do: :erlang.setelement(unquote(key), unquote(acc), unquote(value)))
end)
end
defp build_updates([{key, value} | rest], fields, updates, vars, exprs) do
if index = find_index(fields, key, 2) do
if simple_argument?(value) do
build_updates(rest, fields, [{index, value} | updates], vars, exprs)
else
var = Macro.var(key, __MODULE__)
build_updates(rest, fields, [{index, var} | updates], [var | vars], [value | exprs])
end
else
{:error, key}
end
end
defp build_updates([], _fields, updates, vars, exprs), do: {updates, vars, exprs}
defp simple_argument?({name, _, ctx}) when is_atom(name) and is_atom(ctx), do: true
defp simple_argument?(other), do: Macro.quoted_literal?(other)
# Gets a record key from the given var.
defp get(tag, fields, var, key) do
index =
find_index(fields, key, 2) ||
raise ArgumentError, "record #{inspect(tag)} does not have the key: #{inspect(key)}"
index = find_index(fields, key, 0)
quote do
:erlang.element(unquote(index), unquote(var))
if index do
quote do
:erlang.element(unquote(index), unquote(var))
end
else
raise ArgumentError, "record #{inspect(tag)} does not have the key: #{inspect(key)}"
end
end
defp find_index([{k, _} | _], k, i), do: i
defp find_index([{k, _} | _], k, i), do: i + 2
defp find_index([{_, _} | t], k, i), do: find_index(t, k, i + 1)
defp find_index([], _k, _i), do: nil
@@ -502,4 +446,17 @@ defmodule Record do
defp join_keyword([], [], acc), do: :lists.reverse(acc)
defp join_keyword(rest_fields, _rest_values, acc), do: length(acc) + length(rest_fields)
defp apply_underscore(fields, keyword) do
case Keyword.fetch(keyword, :_) do
{:ok, default} ->
fields
|> Enum.map(fn {k, _} -> {k, default} end)
|> Keyword.merge(keyword)
|> Keyword.delete(:_)
:error ->
keyword
end
end
end
+28 -35
View File
@@ -7,9 +7,9 @@ defmodule Regex do
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`):
[`~r`](Kernel.html#sigil_r/2) or [`~R`](Kernel.html#sigil_R/2):
# A simple regular expression that matches foo anywhere in the string
# A simple regular expressions that matches foo anywhere in the string
~r/foo/
# A regular expression with case insensitive and Unicode options
@@ -41,7 +41,7 @@ defmodule Regex do
expression engine at any time.
For such reasons, we always recommend precompiling Elixir projects using
the Erlang/OTP version meant to run in production. In case cross-compilation is
the OTP version meant to run in production. In case cross-compilation is
really necessary, you can manually invoke `Regex.recompile/1` or
`Regex.recompile!/1` to perform a runtime version check and recompile the
regex if necessary.
@@ -58,7 +58,7 @@ defmodule Regex do
* `dotall` (s) - causes dot to match newlines and also set newline to
anycrlf; the new line setting can be overridden by setting `(*CR)` or
`(*LF)` or `(*CRLF)` or `(*ANY)` according to `:re` documentation
`(*LF)` or `(*CRLF)` or `(*ANY)` according to re documentation
* `multiline` (m) - causes `^` and `$` to mark the beginning and end of
each line; use `\A` and `\z` to match the end or beginning of the string
@@ -79,7 +79,7 @@ defmodule Regex do
* `no_auto_capture` - not available, use `?:` instead
* `newline` - not available, use `(*CR)` or `(*LF)` or `(*CRLF)` or
`(*ANYCRLF)` or `(*ANY)` at the beginning of the regexp according to the
`:re` documentation
re documentation
## Captures
@@ -93,7 +93,7 @@ defmodule Regex do
complete matching part of the string; all explicitly captured subpatterns
are discarded
* `:all_but_first` - all but the first matching subpattern, i.e. all
* `:all_but_first`- all but the first matching subpattern, i.e. all
explicitly captured subpatterns, but not the complete matching part of
the string
@@ -117,9 +117,8 @@ defmodule Regex do
Compiles the regular expression.
The given options can either be a binary with the characters
representing the same regex options given to the
`~r` (see `Kernel.sigil_r/2`) sigil, or a list of options, as
expected by the Erlang's `:re` module.
representing the same regex options given to the `~r` sigil,
or a list of options, as expected by the Erlang's `:re` module.
It returns `{:ok, regex}` in case of success,
`{:error, reason}` otherwise.
@@ -127,14 +126,14 @@ defmodule Regex do
## Examples
iex> Regex.compile("foo")
{:ok, ~r/foo/}
{:ok, ~r"foo"}
iex> Regex.compile("*foo")
{:error, {'nothing to repeat', 0}}
"""
@spec compile(binary, binary | [term]) :: {:ok, t} | {:error, any}
def compile(source, options \\ "") when is_binary(source) do
def compile(source, options \\ "") do
compile(source, options, version())
end
@@ -152,7 +151,7 @@ defmodule Regex do
compile(source, options, "", version)
end
defp compile(source, opts, doc_opts, version) do
defp compile(source, opts, doc_opts, version) when is_binary(source) do
case :re.compile(source, opts) do
{:ok, re_pattern} ->
{:ok, %Regex{re_pattern: re_pattern, re_version: version, source: source, opts: doc_opts}}
@@ -166,7 +165,7 @@ defmodule Regex do
Compiles the regular expression and raises `Regex.CompileError` in case of errors.
"""
@spec compile!(binary, binary | [term]) :: t
def compile!(source, options \\ "") when is_binary(source) do
def compile!(source, options \\ "") do
case compile(source, options) do
{:ok, regex} -> regex
{:error, {reason, at}} -> raise Regex.CompileError, "#{reason} at position #{at}"
@@ -179,13 +178,13 @@ defmodule Regex do
This checks the version stored in the regular expression
and recompiles the regex in case of version mismatch.
"""
@doc since: "1.4.0"
@spec recompile(t) :: t
def recompile(%Regex{} = regex) do
version = version()
case regex do
%{re_version: ^version} ->
# We use Map.get/3 by choice to support old regexes versions.
case Map.get(regex, :re_version, :error) do
^version ->
{:ok, regex}
_ ->
@@ -197,7 +196,6 @@ defmodule Regex do
@doc """
Recompiles the existing regular expression and raises `Regex.CompileError` in case of errors.
"""
@doc since: "1.4.0"
@spec recompile!(t) :: t
def recompile!(regex) do
case recompile(regex) do
@@ -209,10 +207,13 @@ defmodule Regex do
@doc """
Returns the version of the underlying Regex engine.
"""
@doc since: "1.4.0"
@spec version :: term()
# TODO: No longer check for function_exported? on OTP 20+.
def version do
{:re.version(), :erlang.system_info(:endian)}
if function_exported?(:re, :version, 0) do
:re.version()
else
"8.33 2013-05-29"
end
end
@doc """
@@ -256,8 +257,7 @@ defmodule Regex do
## Options
* `:return` - set to `:index` to return byte index and match length.
Defaults to `:binary`.
* `:return` - sets to `:index` to return indexes. Defaults to `:binary`.
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
to see the possible capture values.
@@ -288,12 +288,9 @@ defmodule Regex do
end
@doc """
Returns the given captures as a map or `nil` if no captures are found.
## Options
* `:return` - set to `:index` to return byte index and match length.
Defaults to `:binary`.
Returns the given captures as a map or `nil` if no captures are
found. The option `:return` can be set to `:index` to get indexes
back.
## Examples
@@ -375,8 +372,7 @@ defmodule Regex do
## Options
* `:return` - set to `:index` to return byte index and match length.
Defaults to `:binary`.
* `:return` - sets to `:index` to return indexes. Defaults to `:binary`.
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
to see the possible capture values.
@@ -394,9 +390,6 @@ defmodule Regex do
iex> Regex.scan(~r/\p{Sc}/u, "$, £, and €")
[["$"], ["£"], ["€"]]
iex> Regex.scan(~r/=+/, "=ü†ƒ8===", return: :index)
[[{0, 1}], [{9, 3}]]
"""
@spec scan(t, String.t(), [term]) :: [[String.t()]]
def scan(regex, string, options \\ [])
@@ -439,7 +432,7 @@ defmodule Regex do
iex> Regex.split(~r{-}, "a-b-c")
["a", "b", "c"]
iex> Regex.split(~r{-}, "a-b-c", parts: 2)
iex> Regex.split(~r{-}, "a-b-c", [parts: 2])
["a", "b-c"]
iex> Regex.split(~r{-}, "abc")
@@ -594,7 +587,7 @@ defmodule Regex do
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)
{:arity, arity} = :erlang.fun_info(replacement, :arity)
do_replace(regex, string, {replacement, arity}, options)
end
+56 -216
View File
@@ -11,7 +11,7 @@ defmodule Registry do
Each entry in the registry is associated to the process that has
registered the key. If the process crashes, the keys associated to that
process are automatically removed. All key comparisons in the registry
are done using the match operation (`===/2`).
are done using the match operation (`===`).
The registry can be used for different purposes, such as name lookups (using
the `:via` option), storing properties, custom dispatching rules, or a pubsub
@@ -32,26 +32,10 @@ defmodule Registry do
{:ok, _} = Agent.start_link(fn -> 0 end, name: name)
Agent.get(name, & &1)
#=> 0
Agent.update(name, &(&1 + 1))
Agent.update(name, & &1 + 1)
Agent.get(name, & &1)
#=> 1
In the previous example, we were not interested in associating a value to the
process:
Registry.lookup(Registry.ViaTest, "agent")
#=> [{self(), nil}]
However, in some cases it may be desired to associate a value to the process
using the alternate `{:via, Registry, {registry, key, value}}` tuple:
{:ok, _} = Registry.start_link(keys: :unique, name: Registry.ViaTest)
name = {:via, Registry, {Registry.ViaTest, "agent", :hello}}
{:ok, _} = Agent.start_link(fn -> 0 end, name: name)
Registry.lookup(Registry.ViaTest, "agent")
#=> [{self(), :hello}]
To this point, we have been starting `Registry` using `start_link/1`.
Typically the registry is started as part of a supervision tree though:
{Registry, keys: :unique, name: Registry.ViaTest}
@@ -99,15 +83,14 @@ defmodule Registry do
errors:
require Logger
Registry.dispatch(Registry.DispatcherTest, "hello", fn entries ->
for {pid, {module, function}} <- entries do
try do
apply(module, function, [pid])
catch
kind, reason ->
formatted = Exception.format(kind, reason, __STACKTRACE__)
Logger.error("Registry.dispatch/3 failed with #{formatted}")
formatted = Exception.format(kind, reason, System.stacktrace)
Logger.error "Registry.dispatch/3 failed with #{formatted}"
end
end
end)
@@ -128,15 +111,9 @@ defmodule Registry do
schedulers online, which will make the registry more performant on highly
concurrent environments:
{:ok, _} =
Registry.start_link(
keys: :duplicate,
name: Registry.PubSubTest,
partitions: System.schedulers_online()
)
{:ok, _} = Registry.start_link(keys: :duplicate, name: Registry.PubSubTest,
partitions: System.schedulers_online)
{:ok, _} = Registry.register(Registry.PubSubTest, "hello", [])
Registry.dispatch(Registry.PubSubTest, "hello", fn entries ->
for {pid, _} <- entries, do: send(pid, {:broadcast, "world"})
end)
@@ -194,22 +171,10 @@ defmodule Registry do
@typedoc "The type of registry metadata values"
@type meta_value :: term
@typedoc "A pattern to match on objects in a registry"
@type match_pattern :: atom | term
@typedoc "A guard to be evaluated when matching on objects in a registry"
@type guard :: {atom | term}
@typedoc "A list of guards to be evaluated when matching on objects in a registry"
@type guards :: [guard] | []
## Via callbacks
@doc false
def whereis_name({registry, key}), do: whereis_name(registry, key)
def whereis_name({registry, key, _value}), do: whereis_name(registry, key)
defp whereis_name(registry, key) do
def whereis_name({registry, key}) do
case key_info!(registry) do
{:unique, partitions, key_ets} ->
key_ets = key_ets || key_ets!(registry, key, partitions)
@@ -228,11 +193,8 @@ defmodule Registry do
end
@doc false
def register_name({registry, key}, pid), do: register_name(registry, key, nil, pid)
def register_name({registry, key, value}, pid), do: register_name(registry, key, value, pid)
defp register_name(registry, key, value, pid) when pid == self() do
case register(registry, key, value) do
def register_name({registry, key}, pid) when pid == self() do
case register(registry, key, nil) do
{:ok, _} -> :yes
{:error, _} -> :no
end
@@ -264,23 +226,21 @@ defmodule Registry do
Supervisor.start_link([
{Registry, keys: :unique, name: MyApp.Registry}
], strategy: :one_for_one)
])
For intensive workloads, the registry may also be partitioned (by specifying
the `:partitions` option). If partitioning is required then a good default is to
set the number of partitions to the number of schedulers available:
Registry.start_link(
keys: :unique,
name: MyApp.Registry,
partitions: System.schedulers_online()
)
Registry.start_link(keys: :unique, name: MyApp.Registry,
partitions: System.schedulers_online())
or:
Supervisor.start_link([
{Registry, keys: :unique, name: MyApp.Registry, partitions: System.schedulers_online()}
], strategy: :one_for_one)
{Registry, keys: :unique, name: MyApp.Registry,
partitions: System.schedulers_online()}
])
## Options
@@ -299,7 +259,6 @@ defmodule Registry do
* `:meta` - a keyword list of metadata to be attached to the registry.
"""
@doc since: "1.5.0"
@spec start_link(
keys: keys,
name: registry,
@@ -351,8 +310,13 @@ defmodule Registry do
Registry.Supervisor.start_link(keys, name, partitions, listeners, entries)
end
@doc false
@deprecated "Use Registry.start_link/1 instead"
@doc """
Starts the registry as a supervisor process.
Similar to `start_link/1` except the required options,
`keys` and `name` are given as arguments.
"""
@spec start_link(keys, registry, keyword) :: {:ok, pid} | {:error, term}
def start_link(keys, name, options \\ []) when keys in @keys and is_atom(name) do
start_link([keys: keys, name: name] ++ options)
end
@@ -362,7 +326,7 @@ defmodule Registry do
See `Supervisor`.
"""
@doc since: "1.5.0"
@since "1.5.0"
def child_spec(opts) do
%{
id: Keyword.get(opts, :name, Registry),
@@ -381,17 +345,16 @@ defmodule Registry do
## Examples
iex> Registry.start_link(keys: :unique, name: Registry.UpdateTest)
iex> Registry.start_link(:unique, Registry.UpdateTest)
iex> {:ok, _} = Registry.register(Registry.UpdateTest, "hello", 1)
iex> Registry.lookup(Registry.UpdateTest, "hello")
[{self(), 1}]
iex> Registry.update_value(Registry.UpdateTest, "hello", &(&1 + 1))
iex> Registry.update_value(Registry.UpdateTest, "hello", & &1 + 1)
{2, 1}
iex> Registry.lookup(Registry.UpdateTest, "hello")
[{self(), 2}]
"""
@doc since: "1.4.0"
@spec update_value(registry, key, (value -> value)) ::
{new_value :: term, old_value :: term} | :error
def update_value(registry, key, callback) when is_atom(registry) and is_function(callback, 1) do
@@ -435,9 +398,7 @@ defmodule Registry do
See the module documentation for examples of using the `dispatch/3`
function for building custom dispatching or a pubsub system.
"""
@doc since: "1.4.0"
@spec dispatch(registry, key, dispatcher, keyword) :: :ok
when dispatcher: (entries :: [{pid, value}] -> term) | {module(), atom(), [any()]}
@spec dispatch(registry, key, (entries :: [{pid, value}] -> term), keyword) :: :ok
def dispatch(registry, key, mfa_or_fun, opts \\ [])
when is_atom(registry) and is_function(mfa_or_fun, 1)
when is_atom(registry) and tuple_size(mfa_or_fun) == 3 do
@@ -528,18 +489,18 @@ defmodule Registry do
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.start_link(:unique, Registry.UniqueLookupTest)
iex> Registry.lookup(Registry.UniqueLookupTest, "hello")
[]
iex> {:ok, _} = Registry.register(Registry.UniqueLookupTest, "hello", :world)
iex> Registry.lookup(Registry.UniqueLookupTest, "hello")
[{self(), :world}]
iex> Task.async(fn -> Registry.lookup(Registry.UniqueLookupTest, "hello") end) |> Task.await()
iex> Task.async(fn -> Registry.lookup(Registry.UniqueLookupTest, "hello") end) |> Task.await
[{self(), :world}]
The same applies to duplicate registries:
iex> Registry.start_link(keys: :duplicate, name: Registry.DuplicateLookupTest)
iex> Registry.start_link(:duplicate, Registry.DuplicateLookupTest)
iex> Registry.lookup(Registry.DuplicateLookupTest, "hello")
[]
iex> {:ok, _} = Registry.register(Registry.DuplicateLookupTest, "hello", :world)
@@ -550,7 +511,6 @@ defmodule Registry do
[{self(), :another}, {self(), :world}]
"""
@doc since: "1.4.0"
@spec lookup(registry, key) :: [{pid, value}]
def lookup(registry, key) when is_atom(registry) do
case key_info!(registry) do
@@ -580,14 +540,14 @@ defmodule Registry do
Pattern must be an atom or a tuple that will match the structure of the
value stored in the registry. The atom `:_` can be used to ignore a given
value or tuple element, while the atom `:"$1"` can be used to temporarily assign part
value or tuple element, while :"$1" can be used to temporarily assign part
of pattern to a variable for a subsequent comparison.
Optionally, it is possible to pass a list of guard conditions for more precise matching.
Each guard is a tuple, which describes checks that should be passed by assigned part of pattern.
For example the `$1 > 1` guard condition would be expressed as the `{:>, :"$1", 1}` tuple.
Please note that guard conditions will work only for assigned variables like `:"$1"`, `:"$2"`, etc.
Avoid usage of special match variables `:"$_"` and `:"$$"`, because it might not work as expected.
It is possible to pass list of guard conditions for more precise matching.
Each guard is a tuple, which describes check that should be passed by assigned part of pattern.
For example :"$1" > 1 guard condition would be expressed as {:>, :"$1", 1} tuple.
Please note that guard conditions will work only for assigned variables like :"$1", :"$2", etc.
Avoid usage of special match variables :"$_" and :"$$", because it might not work as expected.
An empty list will be returned if there is no match.
@@ -599,7 +559,7 @@ defmodule Registry do
In the example below we register the current process under the same
key in a duplicate registry but with different values:
iex> Registry.start_link(keys: :duplicate, name: Registry.MatchTest)
iex> Registry.start_link(:duplicate, Registry.MatchTest)
iex> {:ok, _} = Registry.register(Registry.MatchTest, "hello", {1, :atom, 1})
iex> {:ok, _} = Registry.register(Registry.MatchTest, "hello", {2, :atom, 2})
iex> Registry.match(Registry.MatchTest, "hello", {1, :_, :_})
@@ -610,16 +570,13 @@ defmodule Registry do
[{self(), {1, :atom, 1}}, {self(), {2, :atom, 2}}]
iex> Registry.match(Registry.MatchTest, "hello", {:"$1", :_, :"$1"}) |> Enum.sort()
[{self(), {1, :atom, 1}}, {self(), {2, :atom, 2}}]
iex> guards = [{:>, :"$1", 1}]
iex> Registry.match(Registry.MatchTest, "hello", {:_, :_, :"$1"}, guards)
iex> Registry.match(Registry.MatchTest, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 1}])
[{self(), {2, :atom, 2}}]
iex> guards = [{:is_atom, :"$1"}]
iex> Registry.match(Registry.MatchTest, "hello", {:_, :"$1", :_}, guards) |> Enum.sort()
iex> Registry.match(Registry.MatchTest, "hello", {:_, :"$1", :_}, [{:is_atom, :"$1"}]) |> Enum.sort()
[{self(), {1, :atom, 1}}, {self(), {2, :atom, 2}}]
"""
@doc since: "1.4.0"
@spec match(registry, key, match_pattern, guards) :: [{pid, term}]
@spec match(registry, key, match_pattern :: term, guards :: list()) :: [{pid, term}]
def match(registry, key, pattern, guards \\ []) when is_atom(registry) and is_list(guards) do
guards = [{:"=:=", {:element, 1, :"$_"}, {:const, key}} | guards]
spec = [{{:_, {:_, pattern}}, guards, [{:element, 2, :"$_"}]}]
@@ -651,7 +608,7 @@ defmodule Registry do
Registering under a unique registry does not allow multiple entries:
iex> Registry.start_link(keys: :unique, name: Registry.UniqueKeysTest)
iex> Registry.start_link(:unique, Registry.UniqueKeysTest)
iex> Registry.keys(Registry.UniqueKeysTest, self())
[]
iex> {:ok, _} = Registry.register(Registry.UniqueKeysTest, "hello", :world)
@@ -662,7 +619,7 @@ defmodule Registry do
Such is possible for duplicate registries though:
iex> Registry.start_link(keys: :duplicate, name: Registry.DuplicateKeysTest)
iex> Registry.start_link(:duplicate, Registry.DuplicateKeysTest)
iex> Registry.keys(Registry.DuplicateKeysTest, self())
[]
iex> {:ok, _} = Registry.register(Registry.DuplicateKeysTest, "hello", :world)
@@ -671,7 +628,6 @@ defmodule Registry do
["hello", "hello"]
"""
@doc since: "1.4.0"
@spec keys(registry, pid) :: [key]
def keys(registry, pid) when is_atom(registry) and is_pid(pid) do
{kind, partitions, _, pid_ets, _} = info!(registry)
@@ -722,7 +678,7 @@ defmodule Registry do
For unique registries:
iex> Registry.start_link(keys: :unique, name: Registry.UniqueUnregisterTest)
iex> Registry.start_link(:unique, Registry.UniqueUnregisterTest)
iex> Registry.register(Registry.UniqueUnregisterTest, "hello", :world)
iex> Registry.keys(Registry.UniqueUnregisterTest, self())
["hello"]
@@ -733,7 +689,7 @@ defmodule Registry do
For duplicate registries:
iex> Registry.start_link(keys: :duplicate, name: Registry.DuplicateUnregisterTest)
iex> Registry.start_link(:duplicate, Registry.DuplicateUnregisterTest)
iex> Registry.register(Registry.DuplicateUnregisterTest, "hello", :world)
iex> Registry.register(Registry.DuplicateUnregisterTest, "hello", :world)
iex> Registry.keys(Registry.DuplicateUnregisterTest, self())
@@ -744,7 +700,6 @@ defmodule Registry do
[]
"""
@doc since: "1.4.0"
@spec unregister(registry, key) :: :ok
def unregister(registry, key) when is_atom(registry) do
self = self()
@@ -776,7 +731,7 @@ defmodule Registry do
For unique registries it can be used to conditionally unregister a key on
the basis of whether or not it matches a particular value.
iex> Registry.start_link(keys: :unique, name: Registry.UniqueUnregisterMatchTest)
iex> Registry.start_link(:unique, Registry.UniqueUnregisterMatchTest)
iex> Registry.register(Registry.UniqueUnregisterMatchTest, "hello", :world)
iex> Registry.keys(Registry.UniqueUnregisterMatchTest, self())
["hello"]
@@ -791,7 +746,7 @@ defmodule Registry do
For duplicate registries:
iex> Registry.start_link(keys: :duplicate, name: Registry.DuplicateUnregisterMatchTest)
iex> Registry.start_link(:duplicate, Registry.DuplicateUnregisterMatchTest)
iex> Registry.register(Registry.DuplicateUnregisterMatchTest, "hello", :world_a)
iex> Registry.register(Registry.DuplicateUnregisterMatchTest, "hello", :world_b)
iex> Registry.register(Registry.DuplicateUnregisterMatchTest, "hello", :world_c)
@@ -803,9 +758,7 @@ defmodule Registry do
["hello", "hello"]
iex> Registry.lookup(Registry.DuplicateUnregisterMatchTest, "hello")
[{self(), :world_b}, {self(), :world_c}]
"""
@doc since: "1.5.0"
def unregister_match(registry, key, pattern, guards \\ []) when is_list(guards) do
self = self()
@@ -882,7 +835,7 @@ defmodule Registry do
Registering under a unique registry does not allow multiple entries:
iex> Registry.start_link(keys: :unique, name: Registry.UniqueRegisterTest)
iex> Registry.start_link(:unique, Registry.UniqueRegisterTest)
iex> {:ok, _} = Registry.register(Registry.UniqueRegisterTest, "hello", :world)
iex> Registry.register(Registry.UniqueRegisterTest, "hello", :later)
{:error, {:already_registered, self()}}
@@ -891,14 +844,13 @@ defmodule Registry do
Such is possible for duplicate registries though:
iex> Registry.start_link(keys: :duplicate, name: Registry.DuplicateRegisterTest)
iex> Registry.start_link(:duplicate, Registry.DuplicateRegisterTest)
iex> {:ok, _} = Registry.register(Registry.DuplicateRegisterTest, "hello", :world)
iex> {:ok, _} = Registry.register(Registry.DuplicateRegisterTest, "hello", :world)
iex> Registry.keys(Registry.DuplicateRegisterTest, self())
["hello", "hello"]
"""
@doc since: "1.4.0"
@spec register(registry, key, value) :: {:ok, pid} | {:error, {:already_registered, pid}}
def register(registry, key, value) when is_atom(registry) do
self = self()
@@ -907,7 +859,7 @@ defmodule Registry do
key_ets = key_ets || key_ets!(registry, key_partition)
{pid_server, pid_ets} = pid_ets || pid_ets!(registry, pid_partition)
# Notice we write first to the pid_ets table because it will
# Notice we write first to the pid ets table because it will
# always be able to do the cleanup. If we register first to the
# key one and the process crashes, the key will stay there forever.
Process.link(pid_server)
@@ -964,14 +916,13 @@ defmodule Registry do
## Examples
iex> Registry.start_link(keys: :unique, name: Registry.MetaTest, meta: [custom_key: "custom_value"])
iex> Registry.start_link(:unique, Registry.MetaTest, meta: [custom_key: "custom_value"])
iex> Registry.meta(Registry.MetaTest, :custom_key)
{:ok, "custom_value"}
iex> Registry.meta(Registry.MetaTest, :unknown_key)
:error
"""
@doc since: "1.4.0"
@spec meta(registry, meta_key) :: {:ok, meta_value} | :error
def meta(registry, key) when is_atom(registry) and (is_atom(key) or is_tuple(key)) do
try do
@@ -992,7 +943,7 @@ defmodule Registry do
## Examples
iex> Registry.start_link(keys: :unique, name: Registry.PutMetaTest)
iex> Registry.start_link(:unique, Registry.PutMetaTest)
iex> Registry.put_meta(Registry.PutMetaTest, :custom_key, "custom_value")
:ok
iex> Registry.meta(Registry.PutMetaTest, :custom_key)
@@ -1003,7 +954,6 @@ defmodule Registry do
{:ok, "tuple_value"}
"""
@doc since: "1.4.0"
@spec put_meta(registry, meta_key, meta_value) :: :ok
def put_meta(registry, key, value) when is_atom(registry) and (is_atom(key) or is_tuple(key)) do
try do
@@ -1015,120 +965,6 @@ defmodule Registry do
end
end
@doc """
Returns the number of registered keys in a registry.
It runs in constant time.
## Examples
In the example below we register the current process and ask for the
number of keys in the registry:
iex> Registry.start_link(keys: :unique, name: Registry.UniqueCountTest)
iex> Registry.count(Registry.UniqueCountTest)
0
iex> {:ok, _} = Registry.register(Registry.UniqueCountTest, "hello", :world)
iex> {:ok, _} = Registry.register(Registry.UniqueCountTest, "world", :world)
iex> Registry.count(Registry.UniqueCountTest)
2
The same applies to duplicate registries:
iex> Registry.start_link(keys: :duplicate, name: Registry.DuplicateCountTest)
iex> Registry.count(Registry.DuplicateCountTest)
0
iex> {:ok, _} = Registry.register(Registry.DuplicateCountTest, "hello", :world)
iex> {:ok, _} = Registry.register(Registry.DuplicateCountTest, "hello", :world)
iex> Registry.count(Registry.DuplicateCountTest)
2
"""
@doc since: "1.7.0"
@spec count(registry) :: non_neg_integer()
def count(registry) when is_atom(registry) do
case key_info!(registry) do
{_kind, partitions, nil} ->
Enum.reduce(0..(partitions - 1), 0, fn partition_index, acc ->
acc + safe_size(key_ets!(registry, partition_index))
end)
{_kind, 1, key_ets} ->
safe_size(key_ets)
end
end
defp safe_size(ets) do
try do
:ets.info(ets, :size)
catch
:error, :badarg -> 0
end
end
@doc """
Returns the number of `{pid, value}` pairs under the given `key` in `registry`
that match `pattern`.
Pattern must be an atom or a tuple that will match the structure of the
value stored in the registry. The atom `:_` can be used to ignore a given
value or tuple element, while the atom `:"$1"` can be used to temporarily assign part
of pattern to a variable for a subsequent comparison.
Optionally, it is possible to pass a list of guard conditions for more precise matching.
Each guard is a tuple, which describes checks that should be passed by assigned part of pattern.
For example the `$1 > 1` guard condition would be expressed as the `{:>, :"$1", 1}` tuple.
Please note that guard conditions will work only for assigned variables like `:"$1"`, `:"$2"`, etc.
Avoid usage of special match variables `:"$_"` and `:"$$"`, because it might not work as expected.
Zero will be returned if there is no match.
For unique registries, a single partition lookup is necessary. For
duplicate registries, all partitions must be looked up.
## Examples
In the example below we register the current process under the same
key in a duplicate registry but with different values:
iex> Registry.start_link(keys: :duplicate, name: Registry.CountMatchTest)
iex> {:ok, _} = Registry.register(Registry.CountMatchTest, "hello", {1, :atom, 1})
iex> {:ok, _} = Registry.register(Registry.CountMatchTest, "hello", {2, :atom, 2})
iex> Registry.count_match(Registry.CountMatchTest, "hello", {1, :_, :_})
1
iex> Registry.count_match(Registry.CountMatchTest, "hello", {2, :_, :_})
1
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:_, :atom, :_})
2
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:"$1", :_, :"$1"})
2
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 1}])
1
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:_, :"$1", :_}, [{:is_atom, :"$1"}])
2
"""
@doc since: "1.7.0"
@spec count_match(registry, key, match_pattern, guards) :: non_neg_integer()
def count_match(registry, key, pattern, guards \\ [])
when is_atom(registry) and is_list(guards) do
guards = [{:"=:=", {:element, 1, :"$_"}, {:const, key}} | guards]
spec = [{{:_, {:_, pattern}}, guards, [true]}]
case key_info!(registry) do
{:unique, partitions, key_ets} ->
key_ets = key_ets || key_ets!(registry, key, partitions)
:ets.select_count(key_ets, spec)
{:duplicate, 1, key_ets} ->
:ets.select_count(key_ets, spec)
{:duplicate, partitions, _key_ets} ->
Enum.reduce(0..(partitions - 1), 0, fn partition_index, acc ->
count = :ets.select_count(key_ets!(registry, partition_index), spec)
acc + count
end)
end
end
## Helpers
@compile {:inline, hash: 2}
@@ -1233,7 +1069,7 @@ end
defmodule Registry.Partition do
@moduledoc false
# This process owns the equivalent key and pid ETS tables
# This process owns the equivalent key and pid ets tables
# and is responsible for monitoring processes that map to
# its own pid table.
use GenServer
@@ -1325,7 +1161,7 @@ defmodule Registry.Partition do
for {_pid, key, key_ets} <- entries do
key_ets =
case key_ets do
# In case the fake key_ets is being used. See unregister_match/2.
# In case the fake key ets is being used. See unregister_match/2.
{key_ets, _} ->
key_ets
@@ -1342,4 +1178,8 @@ defmodule Registry.Partition do
{:noreply, ets}
end
def handle_info(msg, state) do
super(msg, state)
end
end
+4 -17
View File
@@ -1,18 +1,16 @@
defmodule Set do
@moduledoc ~S"""
Generic API for sets.
WARNING: this module is deprecated.
This module is deprecated, use the `MapSet` module instead.
Use the `MapSet` module instead.
"""
@moduledoc deprecated: "Use MapSet instead"
@type value :: any
@type values :: [value]
@type t :: map
# TODO: Remove by 2.0
message = "Use the MapSet module for working with sets"
# (hard-deprecated in elixir_dispatch)
defmacrop target(set) do
quote do
@@ -23,12 +21,10 @@ defmodule Set do
end
end
@deprecated message
def delete(set, value) do
target(set).delete(set, value)
end
@deprecated message
def difference(set1, set2) do
target1 = target(set1)
target2 = target(set2)
@@ -43,7 +39,6 @@ defmodule Set do
end
end
@deprecated message
def disjoint?(set1, set2) do
target1 = target(set1)
target2 = target(set2)
@@ -61,12 +56,11 @@ defmodule Set do
end
end
@deprecated message
@doc false
def empty(set) do
target(set).empty(set)
end
@deprecated message
def equal?(set1, set2) do
target1 = target(set1)
target2 = target(set2)
@@ -83,7 +77,6 @@ defmodule Set do
end
end
@deprecated message
def intersection(set1, set2) do
target1 = target(set1)
target2 = target(set2)
@@ -98,22 +91,18 @@ defmodule Set do
end
end
@deprecated message
def member?(set, value) do
target(set).member?(set, value)
end
@deprecated message
def put(set, value) do
target(set).put(set, value)
end
@deprecated message
def size(set) do
target(set).size(set)
end
@deprecated message
def subset?(set1, set2) do
target1 = target(set1)
target2 = target(set2)
@@ -125,12 +114,10 @@ defmodule Set do
end
end
@deprecated message
def to_list(set) do
target(set).to_list(set)
end
@deprecated message
def union(set1, set2) do
target1 = target(set1)
target2 = target(set2)
+89 -121
View File
@@ -1,15 +1,14 @@
defmodule Stream do
@moduledoc """
Functions for creating and composing streams.
Module for creating and composing streams.
Streams are composable, lazy enumerables (for an introduction on
enumerables, see the `Enum` module). Any enumerable that generates
Streams are composable, lazy enumerables. Any enumerable that generates
items one by one during enumeration is called a stream. For example,
Elixir's `Range` is a stream:
iex> range = 1..5
1..5
iex> Enum.map(range, &(&1 * 2))
iex> Enum.map range, &(&1 * 2)
[2, 4, 6, 8, 10]
In the example above, as we mapped over the range, the elements being
@@ -95,10 +94,7 @@ defmodule Stream do
@type acc :: any
@type element :: any
@typedoc "Zero-based index."
@type index :: non_neg_integer
@type default :: any
# Require Stream.Reducers and its callbacks
@@ -125,22 +121,13 @@ defmodule Stream do
## Transformers
# TODO: Remove by 2.0
# Deprecate on v1.7
@doc false
@deprecated "Use Stream.chunk_every/2 instead"
def chunk(enum, n), do: chunk(enum, n, n, nil)
# TODO: Remove by 2.0
# Deprecate on v1.7
@doc false
@deprecated "Use Stream.chunk_every/3 instead"
def chunk(enum, n, step) do
chunk_every(enum, n, step, nil)
end
# TODO: Remove by 2.0
@doc false
@deprecated "Use Stream.chunk_every/4 instead"
def chunk(enum, n, step, leftover)
def chunk(enum, n, step, leftover \\ nil)
when is_integer(n) and n > 0 and is_integer(step) and step > 0 do
chunk_every(enum, n, step, leftover || :discard)
end
@@ -148,7 +135,6 @@ defmodule Stream do
@doc """
Shortcut to `chunk_every(enum, count, count)`.
"""
@doc since: "1.5.0"
@spec chunk_every(Enumerable.t(), pos_integer) :: Enumerable.t()
def chunk_every(enum, count), do: chunk_every(enum, count, count, [])
@@ -169,20 +155,19 @@ defmodule Stream do
## Examples
iex> Stream.chunk_every([1, 2, 3, 4, 5, 6], 2) |> Enum.to_list()
iex> Stream.chunk_every([1, 2, 3, 4, 5, 6], 2) |> Enum.to_list
[[1, 2], [3, 4], [5, 6]]
iex> Stream.chunk_every([1, 2, 3, 4, 5, 6], 3, 2, :discard) |> Enum.to_list()
iex> Stream.chunk_every([1, 2, 3, 4, 5, 6], 3, 2, :discard) |> Enum.to_list
[[1, 2, 3], [3, 4, 5]]
iex> Stream.chunk_every([1, 2, 3, 4, 5, 6], 3, 2, [7]) |> Enum.to_list()
iex> Stream.chunk_every([1, 2, 3, 4, 5, 6], 3, 2, [7]) |> Enum.to_list
[[1, 2, 3], [3, 4, 5], [5, 6, 7]]
iex> Stream.chunk_every([1, 2, 3, 4, 5, 6], 3, 3, []) |> Enum.to_list()
iex> Stream.chunk_every([1, 2, 3, 4, 5, 6], 3, 3, []) |> Enum.to_list
[[1, 2, 3], [4, 5, 6]]
"""
@doc since: "1.5.0"
@spec chunk_every(Enumerable.t(), pos_integer, pos_integer, Enumerable.t() | :discard) ::
Enumerable.t()
def chunk_every(enum, count, step, leftover \\ [])
@@ -220,11 +205,11 @@ defmodule Stream do
## Examples
iex> chunk_fun = fn item, acc ->
...> if rem(item, 2) == 0 do
...> {:cont, Enum.reverse([item | acc]), []}
iex> chunk_fun = fn i, acc ->
...> if rem(i, 2) == 0 do
...> {:cont, Enum.reverse([i | acc]), []}
...> else
...> {:cont, [item | acc]}
...> {:cont, [i | acc]}
...> end
...> end
iex> after_fun = fn
@@ -236,7 +221,6 @@ defmodule Stream do
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
"""
@doc since: "1.5.0"
@spec chunk_while(
Enumerable.t(),
acc,
@@ -286,11 +270,11 @@ defmodule Stream do
This function only ever needs to store the last emitted element.
Elements are compared using `===/2`.
Elements are compared using `===`.
## Examples
iex> Stream.dedup([1, 2, 3, 3, 2, 1]) |> Enum.to_list()
iex> Stream.dedup([1, 2, 3, 3, 2, 1]) |> Enum.to_list
[1, 2, 3, 2, 1]
"""
@@ -305,7 +289,7 @@ defmodule Stream do
## Examples
iex> Stream.dedup_by([{1, :x}, {2, :y}, {2, :z}, {1, :x}], fn {x, _} -> x end) |> Enum.to_list()
iex> Stream.dedup_by([{1, :x}, {2, :y}, {2, :z}, {1, :x}], fn {x, _} -> x end) |> Enum.to_list
[{1, :x}, {2, :y}, {1, :x}]
"""
@@ -418,7 +402,7 @@ defmodule Stream do
## Examples
iex> stream = Stream.each([1, 2, 3], fn x -> send(self(), x) end)
iex> stream = Stream.each([1, 2, 3], fn(x) -> send self(), x end)
iex> Enum.to_list(stream)
iex> receive do: (x when is_integer(x) -> x)
1
@@ -446,11 +430,11 @@ defmodule Stream do
## Examples
iex> stream = Stream.flat_map([1, 2, 3], fn x -> [x, x * 2] end)
iex> stream = Stream.flat_map([1, 2, 3], fn(x) -> [x, x * 2] end)
iex> Enum.to_list(stream)
[1, 2, 2, 4, 3, 6]
iex> stream = Stream.flat_map([1, 2, 3], fn x -> [[x]] end)
iex> stream = Stream.flat_map([1, 2, 3], fn(x) -> [[x]] end)
iex> Enum.to_list(stream)
[[1], [2], [3]]
@@ -466,7 +450,7 @@ defmodule Stream do
## Examples
iex> stream = Stream.filter([1, 2, 3], fn x -> rem(x, 2) == 0 end)
iex> stream = Stream.filter([1, 2, 3], fn(x) -> rem(x, 2) == 0 end)
iex> Enum.to_list(stream)
[2]
@@ -478,7 +462,7 @@ defmodule Stream do
@doc false
# TODO: Remove on 2.0
@deprecated "Use Stream.filter/2 + Stream.map/2 instead"
# (hard-deprecated in elixir_dispatch)
def filter_map(enum, filter, mapper) do
lazy(enum, fn f1 -> R.filter_map(filter, mapper, f1) end)
end
@@ -537,8 +521,9 @@ defmodule Stream do
reduce.({command, [acc | collectable]})
catch
kind, reason ->
stacktrace = System.stacktrace()
into.(collectable, :halt)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
{:suspended, [acc | collectable], continuation} ->
{:suspended, acc, &do_into(continuation, collectable, into, &1)}
@@ -555,7 +540,7 @@ defmodule Stream do
## Examples
iex> stream = Stream.map([1, 2, 3], fn x -> x * 2 end)
iex> stream = Stream.map([1, 2, 3], fn(x) -> x * 2 end)
iex> Enum.to_list(stream)
[2, 4, 6]
@@ -575,20 +560,19 @@ defmodule Stream do
## Examples
iex> stream = Stream.map_every(1..10, 2, fn x -> x * 2 end)
iex> stream = Stream.map_every(1..10, 2, fn(x) -> x * 2 end)
iex> Enum.to_list(stream)
[2, 2, 6, 4, 10, 6, 14, 8, 18, 10]
iex> stream = Stream.map_every([1, 2, 3, 4, 5], 1, fn x -> x * 2 end)
iex> stream = Stream.map_every([1, 2, 3, 4, 5], 1, fn(x) -> x * 2 end)
iex> Enum.to_list(stream)
[2, 4, 6, 8, 10]
iex> stream = Stream.map_every(1..5, 0, fn x -> x * 2 end)
iex> stream = Stream.map_every(1..5, 0, fn(x) -> x * 2 end)
iex> Enum.to_list(stream)
[1, 2, 3, 4, 5]
"""
@doc since: "1.4.0"
@spec map_every(Enumerable.t(), non_neg_integer, (element -> any)) :: Enumerable.t()
def map_every(enum, nth, fun)
@@ -606,7 +590,7 @@ defmodule Stream do
## Examples
iex> stream = Stream.reject([1, 2, 3], fn x -> rem(x, 2) == 0 end)
iex> stream = Stream.reject([1, 2, 3], fn(x) -> rem(x, 2) == 0 end)
iex> Enum.to_list(stream)
[1, 3]
@@ -627,13 +611,13 @@ defmodule Stream do
Open up a file, replace all `#` by `%` and stream to another file
without loading the whole file in memory:
File.stream!("/path/to/file")
stream = File.stream!("code")
|> Stream.map(&String.replace(&1, "#", "%"))
|> Stream.into(File.stream!("/path/to/other/file"))
|> Stream.run()
|> Stream.into(File.stream!("new"))
|> Stream.run
No computation will be done until we call one of the `Enum` functions
or `run/1`.
No computation will be done until we call one of the Enum functions
or `Stream.run/1`.
"""
@spec run(Enumerable.t()) :: :ok
def run(stream) do
@@ -768,7 +752,7 @@ defmodule Stream do
## Examples
iex> Stream.timer(10) |> Enum.to_list()
iex> Stream.timer(10) |> Enum.to_list
[0]
"""
@@ -860,8 +844,9 @@ defmodule Stream do
next.({:cont, []})
catch
kind, reason ->
stacktrace = System.stacktrace()
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
{:suspended, vals, next} ->
do_transform_user(:lists.reverse(vals), user_acc, :cont, next, inner_acc, funs)
@@ -882,9 +867,10 @@ defmodule Stream do
user.(val, user_acc)
catch
kind, reason ->
stacktrace = System.stacktrace()
next.({:halt, []})
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
{[], user_acc} ->
do_transform_user(vals, user_acc, next_op, next, inner_acc, funs)
@@ -911,9 +897,10 @@ defmodule Stream do
reduce.(inner_acc)
catch
kind, reason ->
stacktrace = System.stacktrace()
next.({:halt, []})
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
{:done, acc} ->
do_transform_user(vals, user_acc, next_op, next, {:cont, acc}, funs)
@@ -936,13 +923,15 @@ defmodule Stream do
reduce.({op, [:outer | inner_acc]})
catch
kind, reason ->
stacktrace = System.stacktrace()
next.({:halt, []})
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
# Only take into account outer halts when the op is not halt itself.
# Otherwise, we were the ones wishing to halt, so we should just stop.
{:halted, [:outer | acc]} when op != :halt ->
{:halted, [:outer | acc]}
when op != :halt ->
do_transform_user(vals, user_acc, next_op, next, {:cont, acc}, funs)
{:halted, [_ | acc]} ->
@@ -979,11 +968,11 @@ defmodule Stream do
Keep in mind that, in order to know if an element is unique
or not, this function needs to store all unique values emitted
by the stream. Therefore, if the stream is infinite, the number
of items stored will grow infinitely, never being garbage-collected.
of items stored will grow infinitely, never being garbage collected.
## Examples
iex> Stream.uniq([1, 2, 3, 3, 2, 1]) |> Enum.to_list()
iex> Stream.uniq([1, 2, 3, 3, 2, 1]) |> Enum.to_list
[1, 2, 3]
"""
@@ -994,7 +983,7 @@ defmodule Stream do
@doc false
# TODO: Remove on 2.0
@deprecated "Use Stream.uniq_by/2 instead"
# (hard-deprecated in elixir_dispatch)
def uniq(enum, fun) do
uniq_by(enum, fun)
end
@@ -1009,14 +998,14 @@ defmodule Stream do
Keep in mind that, in order to know if an element is unique
or not, this function needs to store all unique values emitted
by the stream. Therefore, if the stream is infinite, the number
of items stored will grow infinitely, never being garbage-collected.
of items stored will grow infinitely, never being garbage collected.
## Example
iex> Stream.uniq_by([{1, :x}, {2, :y}, {1, :z}], fn {x, _} -> x end) |> Enum.to_list()
iex> Stream.uniq_by([{1, :x}, {2, :y}, {1, :z}], fn {x, _} -> x end) |> Enum.to_list
[{1, :x}, {2, :y}]
iex> Stream.uniq_by([a: {:tea, 2}, b: {:tea, 2}, c: {:coffee, 1}], fn {_, y} -> y end) |> Enum.to_list()
iex> Stream.uniq_by([a: {:tea, 2}, b: {:tea, 2}, c: {:coffee, 1}], fn {_, y} -> y end) |> Enum.to_list
[a: {:tea, 2}, c: {:coffee, 1}]
"""
@@ -1093,8 +1082,8 @@ defmodule Stream do
## Examples
iex> concat = Stream.concat(1..3, 4..6)
iex> cycle = Stream.cycle([:a, :b, :c])
iex> Stream.zip(concat, cycle) |> Enum.to_list()
iex> cycle = Stream.cycle([:a, :b, :c])
iex> Stream.zip(concat, cycle) |> Enum.to_list
[{1, :a}, {2, :b}, {3, :c}, {4, :a}, {5, :b}, {6, :c}]
"""
@@ -1111,11 +1100,10 @@ defmodule Stream do
iex> concat = Stream.concat(1..3, 4..6)
iex> cycle = Stream.cycle(["foo", "bar", "baz"])
iex> Stream.zip([concat, [:a, :b, :c], cycle]) |> Enum.to_list()
iex> Stream.zip([concat, [:a, :b, :c], cycle]) |> Enum.to_list
[{1, :a, "foo"}, {2, :b, "bar"}, {3, :c, "baz"}]
"""
@doc since: "1.4.0"
@spec zip([Enumerable.t()]) :: Enumerable.t()
@spec zip(Enumerable.t()) :: Enumerable.t()
def zip(enumerables) do
@@ -1127,7 +1115,7 @@ defmodule Stream do
enum_funs =
Enum.map(enumerables, fn enum ->
{&Enumerable.reduce(enum, &1, step), [], :cont}
{&Enumerable.reduce(enum, &1, step), :cont}
end)
do_zip(enum_funs, acc, fun)
@@ -1154,8 +1142,9 @@ defmodule Stream do
do_zip_next_tuple(zips, acc, callback, [], [])
catch
kind, reason ->
stacktrace = System.stacktrace()
do_zip_close(zips)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
{:next, buffer, acc} ->
do_zip(buffer, acc, callback)
@@ -1168,20 +1157,18 @@ defmodule Stream do
# do_zip_next_tuple/5 computes the next tuple formed by
# the next element of each zipped stream.
defp do_zip_next_tuple([{_, [], :halt} | zips], acc, _callback, _yielded_elems, buffer) 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_tuple([{fun, [], :cont} | zips], acc, callback, yielded_elems, buffer) 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_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
{:suspended, [elem], fun} ->
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], [{fun, :cont} | buffer])
{_, [elem | next_acc]} ->
next_buffer = [{fun, next_acc, :halt} | buffer]
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
{_, [elem]} ->
do_zip_next_tuple(zips, acc, callback, [elem | yielded_elems], [{fun, :halt} | buffer])
{_, []} ->
# The current zipped stream terminated, so we close all the streams
@@ -1191,12 +1178,6 @@ defmodule Stream do
end
end
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_tuple(zips, acc, callback, [elem | yielded_elems], next_buffer)
end
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: it needs to be reversed and converted to a tuple to have the next
@@ -1206,11 +1187,11 @@ defmodule Stream do
end
defp do_zip_close(zips) do
:lists.foreach(fn {fun, _, _} -> fun.({:halt, []}) end, zips)
:lists.foreach(fn {fun, _} -> fun.({:halt, []}) end, zips)
end
defp do_zip_step(x, acc) do
{:suspend, :lists.reverse([x | acc])}
defp do_zip_step(x, []) do
{:suspend, [x]}
end
## Sources
@@ -1244,8 +1225,7 @@ defmodule Stream do
fn acc, fun ->
inner = &do_cycle_each(&1, &2, fun)
outer = &Enumerable.reduce(enumerable, &1, inner)
reduce = check_cycle_first_element(outer)
do_cycle(reduce, outer, acc)
do_cycle(outer, outer, acc)
end
end
@@ -1264,6 +1244,9 @@ defmodule Stream do
{:stream_cycle, acc} ->
{:halted, acc}
else
{state, []} when state in [:done, :halted] ->
raise ArgumentError, "cannot cycle over empty enumerable"
{state, acc} when state in [:done, :halted] ->
do_cycle(cycle, cycle, {:cont, acc})
@@ -1279,25 +1262,13 @@ defmodule Stream do
end
end
defp check_cycle_first_element(reduce) do
fn acc ->
case reduce.(acc) do
{state, []} when state in [:done, :halted] ->
raise ArgumentError, "cannot cycle over empty enumerable"
other ->
other
end
end
end
@doc """
Emits a sequence of values, starting with `start_value`. Successive
values are generated by calling `next_fun` on the previous value.
## Examples
iex> Stream.iterate(0, &(&1 + 1)) |> Enum.take(5)
iex> Stream.iterate(0, &(&1+1)) |> Enum.take(5)
[0, 1, 2, 3, 4]
"""
@@ -1359,16 +1330,14 @@ defmodule Stream do
## Examples
Stream.resource(
fn -> File.open!("sample") end,
fn file ->
case IO.read(file, :line) do
data when is_binary(data) -> {[data], file}
_ -> {:halt, file}
end
end,
fn file -> File.close(file) end
)
Stream.resource(fn -> File.open!("sample") end,
fn file ->
case IO.read(file, :line) do
data when is_binary(data) -> {[data], file}
_ -> {:halt, file}
end
end,
fn file -> File.close(file) end)
"""
@spec resource((() -> acc), (acc -> {[element], acc} | {:halt, acc}), (acc -> term)) ::
@@ -1396,8 +1365,9 @@ defmodule Stream do
end
catch
kind, reason ->
stacktrace = System.stacktrace()
after_fun.(next_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
{:opt, acc, next_acc} ->
do_resource(next_acc, next_fun, acc, fun, after_fun)
@@ -1421,8 +1391,9 @@ defmodule Stream do
reduce.(acc)
catch
kind, reason ->
stacktrace = System.stacktrace()
after_fun.(next_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
{:done, acc} ->
do_resource(next_acc, next_fun, {:cont, acc}, fun, after_fun)
@@ -1440,8 +1411,9 @@ defmodule Stream do
reduce.({op, [:outer | acc]})
catch
kind, reason ->
stacktrace = System.stacktrace()
after_fun.(next_acc)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
{:halted, [:outer | acc]} ->
do_resource(next_acc, next_fun, {:cont, acc}, fun, after_fun)
@@ -1473,10 +1445,7 @@ defmodule Stream do
## Examples
iex> Stream.unfold(5, fn
...> 0 -> nil
...> n -> {n, n - 1}
...> end) |> Enum.to_list()
iex> Stream.unfold(5, fn 0 -> nil; n -> {n, n-1} end) |> Enum.to_list()
[5, 4, 3, 2, 1]
"""
@@ -1505,17 +1474,16 @@ defmodule Stream do
## Examples
iex> Stream.intersperse([1, 2, 3], 0) |> Enum.to_list()
iex> Stream.intersperse([1, 2, 3], 0) |> Enum.to_list
[1, 0, 2, 0, 3]
iex> Stream.intersperse([1], 0) |> Enum.to_list()
iex> Stream.intersperse([1], 0) |> Enum.to_list
[1]
iex> Stream.intersperse([], 0) |> Enum.to_list()
iex> Stream.intersperse([], 0) |> Enum.to_list
[]
"""
@doc since: "1.6.0"
@spec intersperse(Enumerable.t(), any) :: Enumerable.t()
def intersperse(enumerable, intersperse_element) do
Stream.transform(enumerable, false, fn
+1 -1
View File
@@ -25,7 +25,7 @@ defmodule Stream.Reducers do
end
after_fun = fn {acc_buffer, acc_count} ->
if leftover == :discard or acc_count == 0 or acc_count >= count do
if leftover == :discard or acc_count == 0 or (step > count and acc_count >= count) do
{:cont, []}
else
{:cont, :lists.reverse(acc_buffer, Enum.take(leftover, count - acc_count)), []}
+139 -343
View File
@@ -7,7 +7,7 @@ defmodule String do
## Codepoints and grapheme cluster
The functions in this module act according to the Unicode
Standard, version 11.0.0.
Standard, version 10.0.0.
As per the standard, a codepoint is a single Unicode Character,
which may be represented by one or more bytes.
@@ -183,7 +183,7 @@ defmodule String do
## Patterns
Many functions in this module work with patterns. For example,
`String.split/2` can split a string into multiple strings given
`String.split/2` can split a string into multiple patterns given
a pattern. This pattern can be a string, a list of strings or
a compiled pattern:
@@ -198,36 +198,23 @@ defmodule String do
["foo", "bar", ""]
The compiled pattern is useful when the same match will
be done over and over again. Note though that the compiled
be done over and over again. Note though the compiled
pattern cannot be stored in a module attribute as the pattern
is generated at runtime and does not survive compile time.
is generated at runtime and does not survive compile term.
"""
@typedoc """
A UTF-8 encoded binary.
The types `String.t()` and `binary()` are equivalent to analysis tools.
Although, for those reading the documentation, `String.t()` implies
it is a UTF-8 encoded binary.
"""
@type t :: binary
@typedoc "A UTF-8 codepoint. It may be one or more bytes."
@type codepoint :: t
@typedoc "Multiple codepoints that may be perceived as a single character by readers"
@type grapheme :: t
@typedoc "Pattern used in functions like `replace/3` and `split/2`"
@type pattern :: t | [t] | :binary.cp()
@conditional_mappings [:greek]
@doc """
Checks if a string contains only printable characters up to `character_limit`.
Checks if a string contains only printable characters.
Takes an optional `character_limit` as a second argument. If `character_limit` is `0`, this
function will return `true`.
Takes an optional `limit` as a second argument. `printable?/2` only
checks the printability of the string up to the `limit`.
## Examples
@@ -240,47 +227,37 @@ defmodule String do
iex> String.printable?("abc" <> <<0>>, 2)
true
iex> String.printable?("abc" <> <<0>>, 0)
true
"""
@spec printable?(t, 0) :: true
@spec printable?(t, pos_integer | :infinity) :: boolean
def printable?(string, character_limit \\ :infinity)
when is_binary(string) and
(character_limit == :infinity or
(is_integer(character_limit) and character_limit >= 0)) do
recur_printable?(string, character_limit)
end
@spec printable?(t) :: boolean
@spec printable?(t, non_neg_integer | :infinity) :: boolean
def printable?(string, counter \\ :infinity)
defp recur_printable?(_string, 0), do: true
defp recur_printable?(<<>>, _character_limit), do: true
def printable?(<<>>, _), do: true
def printable?(_, 0), do: true
for char <- 0x20..0x7E do
defp recur_printable?(<<unquote(char), rest::binary>>, character_limit) do
recur_printable?(rest, decrement(character_limit))
def printable?(<<unquote(char), rest::binary>>, counter) do
printable?(rest, decrement(counter))
end
end
for char <- '\n\r\t\v\b\f\e\d\a' do
defp recur_printable?(<<unquote(char), rest::binary>>, character_limit) do
recur_printable?(rest, decrement(character_limit))
def printable?(<<unquote(char), rest::binary>>, counter) do
printable?(rest, decrement(counter))
end
end
defp recur_printable?(<<char::utf8, rest::binary>>, character_limit)
when char in 0xA0..0xD7FF
when char in 0xE000..0xFFFD
when char in 0x10000..0x10FFFF do
recur_printable?(rest, decrement(character_limit))
def printable?(<<char::utf8, rest::binary>>, counter)
when char in 0xA0..0xD7FF
when char in 0xE000..0xFFFD
when char in 0x10000..0x10FFFF do
printable?(rest, decrement(counter))
end
defp recur_printable?(_string, _character_limit) do
false
end
def printable?(binary, _) when is_binary(binary), do: false
defp decrement(:infinity), do: :infinity
defp decrement(character_limit), do: character_limit - 1
defp decrement(counter), do: counter - 1
@doc ~S"""
Divides a string into substrings at each Unicode whitespace
@@ -307,11 +284,10 @@ defmodule String do
defdelegate split(binary), to: String.Break
@doc ~S"""
Divides a string into parts based on a pattern.
Divides a string into substrings based on a pattern.
Returns a list of these parts. The pattern can
be a string, a list of strings, a regular expression,
or a compiled pattern.
Returns a list of these substrings. The pattern can
be a string, a list of strings, or a regular expression.
The string is split into as many parts as possible by
default, but can be controlled via the `:parts` option.
@@ -367,12 +343,6 @@ defmodule String do
iex> String.split("abc", ~r{b}, include_captures: true)
["a", "b", "c"]
A compiled pattern:
iex> pattern = :binary.compile_pattern([" ", ","])
iex> String.split("1,2 3,4", pattern)
["1", "2", "3", "4"]
Splitting on empty string returns graphemes:
iex> String.split("abc", "")
@@ -387,15 +357,21 @@ defmodule String do
iex> String.split("abc", "", parts: 3)
["", "a", "bc"]
Be aware that this function can split within or across grapheme boundaries.
A precompiled pattern can also be given:
iex> pattern = :binary.compile_pattern([" ", ","])
iex> String.split("1,2 3,4", pattern)
["1", "2", "3", "4"]
Note this function can split within or across grapheme boundaries.
For example, take the grapheme "é" which is made of the characters
"e" and the acute accent. The following will split the string into two parts:
"e" and the acute accent. The following returns true:
iex> String.split(String.normalize("é", :nfd), "e")
["", "́"]
However, if "é" is represented by the single character "e with acute"
accent, then it will split the string into just one part:
accent, then it will return false:
iex> String.split(String.normalize("é", :nfc), "e")
["é"]
@@ -457,13 +433,13 @@ defmodule String do
@doc """
Returns an enumerable that splits a string on demand.
This is in contrast to `split/3` which splits the
entire string upfront.
This is in contrast to `split/3` which splits all
the string upfront.
This function does not support regular expressions
by design. When using regular expressions, it is often
more efficient to have the regular expressions traverse
the string at once than in parts, like this function does.
Note splitter does not support regular expressions
(as it is often more efficient to have the regular
expressions traverse the string at once than in
multiple passes).
## Options
@@ -480,12 +456,6 @@ defmodule String do
iex> String.splitter("abcd", "", trim: true) |> Enum.take(10)
["a", "b", "c", "d"]
A compiled pattern can also be given:
iex> pattern = :binary.compile_pattern([" ", ","])
iex> String.splitter("1,2 3,4 5,6 7,8,...,99999", pattern) |> Enum.take(4)
["1", "2", "3", "4"]
"""
@spec splitter(t, pattern, keyword) :: Enumerable.t()
def splitter(string, pattern, options \\ [])
@@ -538,19 +508,19 @@ defmodule String do
## Examples
iex> String.split_at("sweetelixir", 5)
iex> String.split_at "sweetelixir", 5
{"sweet", "elixir"}
iex> String.split_at("sweetelixir", -6)
iex> String.split_at "sweetelixir", -6
{"sweet", "elixir"}
iex> String.split_at("abc", 0)
iex> String.split_at "abc", 0
{"", "abc"}
iex> String.split_at("abc", 1000)
iex> String.split_at "abc", 1000
{"abc", ""}
iex> String.split_at("abc", -1000)
iex> String.split_at "abc", -1000
{"", "abc"}
"""
@@ -632,24 +602,8 @@ defmodule String do
"leña"
"""
# TODO: Fully deprecate it on v1.10
@doc deprecated:
"Use :unicode.characters_to_nfc_binary/1 or :unicode.characters_to_nfd_binary/1 instead"
def normalize(string, form)
def normalize(string, :nfd) do
case :unicode.characters_to_nfd_binary(string) do
string when is_binary(string) -> string
{:error, bad, rest} -> bad <> normalize(rest, :nfd)
end
end
def normalize(string, :nfc) do
case :unicode.characters_to_nfc_binary(string) do
string when is_binary(string) -> string
{:error, bad, rest} -> bad <> normalize(rest, :nfc)
end
end
@spec normalize(t, atom) :: t
defdelegate normalize(string, form), to: String.Normalizer
@doc """
Converts all characters in the given string to uppercase according to `mode`.
@@ -690,19 +644,15 @@ defmodule String do
end
def upcase(string, :ascii) when is_binary(string) do
IO.iodata_to_binary(upcase_ascii(string))
for <<x <- string>>,
do: if(x >= ?a and x <= ?z, do: <<x - 32>>, else: <<x>>),
into: ""
end
def upcase(string, mode) when mode in @conditional_mappings do
String.Casing.upcase(string, [], mode)
end
defp upcase_ascii(<<char, rest::bits>>) when char >= ?a and char <= ?z,
do: [char - 32 | upcase_ascii(rest)]
defp upcase_ascii(<<char, rest::bits>>), do: [char | upcase_ascii(rest)]
defp upcase_ascii(<<>>), do: []
@doc """
Converts all characters in the given string to lowercase according to `mode`.
@@ -750,19 +700,15 @@ defmodule String do
end
def downcase(string, :ascii) when is_binary(string) do
IO.iodata_to_binary(downcase_ascii(string))
for <<x <- string>>,
do: if(x >= ?A and x <= ?Z, do: <<x + 32>>, else: <<x>>),
into: ""
end
def downcase(string, mode) when mode in @conditional_mappings do
String.Casing.downcase(string, [], mode)
end
defp downcase_ascii(<<char, rest::bits>>) when char >= ?A and char <= ?Z,
do: [char + 32 | downcase_ascii(rest)]
defp downcase_ascii(<<char, rest::bits>>), do: [char | downcase_ascii(rest)]
defp downcase_ascii(<<>>), do: []
@doc """
Converts the first character in the given string to
uppercase and the remainder to lowercase according to `mode`.
@@ -799,12 +745,12 @@ defmodule String do
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.trim_trailing/1 instead"
# (hard-deprecated in elixir_dispatch)
defdelegate rstrip(binary), to: String.Break, as: :trim_trailing
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.trim_trailing/2 with a binary as second argument instead"
# (hard-deprecated in elixir_dispatch)
def rstrip(string, char) when is_integer(char) do
replace_trailing(string, <<char::utf8>>, "")
end
@@ -832,7 +778,7 @@ defmodule String do
"ola ola world"
"""
@spec replace_leading(t, t, t) :: t
@spec replace_leading(t, t, t) :: t | no_return
def replace_leading(string, match, replacement)
when is_binary(string) and is_binary(match) and is_binary(replacement) do
if match == "" do
@@ -889,7 +835,7 @@ defmodule String do
"hello mundo mundo"
"""
@spec replace_trailing(t, t, t) :: t
@spec replace_trailing(t, t, t) :: t | no_return
def replace_trailing(string, match, replacement)
when is_binary(string) and is_binary(match) and is_binary(replacement) do
if match == "" do
@@ -1014,26 +960,26 @@ defmodule String do
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.trim_leading/1 instead"
# (hard-deprecated in elixir_dispatch)
defdelegate lstrip(binary), to: String.Break, as: :trim_leading
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.trim_leading/2 with a binary as second argument instead"
# (hard-deprecated in elixir_dispatch)
def lstrip(string, char) when is_integer(char) do
replace_leading(string, <<char::utf8>>, "")
end
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.trim/1 instead"
# (hard-deprecated in elixir_dispatch)
def strip(string) do
trim(string)
end
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.trim/2 with a binary second argument instead"
# (hard-deprecated in elixir_dispatch)
def strip(string, char) do
trim(string, <<char::utf8>>)
end
@@ -1145,7 +1091,7 @@ defmodule String do
When `count` is less than or equal to the length of `string`,
given `string` is returned.
Raises `ArgumentError` if the given `padding` contains a non-string element.
Raises `ArgumentError` if the given `padding` contains non-string element.
## Examples
@@ -1187,7 +1133,7 @@ defmodule String do
When `count` is less than or equal to the length of `string`,
given `string` is returned.
Raises `ArgumentError` if the given `padding` contains a non-string element.
Raises `ArgumentError` if the given `padding` contains non-string element.
## Examples
@@ -1258,29 +1204,15 @@ defmodule String do
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.pad_leading/2 instead"
def rjust(subject, len) do
rjust(subject, len, ?\s)
end
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.pad_leading/3 with a binary padding instead"
def rjust(subject, len, pad) when is_integer(pad) and is_integer(len) and len >= 0 do
# (hard-deprecated in elixir_dispatch)
def rjust(subject, len, pad \\ ?\s) when is_integer(pad) and is_integer(len) and len >= 0 do
pad(:leading, subject, len, [<<pad::utf8>>])
end
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.pad_trailing/2 instead"
def ljust(subject, len) do
ljust(subject, len, ?\s)
end
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.pad_trailing/3 with a binary padding instead"
def ljust(subject, len, pad) when is_integer(pad) and is_integer(len) and len >= 0 do
# (hard-deprecated in elixir_dispatch)
def ljust(subject, len, pad \\ ?\s) when is_integer(pad) and is_integer(len) and len >= 0 do
pad(:trailing, subject, len, [<<pad::utf8>>])
end
@@ -1288,7 +1220,7 @@ defmodule String do
Returns a new string created by replacing occurrences of `pattern` in
`subject` with `replacement`.
The `pattern` may be a string, a regular expression, or a compiled pattern.
The `pattern` may be a string or a regular expression.
By default it replaces all occurrences but this behaviour can be controlled
through the `:global` option; see the "Options" section below.
@@ -1339,12 +1271,6 @@ defmodule String do
iex> String.replace("a,b,c", ",", "[]", insert_replaced: [1, 1])
"a[,,]b[,,]c"
A compiled pattern can also be given:
iex> pattern = :binary.compile_pattern(",")
iex> String.replace("a,b,c", pattern, "[]", insert_replaced: 2)
"a[],b[],c"
When an empty string is provided as a `pattern`, the function will treat it as
an implicit empty string between each grapheme and the string will be
interspersed. If an empty string is provided as `replacement` the `subject`
@@ -1417,7 +1343,7 @@ defmodule String do
"̀e"
iex> String.reverse("̀e")
"è"
iex> String.reverse(String.reverse("̀e"))
iex> String.reverse String.reverse("̀e")
"è"
In the first example the accent is before the vowel, so
@@ -1487,46 +1413,23 @@ defmodule String do
@spec codepoints(t) :: [codepoint]
defdelegate codepoints(string), to: String.Unicode
@doc ~S"""
@doc """
Returns the next codepoint in a string.
The result is a tuple with the codepoint and the
remainder of the string or `nil` in case
the string reached its end.
As with other functions in the `String` module, `next_codepoint/1`
works with binaries that are invalid UTF-8. If the string starts
with a sequence of bytes that is not valid in UTF-8 encoding, the
first element of the returned tuple is a binary with the first byte.
As with other functions in the String module, this
function does not check for the validity of the codepoint.
That said, if an invalid codepoint is found, it will
be returned by this function.
## Examples
iex> String.next_codepoint("olá")
{"o", "lá"}
iex> invalid = "\x80\x80OK" # first two bytes are invalid in UTF-8
iex> {_, rest} = String.next_codepoint(invalid)
{<<128>>, <<128, 79, 75>>}
iex> String.next_codepoint(rest)
{<<128>>, "OK"}
## Comparison with binary pattern matching
Binary pattern matching provides a similar way to decompose
a string:
iex> <<codepoint::utf8, rest::binary>> = "Elixir"
"Elixir"
iex> codepoint
69
iex> rest
"lixir"
though not entirely equivalent because `codepoint` comes as
an integer, and the pattern won't match invalid UTF-8.
Binary pattern matching, however, is simpler and more efficient,
so pick the option that better suits your use case.
"""
@compile {:inline, next_codepoint: 1}
@spec next_codepoint(t) :: {codepoint, t} | nil
@@ -1543,13 +1446,13 @@ defmodule String do
iex> String.valid?("ø")
true
iex> String.valid?(<<0xFFFF::16>>)
iex> String.valid?(<<0xFFFF :: 16>>)
false
iex> String.valid?(<<0xEF, 0xB7, 0x90>>)
true
iex> String.valid?("asd" <> <<0xFFFF::16>>)
iex> String.valid?("asd" <> <<0xFFFF :: 16>>)
false
"""
@@ -1562,7 +1465,7 @@ defmodule String do
@doc false
# TODO: Remove on 2.0
@deprecated "Use String.valid?/1 instead"
# (hard-deprecated in elixir_dispatch)
def valid_character?(string) do
case string do
<<_::utf8>> -> valid?(string)
@@ -1671,7 +1574,7 @@ defmodule String do
@spec next_grapheme(t) :: {grapheme, t} | nil
def next_grapheme(binary) do
case next_grapheme_size(binary) do
{size, rest} -> {binary_part(binary, 0, size), rest}
{size, rest} -> {:binary.part(binary, 0, size), rest}
nil -> nil
end
end
@@ -1833,7 +1736,7 @@ defmodule String do
""
"""
@spec slice(t, integer, non_neg_integer) :: grapheme
@spec slice(t, integer, integer) :: grapheme
def slice(_, _, 0) do
""
@@ -1962,55 +1865,36 @@ defmodule String do
@doc """
Returns `true` if `string` starts with any of the prefixes given.
`prefix` can be either a string, a list of strings, or a compiled
pattern.
`prefix` can be either a single prefix or a list of prefixes.
## Examples
iex> String.starts_with?("elixir", "eli")
iex> String.starts_with? "elixir", "eli"
true
iex> String.starts_with?("elixir", ["erlang", "elixir"])
iex> String.starts_with? "elixir", ["erlang", "elixir"]
true
iex> String.starts_with?("elixir", ["erlang", "ruby"])
iex> String.starts_with? "elixir", ["erlang", "ruby"]
false
A compiled pattern can also be given:
iex> pattern = :binary.compile_pattern(["erlang", "elixir"])
iex> String.starts_with?("elixir", pattern)
true
An empty string will always match:
iex> String.starts_with?("elixir", "")
iex> String.starts_with? "elixir", ""
true
iex> String.starts_with?("elixir", ["", "other"])
iex> String.starts_with? "elixir", ["", "other"]
true
"""
@spec starts_with?(t, pattern) :: boolean
def starts_with?(string, prefix) when is_binary(string) and is_binary(prefix) do
starts_with_string?(string, byte_size(string), prefix)
@spec starts_with?(t, t | [t]) :: boolean
def starts_with?(string, []) when is_binary(string) do
false
end
def starts_with?(string, prefix) when is_binary(string) and is_list(prefix) do
string_size = byte_size(string)
Enum.any?(prefix, &starts_with_string?(string, string_size, &1))
"" in prefix or Kernel.match?({0, _}, :binary.match(string, prefix))
end
def starts_with?(string, prefix) when is_binary(string) do
Kernel.match?({0, _}, :binary.match(string, prefix))
end
@compile {:inline, starts_with_string?: 3}
defp starts_with_string?(string, string_size, prefix) when is_binary(prefix) do
prefix_size = byte_size(prefix)
if prefix_size <= string_size do
prefix == binary_part(string, 0, prefix_size)
else
false
end
"" == prefix or Kernel.match?({0, _}, :binary.match(string, prefix))
end
@doc """
@@ -2020,40 +1904,39 @@ defmodule String do
## Examples
iex> String.ends_with?("language", "age")
iex> String.ends_with? "language", "age"
true
iex> String.ends_with?("language", ["youth", "age"])
iex> String.ends_with? "language", ["youth", "age"]
true
iex> String.ends_with?("language", ["youth", "elixir"])
iex> String.ends_with? "language", ["youth", "elixir"]
false
An empty suffix will always match:
iex> String.ends_with?("language", "")
iex> String.ends_with? "language", ""
true
iex> String.ends_with?("language", ["", "other"])
iex> String.ends_with? "language", ["", "other"]
true
"""
@spec ends_with?(t, t | [t]) :: boolean
def ends_with?(string, suffix) when is_binary(string) and is_binary(suffix) do
ends_with_string?(string, byte_size(string), suffix)
def ends_with?(string, suffixes) when is_binary(string) and is_list(suffixes) do
Enum.any?(suffixes, &do_ends_with(string, &1))
end
def ends_with?(string, suffix) when is_binary(string) and is_list(suffix) do
def ends_with?(string, suffix) when is_binary(string) do
do_ends_with(string, suffix)
end
defp do_ends_with(_string, "") do
true
end
defp do_ends_with(string, suffix) when is_binary(suffix) do
string_size = byte_size(string)
Enum.any?(suffix, &ends_with_string?(string, string_size, &1))
end
@compile {:inline, ends_with_string?: 3}
defp ends_with_string?(string, string_size, suffix) when is_binary(suffix) do
suffix_size = byte_size(suffix)
if suffix_size <= string_size do
suffix == binary_part(string, string_size - suffix_size, suffix_size)
else
false
end
scope = {string_size - suffix_size, suffix_size}
suffix_size <= string_size and :nomatch != :binary.match(string, suffix, scope: scope)
end
@doc """
@@ -2076,42 +1959,41 @@ defmodule String do
@doc """
Checks if `string` contains any of the given `contents`.
`contents` can be either a string, a list of strings,
or a compiled pattern.
`contents` can be either a single string or a list of strings.
## Examples
iex> String.contains?("elixir of life", "of")
iex> String.contains? "elixir of life", "of"
true
iex> String.contains?("elixir of life", ["life", "death"])
iex> String.contains? "elixir of life", ["life", "death"]
true
iex> String.contains?("elixir of life", ["death", "mercury"])
iex> String.contains? "elixir of life", ["death", "mercury"]
false
The argument can also be a compiled pattern:
iex> pattern = :binary.compile_pattern(["life", "death"])
iex> String.contains?("elixir of life", pattern)
true
An empty string will always match:
iex> String.contains?("elixir of life", "")
iex> String.contains? "elixir of life", ""
true
iex> String.contains?("elixir of life", ["", "other"])
iex> String.contains? "elixir of life", ["", "other"]
true
Be aware that this function can match within or across grapheme boundaries.
The argument can also be a precompiled pattern:
iex> pattern = :binary.compile_pattern(["life", "death"])
iex> String.contains? "elixir of life", pattern
true
Note this function can match within or across grapheme boundaries.
For example, take the grapheme "é" which is made of the characters
"e" and the acute accent. The following returns `true`:
"e" and the acute accent. The following returns true:
iex> String.contains?(:unicode.characters_to_nfd_binary("é"), "e")
iex> String.contains?(String.normalize("é", :nfd), "e")
true
However, if "é" is represented by the single character "e with acute"
accent, then it will return `false`:
accent, then it will return false:
iex> String.contains?(:unicode.characters_to_nfc_binary("é"), "e")
iex> String.contains?(String.normalize("é", :nfc), "e")
false
"""
@@ -2131,7 +2013,7 @@ defmodule String do
@doc """
Converts a string into a charlist.
Specifically, this function takes a UTF-8 encoded binary and returns a list of its integer
Specifically, this functions takes a UTF-8 encoded binary and returns a list of its integer
codepoints. It is similar to `codepoints/1` except that the latter returns a list of codepoints as
strings.
@@ -2142,7 +2024,6 @@ defmodule String do
iex> String.to_charlist("æß")
'æß'
"""
@spec to_charlist(t) :: charlist
def to_charlist(string) when is_binary(string) do
@@ -2162,14 +2043,15 @@ defmodule String do
Converts a string to an atom.
Warning: this function creates atoms dynamically and atoms are
not garbage-collected. Therefore, `string` should not be an
not garbage collected. Therefore, `string` should not be an
untrusted value, such as input received from a socket or during
a web request. Consider using `to_existing_atom/1` instead.
By default, the maximum number of atoms is `1_048_576`. This limit
can be raised or lowered using the VM option `+t`.
The maximum atom size is of 255 Unicode codepoints.
The maximum atom size is of 255 characters. Prior to OTP 20,
only latin1 characters are allowed.
Inlined by the compiler.
@@ -2187,7 +2069,8 @@ defmodule String do
@doc """
Converts a string to an existing atom.
The maximum atom size is of 255 Unicode codepoints.
The maximum atom size is of 255 characters. Prior to OTP 20,
only latin1 characters are allowed.
Inlined by the compiler.
@@ -2209,11 +2092,6 @@ defmodule String do
@doc """
Returns an integer whose text representation is `string`.
`string` must be the string representation of an integer.
Otherwise, an `ArgumentError` will be raised. If you want
to parse a string that may contain an ill-formatted integer,
use `Integer.parse/1`.
Inlined by the compiler.
## Examples
@@ -2221,11 +2099,6 @@ defmodule String do
iex> String.to_integer("123")
123
Passing a string that does not represent an integer leads to an error:
String.to_integer("invalid data")
#=> ** (ArgumentError) argument error
"""
@spec to_integer(String.t()) :: integer
def to_integer(string) do
@@ -2275,95 +2148,18 @@ defmodule String do
end
@doc """
Computes the bag distance between two strings.
Returns a float value between 0 and 1 representing the bag
Returns a float value between 0 (equates to no similarity) and 1 (is an exact match)
representing [Jaro](https://en.wikipedia.org/wiki/Jaro–Winkler_distance)
distance between `string1` and `string2`.
The bag distance is meant to be an efficient approximation
of the distance between two strings to quickly rule out strings
that are largely different.
The algorithm is outlined in the "String Matching with Metric
Trees Using an Approximate Distance" paper by Ilaria Bartolini,
Paolo Ciaccia, and Marco Patella.
The Jaro distance metric is designed and best suited for short strings such as person names.
## Examples
iex> String.bag_distance("abc", "")
0.0
iex> String.bag_distance("abcd", "a")
0.25
iex> String.bag_distance("abcd", "ab")
0.5
iex> String.bag_distance("abcd", "abc")
0.75
iex> String.bag_distance("abcd", "abcd")
1.0
"""
@spec bag_distance(t, t) :: float
@doc since: "1.8.0"
def bag_distance(_string, ""), do: 0.0
def bag_distance("", _string), do: 0.0
def bag_distance(string1, string2) do
{bag1, length1} = string_to_bag(string1, %{}, 0)
{bag2, length2} = string_to_bag(string2, %{}, 0)
diff1 = bag_difference(bag1, bag2)
diff2 = bag_difference(bag2, bag1)
1 - max(diff1, diff2) / max(length1, length2)
end
defp string_to_bag(string, bag, length) do
case next_grapheme(string) do
{char, rest} ->
bag =
case bag do
%{^char => current} -> %{bag | char => current + 1}
%{} -> Map.put(bag, char, 1)
end
string_to_bag(rest, bag, length + 1)
nil ->
{bag, length}
end
end
defp bag_difference(bag1, bag2) do
Enum.reduce(bag1, 0, fn {char, count1}, sum ->
case bag2 do
%{^char => count2} -> sum + max(count1 - count2, 0)
%{} -> sum + count1
end
end)
end
@doc """
Computes the Jaro distance (similarity) between two strings.
Returns a float value between `0.0` (equates to no similarity) and `1.0`
(is an exact match) representing [Jaro](https://en.wikipedia.org/wiki/Jaro-Winkler_distance)
distance between `string1` and `string2`.
The Jaro distance metric is designed and best suited for short
strings such as person names. Elixir itself uses this function
to provide the "did you mean?" functionality. For instance, when you
are calling a function in a module and you have a typo in the
function name, we attempt to suggest the most similar function
name available, if any, based on the `jaro_distance/2` score.
## Examples
iex> String.jaro_distance("Dwayne", "Duane")
iex> String.jaro_distance("dwayne", "duane")
0.8222222222222223
iex> String.jaro_distance("even", "odd")
0.0
iex> String.jaro_distance("same", "same")
1.0
"""
@spec jaro_distance(t, t) :: float
@@ -2458,16 +2254,16 @@ defmodule String do
[eq: "fox ", del: "ho", ins: "jum", eq: "ps over the ", del: "dog", ins: "lazy cat"]
"""
@spec myers_difference(t, t) :: [{:eq | :ins | :del, t}]
@spec myers_difference(t, t) :: [{:eq | :ins | :del, t}] | nil
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)
end
@doc false
# TODO: Remove by 2.0
@deprecated "Use String.to_charlist/1 instead"
# (hard-deprecated in elixir_dispatch)
@doc false
@spec to_char_list(t) :: charlist
def to_char_list(string), do: String.to_charlist(string)
end
+10 -75
View File
@@ -15,7 +15,7 @@ defmodule StringIO do
use GenServer
@doc ~S"""
@doc """
Creates an IO device.
`string` will be the initial input of the newly created
@@ -23,59 +23,7 @@ defmodule StringIO do
If the `:capture_prompt` option is set to `true`,
prompts (specified as arguments to `IO.get*` functions)
are captured in the output.
The device will be created and sent to the function given.
When the function returns, the device will be closed. The final
result will be a tuple with `:ok` and the result of the function.
## Examples
iex> StringIO.open("foo", [], fn pid ->
...> input = IO.gets(pid, ">")
...> IO.write(pid, "The input was #{input}")
...> StringIO.contents(pid)
...> end)
{:ok, {"", "The input was foo"}}
iex> StringIO.open("foo", [capture_prompt: true], fn pid ->
...> input = IO.gets(pid, ">")
...> IO.write(pid, "The input was #{input}")
...> StringIO.contents(pid)
...> end)
{:ok, {"", ">The input was foo"}}
"""
@doc since: "1.7.0"
@spec open(binary, keyword, (pid -> res)) :: {:ok, res} when res: var
def open(string, options, function)
when is_binary(string) and is_list(options) and is_function(function, 1) do
{:ok, pid} = GenServer.start_link(__MODULE__, {string, options}, [])
try do
{:ok, function.(pid)}
after
{:ok, {_input, _output}} = close(pid)
end
end
@doc ~S"""
Creates an IO device.
`string` will be the initial input of the newly created
device.
`options_or_function` can be a keyword list of options or
a function.
If options are provided, the result will be `{:ok, pid}`, returning the
IO device created. The option `:capture_prompt`, when set to `true`, causes
prompts (which are specified as arguments to `IO.get*` functions) to be
included in the device's output.
If a function is provided, the device will be created and sent to the
function. When the function returns, the device will be closed. The final
result will be a tuple with `:ok` and the result of the function.
are captured.
## Examples
@@ -91,25 +39,10 @@ defmodule StringIO do
iex> StringIO.contents(pid)
{"", ">"}
iex> StringIO.open("foo", fn pid ->
...> input = IO.gets(pid, ">")
...> IO.write(pid, "The input was #{input}")
...> StringIO.contents(pid)
...> end)
{:ok, {"", "The input was foo"}}
"""
@spec open(binary, keyword) :: {:ok, pid}
@spec open(binary, (pid -> res)) :: {:ok, res} when res: var
def open(string, options_or_function \\ [])
def open(string, options_or_function) when is_binary(string) and is_list(options_or_function) do
GenServer.start_link(__MODULE__, {string, options_or_function}, [])
end
def open(string, options_or_function)
when is_binary(string) and is_function(options_or_function, 1) do
open(string, [], options_or_function)
def open(string, options \\ []) when is_binary(string) do
GenServer.start_link(__MODULE__, {string, options}, [])
end
@doc """
@@ -178,8 +111,8 @@ defmodule StringIO do
{:noreply, state}
end
def handle_info(_message, state) do
{:noreply, state}
def handle_info(message, state) do
super(message, state)
end
@impl true
@@ -195,6 +128,10 @@ defmodule StringIO do
{:stop, :normal, {:ok, {input, output}}, state}
end
def handle_call(request, from, state) do
super(request, from, state)
end
defp io_request(from, reply_as, req, state) do
{reply, state} = io_request(req, state)
io_reply(from, reply_as, to_reply(reply))
@@ -279,8 +216,6 @@ defmodule StringIO do
{_, _, _} ->
{{:error, req}, state}
end
rescue
ArgumentError -> {{:error, req}, state}
end
## get_chars
+267 -261
View File
@@ -28,19 +28,16 @@ defmodule Supervisor do
## Callbacks
@impl true
def init(stack) do
{:ok, stack}
end
@impl true
def handle_call(:pop, _from, [head | tail]) do
{:reply, head, tail}
def handle_call(:pop, _from, [h | t]) do
{:reply, h, t}
end
@impl true
def handle_cast({:push, head}, tail) do
{:noreply, [head | tail]}
def handle_cast({:push, h}, t) do
{:noreply, [h | t]}
end
end
@@ -96,24 +93,63 @@ defmodule Supervisor do
Supervisors support different strategies; in the example above, we
have chosen `:one_for_one`. Furthermore, each supervisor can have many
workers and/or supervisors as children, with each one having its own
configuration (as outlined in the "Child specification" section).
workers and supervisors as children, each of them with their specific
configuration, shutdown values, and restart strategies.
The rest of this document will cover how child processes are specified,
how they can be started and stopped, different supervision strategies
and more.
The rest of this document will cover how child processes are started,
how they can be specified, different supervision strategies and more.
## Start and shutdown
When the supervisor starts, it traverses all child specifications and
then starts each child in the order they are defined. This is done by
calling the function defined under the `:start` key in the child
specification and typically defaults to `start_link/1`.
The `start_link/1` (or a custom) is then called for each child process.
The `start_link/1` function must return `{:ok, pid}` where `pid` is the
process identifier of a new process that is linked to the supervisor.
The child process usually starts its work by executing the `init/1`
callback. Generally speaking, the `init` callback is where we initialize
and configure the child process.
The shutdown process happens in reverse order.
When a supervisor shuts down, it terminates all children in the opposite
order they are listed. The termination happens by sending a shutdown exit
signal, via `Process.exit(child_pid, :shutdown)`, to the child process and
then awaiting for a time interval for the child process to terminate. This
interval defaults to 5000 milliseconds. If the child process does not
terminate in this interval, the supervisor abruptly terminates the child
with reason `:brutal_kill`. The shutdown time can be configured in the
child specification which is fully detailed in the next section.
If the child process is not trapping exits, it will shutdown immediately
when it receives the first exit signal. If the child process is trapping
exits, then the `terminate` callback is invoked, and the child process
must terminate in a reasonable time interval before being abruptly
terminated by the supervisor.
In other words, if it is important that a process cleans after itself
when your application or the supervision tree is shutting down, then
this process must trap exits and its child specification should specify
the proper `:shutdown` value, ensuring it terminates within a reasonable
interval.
Now that we understand the start and shutdown process, let's take a
complete look at all of the options provided in the child specification.
## Child specification
The child specification describes how the supervisor starts, shuts down,
and restarts child processes.
The child specification describes how the supervisor start, shutdown and
restart child processes.
The child specification contains 6 keys. The first two are required,
The child specification contains 5 keys. The first two are required
and the remaining ones are optional:
* `:id` - any term used to identify the child specification
* `:id` - a value used to identify the child specification
internally by the supervisor; defaults to the given module.
In the case of conflicting `:id` values, the supervisor will refuse
In case of conflicting `:id`, the supervisor will refuse
to initialize and require explicit IDs. This key is required.
* `:start` - a tuple with the module-function-args to be invoked
@@ -128,11 +164,11 @@ defmodule Supervisor do
is optional and defaults to `5000` if the type is `:worker` or
`:infinity` if the type is `:supervisor`.
* `:type` - specifies that the child process is a `:worker` or a
`:supervisor`. This key is optional and defaults to `:worker`.
* `:type` - if the child process is a `:worker` or a `:supervisor`.
This key is optional and defaults to `:worker`.
There is a sixth key, `:modules`, that is rarely changed. It is set
automatically based on the value in `:start`.
There is a sixth key, called `:modules`, which is rarely changed and
it is set automatically based on the value in `:start`.
Let's understand what the `:shutdown` and `:restart` options control.
@@ -144,11 +180,11 @@ defmodule Supervisor do
terminated using `Process.exit(child, :kill)`.
* any integer >= 0 - the amount of time in milliseconds that the
supervisor will wait for its children to terminate after emitting a
supervisor will wait for children to terminate after emitting a
`Process.exit(child, :shutdown)` signal. If the child process is
not trapping exits, the initial `:shutdown` signal will terminate
the child process immediately. If the child process is trapping
exits, it has the given amount of time to terminate.
exits, it has the given amount of time in milliseconds to terminate.
If it doesn't terminate within the specified time, the child process
is unconditionally terminated by the supervisor via
`Process.exit(child, :kill)`.
@@ -156,10 +192,10 @@ defmodule Supervisor do
* `:infinity` - works as an integer except the supervisor will wait
indefinitely for the child to terminate. If the child process is a
supervisor, the recommended value is `:infinity` to give the supervisor
and its children enough time to shut down. This option can be used with
and its children enough time to shutdown. This option can be used with
regular workers but doing so is discouraged and requires extreme care.
If not used carefully, the child process will never terminate,
preventing your application from terminating as well.
If not used carefully and the child process does not terminate, it means
your application will never terminate as well.
### Restart values (:restart)
@@ -173,12 +209,11 @@ defmodule Supervisor do
* `:permanent` - the child process is always restarted.
* `:temporary` - the child process is never restarted, regardless
of the supervision strategy: any termination (even abnormal) is
considered successful.
of the supervision strategy.
* `:transient` - the child process is restarted only if it
terminates abnormally, i.e., with an exit reason other than
`:normal`, `:shutdown`, or `{:shutdown, term}`.
`:normal`, `:shutdown` or `{:shutdown, term}`.
For a more complete understanding of the exit reasons and their
impact, see the "Exit reasons and restarts" section.
@@ -208,222 +243,71 @@ defmodule Supervisor do
The supervisor will then invoke `Stack.child_spec([:hello])` to retrieve a
child specification. Now the `Stack` module is responsible for building its
own specification, for example, we could write:
own specification. By default, `use GenServer` defines a `Stack.child_spec/1`
function which returns the same child specification we had before:
def child_spec(arg) do
%{
id: Stack,
start: {Stack, :start_link, [arg]}
}
end
%{
id: Stack,
start: {Stack, :start_link, [[:hello]]}
}
Luckily for us, `use GenServer` already defines a `Stack.child_spec/1`
exactly like above. If you need to customize the `GenServer`, you can
pass the options directly to `use GenServer`:
use GenServer, restart: :transient
Finally, note it is also possible to simply pass the `Stack` module as
a child:
It is also possible to simply pass the `Stack` module as a child:
children = [
Stack
]
When only the module name is given, it is equivalent to `{Stack, []}`.
When only the module name is given, it is equivalent to `{Stack, []}`. In this
case, we will end-up with a child specification that looks like this:
%{
id: Stack,
start: {Stack, :start_link, [[]]}
}
By replacing the map specification by `{Stack, [:hello]}` or `Stack`, we keep
the child specification encapsulated in the `Stack` module, using the default
the child specification encapsulated in the Stack module, using the default
implementation defined by `use GenServer`. We can now share our `Stack` worker
with other developers and they can add it directly to their supervision tree
without worrying about the low-level details of the worker.
Overall, the child specification can be one of the following:
* a map representing the child specification itself - as outlined in the
"Child specification" section
* a tuple with a module as first element and the start argument as second -
such as `{Stack, [:hello]}`. In this case, `Stack.child_spec([:hello])`
is called to retrieve the child specification
* a module - such as `Stack`. In this case, `Stack.child_spec([])`
is called to retrieve the child specification
If you need to convert a tuple or a module child specification to a map or
modify a child specification, you can use the `Supervisor.child_spec/2` function.
For example, to run the stack with a different `:id` and a `:shutdown` value of
10 seconds (10_000 milliseconds):
If you need to access or modify how a worker or a supervisor runs, you can use
the `Supervisor.child_spec/2` function. For example, to run the stack with a
different `:id` and a `:shutdown` value of 10 seconds (10_000 milliseconds):
children = [
Supervisor.child_spec({Stack, [:hello]}, id: MyStack, shutdown: 10_000)
]
## Module-based supervisors
The call to `Supervisor.child_spec/2` above will return the following specification:
In the example above, a supervisor was started by passing the supervision
structure to `start_link/2`. However, supervisors can also be created by
explicitly defining a supervision module:
%{
id: MyStack,
start: {Stack, :start_link, [[:hello]]},
shutdown: 10_000
}
defmodule MyApp.Supervisor do
# Automatically defines child_spec/1
use Supervisor
You may also configure the child specification in the Stack module itself to
use a different `:id` or `:shutdown` value by passing options to `use GenServer`:
def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
defmodule Stack do
use GenServer, id: MyStack, shutdown: 10_000
@impl true
def init(_init_arg) do
children = [
{Stack, [:hello]}
]
The options above will customize the `Stack.child_spec/1` function defined
by `use GenServer`. It accepts the same options as the `Supervisor.child_spec/2`
function.
Supervisor.init(children, strategy: :one_for_one)
end
end
The difference between the two approaches is that a module-based
supervisor gives you more direct control over how the supervisor
is initialized. Instead of calling `Supervisor.start_link/2` with
a list of children that are automatically initialized, we manually
initialized the children by calling `Supervisor.init/2` inside its
`c:init/1` callback.
`use Supervisor` also defines a `child_spec/1` function which allows
us to run `MyApp.Supervisor` as a child of another supervisor or
at the top of your supervision tree as:
children = [
MyApp.Supervisor
]
Supervisor.start_link(children, strategy: :one_for_one)
A general guideline is to use the supervisor without a callback
module only at the top of your supervision tree, generally in the
`c:Application.start/2` callback. We recommend using module-based
supervisors for any other supervisor in your application, so they
can run as a child of another supervisor in the tree. The `child_spec/1`
generated automatically by `Supervisor` can be customized with the
following options:
* `:id` - the child specification identifier, defaults to the current module
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
* `:restart` - when the supervisor should be restarted, defaults to `:permanent`
The `@doc` annotation immediately preceding `use Supervisor` will be
attached to the generated `child_spec/1` function.
## `start_link/2`, `init/2`, and strategies
So far we have started the supervisor passing a single child as a tuple
as well as a strategy called `:one_for_one`:
children = [
{Stack, [:hello]}
]
Supervisor.start_link(children, strategy: :one_for_one)
or from inside the `c:init/1` callback:
children = [
{Stack, [:hello]}
]
Supervisor.init(children, strategy: :one_for_one)
The first argument given to `start_link/2` and `init/2` is a list of child
specifications as defined in the "child_spec/1" section above.
The second argument is a keyword list of options:
* `:strategy` - the supervision strategy option. It can be either
`:one_for_one`, `:rest_for_one` or `:one_for_all`. Required.
See the "Strategies" section.
* `:max_restarts` - the maximum number of restarts allowed in
a time frame. Defaults to `3`.
* `:max_seconds` - the time frame in which `:max_restarts` applies.
Defaults to `5`.
* `:name` - a name to register the supervisor process. Supported values are
explained in the "Name registration" section in the documentation for
`GenServer`. Optional.
### Strategies
Supervisors support different supervision strategies (through the
`:strategy` option, as seen above):
* `:one_for_one` - if a child process terminates, only that
process is restarted.
* `:one_for_all` - if a child process terminates, all other child
processes are terminated and then all child processes (including
the terminated one) are restarted.
* `:rest_for_one` - if a child process terminates, the terminated child
process and the rest of the children started after it, are terminated and
restarted.
In the above, process termination refers to unsuccessful termination, which
is determined by the `:restart` option.
There is also a deprecated strategy called `:simple_one_for_one` which
has been replaced by the `DynamicSupervisor`. The `:simple_one_for_one`
supervisor was similar to `:one_for_one` but suits better when dynamically
attaching children. Many functions in this module behaved slightly
differently when this strategy was used. See the `DynamicSupervisor` module
for more information and migration strategies.
### Name registration
A supervisor is bound to the same name registration rules as a `GenServer`.
Read more about these rules in the documentation for `GenServer`.
## Start and shutdown
When the supervisor starts, it traverses all child specifications and
then starts each child in the order they are defined. This is done by
calling the function defined under the `:start` key in the child
specification and typically defaults to `start_link/1`.
The `start_link/1` (or a custom) is then called for each child process.
The `start_link/1` function must return `{:ok, pid}` where `pid` is the
process identifier of a new process that is linked to the supervisor.
The child process usually starts its work by executing the `init/1`
callback. Generally speaking, the `init` callback is where we initialize
and configure the child process.
The shutdown process happens in reverse order.
When a supervisor shuts down, it terminates all children in the opposite
order they are listed. The termination happens by sending a shutdown exit
signal, via `Process.exit(child_pid, :shutdown)`, to the child process and
then awaiting for a time interval for the child process to terminate. This
interval defaults to 5000 milliseconds. If the child process does not
terminate in this interval, the supervisor abruptly terminates the child
with reason `:kill`. The shutdown time can be configured in the child
specification which is fully detailed in the next section.
If the child process is not trapping exits, it will shutdown immediately
when it receives the first exit signal. If the child process is trapping
exits, then the `terminate` callback is invoked, and the child process
must terminate in a reasonable time interval before being abruptly
terminated by the supervisor.
In other words, if it is important that a process cleans after itself
when your application or the supervision tree is shutting down, then
this process must trap exits and its child specification should specify
the proper `:shutdown` value, ensuring it terminates within a reasonable
interval.
You may also completely override the `child_spec/1` function in the Stack module
and return your own child specification. Note there is no guarantee the `child_spec/1`
function will be called by the Supervisor process, as other processes may invoke
it to retrieve the child specification before reaching the supervisor.
## Exit reasons and restarts
A supervisor restarts a child process depending on its `:restart` configuration.
For example, when `:restart` is set to `:transient`, the supervisor does not
restart the child in case it exits with reason `:normal`, `:shutdown` or
`{:shutdown, term}`.
A supervisor restarts a child process depending on its `:restart`
configuration. For example, when `:restart` is set to `:transient`, the
supervisor does not restart the child in case it exits with reason `:normal`,
`:shutdown` or `{:shutdown, term}`.
So one may ask: which exit reason should I choose when exiting? There are
three options:
@@ -439,10 +323,135 @@ defmodule Supervisor do
restarts in transient mode, and linked processes exit with the same
reason unless they're trapping exits
Notice that the supervisor that reaches maximum restart intensity will exit with
Notice that supervisor that reached maximum restart intensity will exit with
`:shutdown` reason. In this case the supervisor will only be restarted if its
child specification was defined with the `:restart` option set to `:permanent`
(the default).
## Module-based supervisors
In the example above, a supervisor was started by passing the supervision
structure to `start_link/2`. However, supervisors can also be created by
explicitly defining a supervision module:
defmodule MyApp.Supervisor do
# Automatically defines child_spec/1
use Supervisor
def start_link(arg) do
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
end
def init(_arg) do
children = [
{Stack, [:hello]}
]
Supervisor.init(children, strategy: :one_for_one)
end
end
The difference between the two approaches is that a module-based
supervisor gives you more direct control over how the supervisor
is initialized. Instead of calling `Supervisor.start_link/2` with
a list of children that are automatically initialized, we have
defined a supervisor alongside its `c:init/1` callback and manually
initialized the children by calling `Supervisor.init/2`, passing
the same arguments we would have given to `start_link/2`.
You may want to use a module-based supervisor if:
* You need to perform some particular action on supervisor
initialization, like setting up an ETS table.
* You want to perform partial hot-code swapping of the
tree. The module-based approach allow you to add and remove
children on a case-by-case basis.
Note `use Supervisor` defines a `child_spec/1` function, allowing
the defined module itself to be put under a supervision tree.
The generated `child_spec/1` can be customized with the following
options:
* `:id` - the child specification id, defaults to the current module
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
* `:restart` - when the supervisor should be restarted, defaults to `:permanent`
## `start_link/2`, `init/2`, and strategies
So far we have started the supervisor passing a single child as a tuple
as well as a strategy called `:one_for_one`:
Supervisor.start_link([
{Stack, [:hello]}
], strategy: :one_for_one)
or from inside the `c:init/1` callback:
Supervisor.init([
{Stack, [:hello]}
], strategy: :one_for_one)
Although we have mentioned that the supervisor automatically expands
`{Stack, [:hello]}` to a child specification by calling
`Stack.child_spec([:hello])`, we haven't formally defined all of the
arguments accepted by `start_link/2` and `init/2`. Let's rectify that
now.
The first argument given to `start_link/2` is a list of children which may
be either:
* a map representing the child specification itself - as outlined in the
"Child specification" section
* a tuple with a module as first element and the start argument as second -
such as `{Stack, [:hello]}`. In this case, `Stack.child_spec([:hello])`
is called to retrieve the child specification
* a module - such as `Stack`. In this case, `Stack.child_spec([])`
is called to retrieve the child specification
The second argument is a keyword list of options:
* `:strategy` - the restart strategy option. It can be either
`:one_for_one`, `:rest_for_one` or `:one_for_all`. See the
"Strategies" section.
* `:max_restarts` - the maximum number of restarts allowed in
a time frame. Defaults to `3`.
* `:max_seconds` - the time frame in which `:max_restarts` applies.
Defaults to `5`.
The `:strategy` option is required and by default a maximum of 3 restarts
is allowed within 5 seconds.
### Strategies
Supervisors support different supervision strategies (through the
`:strategy` option, as seen above):
* `:one_for_one` - if a child process terminates, only that
process is restarted.
* `:one_for_all` - if a child process terminates, all other child
processes are terminated and then all child processes (including
the terminated one) are restarted.
* `:rest_for_one` - if a child process terminates, the "rest" of
the child processes, i.e., the child processes after the terminated
one in start order, are terminated. Then the terminated child
process and the rest of the child processes are restarted.
There is also a deprecated strategy called `:simple_one_for_one` which
has been replaced by the `DynamicSupervisor`. The `:simple_one_for_one`
supervisor was similar to `:one_for_one` but suits better when dynamically
attaching children. Many functions in this module behaved slightly
differently when this strategy is used. See the `DynamicSupervisor` module
for more information and migration strategies.
## Name registration
A supervisor is bound to the same name registration rules as a `GenServer`.
Read more about these rules in the documentation for `GenServer`.
"""
@doc false
@@ -451,18 +460,15 @@ defmodule Supervisor do
import Supervisor.Spec
@behaviour Supervisor
if Module.get_attribute(__MODULE__, :doc) == nil do
@doc """
Returns a specification to start this module under a supervisor.
@doc """
Returns a specification to start this module under a supervisor.
See `Supervisor`.
"""
end
def child_spec(init_arg) do
See `Supervisor`.
"""
def child_spec(arg) do
default = %{
id: __MODULE__,
start: {__MODULE__, :start_link, [init_arg]},
start: {__MODULE__, :start_link, [arg]},
type: :supervisor
}
@@ -470,6 +476,9 @@ defmodule Supervisor do
end
defoverridable child_spec: 1
@doc false
def init(arg)
end
end
@@ -479,7 +488,7 @@ defmodule Supervisor do
Developers typically invoke `Supervisor.init/2` at the end of their
init callback to return the proper supervision flags.
"""
@callback init(init_arg :: term) ::
@callback init(args :: term) ::
{:ok, {:supervisor.sup_flags(), [:supervisor.child_spec()]}}
| :ignore
@@ -521,10 +530,10 @@ defmodule Supervisor do
# Note we have inlined all types for readability
@typedoc "The supervisor specification"
@type child_spec :: %{
required(:id) => atom() | term(),
required(:id) => term(),
required(:start) => {module(), atom(), [term()]},
optional(:restart) => :permanent | :transient | :temporary,
optional(:shutdown) => timeout() | :brutal_kill,
optional(:shutdown) => :brutal_kill | non_neg_integer() | :infinity,
optional(:type) => :worker | :supervisor,
optional(:modules) => [module()] | :dynamic
}
@@ -532,10 +541,10 @@ defmodule Supervisor do
@doc """
Starts a supervisor with the given children.
The children is a list of modules, two-element tuples with module and
The children is a list of modules, 2-element tuples with module and
arguments or a map with the child specification. A strategy is required
to be provided through the `:strategy` option. See
"start_link/2, init/2, and strategies" for examples and other options.
"start_link/2, init/2 and strategies" for examples and other options.
The options can also be used to register a supervisor name.
The supported values are described under the "Name registration"
@@ -565,11 +574,11 @@ defmodule Supervisor do
end
@doc """
Receives a list of `children` to initialize and a set of `options`.
Receives a list of children to initialize and a set of options.
This is typically invoked at the end of the `c:init/1` callback of
module-based supervisors. See the sections "Module-based supervisors"
and "start_link/2, init/2, and strategies" in the module
and "start_link/2, init/2 and strategies" in the module
documentation for more information.
This function returns a tuple containing the supervisor
@@ -577,32 +586,29 @@ defmodule Supervisor do
## Examples
def init(_init_arg) do
children = [
def init(_arg) do
Supervisor.init([
{Stack, [:hello]}
]
Supervisor.init(children, strategy: :one_for_one)
], strategy: :one_for_one)
end
## Options
* `:strategy` - the supervision strategy option. It can be either
* `:strategy` - the restart strategy option. It can be either
`:one_for_one`, `:rest_for_one`, `:one_for_all`, or the deprecated
`:simple_one_for_one`.
* `:max_restarts` - the maximum number of restarts allowed in
a time frame. Defaults to `3`.
* `:max_seconds` - the time frame in seconds in which `:max_restarts`
applies. Defaults to `5`.
* `:max_seconds` - the time frame in which `:max_restarts` applies.
Defaults to `5`.
The `:strategy` option is required and by default a maximum of 3 restarts
is allowed within 5 seconds. Check the `Supervisor` module for a detailed
description of the available strategies.
"""
@doc since: "1.5.0"
# TODO: Warn if simple_one_for_one strategy is used on Elixir v1.9
# TODO: Warn if simple_one_for_one strategy is used on Elixir v1.8.
@spec init([:supervisor.child_spec() | {module, term} | module], [init_option]) :: {:ok, tuple}
def init(children, options) when is_list(children) and is_list(options) do
unless strategy = options[:strategy] do
@@ -624,7 +630,7 @@ defmodule Supervisor do
module.child_spec(arg)
rescue
e in UndefinedFunctionError ->
case __STACKTRACE__ do
case System.stacktrace() do
[{^module, :child_spec, [^arg], _} | _] ->
raise ArgumentError, child_spec_error(module)
@@ -644,7 +650,7 @@ defmodule Supervisor do
defp init_child(other) do
raise ArgumentError, """
supervisors expect each child to be one of the following:
supervisors expect each child to be one of:
* a module
* a {module, arg} tuple
@@ -702,8 +708,8 @@ defmodule Supervisor do
If a module is given, the specification is retrieved by calling
`module.child_spec(arg)`.
After the child specification is retrieved, the fields on `overrides`
are directly applied on the child spec. If `overrides` has keys that
After the child specification is retrieved, the fields on `config`
are directly applied on the child spec. If `config` has keys that
do not map to any child specification field, an error is raised.
See the "Child specification" section in the module documentation
@@ -740,10 +746,10 @@ defmodule Supervisor do
end
@doc """
Starts a module-based supervisor process with the given `module` and `init_arg`.
Starts a module-based supervisor process with the given `module` and `arg`.
To start the supervisor, the `c:init/1` callback will be invoked in the given
`module`, with `init_arg` as its argument. The `c:init/1` callback must return a
`module`, with `arg` as its argument. The `c:init/1` callback must return a
supervisor specification which can be created with the help of the `init/2`
function.
@@ -762,23 +768,23 @@ defmodule Supervisor do
# all to start_link(children, options).
@spec start_link(module, term) :: on_start
@spec start_link(module, term, GenServer.options()) :: on_start
def start_link(module, init_arg, options \\ []) when is_list(options) do
def start_link(module, arg, options \\ []) when is_list(options) do
case Keyword.get(options, :name) do
nil ->
:supervisor.start_link(module, init_arg)
:supervisor.start_link(module, arg)
atom when is_atom(atom) ->
:supervisor.start_link({:local, atom}, module, init_arg)
:supervisor.start_link({:local, atom}, module, arg)
{:global, _term} = tuple ->
:supervisor.start_link(tuple, module, init_arg)
:supervisor.start_link(tuple, module, arg)
{:via, via_module, _term} = tuple when is_atom(via_module) ->
:supervisor.start_link(tuple, module, init_arg)
:supervisor.start_link(tuple, module, arg)
other ->
raise ArgumentError, """
expected :name option to be one of the following:
expected :name option to be one of:
* nil
* atom
@@ -820,7 +826,7 @@ defmodule Supervisor do
call(supervisor, {:start_child, child_spec})
end
# TODO: Deprecate this on Elixir v1.9. Remove and update typespec on v2.0.
# TODO: Deprecate this on Elixir v1.8. Remove and update typespec on v2.0.
def start_child(supervisor, args) when is_list(args) do
call(supervisor, {:start_child, args})
end
@@ -845,9 +851,9 @@ defmodule Supervisor do
"""
@spec terminate_child(supervisor, term()) :: :ok | {:error, error}
when error: :not_found | :simple_one_for_one
# TODO: Deprecate this on Elixir v1.8
def terminate_child(supervisor, child_id)
# TODO: Deprecate this clause on Elixir v1.9
def terminate_child(supervisor, pid) when is_pid(pid) do
call(supervisor, {:terminate_child, pid})
end
+24 -31
View File
@@ -1,6 +1,6 @@
defmodule Supervisor.Spec do
@moduledoc """
Outdated functions for building child specifications.
WARNING: this module is deprecated.
The functions in this module are deprecated and they do not work
with the module-based child specs introduced in Elixir v1.5.
@@ -86,7 +86,7 @@ defmodule Supervisor.Spec do
In this case the supervisor will only be restarted if its child specification was defined with
the `:restart` option is set to `:permanent` (the default).
### Shutdown values (`:shutdown`)
### Shutdown values (:shutdown)
The following shutdown values are supported in the `:shutdown` option:
@@ -94,22 +94,19 @@ defmodule Supervisor.Spec do
using `Process.exit(child, :kill)`
* `:infinity` - if the child process is a supervisor, this is a mechanism
to give the subtree enough time to shut down; it can also be used with
to give the subtree enough time to shutdown; it can also be used with
workers with care
* a non-negative integer - the amount of time in milliseconds
* any integer - the value of `:shutdown` can also be any integer meaning
that the supervisor tells the child process to terminate by calling
`Process.exit(child, :shutdown)` and then waits for an exit signal back.
If no exit signal is received within the specified time,
the child process is unconditionally terminated
If no exit signal is received within the specified time (the value of this
option, in milliseconds), the child process is unconditionally terminated
using `Process.exit(child, :kill)`
"""
@moduledoc deprecated:
"Use the new child specifications outlined in the Supervisor module instead"
# TODO: Deprecate all functions in this module on Elixir v1.9.
# TODO: Deprecate all functions in this module on Elixir v1.8.
# Also deprecate entry in Supervisor.Default.
@typedoc "Supported strategies"
@@ -135,8 +132,8 @@ defmodule Supervisor.Spec do
{child_id, start_fun :: {module, atom, [term]}, restart, shutdown, worker, modules}
@doc """
Receives a list of `children` (workers or supervisors) to
supervise and a set of `options`.
Receives a list of children (workers or supervisors) to
supervise and a set of options.
Returns a tuple containing the supervisor specification. This tuple can be
used as the return value of the `c:init/1` callback when implementing a
@@ -216,16 +213,14 @@ defmodule Supervisor.Spec do
By default, the function `start_link` is invoked on the given
module. Overall, the default values for the options are:
[
id: module,
function: :start_link,
restart: :permanent,
shutdown: 5000,
modules: [module]
]
[id: module,
function: :start_link,
restart: :permanent,
shutdown: 5000,
modules: [module]]
See the "Supervisor and worker options" section in the `Supervisor.Spec` module for more
information on the available options.
Check the documentation for the `Supervisor.Spec` module for more
information on the options.
"""
@spec worker(
module,
@@ -244,21 +239,19 @@ defmodule Supervisor.Spec do
Defines the given `module` as a supervisor which will be started
with the given arguments.
supervisor(module, [], restart: :permanent)
supervisor(ExUnit.Runner, [], restart: :permanent)
By default, the function `start_link` is invoked on the given
module. Overall, the default values for the options are:
[
id: module,
function: :start_link,
restart: :permanent,
shutdown: :infinity,
modules: [module]
]
[id: module,
function: :start_link,
restart: :permanent,
shutdown: :infinity,
modules: [module]]
See the "Supervisor and worker options" section in the `Supervisor.Spec` module for more
information on the available options.
Check the documentation for the `Supervisor.Spec` module for more
information on the options.
"""
@spec supervisor(
module,
+64 -88
View File
@@ -73,13 +73,22 @@ defmodule System do
parts_per_second` seconds. For example, using the `:millisecond` time unit
is equivalent to using `1000` as the time unit (as the time will be returned
in 1/1000 seconds - milliseconds).
Keep in mind the Erlang API prior to version 19.1 will use `:milli_seconds`,
`:micro_seconds` and `:nano_seconds` as time units although Elixir normalizes
their spelling to match the SI convention.
"""
# TODO: Warn all old mappings once Elixir requires Erlang/OTP 19.1+
@type time_unit ::
:second
| :millisecond
| :microsecond
| :nanosecond
| pos_integer
| :seconds
| :milliseconds
| :microseconds
| :nanoseconds
@base_dir :filename.join(__DIR__, "../../..")
@version_file :filename.join(@base_dir, "VERSION")
@@ -101,7 +110,7 @@ defmodule System do
# Read and strip the version from the VERSION file.
defmacrop get_version do
case read_stripped(@version_file) do
"" -> raise "could not read the version number from VERSION"
"" -> raise RuntimeError, message: "could not read the version number from VERSION"
data -> data
end
end
@@ -130,17 +139,12 @@ defmodule System do
# Get the date at compilation time.
defmacrop get_date do
{{year, month, day}, {hour, minute, second}} = :calendar.universal_time()
"~4..0b-~2..0b-~2..0bT~2..0b:~2..0b:~2..0bZ"
|> :io_lib.format([year, month, day, hour, minute, second])
|> :erlang.iolist_to_binary()
IO.iodata_to_binary(:httpd_util.rfc1123_date())
end
@doc """
Returns the endianness.
"""
@spec endianness() :: :little | :big
def endianness do
:erlang.system_info(:endian)
end
@@ -149,7 +153,6 @@ defmodule System do
Returns the endianness the system was compiled with.
"""
@endianness :erlang.system_info(:endian)
@spec compiled_endianness() :: :little | :big
def compiled_endianness do
@endianness
end
@@ -183,7 +186,7 @@ defmodule System do
{:ok, v} = Version.parse(version())
revision_string = if v.pre != [] and revision() != "", do: " (#{revision()})", else: ""
otp_version_string = " (compiled with Erlang/OTP #{get_otp_release()})"
otp_version_string = " (compiled with OTP #{get_otp_release()})"
version() <> revision_string <> otp_version_string
end
@@ -215,32 +218,32 @@ defmodule System do
Returns the current working directory or `nil` if one
is not available.
"""
# TODO: Remove by 2.0
@deprecated "Use File.cwd/0 instead"
@spec cwd() :: String.t() | nil
def cwd do
case File.cwd() do
{:ok, cwd} -> cwd
case :file.get_cwd() do
{:ok, base} -> IO.chardata_to_string(fix_drive_letter(base))
_ -> nil
end
end
defp fix_drive_letter([l, ?:, ?/ | rest] = original) when l in ?A..?Z do
case :os.type() do
{:win32, _} -> [l + ?a - ?A, ?:, ?/ | rest]
_ -> original
end
end
defp fix_drive_letter(original), do: original
@doc """
Current working directory, exception on error.
Returns the current working directory or raises `RuntimeError`.
"""
# TODO: Remove by 2.0
@deprecated "Use File.cwd!/0 instead"
@spec cwd!() :: String.t()
def cwd! do
case File.cwd() do
{:ok, cwd} ->
cwd
_ ->
raise "could not get a current working directory, the current location is not accessible"
end
cwd() ||
raise RuntimeError,
message:
"could not get a current working directory, the current location is not accessible"
end
@doc """
@@ -248,7 +251,6 @@ defmodule System do
Returns the user home directory (platform independent).
"""
@spec user_home() :: String.t() | nil
def user_home do
:elixir_config.get(:home)
end
@@ -259,9 +261,10 @@ defmodule System do
Same as `user_home/0` but raises `RuntimeError`
instead of returning `nil` if no user home is set.
"""
@spec user_home!() :: String.t()
def user_home! do
user_home() || raise "could not find the user home, please set the HOME environment variable"
user_home() ||
raise RuntimeError,
message: "could not find the user home, please set the HOME environment variable"
end
@doc ~S"""
@@ -278,17 +281,9 @@ defmodule System do
Returns `nil` if none of the above are writable.
"""
@spec tmp_dir() :: String.t() | nil
def tmp_dir do
write_env_tmp_dir('TMPDIR') || write_env_tmp_dir('TEMP') || write_env_tmp_dir('TMP') ||
write_tmp_dir('/tmp') || write_cwd_tmp_dir()
end
defp write_cwd_tmp_dir do
case File.cwd() do
{:ok, cwd} -> write_tmp_dir(cwd)
_ -> nil
end
write_tmp_dir('/tmp') || ((cwd = cwd()) && write_tmp_dir(cwd))
end
@doc """
@@ -297,10 +292,12 @@ defmodule System do
Same as `tmp_dir/0` but raises `RuntimeError`
instead of returning `nil` if no temp dir is set.
"""
@spec tmp_dir!() :: String.t()
def tmp_dir! do
tmp_dir() ||
raise "could not get a writable temporary directory, please set the TMPDIR environment variable"
raise RuntimeError,
message:
"could not get a writable temporary directory, " <>
"please set the TMPDIR environment variable"
end
defp write_env_tmp_dir(env) do
@@ -339,10 +336,8 @@ defmodule System do
The function must receive the exit status code as an argument.
"""
@spec at_exit((non_neg_integer -> any)) :: :ok
def at_exit(fun) when is_function(fun, 1) do
:elixir_config.update(:at_exit, &[fun | &1])
:ok
end
@doc """
@@ -446,22 +441,16 @@ defmodule System do
end
@doc """
Deprecated mechanism to retrieve the last exception stacktrace.
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.
Last exception stacktrace.
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.
latest exception.
Inlined by the compiler into `:erlang.get_stacktrace/0`.
"""
# TODO: Fully deprecate it on Elixir v1.9.
# It is currently partially deprecated in elixir_dispatch.erl
def stacktrace do
apply(:erlang, :get_stacktrace, [])
:erlang.get_stacktrace()
end
@doc """
@@ -525,7 +514,6 @@ defmodule System do
System.stop(1)
"""
@doc since: "1.5.0"
@spec stop(non_neg_integer | binary) :: no_return
def stop(status \\ 0)
@@ -563,13 +551,13 @@ defmodule System do
## Examples
iex> System.cmd("echo", ["hello"])
iex> System.cmd "echo", ["hello"]
{"hello\n", 0}
iex> System.cmd("echo", ["hello"], env: [{"MIX_ENV", "test"}])
iex> System.cmd "echo", ["hello"], env: [{"MIX_ENV", "test"}]
{"hello\n", 0}
iex> System.cmd("echo", ["hello"], into: IO.stream(:stdio, :line))
iex> System.cmd "echo", ["hello"], into: IO.stream(:stdio, :line)
hello
{%IO.Stream{}, 0}
@@ -641,8 +629,9 @@ defmodule System do
do_cmd(Port.open({:spawn_executable, cmd}, opts), initial, fun)
catch
kind, reason ->
stacktrace = System.stacktrace()
fun.(initial, :halt)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
{acc, status} -> {fun.(acc, :done), status}
end
@@ -701,7 +690,7 @@ defmodule System do
This time is monotonically increasing and starts in an unspecified
point in time.
Inlined by the compiler.
Inlined by the compiler into `:erlang.monotonic_time/0`.
"""
@spec monotonic_time() :: integer
def monotonic_time do
@@ -726,7 +715,7 @@ defmodule System do
case of time warps although the VM works towards aligning
them. This time is not monotonic.
Inlined by the compiler.
Inlined by the compiler into `:erlang.system_time/0`.
"""
@spec system_time() :: integer
def system_time do
@@ -771,7 +760,7 @@ defmodule System do
See `time_offset/1` for more information.
Inlined by the compiler.
Inlined by the compiler into `:erlang.time_offset/0`.
"""
@spec time_offset() :: integer
def time_offset do
@@ -800,7 +789,7 @@ defmodule System do
This time may be adjusted forwards or backwards in time
with no limitation and is not monotonic.
Inlined by the compiler.
Inlined by the compiler into `:os.system_time/0`.
"""
@spec os_time() :: integer
def os_time do
@@ -819,7 +808,7 @@ defmodule System do
end
@doc """
Returns the Erlang/OTP release number.
Returns the OTP release number.
"""
@spec otp_release :: String.t()
def otp_release do
@@ -863,7 +852,7 @@ defmodule System do
All modifiers listed above can be combined; repeated modifiers in `modifiers`
will be ignored.
Inlined by the compiler.
Inlined by the compiler into `:erlang.unique_integer/1`.
"""
@spec unique_integer([:positive | :monotonic]) :: integer
def unique_integer(modifiers \\ []) do
@@ -883,39 +872,26 @@ defmodule System do
defp normalize_time_unit(:native), do: :native
defp normalize_time_unit(:second), do: :second
defp normalize_time_unit(:millisecond), do: :millisecond
defp normalize_time_unit(:microsecond), do: :microsecond
defp normalize_time_unit(:nanosecond), do: :nanosecond
defp normalize_time_unit(:second), do: :seconds
defp normalize_time_unit(:millisecond), do: :milli_seconds
defp normalize_time_unit(:microsecond), do: :micro_seconds
defp normalize_time_unit(:nanosecond), do: :nano_seconds
defp normalize_time_unit(:seconds), do: warn(:seconds, :second)
defp normalize_time_unit(:milliseconds), do: warn(:milliseconds, :millisecond)
defp normalize_time_unit(:microseconds), do: warn(:microseconds, :microsecond)
defp normalize_time_unit(:nanoseconds), do: warn(:nanoseconds, :nanosecond)
defp normalize_time_unit(:milli_seconds), do: warn(:milli_seconds, :millisecond)
defp normalize_time_unit(:micro_seconds), do: warn(:micro_seconds, :microsecond)
defp normalize_time_unit(:nano_seconds), do: warn(:nano_seconds, :nanosecond)
defp normalize_time_unit(:seconds), do: :seconds
defp normalize_time_unit(:milliseconds), do: :milli_seconds
defp normalize_time_unit(:microseconds), do: :micro_seconds
defp normalize_time_unit(:nanoseconds), do: :nano_seconds
defp normalize_time_unit(unit) when is_integer(unit) and unit > 0, do: unit
defp normalize_time_unit(erlang_unit)
when erlang_unit in [:milli_seconds, :micro_seconds, :nano_seconds] do
erlang_unit
end
defp normalize_time_unit(other) do
raise ArgumentError,
"unsupported time unit. Expected :second, :millisecond, " <>
":microsecond, :nanosecond, or a positive integer, " <> "got #{inspect(other)}"
end
defp warn(unit, replacement_unit) do
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
stacktrace = Enum.drop(stacktrace, 3)
:elixir_config.warn({System, unit}, stacktrace) &&
IO.warn(
"deprecated time unit: #{inspect(unit)}. A time unit should be " <>
":second, :millisecond, :microsecond, :nanosecond, or a positive integer",
stacktrace
)
replacement_unit
end
end
+101 -186
View File
@@ -9,7 +9,7 @@ defmodule Task do
by computing a value asynchronously:
task = Task.async(fn -> do_some_work() end)
res = do_some_other_work()
res = do_some_other_work()
res + Task.await(task)
Tasks spawned with `async` can be awaited on by their caller
@@ -56,18 +56,8 @@ defmodule Task do
## Supervised tasks
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)
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:
It is also possible to spawn a task under a supervisor.
It is often done by defining the task in its own module:
defmodule MyTask do
use Task
@@ -83,23 +73,21 @@ defmodule Task do
And then passing it to the supervisor:
Supervisor.start_link([
{MyTask, arg}
], strategy: :one_for_one)
Supervisor.start_link([MyTask])
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 expecte
by supervisors).
the caller, they cannot be awaited on. Note `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
Note `use Task` defines a `child_spec/1` function, allowing the
defined module to be put under a supervision tree. The generated
`child_spec/1` can be customized with the following options:
* `:id` - the child specification identifier, defaults to the current module
* `:id` - the child specification id, defaults to the current module
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
* `:restart` - when the child should be restarted, defaults to `:temporary`
* `:shutdown` - how to shut down the child, either immediately or by giving it time to shut down
* `:shutdown` - how to shut down the child
Opposite to `GenServer`, `Agent` and `Supervisor`, a Task has
a default `:restart` of `:temporary`. This means the task will
@@ -112,10 +100,7 @@ defmodule Task do
use Task, restart: :permanent
See the "Child specification" section in the `Supervisor` module
for more detailed information. The `@doc` annotation immediately
preceding `use Task` will be attached to the generated `child_spec/1`
function.
See the `Supervisor` docs for more information.
## Dynamically supervised tasks
@@ -125,12 +110,9 @@ defmodule Task do
A short example is:
{:ok, pid} = Task.Supervisor.start_link()
task =
Task.Supervisor.async(pid, fn ->
# Do something
end)
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
@@ -138,7 +120,7 @@ defmodule Task do
Supervisor.start_link([
{Task.Supervisor, name: MyApp.TaskSupervisor}
], strategy: :one_for_one)
])
Now you can dynamically start supervised tasks:
@@ -150,22 +132,21 @@ defmodule Task do
Task.Supervisor.async(MyApp.TaskSupervisor, fn ->
# Do something
end)
|> Task.await()
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
Since Elixir provides a Task supervisor, it is easy to use one
to dynamically spawn 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])
Task.Supervisor.async({MyApp.DistSupervisor, :remote@local},
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
@@ -188,34 +169,16 @@ defmodule Task do
* `:owner` - the PID of the process that started the task
"""
@enforce_keys [:pid, :ref, :owner]
defstruct pid: nil, ref: nil, owner: nil
@typedoc """
The Task type.
See `%Task{}` for information about each field of the structure.
"""
@type t :: %__MODULE__{
pid: pid() | nil,
ref: reference() | nil,
owner: pid() | nil
}
defguardp is_timeout(timeout)
when timeout == :infinity or (is_integer(timeout) and timeout >= 0)
@type t :: %__MODULE__{}
@doc """
Returns a specification to start a task under a supervisor.
`arg` is passed as the argument to `Task.start_link/1` in the `:start` field
of the spec.
For more information, see the `Supervisor` module,
the `Supervisor.child_spec/2` function and the `t:Supervisor.child_spec/0` type.
See `Supervisor`.
"""
@doc since: "1.5.0"
@spec child_spec(term) :: Supervisor.child_spec()
@since "1.5.0"
def child_spec(arg) do
%{
id: Task,
@@ -227,18 +190,12 @@ defmodule Task do
@doc false
defmacro __using__(opts) do
quote location: :keep, bind_quoted: [opts: opts] do
if Module.get_attribute(__MODULE__, :doc) == nil do
@doc """
Returns a specification to start this module under a supervisor.
`arg` is passed as the argument to `Task.start_link/1` in the `:start` field
of the spec.
For more information, see the `Supervisor` module,
the `Supervisor.child_spec/2` function and the `t:Supervisor.child_spec/0` type.
"""
end
@doc """
Returns a specification to start this module under a supervisor.
See `Supervisor`.
"""
@since "1.5.0"
def child_spec(arg) do
default = %{
id: __MODULE__,
@@ -256,12 +213,10 @@ defmodule Task do
@doc """
Starts a process linked to the current process.
`fun` must be a zero-arity anonymous function.
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
def start_link(fun) do
start_link(:erlang, :apply, [fun, []])
end
@@ -269,23 +224,19 @@ defmodule Task do
Starts a task as part of a supervision tree.
"""
@spec start_link(module, atom, [term]) :: {:ok, pid}
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)
def start_link(mod, fun, args) do
Task.Supervised.start_link(get_info(self()), {mod, fun, args})
end
@doc """
Starts a task.
`fun` must be a zero-arity anonymous function.
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
def start(fun) do
start(:erlang, :apply, [fun, []])
end
@@ -297,27 +248,24 @@ defmodule Task do
be linked to the current process.
"""
@spec start(module, atom, [term]) :: {:ok, pid}
def start(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(get_owner(self()), get_callers(self()), mfa)
def start(mod, fun, args) do
Task.Supervised.start(get_info(self()), {mod, fun, args})
end
@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.
Read the `Task` module documentation for more information about the
general usage of `async/1` and `async/3`.
Read the `Task` module documentation for more info on 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
def async(fun) do
async(:erlang, :apply, [fun, []])
end
@@ -328,8 +276,8 @@ defmodule Task do
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/1` and `async/3`.
Read the `Task` module documentation for more info on general
usage of `async/1` and `async/3`.
## Linking
@@ -385,53 +333,50 @@ defmodule Task do
and `result` is the return value of the task function.
"""
@spec async(module, atom, [term]) :: t
def async(module, function_name, args)
when is_atom(module) and is_atom(function_name) and is_list(args) do
mfa = {module, function_name, args}
def async(mod, fun, args) do
mfa = {mod, fun, args}
owner = self()
{:ok, pid} = Task.Supervised.start_link(get_owner(owner), get_callers(owner), :nomonitor, mfa)
pid = Task.Supervised.spawn_link(owner, get_info(owner), mfa)
ref = Process.monitor(pid)
send(pid, {owner, ref})
%Task{pid: pid, ref: ref, owner: owner}
end
@doc """
Returns a stream where the given function (`module` and `function_name`)
is mapped concurrently on each item in `enumerable`.
Returns a stream that runs the given `module`, `function`, and `args`
concurrently on each item in `enumerable`.
Each item of `enumerable` will be prepended to the given `args` and
processed by its own task. The tasks will be linked to an intermediate
process that is then linked to the current process. This means a failure
in a task terminates the current process and a failure in the current process
Each item will be prepended to the given `args` and processed by its
own task. The tasks will be linked to an intermediate process that is
then linked to the current process. This means a failure in a task
terminates the current process and a failure in the current process
terminates all tasks.
When streamed, each task will emit `{:ok, value}` upon successful
completion or `{:exit, reason}` if the caller is trapping exits.
The order of results depends on the value of the `:ordered` option.
Results are emitted in the same order as the original `enumerable`.
The level of concurrency and the time tasks are allowed to run can
be controlled via options (see the "Options" section below).
The level of concurrency can be controlled via the `:max_concurrency`
option and defaults to `System.schedulers_online/0`. A timeout
can also be given as an option representing the maximum amount of
time to wait without a task reply.
Consider using `Task.Supervisor.async_stream/6` to start tasks
Finally, consider using `Task.Supervisor.async_stream/6` to start tasks
under a supervisor. If you find yourself trapping exits to handle exits
inside the async stream, consider using `Task.Supervisor.async_stream_nolink/6`
to start tasks that are not linked to the calling process.
to start tasks that are not linked to the current process.
## Options
* `:max_concurrency` - sets the maximum number of tasks to run
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. 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) each
task is allowed to execute for. Defaults to `5000`.
* `:on_timeout` - what to do when a task times out. The possible
* `:on_timeout` - what do to when a task times out. The possible
values are:
* `:exit` (default) - the process that spawned the tasks exits.
* `:kill_task` - the task that timed out is killed. The value
@@ -447,32 +392,21 @@ defmodule Task do
The concurrency can be increased or decreased using the `:max_concurrency`
option. For example, if the tasks are IO heavy, the value can be increased:
max_concurrency = System.schedulers_online() * 2
max_concurrency = System.schedulers_online * 2
stream = Task.async_stream(collection, Mod, :expensive_fun, [], max_concurrency: max_concurrency)
Enum.to_list(stream)
If you do not care about the results of the computation, you can run
the stream with `Stream.run/1`. Also set `ordered: false`, as you don't
care about the order of the results either:
stream = Task.async_stream(collection, Mod, :expensive_fun, [], ordered: false)
Stream.run(stream)
"""
@doc since: "1.4.0"
@spec async_stream(Enumerable.t(), module, atom, [term], keyword) :: Enumerable.t()
def async_stream(enumerable, module, function_name, args, options \\ [])
when is_atom(module) and is_atom(function_name) and is_list(args) do
build_stream(enumerable, {module, function_name, args}, options)
def async_stream(enumerable, module, function, args, options \\ [])
when is_atom(module) and is_atom(function) and is_list(args) do
build_stream(enumerable, {module, function, args}, options)
end
@doc """
Returns a stream that runs the given function `fun` concurrently
on each item in `enumerable`.
Works the same as `async_stream/5` but with an anonymous function instead of a
module-function-arguments tuple. `fun` must be a one-arity anonymous function.
Each `enumerable` item is passed as argument to the given function `fun` and
processed by its own task. The tasks will be linked to the current process,
similarly to `async/1`.
@@ -482,44 +416,34 @@ defmodule Task do
Count the codepoints in each string asynchronously, then add the counts together using reduce.
iex> strings = ["long string", "longer string", "there are many of these"]
iex> stream = Task.async_stream(strings, fn text -> text |> String.codepoints() |> Enum.count() end)
iex> stream = Task.async_stream(strings, fn text -> text |> String.codepoints |> Enum.count end)
iex> Enum.reduce(stream, 0, fn {:ok, num}, acc -> num + acc end)
47
See `async_stream/5` for discussion, options, and more examples.
"""
@doc since: "1.4.0"
@spec async_stream(Enumerable.t(), (term -> term), keyword) :: Enumerable.t()
def async_stream(enumerable, fun, options \\ [])
when is_function(fun, 1) and is_list(options) do
def async_stream(enumerable, fun, options \\ []) when is_function(fun, 1) do
build_stream(enumerable, fun, options)
end
defp build_stream(enumerable, fun, options) do
&Task.Supervised.stream(enumerable, &1, &2, fun, options, fn [owner | _] = callers, mfa ->
{:ok, pid} = Task.Supervised.start_link(get_owner(owner), callers, :nomonitor, mfa)
{:ok, :link, pid}
&Task.Supervised.stream(enumerable, &1, &2, fun, options, fn owner, mfa ->
{:link, Task.Supervised.spawn_link(owner, get_info(owner), mfa)}
end)
end
# Returns a tuple with the node where this is executed and either the
# registered name of the given PID or the PID of where this is executed. Used
# registered name of the given pid or the pid of where this is executed. Used
# when exiting from tasks to print out from where the task was started.
defp get_owner(pid) do
defp get_info(pid) do
self_or_name =
case Process.info(pid, :registered_name) do
{:registered_name, name} when is_atom(name) -> name
_ -> pid
{:registered_name, []} -> self()
{:registered_name, name} -> name
end
{node(), self_or_name, pid}
end
defp get_callers(owner) do
case :erlang.get(:"$callers") do
[_ | _] = list -> [owner | list]
_ -> [owner]
end
{node(), self_or_name}
end
@doc """
@@ -528,7 +452,7 @@ defmodule Task do
In case the task process dies, the current process will exit with the same
reason as the task.
A timeout in milliseconds or `:infinity`, can be given with a default value of `5000`. If the
A timeout, in milliseconds, can be given with default value of `5000`. If the
timeout is exceeded, then the current process will exit. If the task process
is linked to the current process which is the case when a task is started with
`async`, then the task process will also exit. If the task process is trapping
@@ -547,9 +471,7 @@ defmodule Task do
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`.
coming from a task inside your `GenServer.handle_info/2` callback.
## Examples
@@ -558,12 +480,14 @@ defmodule Task do
2
"""
@spec await(t, timeout) :: term
def await(%Task{ref: ref, owner: owner} = task, timeout \\ 5000) when is_timeout(timeout) do
if owner != self() do
raise ArgumentError, invalid_owner_error(task)
end
@spec await(t, timeout) :: term | no_return
def await(task, timeout \\ 5000)
def await(%Task{owner: owner} = task, _) when owner != self() do
raise ArgumentError, invalid_owner_error(task)
end
def await(%Task{ref: ref} = task, timeout) do
receive do
{^ref, reply} ->
Process.demonitor(ref, [:flush])
@@ -580,7 +504,7 @@ defmodule Task do
@doc false
# TODO: Remove on 2.0
@deprecated "Pattern match directly on the message instead"
# (hard-deprecated in elixir_dispatch)
def find(tasks, {ref, reply}) when is_reference(ref) do
Enum.find_value(tasks, fn
%Task{ref: ^ref} = task ->
@@ -617,7 +541,7 @@ defmodule Task do
* it isn't linked to the caller
* the caller is trapping exits
A timeout, in milliseconds or `:infinity`, can be given with a default value
A timeout, in milliseconds, can be given with default value
of `5000`. If the time runs out before a message from
the task is received, this function will return `nil`
and the monitor will remain active. Therefore `yield/2` can be
@@ -634,9 +558,8 @@ defmodule Task do
case Task.yield(task, timeout) || Task.shutdown(task) do
{:ok, result} ->
result
nil ->
Logger.warn("Failed to get a result in #{timeout}ms")
Logger.warn "Failed to get a result in #{timeout}ms"
nil
end
@@ -645,11 +568,13 @@ defmodule Task do
handle this case and return the result.
"""
@spec yield(t, timeout) :: {:ok, term} | {:exit, term} | nil
def yield(%Task{ref: ref, owner: owner} = task, timeout \\ 5000) when is_timeout(timeout) do
if owner != self() do
raise ArgumentError, invalid_owner_error(task)
end
def yield(task, timeout \\ 5000)
def yield(%Task{owner: owner} = task, _) when owner != self() do
raise ArgumentError, invalid_owner_error(task)
end
def yield(%Task{ref: ref} = task, timeout) do
receive do
{^ref, reply} ->
Process.demonitor(ref, [:flush])
@@ -671,10 +596,8 @@ defmodule Task do
This function receives a list of tasks and waits for their
replies in the given time interval. It returns a list
of two-element tuples, with the task as the first element
and the yielded result as the second. The tasks in the returned
list will be in the same order as the tasks supplied in the `tasks`
input argument.
of tuples of two elements, with the task as the first element
and the yielded result as the second.
Similarly to `yield/2`, each task's result will be
@@ -683,9 +606,6 @@ defmodule Task do
* `{:exit, reason}` if the task has died
* `nil` if the task keeps running past the timeout
A timeout, in milliseconds or `:infinity`, can be given with a default value
of `5000`.
Check `yield/2` for more information.
## Example
@@ -707,33 +627,28 @@ defmodule Task do
tasks_with_results = Task.yield_many(tasks, 5000)
results =
Enum.map(tasks_with_results, fn {task, res} ->
# Shut down the tasks that did not reply nor exit
res || Task.shutdown(task, :brutal_kill)
end)
results = Enum.map(tasks_with_results, fn {task, res} ->
# Shutdown the tasks that did not reply nor exit
res || Task.shutdown(task, :brutal_kill)
end)
# Here we are matching only on {:ok, value} and
# ignoring {:exit, _} (crashed tasks) and `nil` (no replies)
for {:ok, value} <- results do
IO.inspect(value)
IO.inspect value
end
In the example above, we create tasks that sleep from 1
up to 10 seconds and return the number of seconds they slept for.
up to 10 seconds and return the number of seconds they slept.
If you execute the code all at once, you should see 1 up to 5
printed, as those were the tasks that have replied in the
given time. All other tasks will have been shut down using
the `Task.shutdown/2` call.
"""
@spec yield_many([t], timeout) :: [{t, {:ok, term} | {:exit, term} | nil}]
def yield_many(tasks, timeout \\ 5000) when is_timeout(timeout) do
def yield_many(tasks, timeout \\ 5000) do
timeout_ref = make_ref()
timer_ref =
if timeout != :infinity do
Process.send_after(self(), timeout_ref, timeout)
end
timer_ref = Process.send_after(self(), timeout_ref, timeout)
try do
yield_many(tasks, timeout_ref, :infinity)
@@ -741,7 +656,7 @@ defmodule Task do
{:noconnection, reason} ->
exit({reason, {__MODULE__, :yield_many, [tasks, timeout]}})
after
timer_ref && Process.cancel_timer(timer_ref)
Process.cancel_timer(timer_ref)
receive do: (^timeout_ref -> :ok), after: (0 -> :ok)
end
end
@@ -781,7 +696,7 @@ defmodule Task do
`{:exit, reason}` if the task died, otherwise `nil`.
The second argument is either a timeout or `:brutal_kill`. In case
of a timeout, a `:shutdown` exit signal is sent to the task process
of a `timeout`, a `:shutdown` exit signal is sent to the task process
and if it does not exit within the timeout, it is killed. With `:brutal_kill`
the task is killed straight away. In case the task terminates abnormally
(possibly killed by another process), this function will exit with the same reason.
@@ -790,10 +705,10 @@ defmodule Task do
exiting with reason `:normal` or if the task is trapping exits. If the caller is
exiting with a reason other than `:normal` and the task is not trapping exits, the
caller's exit signal will stop the task. The caller can exit with reason
`:shutdown` to shut down all of its linked processes, including tasks, that
`:shutdown` to shutdown all of its linked processes, including tasks, that
are not trapping exits without generating any log messages.
If a task's monitor has already been demonitored or received and there is not
If a task's monitor has already been demonitored or received and there is not
a response waiting in the message queue this function will return
`{:exit, :noproc}` as the result or exit reason can not be determined.
"""
@@ -824,7 +739,7 @@ defmodule Task do
end
end
def shutdown(%Task{pid: pid} = task, timeout) when is_timeout(timeout) do
def shutdown(%Task{pid: pid} = task, timeout) do
mon = Process.monitor(pid)
exit(pid, :shutdown)
+98 -145
View File
@@ -2,37 +2,40 @@ defmodule Task.Supervised do
@moduledoc false
@ref_timeout 5000
def start(owner, callers, fun) do
{:ok, :proc_lib.spawn(__MODULE__, :noreply, [owner, callers, fun])}
def start(info, fun) do
{:ok, :proc_lib.spawn(__MODULE__, :noreply, [info, fun])}
end
def start_link(owner, callers, fun) do
{:ok, :proc_lib.spawn_link(__MODULE__, :noreply, [owner, callers, fun])}
def start_link(info, fun) do
{:ok, :proc_lib.spawn_link(__MODULE__, :noreply, [info, fun])}
end
def start_link(owner, callers, monitor, fun) do
{:ok, :proc_lib.spawn_link(__MODULE__, :reply, [owner, callers, monitor, fun])}
def start_link(caller, monitor, info, fun) do
{:ok, spawn_link(caller, monitor, info, fun)}
end
def reply({_, _, owner_pid} = owner, callers, monitor, mfa) do
def spawn_link(caller, monitor \\ :nomonitor, info, fun) do
:proc_lib.spawn_link(__MODULE__, :reply, [caller, monitor, info, fun])
end
def reply(caller, monitor, info, mfa) do
initial_call(mfa)
put_callers(callers)
case monitor do
:monitor ->
mref = Process.monitor(owner_pid)
reply(owner, owner_pid, mref, @ref_timeout, mfa)
mref = Process.monitor(caller)
reply(caller, mref, @ref_timeout, info, mfa)
:nomonitor ->
reply(owner, owner_pid, nil, :infinity, mfa)
reply(caller, nil, :infinity, info, mfa)
end
end
defp reply(owner, owner_pid, mref, timeout, mfa) do
defp reply(caller, mref, timeout, info, mfa) do
receive do
{^owner_pid, ref} ->
{^caller, ref} ->
_ = if mref, do: Process.demonitor(mref, [:flush])
send(owner_pid, {ref, invoke_mfa(owner, mfa)})
send(caller, {ref, do_apply(info, mfa)})
{:DOWN, ^mref, _, _, reason} when is_reference(mref) ->
exit({:shutdown, reason})
@@ -61,14 +64,9 @@ defmodule Task.Supervised do
end
end
def noreply(owner, callers, mfa) do
def noreply(info, mfa) do
initial_call(mfa)
put_callers(callers)
invoke_mfa(owner, mfa)
end
defp put_callers(callers) do
Process.put(:"$callers", callers)
do_apply(info, mfa)
end
defp initial_call(mfa) do
@@ -76,8 +74,8 @@ defmodule Task.Supervised do
end
defp get_initial_call({:erlang, :apply, [fun, []]}) when is_function(fun, 0) do
{:module, module} = Function.info(fun, :module)
{:name, name} = Function.info(fun, :name)
{:module, module} = :erlang.fun_info(fun, :module)
{:name, name} = :erlang.fun_info(fun, :name)
{module, name, 0}
end
@@ -85,26 +83,31 @@ defmodule Task.Supervised do
{mod, fun, length(args)}
end
defp invoke_mfa(owner, {module, fun, args} = mfa) do
defp do_apply(info, {module, fun, args} = mfa) do
try do
apply(module, fun, args)
catch
:exit, value
when value == :normal
when value == :shutdown
when tuple_size(value) == 2 and elem(value, 0) == :shutdown ->
:erlang.raise(:exit, value, __STACKTRACE__)
:error, value ->
reason = {value, System.stacktrace()}
exit(info, mfa, reason, reason)
kind, value ->
log(owner, mfa, {log_value(kind, value), __STACKTRACE__})
:erlang.raise(kind, value, __STACKTRACE__)
:throw, value ->
reason = {{:nocatch, value}, System.stacktrace()}
exit(info, mfa, reason, reason)
:exit, value ->
exit(info, mfa, {value, System.stacktrace()}, value)
end
end
defp log_value(:throw, value), do: {:nocatch, value}
defp log_value(_, value), do: value
defp exit(_info, _mfa, _log_reason, reason)
when reason == :normal
when reason == :shutdown
when tuple_size(reason) == 2 and elem(reason, 0) == :shutdown do
exit(reason)
end
defp log(owner, mfa, reason) do
defp exit(info, mfa, log_reason, reason) do
{fun, args} = get_running(mfa)
message =
@@ -113,15 +116,16 @@ defmodule Task.Supervised do
'** When function == ~p~n' ++
'** arguments == ~p~n' ++ '** Reason for termination == ~n' ++ '** ~p~n'
:error_logger.format(message, [self(), get_from(owner), fun, args, get_reason(reason)])
:error_logger.format(message, [self(), get_from(info), fun, args, get_reason(log_reason)])
exit(reason)
end
defp get_from({node, pid_or_name, _pid}) when node == node(), do: pid_or_name
defp get_from({node, name, _pid}) when is_atom(name), do: {node, name}
defp get_from({_node, _name, pid}), do: pid
defp get_from({node, pid_or_name}) when node == node(), do: pid_or_name
defp get_from(other), do: other
defp get_running({:erlang, :apply, [fun, []]}) when is_function(fun, 0), do: {fun, []}
defp get_running({mod, fun, args}), do: {Function.capture(mod, fun, length(args)), args}
defp get_running({mod, fun, args}), do: {:erlang.make_fun(mod, fun, length(args)), args}
defp get_reason({:undef, [{mod, fun, args, _info} | _] = stacktrace} = reason)
when is_atom(mod) and is_atom(fun) do
@@ -153,7 +157,6 @@ defmodule Task.Supervised do
timeout = Keyword.get(options, :timeout, 5000)
on_timeout = Keyword.get(options, :on_timeout, :exit)
parent = self()
callers = get_callers()
{:trap_exit, trap_exit?} = Process.info(self(), :trap_exit)
@@ -163,10 +166,7 @@ defmodule Task.Supervised do
spawn_opts = [:link, :monitor]
{monitor_pid, monitor_ref} =
Process.spawn(
fn -> stream_monitor(callers, mfa, spawn, trap_exit?, timeout) end,
spawn_opts
)
Process.spawn(fn -> stream_monitor(parent, mfa, spawn, trap_exit?, timeout) end, spawn_opts)
# Now that we have the pid of the "monitor" process and the reference of the
# monitor we use to monitor such process, we can inform the monitor process
@@ -193,13 +193,6 @@ defmodule Task.Supervised do
)
end
defp get_callers do
case :erlang.get(:"$callers") do
[_ | _] = list -> [self() | list]
_ -> [self()]
end
end
defp stream_reduce({:halt, acc}, _max, _spawned, _delivered, _waiting, next, config) do
%{monitor_pid: monitor_pid, monitor_ref: monitor_ref, timeout: timeout} = config
stream_close(monitor_pid, monitor_ref, timeout)
@@ -302,8 +295,9 @@ defmodule Task.Supervised do
next.({:cont, []})
catch
kind, reason ->
stacktrace = System.stacktrace()
stream_close(monitor_pid, monitor_ref, timeout)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
{:suspended, [value], next} ->
waiting = stream_spawn(value, spawned, waiting, monitor_pid, monitor_ref, timeout)
@@ -330,9 +324,10 @@ defmodule Task.Supervised do
reducer.(reply, acc)
catch
kind, reason ->
stacktrace = System.stacktrace()
is_function(next) && next.({:halt, []})
stream_close(monitor_pid, monitor_ref, timeout)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
end
end
@@ -359,9 +354,10 @@ defmodule Task.Supervised do
reducer.(reply, acc)
catch
kind, reason ->
stacktrace = System.stacktrace()
is_function(next) && next.({:halt, []})
stream_close(monitor_pid, monitor_ref, timeout)
:erlang.raise(kind, reason, __STACKTRACE__)
:erlang.raise(kind, reason, stacktrace)
else
pair ->
stream_deliver(
@@ -425,26 +421,15 @@ defmodule Task.Supervised do
send(pid, {self(), {monitor_ref, spawned}})
Map.put(waiting, spawned, {pid, :running})
{:max_children, ^monitor_ref} ->
stream_close(monitor_pid, monitor_ref, timeout)
raise """
reached the maximum number of tasks for this task supervisor. The maximum number \
of tasks that are allowed to run at the same time under this supervisor can be \
configured with the :max_children option passed to Task.Supervisor.start_link/1. When \
using async_stream or async_stream_nolink, make sure to configure :max_concurrency to \
be lower or equal to :max_children and pay attention to whether other tasks are also \
spawned under the same task supervisor.\
"""
{:DOWN, ^monitor_ref, _, ^monitor_pid, reason} ->
stream_cleanup_inbox(monitor_pid, monitor_ref)
exit({reason, {__MODULE__, :stream, [timeout]}})
end
end
defp stream_monitor([parent_pid | _] = callers, mfa, spawn, trap_exit?, timeout) do
defp stream_monitor(parent_pid, mfa, spawn, trap_exit?, timeout) do
Process.flag(:trap_exit, trap_exit?)
parent_ref = Process.monitor(parent_pid)
# Let's wait for the parent process to tell this process the monitor ref
@@ -453,7 +438,7 @@ defmodule Task.Supervised do
receive do
{^parent_pid, monitor_ref} ->
config = %{
callers: callers,
parent_pid: parent_pid,
parent_ref: parent_ref,
mfa: mfa,
spawn: spawn,
@@ -470,7 +455,8 @@ defmodule Task.Supervised do
defp stream_monitor_loop(running_tasks, config) do
%{
callers: [parent_pid | _] = callers,
parent_pid: parent_pid,
parent_ref: parent_ref,
mfa: mfa,
spawn: spawn,
monitor_ref: monitor_ref,
@@ -481,35 +467,55 @@ defmodule Task.Supervised do
# The parent process is telling us to spawn a new task to process
# "value". We spawn it and notify the parent about its pid.
{:spawn, position, value} ->
case spawn.(callers, normalize_mfa_with_arg(mfa, value)) do
{:ok, type, pid} ->
ref = Process.monitor(pid)
{type, pid} = spawn.(parent_pid, normalize_mfa_with_arg(mfa, value))
ref = Process.monitor(pid)
# Schedule a timeout message to ourselves, unless the timeout was set to :infinity
timer_ref =
case timeout do
:infinity -> nil
timeout -> Process.send_after(self(), {:timeout, {monitor_ref, ref}}, timeout)
end
# Schedule a timeout message to ourselves, unless the timeout was set to :infinity
timer_ref =
case timeout do
:infinity -> nil
timeout -> Process.send_after(self(), {:timeout, {monitor_ref, ref}}, timeout)
end
send(parent_pid, {:spawned, {monitor_ref, position}, pid})
send(parent_pid, {:spawned, {monitor_ref, position}, pid})
running_tasks =
Map.put(running_tasks, ref, %{
position: position,
type: type,
pid: pid,
timer_ref: timer_ref,
timed_out?: false
})
task_info = %{
position: position,
type: type,
pid: pid,
timer_ref: timer_ref,
timed_out?: false
}
stream_monitor_loop(running_tasks, config)
running_tasks = Map.put(running_tasks, ref, task_info)
stream_monitor_loop(running_tasks, config)
{:error, :max_children} ->
send(parent_pid, {:max_children, monitor_ref})
stream_waiting_for_stop_loop(running_tasks, config)
# The parent process is telling us to stop because the stream is being
# closed. In this case, we forcibly kill all spawned processes and then
# exit gracefully ourselves.
{:stop, ^monitor_ref} ->
Process.flag(:trap_exit, true)
for {ref, %{pid: pid}} <- running_tasks do
Process.exit(pid, :kill)
receive do
{:DOWN, ^ref, _, _, _} -> :ok
end
end
exit(:normal)
# The parent process went down with a given reason. We kill all the
# spawned processes (that are also linked) with the same reason, and then
# exit ourselves with the same reason.
{:DOWN, ^parent_ref, _, _, reason} ->
for {_ref, %{type: :link, pid: pid}} <- running_tasks do
Process.exit(pid, reason)
end
exit(reason)
# One of the spawned processes went down. We inform the parent process of
# this and keep going.
{:DOWN, ref, _, _, reason} ->
@@ -543,62 +549,9 @@ defmodule Task.Supervised do
{:EXIT, _, _} ->
stream_monitor_loop(running_tasks, config)
other ->
handle_stop_or_parent_down(other, running_tasks, config)
stream_monitor_loop(running_tasks, config)
end
end
defp stream_waiting_for_stop_loop(running_tasks, config) do
receive do
message ->
handle_stop_or_parent_down(message, running_tasks, config)
stream_waiting_for_stop_loop(running_tasks, config)
end
end
# The parent process is telling us to stop because the stream is being
# closed. In this case, we forcibly kill all spawned processes and then
# exit gracefully ourselves.
defp handle_stop_or_parent_down(
{:stop, monitor_ref},
running_tasks,
%{monitor_ref: monitor_ref}
) do
Process.flag(:trap_exit, true)
for {_ref, %{pid: pid}} <- running_tasks, do: Process.exit(pid, :kill)
for {ref, _task} <- running_tasks do
receive do
{:DOWN, ^ref, _, _, _} -> :ok
end
end
exit(:normal)
end
# The parent process went down with a given reason. We kill all the
# spawned processes (that are also linked) with the same reason, and then
# exit ourselves with the same reason.
defp handle_stop_or_parent_down(
{:DOWN, parent_ref, _, _, reason},
running_tasks,
%{parent_ref: parent_ref}
) do
for {_ref, %{type: :link, pid: pid}} <- running_tasks do
Process.exit(pid, reason)
end
exit(reason)
end
# We ignore all other messages.
defp handle_stop_or_parent_down(_other, _running_tasks, _config) do
:ok
end
defp unlink_and_kill(pid) do
caller = self()
ref = make_ref()
+43 -143
View File
@@ -12,7 +12,7 @@ defmodule Task.Supervisor do
{Task.Supervisor, name: MyApp.TaskSupervisor}
]
Supervisor.start_link(children, strategy: :one_for_one)
Supervisor.start_link(strategy: :one_for_one)
The options given in the child specification are documented in `start_link/1`.
@@ -31,17 +31,10 @@ defmodule Task.Supervisor do
| {:shutdown, :supervisor.shutdown()}
@doc false
def child_spec(opts) when is_list(opts) do
id =
case Keyword.get(opts, :name, Task.Supervisor) do
name when is_atom(name) -> name
{:global, name} -> name
{:via, _module, name} -> name
end
def child_spec(arg) do
%{
id: id,
start: {Task.Supervisor, :start_link, [opts]},
id: Task.Supervisor,
start: {Task.Supervisor, :start_link, [arg]},
type: :supervisor
}
end
@@ -78,7 +71,7 @@ defmodule Task.Supervisor do
give them directly to `start_child` and `async`.
"""
@spec start_link([option]) :: Supervisor.on_start()
# TODO: Deprecate passing restart and shutdown here on Elixir v1.9.
# TODO: Deprecate passing restart and shutdown here on Elixir v1.8.
def start_link(options \\ []) do
{restart, options} = Keyword.pop(options, :restart, :temporary)
{shutdown, options} = Keyword.pop(options, :shutdown, 5000)
@@ -97,13 +90,10 @@ defmodule Task.Supervisor do
@doc """
Starts a task that can be awaited on.
The `supervisor` must be a reference as defined in `Supervisor`.
The `supervisor` must be a reference as defined in `Task.Supervisor`.
The task will still be linked to the caller, see `Task.async/3` for
more information and `async_nolink/2` for a non-linked variant.
Raises an error if `supervisor` has reached the maximum number of
children.
## Options
* `:shutdown` - `:brutal_kill` if the tasks must be killed directly on shutdown
@@ -118,13 +108,10 @@ defmodule Task.Supervisor do
@doc """
Starts a task that can be awaited on.
The `supervisor` must be a reference as defined in `Supervisor`.
The `supervisor` must be a reference as defined in `Task.Supervisor`.
The task will still be linked to the caller, see `Task.async/3` for
more information and `async_nolink/2` for a non-linked variant.
Raises an error if `supervisor` has reached the maximum number of
children.
## Options
* `:shutdown` - `:brutal_kill` if the tasks must be killed directly on shutdown
@@ -139,13 +126,10 @@ defmodule Task.Supervisor do
@doc """
Starts a task that can be awaited on.
The `supervisor` must be a reference as defined in `Supervisor`.
The `supervisor` must be a reference as defined in `Task.Supervisor`.
The task won't be linked to the caller, see `Task.async/3` for
more information.
Raises an error if `supervisor` has reached the maximum number of
children.
## Options
* `:shutdown` - `:brutal_kill` if the tasks must be killed directly on shutdown
@@ -165,53 +149,6 @@ defmodule Task.Supervisor do
terminates, the caller's process will always receive a `:DOWN` message
with the same `ref` value that is held by the task struct. If the task
terminates normally, the reason in the `:DOWN` message will be `:normal`.
## Examples
Typically, you use `async_nolink/3` when there is a reasonable expectation that
the task may fail, and you don't want it to take down the caller. Let's see an
example where a `GenServer` is meant to run a single task and track its status:
defmodule MyApp.Server do
use GenServer
# ...
def start_task do
GenServer.call(__MODULE__, :start_task)
end
# In this case the task is already running, so we just return :ok.
def handle_call(:start_task, %{ref: ref} = state) when is_reference(ref) do
{:reply, :ok, state}
end
# The task is not running yet, so let's start it.
def handle_cast(:start_task, %{ref: nil} = state) do
task =
Task.Supervisor.async_nolink(MyApp.TaskSupervisor, fn ->
...
end)
# We return :ok and the server will continue running
{:reply, :ok, %{state | ref: task.ref}}
end
# The task completed successfully
def handle_info({ref, answer}, %{ref: ref} = state) do
# We don't care about the DOWN message now, so let's demonitor and flush it
Process.demonitor(ref, [:flush])
# Do something with the result and then return
{:noreply, %{state | ref: nil}}
end
# The task failed
def handle_info({:DOWN, ref, :process, _pid, _reason}, %{ref: ref} = state) do
# Log and possibly restart the task...
{:noreply, %{state | ref: nil}}
end
end
"""
@spec async_nolink(Supervisor.supervisor(), (() -> any), Keyword.t()) :: Task.t()
def async_nolink(supervisor, fun, options \\ []) do
@@ -221,13 +158,10 @@ defmodule Task.Supervisor do
@doc """
Starts a task that can be awaited on.
The `supervisor` must be a reference as defined in `Supervisor`.
The `supervisor` must be a reference as defined in `Task.Supervisor`.
The task won't be linked to the caller, see `Task.async/3` for
more information.
Raises an error if `supervisor` has reached the maximum number of
children.
Note this function requires the task supervisor to have `:temporary`
as the `:restart` option (the default), as `async_nolink/4` keeps a
direct reference to the task which is lost if the task is restarted.
@@ -238,8 +172,8 @@ defmodule Task.Supervisor do
end
@doc """
Returns a stream where the given function (`module` and `function`)
is mapped concurrently on each item in `enumerable`.
Returns a stream that runs the given `module`, `function`, and `args`
concurrently on each item in `enumerable`.
Each item will be prepended to the given `args` and processed by its
own task. The tasks will be spawned under the given `supervisor` and
@@ -247,38 +181,35 @@ defmodule Task.Supervisor do
When streamed, each task will emit `{:ok, value}` upon successful
completion or `{:exit, reason}` if the caller is trapping exits.
The order of results depends on the value of the `:ordered` option.
Results are emitted in the same order as the original `enumerable`.
The level of concurrency and the time tasks are allowed to run can
be controlled via options (see the "Options" section below).
The level of concurrency can be controlled via the `:max_concurrency`
option and defaults to `System.schedulers_online/0`. A timeout
can also be given as an option representing the maximum amount of
time to wait without a task reply.
If you find yourself trapping exits to handle exits inside
Finally, if you find yourself trapping exits to handle exits inside
the async stream, consider using `async_stream_nolink/6` to start tasks
that are not linked to the calling process.
that are not linked to the current process.
## Options
* `:max_concurrency` - sets the maximum number of tasks to run
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. 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 to wait (in milliseconds)
without receiving a task reply (across all running tasks).
Defaults to `5000`.
* `:on_timeout` - what do to when a task times out. The possible
values are:
* `:exit` (default) - the process that spawned the tasks exits.
* `:kill_task` - the task that timed out is killed. The value
emitted for that task is `{:exit, :timeout}`.
* `:shutdown` - `:brutal_kill` if the tasks must be killed directly on shutdown
or an integer indicating the timeout value. Defaults to `5000` milliseconds.
or an integer indicating the timeout value, defaults to 5000 milliseconds.
## Examples
@@ -288,7 +219,6 @@ defmodule Task.Supervisor do
Enum.to_list(stream)
"""
@doc since: "1.4.0"
@spec async_stream(Supervisor.supervisor(), Enumerable.t(), module, atom, [term], keyword) ::
Enumerable.t()
def async_stream(supervisor, enumerable, module, function, args, options \\ [])
@@ -306,7 +236,6 @@ defmodule Task.Supervisor do
See `async_stream/6` for discussion, options, and examples.
"""
@doc since: "1.4.0"
@spec async_stream(Supervisor.supervisor(), Enumerable.t(), (term -> term), keyword) ::
Enumerable.t()
def async_stream(supervisor, enumerable, fun, options \\ []) when is_function(fun, 1) do
@@ -314,8 +243,8 @@ defmodule Task.Supervisor do
end
@doc """
Returns a stream where the given function (`module` and `function`)
is mapped concurrently on each item in `enumerable`.
Returns a stream that runs the given `module`, `function`, and `args`
concurrently on each item in `enumerable`.
Each item in `enumerable` will be prepended to the given `args` and processed
by its own task. The tasks will be spawned under the given `supervisor` and
@@ -323,7 +252,6 @@ defmodule Task.Supervisor do
See `async_stream/6` for discussion, options, and examples.
"""
@doc since: "1.4.0"
@spec async_stream_nolink(
Supervisor.supervisor(),
Enumerable.t(),
@@ -347,7 +275,6 @@ defmodule Task.Supervisor do
See `async_stream/6` for discussion and examples.
"""
@doc since: "1.4.0"
@spec async_stream_nolink(Supervisor.supervisor(), Enumerable.t(), (term -> term), keyword) ::
Enumerable.t()
def async_stream_nolink(supervisor, enumerable, fun, options \\ []) when is_function(fun, 1) do
@@ -390,12 +317,11 @@ defmodule Task.Supervisor do
or an integer indicating the timeout value, defaults to 5000 milliseconds.
"""
@spec start_child(Supervisor.supervisor(), (() -> any), keyword) ::
DynamicSupervisor.on_start_child()
@spec start_child(Supervisor.supervisor(), (() -> any)) :: {:ok, pid}
def start_child(supervisor, fun, options \\ []) do
restart = options[:restart]
shutdown = options[:shutdown]
args = [get_owner(self()), get_callers(self()), {:erlang, :apply, [fun, []]}]
args = [get_info(self()), {:erlang, :apply, [fun, []]}]
start_child_with_spec(supervisor, args, restart, shutdown)
end
@@ -405,75 +331,49 @@ defmodule Task.Supervisor do
Similar to `start_child/2` except the task is specified
by the given `module`, `fun` and `args`.
"""
@spec start_child(Supervisor.supervisor(), module, atom, [term], keyword) ::
DynamicSupervisor.on_start_child()
@spec start_child(Supervisor.supervisor(), module, atom, [term]) :: {:ok, pid}
def start_child(supervisor, module, fun, args, options \\ [])
when is_atom(fun) and is_list(args) do
restart = options[:restart]
shutdown = options[:shutdown]
args = [get_owner(self()), get_callers(self()), {module, fun, args}]
args = [get_info(self()), {module, fun, args}]
start_child_with_spec(supervisor, args, restart, shutdown)
end
defp start_child_with_spec(supervisor, args, restart, shutdown) do
# TODO: This only exists because we need to support reading restart/shutdown
# from two different places. Remove this and the associated clause in DynamicSupervisor
# on Elixir v2.0
# TODO: Remove this on Elixir v2.0 and the associated clause in DynamicSupervisor
GenServer.call(supervisor, {:start_task, args, restart, shutdown}, :infinity)
end
defp get_owner(pid) do
self_or_name =
case Process.info(pid, :registered_name) do
{:registered_name, name} when is_atom(name) -> name
_ -> pid
defp get_info(self) do
name =
case Process.info(self, :registered_name) do
{:registered_name, []} -> self
{:registered_name, name} -> name
end
{node(), self_or_name, pid}
end
defp get_callers(owner) do
case :erlang.get(:"$callers") do
[_ | _] = list -> [owner | list]
_ -> [owner]
end
{node(), name}
end
defp async(supervisor, link_type, module, fun, args, options) do
owner = self()
args = [get_owner(owner), get_callers(owner), :monitor, {module, fun, args}]
args = [owner, :monitor, get_info(owner), {module, fun, args}]
shutdown = options[:shutdown]
case start_child_with_spec(supervisor, args, :temporary, shutdown) do
{:ok, pid} ->
if link_type == :link, do: Process.link(pid)
ref = Process.monitor(pid)
send(pid, {owner, ref})
%Task{pid: pid, ref: ref, owner: owner}
{:error, :max_children} ->
raise """
reached the maximum number of tasks for this task supervisor. The maximum number \
of tasks that are allowed to run at the same time under this supervisor can be \
configured with the :max_children option passed to Task.Supervisor.start_link/1\
"""
end
{:ok, pid} = start_child_with_spec(supervisor, args, :temporary, shutdown)
if link_type == :link, do: Process.link(pid)
ref = Process.monitor(pid)
send(pid, {owner, ref})
%Task{pid: pid, ref: ref, owner: owner}
end
defp build_stream(supervisor, link_type, enumerable, fun, options) do
shutdown = options[:shutdown]
&Task.Supervised.stream(enumerable, &1, &2, fun, options, fn [owner | _] = callers, mfa ->
args = [get_owner(owner), callers, :monitor, mfa]
case start_child_with_spec(supervisor, args, :temporary, shutdown) do
{:ok, pid} ->
if link_type == :link, do: Process.link(pid)
{:ok, link_type, pid}
{:error, :max_children} ->
{:error, :max_children}
end
&Task.Supervised.stream(enumerable, &1, &2, fun, options, fn owner, mfa ->
args = [owner, :monitor, get_info(owner), mfa]
{:ok, pid} = start_child_with_spec(supervisor, args, :temporary, shutdown)
if link_type == :link, do: Process.link(pid)
{link_type, pid}
end)
end
end
+46 -23
View File
@@ -2,41 +2,64 @@ defmodule Tuple do
@moduledoc """
Functions for working with tuples.
Please note the following functions for tuples are found in `Kernel`:
* `elem/2` - access a tuple by index
* `put_elem/3` - insert a value into a tuple by index
* `tuple_size/1` - get the number of elements in a tuple
Tuples are intended as fixed-size containers for multiple elements.
To manipulate a collection of elements, use a list instead. `Enum`
functions do not work on tuples.
Tuples are denoted with curly braces:
Tuples are composite data types with a fixed number of elements. Tuples
can contain elements of any type, and a tuple can contain elements of
different types. Curly braces can be used to create tuples:
iex> {}
{}
iex> {1, :two, "three"}
{1, :two, "three"}
A tuple may contain elements of different types, which are stored
contiguously in memory. Accessing any element takes constant time,
but modifying a tuple, which produces a shallow copy, takes linear time.
Tuples are good for reading data while lists are better for traversals.
Tuples store elements contiguously in memory. This means accessing a
tuple element by index doesn't depend on the number of elements in the
tuple. We say the operation is done in constant-time, via the
`Kernel.elem/1` function:
Tuples are typically used either when a function has multiple return values
or for error handling. `File.read/1` returns `{:ok, contents}` if reading
the given file is successful, or else `{:error, reason}` such as when
the file does not exist.
iex> tuple = {1, :two, "three"}
iex> elem(tuple, 0)
1
iex> elem(tuple, 2)
"three"
The functions in this module that add and remove elements from tuples are
rarely used in practice, as they typically imply tuples are being used as
collections. To append to a tuple, it is preferable to use pattern matching:
Same goes for getting the tuple size with `Kernel.tuple_size/1`:
iex> tuple_size({})
0
iex> tuple_size({1, 2, 3})
3
Tuples being stored contiguously in memory also means that updating a tuple
(for example replacing an element with `Kernel.put_elem/3`) will make a
shallow copy of the whole tuple. The tuple elements are still shared thanks
to immutability.
Tuples are not meant to be used as a "collection" type but rather as a
fixed-size container for multiple elements. That's why it is not possible
to traverse a tuple dynamically using the functions in the `Enum` module.
For example, tuples are often used to have functions return "enriched"
values: a common pattern is for functions to return `{:ok, value}` for
successful cases and `{:error, reason}` for unsuccessful cases. This is
exactly what `File.read/1` does: it returns `{:ok, contents}` if reading
the given file is successful, or `{:error, reason}` otherwise, such as
when the file does not exist.
The most common operations performed on tuples are available in `Kernel`
(`Kernel.tuple_size/1`, `Kernel.elem/2`, `Kernel.put_elem/3`, and others)
and are automatically imported into your code. The functions in this module
cover other cases, such as dynamic creation of tuples (`Tuple.duplicate/2`)
and conversion to list (`Tuple.to_list/1`). The functions that add and remove
elements from tuples, changing their size, are rarely used in practice, as
they typically imply tuples are being used as collections. Even if you have
a tuple `{:ok, atom}` and you want to append another element to it, such as
an empty map, it is preferrable to rely on pattern matching and create a new
tuple than manipulating it dynamically:
tuple = {:ok, :example}
# Avoid
Tuple.insert_at(tuple, 2, %{})
Tuple.insert_at(tuple, 2, %{}}
# Prefer
{:ok, atom} = tuple
+23 -77
View File
@@ -47,7 +47,7 @@ defmodule URI do
"""
@spec default_port(binary) :: nil | non_neg_integer
def default_port(scheme) when is_binary(scheme) do
:elixir_config.get({:uri, scheme}, nil)
:elixir_config.safe_get({:uri, scheme}, nil)
end
@doc """
@@ -88,7 +88,7 @@ defmodule URI do
iex> URI.encode_query(query)
"key=value+with+spaces"
iex> URI.encode_query(%{key: [:a, :list]})
iex> URI.encode_query %{key: [:a, :list]}
** (ArgumentError) encode_query/1 values cannot be lists, got: [:a, :list]
"""
@@ -128,7 +128,7 @@ defmodule URI do
%{"percent" => "oh yes!", "starting" => "map"}
"""
@spec decode_query(binary, %{binary => binary}) :: %{binary => binary}
@spec decode_query(binary, map) :: map
def decode_query(query, map \\ %{})
# TODO: Remove on 2.0
@@ -258,7 +258,7 @@ defmodule URI do
end
@doc """
Percent-escapes all characters that require escaping in a string.
Percent-escapes all characters that require escaped in a string.
This means reserved characters, such as `:` and `/`, and the so-
called unreserved characters, which have the same meaning both
@@ -322,8 +322,8 @@ defmodule URI do
## Examples
iex> URI.decode("https%3A%2F%2Felixir-lang.org")
"https://elixir-lang.org"
iex> URI.decode("http%3A%2F%2Felixir-lang.org")
"http://elixir-lang.org"
"""
@spec decode(binary) :: binary
@@ -390,53 +390,22 @@ defmodule URI do
## Examples
iex> URI.parse("https://elixir-lang.org/")
%URI{
authority: "elixir-lang.org",
fragment: nil,
host: "elixir-lang.org",
path: "/",
port: 443,
query: nil,
scheme: "https",
userinfo: nil
}
iex> URI.parse("http://elixir-lang.org/")
%URI{scheme: "http", path: "/", query: nil, fragment: nil,
authority: "elixir-lang.org", userinfo: nil,
host: "elixir-lang.org", port: 80}
iex> URI.parse("//elixir-lang.org/")
%URI{
authority: "elixir-lang.org",
fragment: nil,
host: "elixir-lang.org",
path: "/",
port: nil,
query: nil,
scheme: nil,
userinfo: nil
}
%URI{authority: "elixir-lang.org", fragment: nil, host: "elixir-lang.org",
path: "/", port: nil, query: nil, scheme: nil, userinfo: nil}
iex> URI.parse("/foo/bar")
%URI{
authority: nil,
fragment: nil,
host: nil,
path: "/foo/bar",
port: nil,
query: nil,
scheme: nil,
userinfo: nil
}
%URI{authority: nil, fragment: nil, host: nil, path: "/foo/bar",
port: nil, query: nil, scheme: nil, userinfo: nil}
iex> URI.parse("foo/bar")
%URI{
authority: nil,
fragment: nil,
host: nil,
path: "foo/bar",
port: nil,
query: nil,
scheme: nil,
userinfo: nil
}
%URI{authority: nil, fragment: nil, host: nil, path: "foo/bar",
port: nil, query: nil, scheme: nil, userinfo: nil}
"""
@spec parse(t | binary) :: t
@@ -447,17 +416,17 @@ defmodule URI do
def parse(string) when is_binary(string) do
# From https://tools.ietf.org/html/rfc3986#appendix-B
regex =
Regex.recompile!(~r{^(([a-z][a-z0-9\+\-\.]*):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?}i)
Regex.recompile!(
~r/^(([a-z][a-z0-9\+\-\.]*):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/i
)
parts = Regex.run(regex, string)
destructure [_, _, scheme, _, authority, path, query_with_question_mark, _, _, fragment],
parts
destructure [_, _, scheme, _, authority, path, _, query, _, fragment], parts
scheme = nillify(scheme)
authority = nillify(authority)
path = nillify(path)
query = nillify_query(query_with_question_mark)
query = nillify(query)
{userinfo, host, port} = split_authority(authority)
scheme = scheme && String.downcase(scheme)
@@ -475,9 +444,6 @@ defmodule URI do
}
end
defp nillify_query("?" <> query), do: query
defp nillify_query(_other), do: nil
# Split an authority into its userinfo, host and port parts.
defp split_authority(string) do
regex = Regex.recompile!(~r/(^(.*)@)?(\[[a-zA-Z0-9:.]*\]|[^:]*)(:(\d*))?/)
@@ -499,32 +465,12 @@ defmodule URI do
@doc """
Returns the string representation of the given `URI` struct.
## Examples
iex> URI.to_string(URI.parse("http://google.com"))
"http://google.com"
iex> URI.to_string(%URI{scheme: "foo", host: "bar.baz"})
"foo://bar.baz"
Note that when creating this string representation, the `authority` will be
used if the host is `nil`. Otherwise, the `userinfo`, `host`, and `port` will
be used.
iex> URI.to_string(%URI{authority: "foo@example.com:80"})
"//foo@example.com:80"
iex> URI.to_string(%URI{userinfo: "bar", host: "example.org", port: 81})
"//bar@example.org:81"
iex> URI.to_string(%URI{
...> authority: "foo@example.com:80",
...> userinfo: "bar",
...> host: "example.org",
...> port: 81
...> })
"//bar@example.org:81"
"""
@spec to_string(t) :: binary
defdelegate to_string(uri), to: String.Chars.URI
@@ -537,10 +483,10 @@ defmodule URI do
## Examples
iex> URI.merge(URI.parse("http://google.com"), "/query") |> to_string()
iex> URI.merge(URI.parse("http://google.com"), "/query") |> to_string
"http://google.com/query"
iex> URI.merge("http://example.com", "http://google.com") |> to_string()
iex> URI.merge("http://example.com", "http://google.com") |> to_string
"http://google.com"
"""
+40 -78
View File
@@ -14,17 +14,13 @@ defmodule Version do
MAJOR.MINOR.PATCH
Pre-releases are supported by optionally appending a hyphen and a series of
period-separated identifiers immediately following the patch version.
Identifiers consist of only ASCII alphanumeric characters and hyphens (`[0-9A-Za-z-]`):
Pre-releases are supported by appending `-[0-9A-Za-z-\.]`:
"1.0.0-alpha.3"
Build information can be added by appending a plus sign and a series of
dot-separated identifiers immediately following the patch or pre-release version.
Identifiers consist of only ASCII alphanumeric characters and hyphens (`[0-9A-Za-z-]`):
Build information can be added by appending `+[0-9A-Za-z-\.]`:
"1.0.0-alpha.3+20130417140000.amd64"
"1.0.0-alpha.3+20130417140000"
## Struct
@@ -35,9 +31,9 @@ defmodule Version do
## Requirements
Requirements allow you to specify which versions of a given
dependency you are willing to work against. Requirements support the common
comparison operators such as `>`, `>=`, `<`, `<=`, `==`, `!=` that work as one would expect,
and additionally the special operator `~>` described in detail further below.
dependency you are willing to work against. Requirements support common
operators like `>=`, `<=`, `>`, `==`, and friends that
work as one would expect:
# Only version 2.0.0
"== 2.0.0"
@@ -55,11 +51,10 @@ defmodule Version do
"~> 2.0.0"
`~>` will never include pre-release versions of its upper bound,
regardless of the usage of the `:allow_pre` option, or whether the operand
is a pre-release version. It can also be used to set an upper bound on only the major
`~>` will never include pre-release versions of its upper bound.
It can also be used to set an upper bound on only the major
version part. See the table below for `~>` requirements and
their corresponding translations.
their corresponding translation.
`~>` | Translation
:------------- | :---------------------
@@ -69,28 +64,23 @@ defmodule Version do
`~> 2.0` | `>= 2.0.0 and < 3.0.0`
`~> 2.1` | `>= 2.1.0 and < 3.0.0`
The requirement operand after the `~>` is allowed to omit the patch version,
allowing us to express `~> 2.1` or `~> 2.1-dev`, something that wouldn't be allowed
when using the common comparison operators.
When the `:allow_pre` option is set `false` in `Version.match?/3`, the requirement
will not match a pre-release version unless the operand is a pre-release version.
When `allow_pre: false` is set, the requirement will not match a
pre-release version unless the operand is a pre-release version.
The default is to always allow pre-releases but note that in
Hex `:allow_pre` is set to `false`. See the table below for examples.
Requirement | Version | `:allow_pre` | Matches
:------------- | :---------- | :---------------- | :------
`~> 2.0` | `2.1.0` | `true` or `false` | `true`
`~> 2.0` | `3.0.0` | `true` or `false` | `false`
`~> 2.0.0` | `2.0.5` | `true` or `false` | `true`
`~> 2.0.0` | `2.1.0` | `true` or `false` | `false`
`~> 2.1.2` | `2.1.6-dev` | `true` | `true`
`~> 2.1.2` | `2.1.6-dev` | `false` | `false`
`~> 2.1-dev` | `2.2.0-dev` | `true` or `false` | `true`
`~> 2.1.2-dev` | `2.1.6-dev` | `true` or `false` | `true`
`>= 2.1.0` | `2.2.0-dev` | `true` | `true`
`>= 2.1.0` | `2.2.0-dev` | `false` | `false`
`>= 2.1.0-dev` | `2.2.6-dev` | `true` or `false` | `true`
Requirement | Version | `:allow_pre` | Matches
:------------- | :---------- | :----------- | :------
`~> 2.0` | `2.1.0` | - | `true`
`~> 2.0` | `3.0.0` | - | `false`
`~> 2.0.0` | `2.0.1` | - | `true`
`~> 2.0.0` | `2.1.0` | - | `false`
`~> 2.1.2` | `2.1.3-dev` | `true` | `true`
`~> 2.1.2` | `2.1.3-dev` | `false` | `false`
`~> 2.1-dev` | `2.2.0-dev` | `false` | `true`
`~> 2.1.2-dev` | `2.1.3-dev` | `false` | `true`
`>= 2.1.0` | `2.2.0-dev` | `false` | `false`
`>= 2.1.0-dev` | `2.2.3-dev` | `true` | `true`
"""
@@ -99,28 +89,27 @@ defmodule Version do
@type version :: String.t() | t
@type requirement :: String.t() | Version.Requirement.t()
@type major :: non_neg_integer
@type minor :: non_neg_integer
@type patch :: non_neg_integer
@type major :: String.t() | non_neg_integer
@type minor :: non_neg_integer | nil
@type patch :: non_neg_integer | nil
@type pre :: [String.t() | non_neg_integer]
@type build :: String.t() | nil
@type matchable :: {major :: major, minor :: minor, patch :: patch, pre :: pre}
@type t :: %__MODULE__{major: major, minor: minor, patch: patch, pre: pre, build: build}
defmodule Requirement do
@moduledoc false
defstruct [:source, :matchspec, :compiled]
@type t :: %__MODULE__{source: String.t(), matchspec: :ets.match_spec(), compiled: boolean}
@type t :: %__MODULE__{}
end
defmodule InvalidRequirementError do
defexception [:requirement]
@impl true
def exception(requirement) when is_binary(requirement) do
%__MODULE__{requirement: requirement}
end
@impl true
def message(%{requirement: requirement}) do
"invalid requirement: #{inspect(requirement)}"
end
@@ -129,12 +118,10 @@ defmodule Version do
defmodule InvalidVersionError do
defexception [:version]
@impl true
def exception(version) when is_binary(version) do
%__MODULE__{version: version}
end
@impl true
def message(%{version: version}) do
"invalid version: #{inspect(version)}"
end
@@ -152,8 +139,8 @@ defmodule Version do
## Options
* `:allow_pre` (boolean) - when `false`, pre-release versions will not match
unless the operand is a pre-release version. Defaults to `true`.
For examples, please refer to the table above under the "Requirements" section.
unless the operand is a pre-release version. See the table above
for examples. Defaults to `true`.
## Examples
@@ -163,12 +150,6 @@ defmodule Version do
iex> Version.match?("2.0.0", "== 1.0.0")
false
iex> Version.match?("2.1.6-dev", "~> 2.1.2")
true
iex> Version.match?("2.1.6-dev", "~> 2.1.2", allow_pre: false)
false
iex> Version.match?("foo", "== 1.0.0")
** (Version.InvalidVersionError) invalid version: "foo"
@@ -180,7 +161,13 @@ defmodule Version do
def match?(version, requirement, opts \\ [])
def match?(version, requirement, opts) when is_binary(requirement) do
match?(version, parse_requirement!(requirement), opts)
case parse_requirement(requirement) do
{:ok, requirement} ->
match?(version, requirement, opts)
:error ->
raise InvalidRequirementError, requirement
end
end
def match?(version, %Requirement{matchspec: spec, compiled: false}, opts) do
@@ -276,7 +263,7 @@ defmodule Version do
@doc """
Parses a version string into a `Version`.
If `string` is an invalid version, a `Version.InvalidVersionError` is raised.
If `string` is an invalid version, an `InvalidVersionError` is raised.
## Examples
@@ -287,7 +274,7 @@ defmodule Version do
** (Version.InvalidVersionError) invalid version: "2.0-alpha1"
"""
@spec parse!(String.t()) :: t
@spec parse!(String.t()) :: t | no_return
def parse!(string) when is_binary(string) do
case parse(string) do
{:ok, version} -> version
@@ -319,32 +306,6 @@ defmodule Version do
end
end
@doc """
Parses a version requirement string into a `Version.Requirement` struct.
If `string` is an invalid requirement, a `Version.InvalidRequirementError` is raised.
## Examples
iex> Version.parse_requirement!("== 2.0.1")
#Version.Requirement<== 2.0.1>
iex> Version.parse_requirement!("== == 2.0.1")
** (Version.InvalidRequirementError) invalid requirement: "== == 2.0.1"
"""
@doc since: "1.8.0"
@spec parse_requirement!(String.t()) :: Requirement.t()
def parse_requirement!(string) when is_binary(string) do
case Version.Parser.parse_requirement(string) do
{:ok, spec} ->
%Requirement{source: string, matchspec: spec, compiled: false}
:error ->
raise InvalidRequirementError, string
end
end
@doc """
Compiles a requirement to its internal representation with
`:ets.match_spec_compile/1` for faster matching.
@@ -429,6 +390,7 @@ defmodule Version do
to_matchspec(lexed)
end
@spec parse_version(String.t()) :: {:ok, Version.matchable()} | :error
def parse_version(string, approximate? \\ false) when is_binary(string) do
destructure [version_with_pre, build], String.split(string, "+", parts: 2)
destructure [version, pre], String.split(version_with_pre, "-", parts: 2)

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