Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ddc554a7ef | ||
|
|
b6d27b5333 | ||
|
|
2e54537524 | ||
|
|
0d8fa1a0ff | ||
|
|
764d0c1008 | ||
|
|
5f86d6158b | ||
|
|
1ec9d1d7bd | ||
|
|
462a9deef3 | ||
|
|
c9d6ea50db | ||
|
|
d0694b5d58 | ||
|
|
16fe53bc10 | ||
|
|
a9a271e360 | ||
|
|
62697dfa00 | ||
|
|
132b80acb7 | ||
|
|
f5e35dade5 | ||
|
|
3d72978af2 | ||
|
|
51d61cbdfb | ||
|
|
4d3084b3fb | ||
|
|
09a13b0600 | ||
|
|
5f96a9b37f | ||
|
|
762b5c0870 | ||
|
|
a9f1be07ca | ||
|
|
f4b6375125 | ||
|
|
ccc24ea5b2 | ||
|
|
65f1b2df1e | ||
|
|
479e46fc7e | ||
|
|
4f634bd244 | ||
|
|
8e1ebafebc | ||
|
|
a89e656a65 | ||
|
|
e615b3dd0e | ||
|
|
6586e19a22 | ||
|
|
8291c2f795 | ||
|
|
f663b2e0de | ||
|
|
e6c893cf89 | ||
|
|
705a637acf | ||
|
|
6b74116cb0 | ||
|
|
9565310fb8 | ||
|
|
718123157c | ||
|
|
be46a1f999 | ||
|
|
0e8de11541 | ||
|
|
f6b0855d31 | ||
|
|
8e6cc2c74d | ||
|
|
db5c1557bb | ||
|
|
984495fd90 | ||
|
|
a0d5c4f4fb | ||
|
|
510ac444e3 | ||
|
|
e13c944ba4 | ||
|
|
6a0e9390a2 | ||
|
|
d59c801f33 | ||
|
|
c107a2fe26 | ||
|
|
17853e63d8 | ||
|
|
338e242a59 | ||
|
|
2de4680c6e | ||
|
|
1cffb06981 | ||
|
|
e0f1ba28dd | ||
|
|
45c7f828ef | ||
|
|
9169b6e302 | ||
|
|
c2bdc66b9e | ||
|
|
d16f354a3b | ||
|
|
dd30768354 | ||
|
|
bf1d993b2f | ||
|
|
755639f538 | ||
|
|
97e0b1e1ff | ||
|
|
c2a9c93f02 | ||
|
|
d896f541f1 | ||
|
|
f033672fa8 | ||
|
|
717c72e71b | ||
|
|
9ac830f56b | ||
|
|
0ded67dd60 | ||
|
|
abbf173da7 | ||
|
|
fc1c303481 | ||
|
|
5e1e2e6a83 | ||
|
|
77d708e1bc | ||
|
|
cb0f900b1c | ||
|
|
815420ce0f | ||
|
|
cba11d6941 | ||
|
|
966f8f74ae | ||
|
|
55241d92e2 | ||
|
|
11086ac450 | ||
|
|
0c81857622 | ||
|
|
bd0a2e8a12 | ||
|
|
df50dca83a | ||
|
|
f0969f0aa0 | ||
|
|
ad489d7d80 | ||
|
|
f8afbf3321 | ||
|
|
3b5f10874f | ||
|
|
e17b2470c6 | ||
|
|
8a1c5c38d0 | ||
|
|
00deab98bf | ||
|
|
47f4721c0b | ||
|
|
beb69232c4 | ||
|
|
4b43c7ae20 | ||
|
|
ca49a53ab2 | ||
|
|
1ff0e1ac7a | ||
|
|
ae66125f2e | ||
|
|
3025cc759b | ||
|
|
8f18a5f4ff | ||
|
|
1626f9659b | ||
|
|
9083937177 | ||
|
|
da90bb741e | ||
|
|
51d56274de | ||
|
|
79fcebb407 | ||
|
|
2b588dfb3e | ||
|
|
2ba98f247a | ||
|
|
bc9b5ea700 | ||
|
|
992c240b14 | ||
|
|
79ae08f962 | ||
|
|
5d7ee90cba | ||
|
|
66cf08ea89 | ||
|
|
ad62bb970d | ||
|
|
18eef65839 | ||
|
|
0611069c74 | ||
|
|
f2b31a6626 | ||
|
|
3b41c9f396 | ||
|
|
0eb95c9c75 | ||
|
|
b898f09870 | ||
|
|
e89139bcf0 | ||
|
|
ecad849319 | ||
|
|
2658f3666a | ||
|
|
3907a3062f | ||
|
|
bd9751d669 | ||
|
|
e4ebe291f2 | ||
|
|
6b6bcea76c | ||
|
|
96260316ca | ||
|
|
47f2696820 | ||
|
|
740cc1ff0b | ||
|
|
63b8d0ba34 | ||
|
|
12102d8015 | ||
|
|
67e575eca7 | ||
|
|
f3960a2f34 | ||
|
|
824fcea04a | ||
|
|
a6928f774c | ||
|
|
3d08d243d8 | ||
|
|
301e0f6b63 | ||
|
|
7c56a02b0a | ||
|
|
e442bc630b | ||
|
|
3dbefd7704 | ||
|
|
fd1d15a8c5 | ||
|
|
56fcd2a45d | ||
|
|
900043c7f2 | ||
|
|
14f0625911 | ||
|
|
d76c35818e | ||
|
|
6a581275aa | ||
|
|
bcbe91a25c | ||
|
|
949ac2b517 | ||
|
|
adb1fa1374 | ||
|
|
bd275ea51c | ||
|
|
d02755530a | ||
|
|
2ab2bffc00 | ||
|
|
701c0b433d | ||
|
|
895adcb001 | ||
|
|
e6e6f8d484 | ||
|
|
09b0ee3f12 | ||
|
|
5c6f7b9ce5 | ||
|
|
adbbba118d | ||
|
|
fab66ad834 | ||
|
|
31ef3abff0 | ||
|
|
021a84aafe | ||
|
|
d8fd6ef6ff | ||
|
|
f8f4e1dd79 | ||
|
|
d3af8ea8aa | ||
|
|
d587434be2 | ||
|
|
6aadfba586 | ||
|
|
625f45930a | ||
|
|
cb47d08c13 | ||
|
|
9a5d1a39d1 | ||
|
|
27102e276d | ||
|
|
0b646c6188 | ||
|
|
3879f0d8b3 | ||
|
|
24369d5724 | ||
|
|
3d20f049e6 | ||
|
|
8d07fe6f59 | ||
|
|
d27b285687 | ||
|
|
1684c8067e |
+5
-1
@@ -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
|
||||
]
|
||||
]
|
||||
|
||||
+24
-37
@@ -1,47 +1,34 @@
|
||||
language: bash
|
||||
language: erlang
|
||||
sudo: false
|
||||
|
||||
env:
|
||||
global:
|
||||
- ELIXIR_ASSERT_TIMEOUT=2000
|
||||
matrix:
|
||||
- OTP_RELEASE=OTP-22.0 CHECK_REPRODUCIBLE=true CHECK_POSIX_COMPLIANT=true
|
||||
- OTP_RELEASE=OTP-21.3.8
|
||||
- OTP_RELEASE=OTP-21.2
|
||||
- OTP_RELEASE=OTP-21.1
|
||||
- OTP_RELEASE=OTP-21.0
|
||||
- OTP_RELEASE=OTP-20.3
|
||||
- OTP_RELEASE=OTP-20.2
|
||||
- OTP_RELEASE=OTP-20.1
|
||||
- OTP_RELEASE=OTP-20.0
|
||||
- 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
|
||||
- ELIXIRC_OPTS="--warnings-as-errors" ERLC_OPTS="+warning_as_errors" make compile
|
||||
- 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
|
||||
|
||||
# Check for reproducible builds only in the latest OTP release
|
||||
- if [ -n "$CHECK_REPRODUCIBLE" ]; then make check_reproducible; fi
|
||||
|
||||
# Check for POSIX compliant shell scripts
|
||||
- if [ -n "$CHECK_POSIX_COMPLIANT" ]; then
|
||||
shellcheck -e SC2039,2086 bin/elixir && echo "bin/elixir is POSIX compliant";
|
||||
shellcheck bin/elixirc && echo "bin/elixirc is POSIX compliant";
|
||||
shellcheck bin/iex && echo "bin/iex is POSIX compliant";
|
||||
fi
|
||||
notifications:
|
||||
recipients:
|
||||
- jose.valim@gmail.com
|
||||
- eric.meadows.jonsson@gmail.com
|
||||
- lexmag@me.com
|
||||
- an.leopardi@gmail.com
|
||||
|
||||
+305
-215
@@ -1,295 +1,385 @@
|
||||
# Changelog for Elixir v1.9
|
||||
# Changelog for Elixir v1.6
|
||||
|
||||
## Releases
|
||||
## Code formatter
|
||||
|
||||
The main feature in Elixir v1.9 is the addition of releases. A release is a self-contained directory that consists of your application code, all of its dependencies, plus the whole Erlang Virtual Machine (VM) and runtime. Once a release is assembled, it can be packaged and deployed to a target as long as the target runs on the same operating system (OS) distribution and version as the machine running the `mix release` command.
|
||||
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.
|
||||
|
||||
You can start a new project and assemble a release for it in three easy steps:
|
||||
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.
|
||||
|
||||
$ mix new my_app
|
||||
$ cd my_app
|
||||
$ MIX_ENV=prod mix release
|
||||
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.
|
||||
|
||||
A release will be assembled in `_build/prod/rel/my_app`. Inside the release, there will be a `bin/my_app` file which is the entry point to your system. It supports multiple commands, such as:
|
||||
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.
|
||||
|
||||
* `bin/my_app start`, `bin/my_app start_iex`, `bin/my_app restart`, and `bin/my_app stop` - for general management of the release
|
||||
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.
|
||||
|
||||
* `bin/my_app rpc COMMAND` and `bin/my_app remote` - for running commands on the running system or to connect to the running system
|
||||
## Dynamic Supervisor
|
||||
|
||||
* `bin/my_app eval COMMAND` - to start a fresh system that runs a single command and then shuts down
|
||||
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`.
|
||||
|
||||
* `bin/my_app daemon` and `bin/my_app daemon_iex` - to start the system as a daemon on Unix-like systems
|
||||
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 being handled by a separate supervised process. Those cases were handled in the Supervisor module under a special strategy called `:simple_one_for_one`.
|
||||
|
||||
* `bin/my_app install` - to install the system as a service on Windows machines
|
||||
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.
|
||||
|
||||
### Why releases?
|
||||
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.
|
||||
|
||||
Releases allow developers to precompile and package all of their code and the runtime into a single unit. The benefits of releases are:
|
||||
## `@deprecated` and `@since` attributes
|
||||
|
||||
* Code preloading. The VM has two mechanisms for loading code: interactive and embedded. By default, it runs in the interactive mode which dynamically loads modules when they are used for the first time. The first time your application calls `Enum.map/2`, the VM will find the `Enum` module and load it. There’s a downside. When you start a new server in production, it may need to load many other modules, causing the first requests to have an unusual spike in response time. Releases run in embedded mode, which loads all available modules upfront, guaranteeing your system is ready to handle requests after booting.
|
||||
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:
|
||||
|
||||
* Configuration and customization. Releases give developers fine grained control over system configuration and the VM flags used to start the system.
|
||||
@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
|
||||
|
||||
* Self-contained. A release does not require the source code to be included in your production artifacts. All of the code is precompiled and packaged. Releases do not even require Erlang or Elixir in your servers, as they include the Erlang VM and its runtime by default. Furthermore, both Erlang and Elixir standard libraries are stripped to bring only the parts you are actually using.
|
||||
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.
|
||||
|
||||
* Multiple releases. You can assemble different releases with different configuration per application or even with different applications altogether.
|
||||
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.
|
||||
|
||||
### Hooks and Configuration
|
||||
## defguard and defguardp
|
||||
|
||||
Releases also provide built-in hooks for configuring almost every need of the production system:
|
||||
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:
|
||||
|
||||
* `config/config.exs` (and `config/prod.exs`) - provides build-time application configuration, which is executed when the release is assembled
|
||||
def serve_drinks(%User{age: age}) when age >= 21 do
|
||||
# Code that serves drinks!
|
||||
end
|
||||
|
||||
* `config/releases.exs` - provides runtime application configuration. It is executed every time the release boots and is further extensible via config providers
|
||||
`%User{age: age}` is matching on a `User` struct with an age field and `when age >= 21` is the guard.
|
||||
|
||||
* `rel/vm.args.eex` - a template file that is copied into every release and provides static configuration of the Erlang Virtual Machine and other runtime flags
|
||||
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`:
|
||||
|
||||
* `rel/env.sh.eex` and `rel/env.bat.eex` - template files that are copied into every release and executed on every command to set up environment variables, including ones specific to the VM, and the general environment
|
||||
defguard is_drinking_age(age) when age >= 21
|
||||
|
||||
We have written extensive documentation on releases, so we recommend checking it out for more information.
|
||||
def serve_drinks(%User{age: age}) when is_drinking_age(age) do
|
||||
# Code that serves drinks!
|
||||
end
|
||||
|
||||
## Configuration overhaul
|
||||
## IEx improvements
|
||||
|
||||
A new `Config` module has been added to Elixir. The previous configuration API, `Mix.Config`, was part of the Mix build tool. But since releases provide runtime configuration and Mix is not included in releases, we ported the `Mix.Config` API to Elixir. In other words, `use Mix.Config` has been soft-deprecated in favor of `import Config`.
|
||||
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.
|
||||
|
||||
Another important change related to configuration is that `mix new` will no longer generate a `config/config.exs` file. [Relying on configuration is undesired for most libraries](https://hexdocs.pm/elixir/library-guidelines.html#avoid-application-configuration) and the generated config files pushed library authors in the wrong direction. Furthermore, `mix new --umbrella` will no longer generate a configuration for each child app, instead all configuration should be declared in the umbrella root. That's how it has always behaved, we are now making it explicit.
|
||||
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.
|
||||
|
||||
## Other enhancements
|
||||
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:
|
||||
|
||||
There are many other enhancements. The Elixir CLI got a handful of new options in order to best support releases. `Logger` now computes its sync/async/discard thresholds in a decentralized fashion, reducing contention. `EEx` templates support more complex expressions than before. Finally, there is a new `~U` sigil for working with UTC DateTimes as well as new functions in the `File`, `Registry`, and `System` modules.
|
||||
break! SomeFunction.call(:foo, _, _)
|
||||
|
||||
## v1.9.4 (2019-11-05)
|
||||
## mix xref
|
||||
|
||||
`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.
|
||||
|
||||
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:
|
||||
|
||||
$ mix xref callers SomeModule --include-siblings
|
||||
|
||||
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:
|
||||
|
||||
$ mix xref graph --format stats
|
||||
Tracked files: 129 (nodes)
|
||||
Compile dependencies: 256 (edges)
|
||||
Structs dependencies: 46 (edges)
|
||||
Runtime dependencies: 266 (edges)
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
`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:
|
||||
|
||||
# To get all files that depend on lib/foo.ex
|
||||
mix xref graph --sink lib/foo.ex --only-nodes
|
||||
|
||||
# To get all files that depend on lib/foo.ex at compile time
|
||||
mix xref graph --label compile --sink lib/foo.ex --only-nodes
|
||||
|
||||
# To get all files lib/foo.ex depends on
|
||||
mix xref graph --source lib/foo.ex --only-nodes
|
||||
|
||||
# To limit statistics only to compile time dependencies
|
||||
mix xref graph --format stats --label compile
|
||||
|
||||
Those improvements will help developers better understand the relationship between files and reveal potentially complex parts of their systems.
|
||||
|
||||
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.6 (2018-06-20)
|
||||
|
||||
This release supports Erlang/OTP 21.0 by removing all warnings and by properly supporting the new Erlang logger module.
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix local.hex] Remove invalid deprecation warning on `mix local.hex` command
|
||||
|
||||
## v1.9.3 (2019-11-05)
|
||||
|
||||
Note this release deprecates the use of URLs on `mix archive.install`, `mix escript.install`, and `mix local.rebar`. Support for passing URLs to said commands will be fully removed on Elixir v1.10, as they are unsafe. Thanks to Bram Verburg for the report and for providing a fix.
|
||||
|
||||
The alternative is straight-forward: you can simply download the artifact via the command line and then invoke the command with a file system path. For example, instead of:
|
||||
|
||||
$ mix archive.install https://example.org/installer.ez
|
||||
|
||||
You can execute on Unix (Linux, MacOS X):
|
||||
|
||||
$ wget https://example.org/installer.ez
|
||||
$ mix archive.install installer.ez
|
||||
|
||||
or
|
||||
|
||||
$ curl -o installer.ez https://example.org/installer.ez
|
||||
$ mix archive.install installer.ez
|
||||
|
||||
On Windows (Win7 or later):
|
||||
|
||||
> powershell -Command "Invoke-WebRequest https://example.org/installer.ez -OutFile installer.ez"
|
||||
> mix archive.install installer.ez
|
||||
|
||||
or
|
||||
|
||||
> powershell -Command "(New-Object Net.WebClient).DownloadFile('https://example.org/installer.ez', 'installer.ez')"
|
||||
> mix archive.install installer.ez
|
||||
|
||||
Note that, if you are a library author, consider providing installable escripts and archives through Hex, such as Phoenix:
|
||||
|
||||
$ mix archive.install hex phx_new
|
||||
|
||||
Installations through Hex are always safe and they come with version management and all other benefits from Hex too.
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix release] Add :tar option for releases to create a tarball
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix release] Use `default_release` option when name is not given
|
||||
* [mix release] Make release's boot script contents deterministic
|
||||
|
||||
### 3. Deprecations
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix archive.install] Warn when installing from URI
|
||||
* [mix escript.install] Warn when installing from URI
|
||||
* [mix local.rebar] Warn when installing from URI
|
||||
|
||||
## v1.9.2 (2019-10-12)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix release] Allow `{:from_app, app_name}` as a version for releases
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Kernel] Ensure compilation works for a variable named `super`
|
||||
* [Kernel] Ensure capture operator of a local function expands correctly inside a macro
|
||||
* [Regex] Ensure dynamic recompilation of regexes considers options. This fixes an issue where parsing the protocol in `URI.parse/1` seemingly looked case sensitive when running Elixir precompiled on another machine
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix release] Use `Base.encode32` when generating cookie to avoid unsafe chars
|
||||
* [mix release] Fix `install` command on Windows
|
||||
* [mix release] Quote executable path on Windows to ensure it works on directories with spaces
|
||||
|
||||
## v1.9.1 (2019-07-18)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix format] Print relative paths in `--check-formatted` output
|
||||
* [mix release] Support included applications
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] Fix formatter wrongly removing nested parens in nested calls
|
||||
* [Base] Do not raise when finding bad digits in `Base.decode32!` with `case: :mixed`
|
||||
* [Code] Preserve the user's choice when `fn` is followed by a newline and it has only a single clause
|
||||
* [DynamicSupervisor] Properly account for restarting children in the `:max_children` configuration
|
||||
* [String] Add performant impl for string upcase/downcase `:ascii` mode
|
||||
* [Task.Supervisor] Fix type spec for `start_child/4`
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Do not crash translator on poorly formatted supervisor names
|
||||
* [Logger] Do not crash truncation when truncate is set to infinity
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix compile] Raise readable error for mismatched sources during compilation
|
||||
* [mix release] Preserve UTF8 encoding in release config files
|
||||
* [mix format] Match files starting with dot
|
||||
|
||||
## v1.9.0 (2019-06-24)
|
||||
## v1.6.5 (2018-05-07)
|
||||
|
||||
This release supports Erlang/OTP 21.0-rc by removing all warnings and by properly redirecting logger output. Note it is not guaranteed it will support Erlang/OTP 21.0 final.
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] Preserve the user's choice in the formatter on parens call with next break fits
|
||||
* [Code] Preserve the user's choice in the formatter on calls without parens when we have one argument per line
|
||||
* [Code] Fix formatting when there is a tilde in the first element of a bitstring
|
||||
* [Kernel] Support specsdiff flag on `__info__` spec clauses
|
||||
* [Kernel] Do not exclude hygienic vars in `defguard`
|
||||
* [Kernel.SpecialForms] Mark `for` comprehensions as generated to avoid dialyzer warnings
|
||||
* [Macro] Make sure `Macro.to_string/2` emits valid quoted expressions
|
||||
* [Task] Support `:infinity` timeout on `Task.yield_many/2`
|
||||
* [Task.Supervisor] Do not crash spawning supervised tasks when the parent process is dead
|
||||
* [URI] Fix parsing of URIs with trailing `?`
|
||||
|
||||
## v1.6.4 (2018-03-16)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code.Formatter] Do not double escape quoted keyword list identifiers
|
||||
* [Kernel] Properly support `into: binary` in Erlang/OTP 20.3
|
||||
|
||||
## v1.6.3 (2018-03-09)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code.Formatter] Support comments in the middle of pipelines, `when` and `|` expressions
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code.Formatter] Consider commas when breaking groups
|
||||
* [Code.Formatter] Ensure proper precedence between `&` and operators
|
||||
* [Code.Formatter] Consider `.formatter.exs` when formatting stdin
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger.Translator] Ensure logger doesn't crash when reporting named `DynamicSupervisor`
|
||||
|
||||
## v1.6.2 (2018-02-28)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Mix
|
||||
|
||||
* [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
|
||||
|
||||
* [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.Shell] Bring back `Mix.Shell.cmd/2` - this arity was defined via a default argument that was accidentally removed
|
||||
|
||||
## v1.6.1 (2018-01-29)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [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 more complex mixed expressions when tokenizing
|
||||
* [EEx] Allow markers `/` and `|` to be used in a custom EEx engine
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Access] Allow `Access.at/1` to handle negative index
|
||||
* [CLI] Add support for `--boot`, `--boot-var`, `--erl-config`, `--pipe-to`, `--rpc-eval`, and `--vm-args` options
|
||||
* [Code] Add `static_atom_encoder` option to `Code.string_to_quoted/2`
|
||||
* [Code] Support `:force_do_end_blocks` on `Code.format_string!/2` and `Code.format_file!/2`
|
||||
* [Code] Do not raise on deadlocks on `Code.ensure_compiled/1`
|
||||
* [Config] Add `Config`, `Config.Reader`, and `Config.Provider` modules for working with configuration
|
||||
* [File] Add `File.rename!/2`
|
||||
* [Inspect] Add `:inspect_fun` and `:custom_options` to `Inspect.Opts`
|
||||
* [Kernel] Add `~U` sigil for UTC date times
|
||||
* [Kernel] Optimize `&super/arity` and `&super(&1)`
|
||||
* [Kernel] Optimize generated code for `with` with a catch-all clause
|
||||
* [Kernel] Validate `__struct__` key in map returned by `__struct__/0,1`
|
||||
* [Module] Add `Module.get_attribute/3`
|
||||
* [Protocol] Improve `Protocol.UndefinedError` messages to also include the type that was attempted to dispatch on
|
||||
* [Protocol] Optimize performance of dynamic dispatching for non-consolidated protocols
|
||||
* [Record] Include field names in generated type for records
|
||||
* [Regex] Automatically recompile regexes
|
||||
* [Registry] Add `Registry.select/2`
|
||||
* [System] Add `System.restart/0`, `System.pid/0` and `System.no_halt/1`
|
||||
* [System] Add `System.get_env/2`, `System.fetch_env/1`, and `System.fetch_env!/1`
|
||||
* [System] Support `SOURCE_DATE_EPOCH` for reproducible builds
|
||||
* [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] Allow multiple `:exclude` on configuration/CLI
|
||||
* [ExUnit.DocTest] No longer wrap doctest errors in custom exceptions. They ended-up hiding more information than showing
|
||||
* [ExUnit.DocTest] Display the actual doctest code when doctest fails
|
||||
* [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.CLI] Copy ticktime from remote node on IEx `--remsh`
|
||||
* [IEx.CLI] Automatically add a host on node given to `--remsh`
|
||||
* [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] Use a decentralized mode computation for Logger which allows overloads to be detected more quickly
|
||||
* [Logger] Use `persistent_term` to store configuration whenever available for performance
|
||||
* [Logger] Add `:discard_threshold` to Logger to help with message queue overflow
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Follow XDG base dir specification in Mix for temporary and configuration files
|
||||
* [Mix.Generator] Add `copy_file/3`, `copy_template/4`, and `overwite?/2`
|
||||
* [Mix.Project] Add `preferred_cli_target` that works like `preferred_cli_env`
|
||||
* [mix archive.uninstall] Allow `mix archive.uninstall APP` to uninstall any installed version of APP
|
||||
* [mix new] No longer generate a `config/` directory for mix new
|
||||
* [mix release] Add support for releases
|
||||
* [mix release.init] Add templates for release configuration
|
||||
* [mix test] Allow running tests for a given umbrella app from the umbrella root with `mix test apps/APP/test`. Test failures also include the `apps/APP` prefix in the test location
|
||||
* [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
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### EEx
|
||||
#### Elixir
|
||||
|
||||
* [EEx] Consistently trim newlines when you have a single EEx expression per line on multiple lines
|
||||
* [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
|
||||
|
||||
* [Code] Quote `::` in `Code.format_string!/1` to avoid ambiguity
|
||||
* [Code] Do not crash formatter on false positive sigils
|
||||
* [Enum] Ensure the first equal entry is returned by `Enum.min/2` and `Enum.max/2`
|
||||
* [Kernel] Improve error message when string interpolation is used in a guard
|
||||
* [Kernel] Properly merge and handle docs for callbacks with multiple clauses
|
||||
* [Kernel] Guarantee reproducible builds on modules with dozens of specs
|
||||
* [Kernel] Resolve `__MODULE__` accordingly in nested `defmodule` to avoid double nesting
|
||||
* [Kernel] Type variables starting with an underscore (`_foo`) should not raise compile error
|
||||
* [Kernel] Keep order of elements when macro `in/2` is expanded with a literal list on the right-hand side
|
||||
* [Kernel] Print proper location on undefined function error from dynamically generated functions
|
||||
* [Kernel] **Potentially breaking** Do not leak aliases when nesting module definitions that are fully namespaced modules. If you defined `defmodule Elixir.Foo.Bar` inside `defmodule Foo`, previous Elixir versions would automatically define an alias, but fully namespaced modules such as `Elixir.Foo.Bar` should never define or require an alias. If you were accidentally relying on this broken behaviour, your code may no longer work
|
||||
* [System] Make sure `:init.get_status/0` is set to `{:started, :started}` once the system starts
|
||||
* [Path] Do not expand `~` in `Path.expand/2` when not followed by a path separator
|
||||
* [Protocol] Ensure `debug_info` is kept in protocols
|
||||
* [Regex] Ensure inspect returns valid `~r//` expressions when they are manually compiled with backslashes
|
||||
* [Registry] Fix ETS leak in `Registry.register/2` for already registered calls in unique registries while the process is still alive
|
||||
* [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] Raise error if attempting to run single line tests on multiple files
|
||||
* [ExUnit] Return proper error on duplicate child IDs on `start_supervised`
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Automatically shut down IEx if we receive EOF
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Don't discard Logger messages from other nodes as to leave a trail on both systems
|
||||
* [ExUnit.Formatter] `:case_started` and `:case_finished` events are deprecated in favor of `:module_started` and `:module_finished`
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix compile] Ensure Erlang-based Mix compilers (erlang, leex, yecc) set valid position on diagnostics
|
||||
* [mix compile] Ensure compilation halts in an umbrella project if one of the siblings fail to compile
|
||||
* [mix deps] Raise an error if the umbrella app's dir name and `mix.exs` app name don't match
|
||||
* [mix deps.compile] Fix subcommand splitting bug in rebar3
|
||||
* [mix test] Do not consider modules that are no longer cover compiled when computing coverage report, which could lead to flawed reports
|
||||
* [Mix.Compilers.Erlang] Returning `{:ok, val} | :error` from custom Erlang compilers is deprecated in favor of `{:ok, val, warnings} | {:error, errors, warnings}`
|
||||
|
||||
### 3. Soft-deprecations (no warnings emitted)
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix.Config] `Mix.Config` has been deprecated in favor of the `Config` module that now ships as part of Elixir itself. Reading configuration files should now be done by the `Config.Reader` module
|
||||
|
||||
### 4. Hard-deprecations
|
||||
### 4. Deprecations
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [CLI] Deprecate `--detached` option, use `--erl "-detached"` instead
|
||||
* [Map] Deprecate Enumerable keys in `Map.drop/2`, `Map.split/2`, and `Map.take/2`
|
||||
* [String] The `:insert_replaced` option in `String.replace/4` has been deprecated. Instead you may pass a function as a replacement or use `:binary.replace/4` if you need to support earlier Elixir versions
|
||||
* [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 `_ .. _`
|
||||
|
||||
#### Mix
|
||||
## v1.5
|
||||
|
||||
* [Mix.Project] Deprecate `Mix.Project.load_paths/1` in favor of `Mix.Project.compile_path/1`
|
||||
|
||||
## v1.8
|
||||
|
||||
The CHANGELOG for v1.8 releases can be found [in the v1.8 branch](https://github.com/elixir-lang/elixir/blob/v1.8/CHANGELOG.md).
|
||||
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).
|
||||
|
||||
+3
-7
@@ -39,11 +39,11 @@ If you participate in or contribute to the Elixir ecosystem in any way, you are
|
||||
|
||||
Explicit enforcement of the Code of Conduct applies to the official mediums operated by the Elixir project:
|
||||
|
||||
* The [official GitHub projects][1] and code reviews.
|
||||
* The official GitHub projects and code reviews.
|
||||
* The official elixir-lang mailing lists.
|
||||
* The **[#elixir-lang][2]** IRC channel on [Freenode][3].
|
||||
* The #elixir-lang IRC channel on Freenode.
|
||||
|
||||
Other Elixir activities (such as conferences, meetups, and unofficial forums) are encouraged to adopt this Code of Conduct. Such groups must provide their own contact information.
|
||||
Other Elixir activities (such as conferences, meetups, and other unofficial forums) are encouraged to adopt this Code of Conduct. Such groups must provide their own contact information.
|
||||
|
||||
Project maintainers may remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
|
||||
|
||||
@@ -54,7 +54,3 @@ Instances of abusive, harassing, or otherwise unacceptable behavior may be repor
|
||||
## Acknowledgements
|
||||
|
||||
This document was based on the Code of Conduct from the Go project with parts derived from Django's Code of Conduct, Rust's Code of Conduct and the Contributor Covenant.
|
||||
|
||||
[1]: https://github.com/elixir-lang/
|
||||
[2]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[3]: https://www.freenode.net
|
||||
|
||||
+3
-5
@@ -1,14 +1,13 @@
|
||||
### Precheck
|
||||
|
||||
* Do not use the issue 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
|
||||
* 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
|
||||
* 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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
REBAR ?= "$(CURDIR)/rebar"
|
||||
PREFIX ?= /usr/local
|
||||
SHARE_PREFIX ?= $(PREFIX)/share
|
||||
MAN_PREFIX ?= $(SHARE_PREFIX)/man
|
||||
CANONICAL := v1.9/ # master/ or vMAJOR.MINOR/
|
||||
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict $(ELIXIRC_OPTS)
|
||||
ERLC := erlc -I lib/elixir/include $(ERLC_OPTS)
|
||||
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
|
||||
@@ -16,18 +15,16 @@ INSTALL_DATA = $(INSTALL) -m644
|
||||
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) )
|
||||
SOURCE_DATE_EPOCH_PATH = lib/elixir/tmp/ebin_reproducible
|
||||
SOURCE_DATE_EPOCH_FILE = $(SOURCE_DATE_EPOCH_PATH)/SOURCE_DATE_EPOCH
|
||||
|
||||
.PHONY: install compile erlang elixir unicode app build_plt clean_plt dialyze test check_reproducible 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
|
||||
@@ -36,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)"
|
||||
@@ -44,48 +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
|
||||
|
||||
define WRITE_SOURCE_DATE_EPOCH
|
||||
$(shell mkdir -p $(SOURCE_DATE_EPOCH_PATH) && bin/elixir -e \
|
||||
'IO.puts System.build_info()[:date] \
|
||||
|> DateTime.from_iso8601() \
|
||||
|> elem(1) \
|
||||
|> DateTime.to_unix()' > $(SOURCE_DATE_EPOCH_FILE))
|
||||
endef
|
||||
|
||||
define READ_SOURCE_DATE_EPOCH
|
||||
$(strip $(shell cat $(SOURCE_DATE_EPOCH_FILE)))
|
||||
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
|
||||
@@ -93,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/*
|
||||
@@ -127,33 +108,10 @@ install: compile
|
||||
done
|
||||
$(MAKE) install_man
|
||||
|
||||
check_reproducible: compile
|
||||
$(Q) echo "==> Checking for reproducible builds..."
|
||||
$(Q) rm -rf lib/*/tmp/ebin_reproducible/
|
||||
$(call WRITE_SOURCE_DATE_EPOCH)
|
||||
$(Q) mkdir -p lib/elixir/tmp/ebin_reproducible/ \
|
||||
lib/eex/tmp/ebin_reproducible/ \
|
||||
lib/iex/tmp/ebin_reproducible/ \
|
||||
lib/logger/tmp/ebin_reproducible/ \
|
||||
lib/mix/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/elixir/ebin/* lib/elixir/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/eex/ebin/* lib/eex/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/iex/ebin/* lib/iex/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/logger/ebin/* lib/logger/tmp/ebin_reproducible/
|
||||
$(Q) mv lib/mix/ebin/* lib/mix/tmp/ebin_reproducible/
|
||||
SOURCE_DATE_EPOCH=$(call READ_SOURCE_DATE_EPOCH) $(MAKE) compile
|
||||
$(Q) echo "Diffing..."
|
||||
$(Q) diff -r lib/elixir/ebin/ lib/elixir/tmp/ebin_reproducible/
|
||||
$(Q) diff -r lib/eex/ebin/ lib/eex/tmp/ebin_reproducible/
|
||||
$(Q) diff -r lib/iex/ebin/ lib/iex/tmp/ebin_reproducible/
|
||||
$(Q) diff -r lib/logger/ebin/ lib/logger/tmp/ebin_reproducible/
|
||||
$(Q) diff -r lib/mix/ebin/ lib/mix/tmp/ebin_reproducible/
|
||||
$(Q) echo "Builds are reproducible"
|
||||
|
||||
clean:
|
||||
cd lib/elixir && $(REBAR) clean
|
||||
rm -rf ebin
|
||||
rm -rf lib/*/ebin
|
||||
rm -rf $(PARSER)
|
||||
$(Q) $(MAKE) clean_residual_files
|
||||
|
||||
clean_elixir:
|
||||
@@ -175,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
|
||||
|
||||
@@ -226,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
|
||||
|
||||
@@ -249,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;
|
||||
@@ -264,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"; \
|
||||
@@ -288,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
|
||||
|
||||
@@ -313,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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -11,23 +13,7 @@ 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
|
||||
|
||||
https://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.
|
||||
|
||||
== 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
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
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,
|
||||
|
||||
@@ -2,36 +2,15 @@
|
||||
=========
|
||||
[](https://travis-ci.org/elixir-lang/elixir)
|
||||
[](https://ci.appveyor.com/project/josevalim/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/).
|
||||
|
||||
## Policies
|
||||
|
||||
New releases are announced in the [announcement mailing list][8].
|
||||
You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com and replying to the confirmation email.
|
||||
|
||||
All security releases [will be tagged with `[security]`][10]. For more information, please read our [Security Policy][9].
|
||||
|
||||
All interactions in our official communication channels follow our [Code of Conduct][1].
|
||||
|
||||
## Bug reports
|
||||
|
||||
For reporting bugs, [visit our issue tracker][2] and follow the steps
|
||||
for reporting a new issue. **Please disclose security vulnerabilities
|
||||
privately at elixir-security@googlegroups.com**.
|
||||
[check Elixir's website](http://elixir-lang.org/).
|
||||
|
||||
## Compiling from source
|
||||
|
||||
For the many different ways to install Elixir,
|
||||
[see our installation instructions on the website](https://elixir-lang.org/install.html).
|
||||
To compile from source, you can follow the steps below.
|
||||
|
||||
First, [install Erlang](https://elixir-lang.org/install.html#installing-erlang). Then clone this repository to your machine, compile and test it:
|
||||
To run Elixir from source, clone this repository to your machine, compile and test it:
|
||||
|
||||
```sh
|
||||
git clone https://github.com/elixir-lang/elixir.git
|
||||
@@ -47,17 +26,24 @@ If Elixir fails to build (specifically when pulling in a new version via
|
||||
`git`), be sure to remove any previous build artifacts by running
|
||||
`make clean`, then `make test`.
|
||||
|
||||
If tests pass, you can use Interactive Elixir by running `bin/iex` in your terminal.
|
||||
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 that 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.
|
||||
|
||||
## 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.
|
||||
|
||||
## Proposing new features
|
||||
|
||||
For proposing new features, please start a discussion in the
|
||||
@@ -65,14 +51,17 @@ For proposing new features, please start a discussion in the
|
||||
to argue and explain why a feature is useful and how it will impact the
|
||||
codebase and the community.
|
||||
|
||||
Once a proposal is accepted, it will be added to [the issue tracker][2].
|
||||
The issue tracker focuses on *actionable items* and it holds a list of
|
||||
Once a proposal is accepted, it will be added to [the issues tracker][2].
|
||||
The issues tracker focuses on *actionable items* and it holds a list of
|
||||
upcoming enhancements and pending bugs. All entries in the tracker are
|
||||
tagged for clarity and to ease collaboration.
|
||||
|
||||
Features and bug fixes that have already been merged and will be included
|
||||
in the next release are marked as "closed" in the issue tracker and are
|
||||
added to the [changelog][7].
|
||||
in the next release are marked as "closed" in the issues tracker and are
|
||||
added to the [CHANGELOG](CHANGELOG.md).
|
||||
|
||||
Finally, remember all interactions in our official spaces follow our
|
||||
[Code of Conduct][7].
|
||||
|
||||
## Contributing
|
||||
|
||||
@@ -80,24 +69,24 @@ We welcome everyone to contribute to Elixir. To do so, there are a few
|
||||
things you need to know about the code. First, Elixir code is divided
|
||||
in applications inside the `lib` folder:
|
||||
|
||||
* `elixir` - Elixir's kernel and standard library
|
||||
* `elixir` - Contains Elixir's kernel and stdlib
|
||||
|
||||
* `eex` - EEx is the template engine that allows you to embed Elixir
|
||||
* `eex` - Template engine that allows you to embed Elixir
|
||||
|
||||
* `ex_unit` - ExUnit is a simple test framework that ships with Elixir
|
||||
* `ex_unit` - Simple test framework that ships with Elixir
|
||||
|
||||
* `iex` - IEx stands for Interactive Elixir: Elixir's interactive shell
|
||||
* `iex` - IEx, Elixir's interactive shell
|
||||
|
||||
* `logger` - Logger is the built-in logger
|
||||
* `logger` - The built-in logger
|
||||
|
||||
* `mix` - Mix is Elixir's build tool
|
||||
* `mix` - Elixir's build tool
|
||||
|
||||
You can run all tests in the root directory with `make test` and you can
|
||||
also run tests for a specific framework `make test_#{APPLICATION}`, for example,
|
||||
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:
|
||||
|
||||
@@ -112,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:
|
||||
@@ -126,16 +115,16 @@ make clean_elixir compile
|
||||
Similarly, if you can't get Elixir to compile or the tests to pass after
|
||||
updating an existing checkout, run `make clean compile`. You can check
|
||||
[the official build status on Travis-CI](https://travis-ci.org/elixir-lang/elixir).
|
||||
More tasks can be found by reading the [Makefile](Makefile).
|
||||
More tasks can be found by reading the [Makefile](./Makefile).
|
||||
|
||||
With tests running and passing, you are ready to contribute to Elixir and
|
||||
[send a pull request](https://help.github.com/articles/using-pull-requests/).
|
||||
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
|
||||
|
||||
@@ -153,13 +142,12 @@ 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
|
||||
into the repository. If you have carefully organized your commits and
|
||||
believe they should be merged without squashing, please mention it in
|
||||
a comment.
|
||||
believe they should be merged without squashing, leave a comment.
|
||||
|
||||
## Building documentation
|
||||
|
||||
@@ -170,13 +158,7 @@ to be installed and built alongside Elixir:
|
||||
# After cloning and compiling Elixir, in its parent directory:
|
||||
git clone git://github.com/elixir-lang/ex_doc.git
|
||||
cd ex_doc && ../elixir/bin/mix do deps.get, compile
|
||||
```
|
||||
|
||||
Now go back to Elixir's root directory and run:
|
||||
|
||||
```sh
|
||||
make docs # to generate HTML pages
|
||||
make docs DOCS_FORMAT=epub # to generate EPUB documents
|
||||
cd ../elixir && make docs
|
||||
```
|
||||
|
||||
This will produce documentation sets for `elixir`, `mix`, etc. under
|
||||
@@ -185,30 +167,26 @@ the `doc` directory. If you are planning to contribute documentation,
|
||||
|
||||
## Development links
|
||||
|
||||
* [Elixir Getting Started guide][1]
|
||||
* [Elixir Documentation][6]
|
||||
* [Elixir Core Mailing list (development)][3]
|
||||
* [Announcement mailing list][8]
|
||||
* [Code of Conduct][1]
|
||||
* [Issue tracker][2]
|
||||
* [Changelog][7]
|
||||
* [Security Policy][9]
|
||||
* [Issues tracker][2]
|
||||
* [Code of Conduct][7]
|
||||
* **[#elixir-lang][4]** on [Freenode][5] IRC
|
||||
|
||||
[1]: CODE_OF_CONDUCT.md
|
||||
[1]: https://elixir-lang.org/getting-started/introduction.html
|
||||
[2]: https://github.com/elixir-lang/elixir/issues
|
||||
[3]: https://groups.google.com/group/elixir-lang-core
|
||||
[4]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[5]: https://www.freenode.net
|
||||
[6]: https://elixir-lang.org/docs.html
|
||||
[7]: CHANGELOG.md
|
||||
[8]: https://groups.google.com/group/elixir-lang-ann
|
||||
[9]: SECURITY.md
|
||||
[10]: https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date
|
||||
[5]: http://www.freenode.net
|
||||
[6]: http://elixir-lang.org/docs.html
|
||||
[7]: CODE_OF_CONDUCT.md
|
||||
|
||||
## License
|
||||
|
||||
"Elixir" and the Elixir logo are copyright (c) 2012 Plataformatec.
|
||||
|
||||
Elixir source code is released under Apache License 2.0.
|
||||
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
@@ -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 /SECURITY.md and "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
|
||||
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported versions
|
||||
|
||||
Elixir applies bug fixes only to the latest minor branch. Security patches are available for the last 5 minor branches:
|
||||
|
||||
| Elixir version | Support
|
||||
| -------------- | ------------------------------
|
||||
| 1.9 | Bug fixes and security patches
|
||||
| 1.8 | Security patches only
|
||||
| 1.7 | Security patches only
|
||||
| 1.6 | Security patches only
|
||||
| 1.5 | Security patches only
|
||||
|
||||
## Announcements
|
||||
|
||||
New releases are announced in the read-only [announcements mailing list](https://groups.google.com/group/elixir-lang-ann). You can subscribe by sending an email to elixir-lang-ann+subscribe@googlegroups.com and replying to the confirmation email.
|
||||
|
||||
All security releases [will be tagged with `[security]`](https://groups.google.com/forum/#!searchin/elixir-lang-ann/%5Bsecurity%5D%7Csort:date).
|
||||
|
||||
## Reporting a vulnerability
|
||||
|
||||
Please disclose security vulnerabilities privately at elixir-security@googlegroups.com
|
||||
+66
-168
@@ -1,56 +1,31 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo "Usage: $(basename "$0") [options] [.exs file] [data]
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
## General options
|
||||
-e COMMAND Evaluates the given command (*)
|
||||
-r FILE Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in PATH
|
||||
-pr FILE Requires the given files/patterns in parallel (*)
|
||||
-pa PATH Prepends the given path to Erlang code path (*)
|
||||
-pz PATH Appends the given path to Erlang code path (*)
|
||||
|
||||
-e \"COMMAND\" Evaluates the given command (*)
|
||||
-h, --help Prints this message and exits
|
||||
-r \"FILE\" Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in \$PATH
|
||||
-pr \"FILE\" Requires the given files/patterns in parallel (*)
|
||||
-pa \"PATH\" Prepends the given path to Erlang code path (*)
|
||||
-pz \"PATH\" Appends the given path to Erlang code path (*)
|
||||
-v, --version Prints Elixir version and exits
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
--help, -h Prints this message and exits
|
||||
--hidden Makes a hidden node
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
--version, -v Prints Elixir version and exits
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--erl \"SWITCHES\" Switches to be passed down to Erlang (*)
|
||||
--eval \"COMMAND\" Evaluates the given command, same as -e (*)
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
|
||||
Options given after the .exs file or -- are passed down to the executed code.
|
||||
Options can be passed to the Erlang runtime using \$ELIXIR_ERL_OPTIONS or --erl.
|
||||
|
||||
## Distribution options
|
||||
|
||||
The following options are related to node distribution.
|
||||
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--rpc-eval NODE \"COMMAND\" Evaluates the given command on the given remote node (*)
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
|
||||
## Release options
|
||||
|
||||
The following options are generally used under releases.
|
||||
|
||||
--boot \"FILE\" Uses the given FILE.boot to start the system
|
||||
--boot-var VAR \"VALUE\" Makes \$VAR available as VALUE to FILE.boot (*)
|
||||
--erl-config \"FILE\" Loads configuration in FILE.config written in Erlang (*)
|
||||
--pipe-to \"PIPEDIR\" \"LOGDIR\" Starts the Erlang VM as a named PIPEDIR and LOGDIR
|
||||
--vm-args \"FILE\" Passes the contents in file as arguments to the VM
|
||||
|
||||
--pipe-to starts Elixir detached from console (Unix-like only).
|
||||
It will attempt to create PIPEDIR and LOGDIR if they don't exist.
|
||||
See run_erl to learn more. To reattach, run: to_erl PIPEDIR.
|
||||
|
||||
** Options marked with (*) can be given more than once." >&2
|
||||
** Options marked with (*) can be given more than once
|
||||
** Options given after the .exs file or -- are passed down to the executed code
|
||||
** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS or --erl" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -60,142 +35,70 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
# Stores static erlang arguments and --erl (which is passed as is)
|
||||
ERL=""
|
||||
|
||||
# Stores erl arguments preserving spaces/quotes (mimics an array)
|
||||
erl () {
|
||||
eval "E${E}=\$1"
|
||||
E=$((E + 1))
|
||||
}
|
||||
|
||||
# Checks if a string starts with prefix. Usage: starts_with "$STRING" "$PREFIX"
|
||||
starts_with () {
|
||||
case $1 in
|
||||
"$2"*) true;;
|
||||
*) false;;
|
||||
esac
|
||||
}
|
||||
|
||||
ERL_EXEC="erl"
|
||||
MODE="elixir"
|
||||
ERL_EXEC="erl"
|
||||
ERL=""
|
||||
I=1
|
||||
E=0
|
||||
LENGTH=$#
|
||||
set -- "$@" -extra
|
||||
|
||||
while [ $I -le $LENGTH ]; do
|
||||
while [ $I -le $# ]; do
|
||||
S=1
|
||||
case "$1" in
|
||||
eval "PEEK=\${$I}"
|
||||
case "$PEEK" in
|
||||
+iex)
|
||||
set -- "$@" "$1"
|
||||
MODE="iex"
|
||||
;;
|
||||
+elixirc)
|
||||
set -- "$@" "$1"
|
||||
MODE="elixirc"
|
||||
;;
|
||||
-v|--no-halt)
|
||||
set -- "$@" "$1"
|
||||
-v|--compile|--no-halt)
|
||||
;;
|
||||
-e|-r|-pr|-pa|-pz|--app|--eval|--remsh|--dot-iex)
|
||||
-e|-r|-pr|-pa|-pz|--remsh|--app)
|
||||
S=2
|
||||
set -- "$@" "$1" "$2"
|
||||
;;
|
||||
--rpc-eval)
|
||||
S=3
|
||||
set -- "$@" "$1" "$2" "$3"
|
||||
--detached|--hidden)
|
||||
ERL="$ERL `echo $PEEK | cut -c 2-`"
|
||||
;;
|
||||
--detached)
|
||||
echo "warning: the --detached option is deprecated" >&2
|
||||
ERL="$ERL -detached"
|
||||
--cookie)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL -setcookie "$VAL""
|
||||
;;
|
||||
--hidden)
|
||||
ERL="$ERL -hidden"
|
||||
--sname|--name)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL `echo $PEEK | cut -c 2-` "$VAL""
|
||||
;;
|
||||
--logger-otp-reports)
|
||||
S=2
|
||||
if [ "$2" = 'true' ] || [ "$2" = 'false' ]; then
|
||||
ERL="$ERL -logger handle_otp_reports $2"
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
if [ "$VAL" = 'true' ] || [ "$VAL" = 'false' ]; then
|
||||
ERL="$ERL -logger handle_otp_reports "$VAL""
|
||||
fi
|
||||
;;
|
||||
--logger-sasl-reports)
|
||||
S=2
|
||||
if [ "$2" = 'true' ] || [ "$2" = 'false' ]; then
|
||||
ERL="$ERL -logger handle_sasl_reports $2"
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
if [ "$VAL" = 'true' ] || [ "$VAL" = 'false' ]; then
|
||||
ERL="$ERL -logger handle_sasl_reports "$VAL""
|
||||
fi
|
||||
;;
|
||||
--erl)
|
||||
S=2
|
||||
ERL="$ERL $2"
|
||||
;;
|
||||
--cookie)
|
||||
S=2
|
||||
erl "-setcookie"
|
||||
erl "$2"
|
||||
;;
|
||||
--sname|--name)
|
||||
S=2
|
||||
erl "$(echo "$1" | cut -c 2-)"
|
||||
erl "$2"
|
||||
;;
|
||||
--erl-config)
|
||||
S=2
|
||||
erl "-config"
|
||||
erl "$2"
|
||||
;;
|
||||
--vm-args)
|
||||
S=2
|
||||
erl "-args_file"
|
||||
erl "$2"
|
||||
;;
|
||||
--boot)
|
||||
S=2
|
||||
erl "-boot"
|
||||
erl "$2"
|
||||
;;
|
||||
--boot-var)
|
||||
S=3
|
||||
erl "-boot_var"
|
||||
erl "$2"
|
||||
erl "$3"
|
||||
;;
|
||||
--pipe-to)
|
||||
S=3
|
||||
RUN_ERL_PIPE="$2"
|
||||
RUN_ERL_LOG="$3"
|
||||
if [ "$(starts_with "$RUN_ERL_PIPE" "-")" ]; then
|
||||
echo "--pipe-to : PIPEDIR cannot be a switch" >&2 && exit 1
|
||||
elif [ "$(starts_with "$RUN_ERL_LOG" "-")" ]; then
|
||||
echo "--pipe-to : LOGDIR cannot be a switch" >&2 && exit 1
|
||||
fi
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL "$VAL""
|
||||
;;
|
||||
--werl)
|
||||
if [ "$OS" = "Windows_NT" ]; then ERL_EXEC="werl"; fi
|
||||
USE_WERL=true
|
||||
;;
|
||||
*)
|
||||
while [ $I -le $LENGTH ]; do
|
||||
I=$((I + 1))
|
||||
set -- "$@" "$1"
|
||||
shift
|
||||
done
|
||||
break
|
||||
;;
|
||||
esac
|
||||
|
||||
I=$((I + S))
|
||||
shift $S
|
||||
done
|
||||
|
||||
I=$((E - 1))
|
||||
while [ $I -ge 0 ]; do
|
||||
eval "VAL=\$E$I"
|
||||
set -- "$VAL" "$@"
|
||||
I=$((I - 1))
|
||||
I=$(expr $I + $S)
|
||||
done
|
||||
|
||||
SELF=$(readlink_f "$0")
|
||||
@@ -204,26 +107,21 @@ SCRIPT_PATH=$(dirname "$SELF")
|
||||
if [ "$OSTYPE" = "cygwin" ]; then SCRIPT_PATH=$(cygpath -m "$SCRIPT_PATH"); fi
|
||||
if [ "$MODE" != "iex" ]; then ERL="-noshell -s elixir start_cli $ERL"; fi
|
||||
|
||||
# Check for terminal support
|
||||
if [ "$OS" != "Windows_NT" ]; then
|
||||
if test -t 1 -a -t 2; then ERL="-elixir ansi_enabled true $ERL"; fi
|
||||
fi
|
||||
|
||||
ERTS_BIN=
|
||||
set -- "$ERTS_BIN$ERL_EXEC" -pa "$SCRIPT_PATH"/../lib/*/ebin $ELIXIR_ERL_OPTIONS $ERL "$@"
|
||||
|
||||
if [ -n "$RUN_ERL_PIPE" ]; then
|
||||
ESCAPED=""
|
||||
for PART in "$@"; do
|
||||
ESCAPED="$ESCAPED $(echo "$PART" | sed 's/[^a-zA-Z0-9_\-\/]/\\&/g')"
|
||||
done
|
||||
mkdir -p "$RUN_ERL_PIPE"
|
||||
mkdir -p "$RUN_ERL_LOG"
|
||||
ERL_EXEC="run_erl"
|
||||
set -- "$ERTS_BIN$ERL_EXEC" -daemon "$RUN_ERL_PIPE/" "$RUN_ERL_LOG/" "$ESCAPED"
|
||||
if [ "$OS" = "Windows_NT" ] && [ $USE_WERL ]; then
|
||||
ERL_EXEC="werl"
|
||||
fi
|
||||
|
||||
if [ -n "$ELIXIR_CLI_DRY_RUN" ]; then
|
||||
echo "$@"
|
||||
else
|
||||
exec "$@"
|
||||
fi
|
||||
if [ -z "$ERL_PATH" ]; then
|
||||
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 "$@"
|
||||
|
||||
+68
-118
@@ -1,5 +1,5 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
setlocal enabledelayedexpansion
|
||||
setlocal
|
||||
if ""%1""=="""" goto documentation
|
||||
if /I ""%1""==""--help"" goto documentation
|
||||
if /I ""%1""==""-h"" goto documentation
|
||||
@@ -10,155 +10,105 @@ goto parseopts
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo ## General options
|
||||
echo -e COMMAND Evaluates the given command (*)
|
||||
echo -r FILE Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in PATH
|
||||
echo -pr FILE Requires the given files/patterns in parallel (*)
|
||||
echo -pa PATH Prepends the given path to Erlang code path (*)
|
||||
echo -pz PATH Appends the given path to Erlang code path (*)
|
||||
echo.
|
||||
echo -e "COMMAND" Evaluates the given command (*)
|
||||
echo -h, --help Prints this message and exits
|
||||
echo -r "FILE" Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in $PATH
|
||||
echo -pr "FILE" Requires the given files/patterns in parallel (*)
|
||||
echo -pa "PATH" Prepends the given path to Erlang code path (*)
|
||||
echo -pz "PATH" Appends the given path to Erlang code path (*)
|
||||
echo -v, --version Prints Elixir version and exits
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
echo --help, -h Prints this message and exits
|
||||
echo --hidden Makes a hidden node
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo --version, -v Prints Elixir version and exits
|
||||
echo --werl Uses Erlang's Windows shell GUI
|
||||
echo.
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --erl "SWITCHES" Switches to be passed down to Erlang (*)
|
||||
echo --eval "COMMAND" Evaluates the given command, same as -e (*)
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo.
|
||||
echo Options given after the .exs file or -- are passed down to the executed code.
|
||||
echo Options can be passed to the Erlang runtime using $ELIXIR_ERL_OPTIONS or --erl.
|
||||
echo.
|
||||
echo ## Distribution options
|
||||
echo.
|
||||
echo The following options are related to node distribution.
|
||||
echo.
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --hidden Makes a hidden node
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --rpc-eval NODE "COMMAND" Evaluates the given command on the given remote node (*)
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo.
|
||||
echo ## Release options
|
||||
echo.
|
||||
echo The following options are generally used under releases.
|
||||
echo.
|
||||
echo --boot "FILE" Uses the given FILE.boot to start the system
|
||||
echo --boot-var VAR "VALUE" Makes $VAR available as VALUE to FILE.boot (*)
|
||||
echo --erl-config "FILE" Loads configuration in FILE.config written in Erlang (*)
|
||||
echo --vm-args "FILE" Passes the contents in file as arguments to the VM
|
||||
echo.
|
||||
echo --pipe-to is not supported on Windows. If set, Elixir won't boot.
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once.
|
||||
echo ** Options marked with (*) can be given more than once
|
||||
echo ** Options given after the .exs file or -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS or --erl
|
||||
goto end
|
||||
|
||||
:parseopts
|
||||
|
||||
rem Parameters for Elixir
|
||||
set parsElixir=
|
||||
|
||||
rem Parameters for Erlang
|
||||
set parsErlang=
|
||||
|
||||
rem Make sure we keep a copy of all parameters
|
||||
set allPars=%*
|
||||
|
||||
rem Get the original path name from the batch file
|
||||
set originPath=%~dp0
|
||||
|
||||
rem Optional parameters before the "-extra" parameter
|
||||
set beforeExtra=
|
||||
|
||||
rem Option which determines whether the loop is over
|
||||
set endLoop=0
|
||||
rem Flag which determines whether or not to use werl vs erl
|
||||
set useWerl=0
|
||||
|
||||
rem Designates which mode / Elixir component to run as
|
||||
set runMode="elixir"
|
||||
|
||||
rem Designates the path to the current script
|
||||
set SCRIPT_PATH=%~dp0
|
||||
|
||||
rem Designates the path to the ERTS system
|
||||
set ERTS_BIN=
|
||||
|
||||
rem Recursive loop called for each parameter that parses the cmd line parameters
|
||||
:startloop
|
||||
set "par=%~1"
|
||||
if "!par!"=="" (
|
||||
rem skip if no parameter
|
||||
set par="%1"
|
||||
shift
|
||||
if "%par%"=="" (
|
||||
rem if no parameters defined
|
||||
goto expand_erl_libs
|
||||
)
|
||||
shift
|
||||
set par="!par:"=\"!"
|
||||
if !endLoop! == 1 (
|
||||
set parsElixir=!parsElixir! !par!
|
||||
goto startloop
|
||||
if "%par%"=="""" (
|
||||
rem if no parameters defined - special case for parameter that is already quoted
|
||||
goto expand_erl_libs
|
||||
)
|
||||
rem ******* EXECUTION OPTIONS **********************
|
||||
if !par!=="--werl" (set useWerl=1 && goto startloop)
|
||||
if !par!=="+iex" (set parsElixir=!parsElixir! +iex && set runMode="iex" && goto startloop)
|
||||
if !par!=="+elixirc" (set parsElixir=!parsElixir! +elixirc && set runMode="elixirc" && goto startloop)
|
||||
rem ******* EVAL PARAMETERS ************************
|
||||
if ""==!par:-e=! (
|
||||
set "VAR=%~1"
|
||||
set parsElixir=!parsElixir! -e "!VAR:"=\"!"
|
||||
shift
|
||||
goto startloop
|
||||
)
|
||||
if ""==!par:--eval=! (
|
||||
set "VAR=%~1"
|
||||
set parsElixir=!parsElixir! --eval "!VAR:"=\"!"
|
||||
shift
|
||||
goto startloop
|
||||
)
|
||||
if ""==!par:--rpc-eval=! (
|
||||
set "VAR=%~2"
|
||||
set parsElixir=!parsElixir! --rpc-eval %1 "!VAR:"=\"!"
|
||||
shift
|
||||
shift
|
||||
goto startloop
|
||||
)
|
||||
if "%par%"==""--werl"" (set useWerl=1)
|
||||
if "%par%"==""+iex"" (set runMode="iex")
|
||||
rem ******* ELIXIR PARAMETERS **********************
|
||||
if ""==!par:-r=! (set "parsElixir=!parsElixir! -r %1" && shift && goto startloop)
|
||||
if ""==!par:-pr=! (set "parsElixir=!parsElixir! -pr %1" && shift && goto startloop)
|
||||
if ""==!par:-pa=! (set "parsElixir=!parsElixir! -pa %1" && shift && goto startloop)
|
||||
if ""==!par:-pz=! (set "parsElixir=!parsElixir! -pz %1" && shift && goto startloop)
|
||||
if ""==!par:-v=! (set "parsElixir=!parsElixir! -v" && goto startloop)
|
||||
if ""==!par:--app=! (set "parsElixir=!parsElixir! --app %1" && shift && goto startloop)
|
||||
if ""==!par:--no-halt=! (set "parsElixir=!parsElixir! --no-halt" && goto startloop)
|
||||
if ""==!par:--remsh=! (set "parsElixir=!parsElixir! --remsh %1" && shift && goto startloop)
|
||||
if ""==!par:--dot-iex=! (set "parsElixir=!parsElixir! --dot-iex %1" && shift && goto startloop)
|
||||
rem Note: we don't have to do anything with options that don't take an argument
|
||||
if """"=="%par:-e=%" (shift)
|
||||
if """"=="%par:-r=%" (shift)
|
||||
if """"=="%par:-pr=%" (shift)
|
||||
if """"=="%par:-pa=%" (shift)
|
||||
if """"=="%par:-pz=%" (shift)
|
||||
if """"=="%par:--app=%" (shift)
|
||||
if """"=="%par:--remsh=%" (shift)
|
||||
rem ******* ERLANG PARAMETERS **********************
|
||||
if ""==!par:--boot=! (set "parsErlang=!parsErlang! -boot %1" && shift && goto startloop)
|
||||
if ""==!par:--boot-var=! (set "parsErlang=!parsErlang! -boot_var %1 %2" && shift && shift && goto startloop)
|
||||
if ""==!par:--cookie=! (set "parsErlang=!parsErlang! -setcookie %1" && shift && goto startloop)
|
||||
if ""==!par:--hidden=! (set "parsErlang=!parsErlang! -hidden" && goto startloop)
|
||||
if ""==!par:--detached=! (set "parsErlang=!parsErlang! -detached" && echo warning: the --detached option is deprecated && goto startloop)
|
||||
if ""==!par:--erl-config=! (set "parsErlang=!parsErlang! -config %1" && shift && goto startloop)
|
||||
if ""==!par:--logger-otp-reports=! (set "parsErlang=!parsErlang! -logger handle_otp_reports %1" && shift && goto startloop)
|
||||
if ""==!par:--logger-sasl-reports=! (set "parsErlang=!parsErlang! -logger handle_sasl_reports %1" && shift && goto startloop)
|
||||
if ""==!par:--name=! (set "parsErlang=!parsErlang! -name %1" && shift && goto startloop)
|
||||
if ""==!par:--sname=! (set "parsErlang=!parsErlang! -sname %1" && shift && goto startloop)
|
||||
if ""==!par:--vm-args=! (set "parsErlang=!parsErlang! -args_file %1" && shift && goto startloop)
|
||||
if ""==!par:--erl=! (set "beforeExtra=!beforeExtra! %~1" && shift && goto startloop)
|
||||
if ""==!par:--pipe-to=! (echo --pipe-to : Option is not supported on Windows && goto end)
|
||||
set endLoop=1
|
||||
set parsElixir=!parsElixir! !par!
|
||||
goto startloop
|
||||
if """"=="%par:--detached=%" (set parsErlang=%parsErlang% -detached)
|
||||
if """"=="%par:--hidden=%" (set parsErlang=%parsErlang% -hidden)
|
||||
if """"=="%par:--cookie=%" (set parsErlang=%parsErlang% -setcookie %1 && shift)
|
||||
if """"=="%par:--sname=%" (set parsErlang=%parsErlang% -sname %1 && shift)
|
||||
if """"=="%par:--name=%" (set parsErlang=%parsErlang% -name %1 && shift)
|
||||
if """"=="%par:--logger-otp-reports=%" (set parsErlang=%parsErlang% -logger handle_otp_reports %1 && shift)
|
||||
if """"=="%par:--logger-sasl-reports=%" (set parsErlang=%parsErlang% -logger handle_sasl_reports %1 && shift)
|
||||
if """"=="%par:--erl=%" (set "beforeExtra=%beforeExtra% %~1" && shift)
|
||||
goto:startloop
|
||||
|
||||
rem ******* assume all pre-params are parsed ********************
|
||||
:expand_erl_libs
|
||||
rem expand all ebin paths as Windows does not support the ..\*\ebin wildcard
|
||||
rem ******* expand all ebin paths as Windows does not support the ..\*\ebin wildcard ********************
|
||||
setlocal enabledelayedexpansion
|
||||
set ext_libs=
|
||||
for /d %%d in ("!SCRIPT_PATH!..\lib\*.") do (
|
||||
for /d %%d in ("%originPath%..\lib\*.") do (
|
||||
set ext_libs=!ext_libs! -pa "%%~fd\ebin"
|
||||
)
|
||||
setlocal disabledelayedexpansion
|
||||
|
||||
:run
|
||||
if not !runMode! == "iex" (
|
||||
set beforeExtra=-noshell -s elixir start_cli !beforeExtra!
|
||||
if not %runMode% == "iex" (
|
||||
set beforeExtra=-noshell -s elixir start_cli %beforeExtra%
|
||||
)
|
||||
if defined useWerl (
|
||||
start "" "!ERTS_BIN!werl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
|
||||
if %useWerl% equ 1 (
|
||||
start werl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
) else (
|
||||
"!ERTS_BIN!erl.exe" !ext_libs! !ELIXIR_ERL_OPTIONS! !parsErlang! !beforeExtra! -extra !parsElixir!
|
||||
erl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
)
|
||||
:end
|
||||
endlocal
|
||||
endlocal
|
||||
|
||||
+7
-9
@@ -1,22 +1,20 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo "Usage: $(basename "$0") [elixir switches] [compiler switches] [.ex files]
|
||||
echo "Usage: `basename $0` [elixir switches] [compiler switches] [.ex files]
|
||||
|
||||
-h, --help Prints this message and exits
|
||||
-o The directory to output compiled files
|
||||
-v, --version Prints Elixir version and exits
|
||||
|
||||
--help, -h Prints this message and exits
|
||||
--ignore-module-conflict Does not emit warnings if a module was previously defined
|
||||
--no-debug-info Does not attach debug info to compiled modules
|
||||
--no-docs Does not attach documentation to compiled modules
|
||||
--verbose Prints compilation status
|
||||
--version, -v Prints Elixir version and exits
|
||||
--warnings-as-errors Treats warnings as errors and return non-zero exit code
|
||||
|
||||
Options given after -- are passed down to the executed code.
|
||||
Options can be passed to the Erlang runtime using \$ELIXIR_ERL_OPTIONS.
|
||||
Options can be passed to the Erlang compiler using \$ERL_COMPILER_OPTIONS." >&2
|
||||
** Options given after -- are passed down to the executed code
|
||||
** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -26,7 +24,7 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,35 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
if [ $# -gt 0 ] && ([ "$1" = "--help" ] || [ "$1" = "-h" ]); then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo "Usage: $(basename "$0") [options] [.exs file] [data]
|
||||
-e COMMAND Evaluates the given command (*)
|
||||
-r FILE Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in PATH
|
||||
-pr FILE Requires the given files/patterns in parallel (*)
|
||||
-pa PATH Prepends the given path to Erlang code path (*)
|
||||
-pz PATH Appends the given path to Erlang code path (*)
|
||||
|
||||
The following options are exclusive to IEx:
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
--help, -h Prints this message and exits
|
||||
--hidden Makes a hidden node
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
--version, -v Prints IEx version and exits
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
|
||||
--dot-iex \"PATH\" Overrides default .iex.exs file and uses path instead;
|
||||
path can be empty, then no file will be loaded
|
||||
--remsh NAME Connects to a node using a remote shell
|
||||
--dot-iex PATH Overrides default .iex.exs file and uses path instead;
|
||||
path can be empty, then no file will be loaded
|
||||
--remsh NAME Connects to a node using a remote shell
|
||||
|
||||
It accepts all other options listed by \"elixir --help\"." >&2
|
||||
** Options marked with (*) can be given more than once
|
||||
** Options given after the .exs file or -- are passed down to the executed code
|
||||
** Options can be passed to the VM using ELIXIR_ERL_OPTIONS or --erl" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -20,7 +39,7 @@ readlink_f () {
|
||||
if [ -h "$filename" ]; then
|
||||
readlink_f "$(readlink "$filename")"
|
||||
else
|
||||
echo "$(pwd -P)/$filename"
|
||||
echo "`pwd -P`/$filename"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
+28
-9
@@ -1,4 +1,4 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
setlocal
|
||||
if /I ""%1""==""--help"" goto documentation
|
||||
if /I ""%1""==""-h"" goto documentation
|
||||
@@ -9,19 +9,38 @@ goto run
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo The following options are exclusive to IEx:
|
||||
echo -e COMMAND Evaluates the given command (*)
|
||||
echo -r FILE Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in PATH
|
||||
echo -pr FILE Requires the given files/patterns in parallel (*)
|
||||
echo -pa PATH Prepends the given path to Erlang code path (*)
|
||||
echo -pz PATH Appends the given path to Erlang code path (*)
|
||||
echo.
|
||||
echo --dot-iex "PATH" Overrides default .iex.exs file and uses path instead;
|
||||
echo path can be empty, then no file will be loaded
|
||||
echo --remsh NAME Connects to a node using a remote shell
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
echo --help, -h Prints this message and exits
|
||||
echo --hidden Makes a hidden node
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo --version, -v Prints IEx version and exits
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo.
|
||||
echo Set the IEX_WITH_WERL environment variable to always use werl.
|
||||
echo It accepts all other options listed by "elixir --help".
|
||||
echo --dot-iex PATH Overrides default .iex.exs file and uses path instead;
|
||||
echo path can be empty, then no file will be loaded
|
||||
echo --remsh NAME Connects to a node using a remote shell
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once
|
||||
echo ** Options given after the .exs file or -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang VM using ELIXIR_ERL_OPTIONS or --erl
|
||||
goto end
|
||||
|
||||
:run
|
||||
if defined IEX_WITH_WERL (@set __ELIXIR_IEX_FLAGS=--werl) else (set __ELIXIR_IEX_FLAGS=)
|
||||
@if defined IEX_WITH_WERL (@set __ELIXIR_IEX_FLAGS=--werl) else (set __ELIXIR_IEX_FLAGS=)
|
||||
call "%~dp0\elixir.bat" --no-halt --erl "-noshell -user Elixir.IEx.CLI" +iex %__ELIXIR_IEX_FLAGS% %*
|
||||
:end
|
||||
endlocal
|
||||
|
||||
+10
-14
@@ -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
|
||||
@@ -43,8 +42,7 @@ defmodule EEx do
|
||||
* `:file` - the file to be used in the template. Defaults to the given
|
||||
file the template is read from or to "nofile" when compiling from a string.
|
||||
* `:engine` - the EEx engine to be used for compilation.
|
||||
* `:trim` - trims whitespace left/right of quotation tags. If a quotation
|
||||
tag appears on its own in a given line, line endings are also removed.
|
||||
* `:trim` - trims whitespace left/right of quotation tags
|
||||
|
||||
## Engine
|
||||
|
||||
@@ -85,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:
|
||||
@@ -106,7 +104,7 @@ defmodule EEx do
|
||||
|
||||
iex> defmodule Sample do
|
||||
...> require EEx
|
||||
...> EEx.function_from_string(:def, :sample, "<%= a + b %>", [:a, :b])
|
||||
...> EEx.function_from_string :def, :sample, "<%= a + b %>", [:a, :b]
|
||||
...> end
|
||||
iex> Sample.sample(1, 2)
|
||||
"3"
|
||||
@@ -142,12 +140,11 @@ defmodule EEx do
|
||||
# sample.ex
|
||||
defmodule Sample do
|
||||
require EEx
|
||||
EEx.function_from_file(:def, :sample, "sample.eex", [:a, :b])
|
||||
EEx.function_from_file :def, :sample, "sample.eex", [:a, :b]
|
||||
end
|
||||
|
||||
# iex
|
||||
Sample.sample(1, 2)
|
||||
#=> "3"
|
||||
Sample.sample(1, 2) #=> "3"
|
||||
|
||||
"""
|
||||
defmacro function_from_file(kind, name, file, args \\ [], options \\ []) do
|
||||
@@ -169,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
|
||||
@@ -178,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)
|
||||
@@ -189,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"
|
||||
|
||||
"""
|
||||
@@ -209,8 +206,7 @@ defmodule EEx do
|
||||
foo <%= bar %>
|
||||
|
||||
# iex
|
||||
EEx.eval_file("sample.eex", bar: "baz")
|
||||
#=> "foo baz"
|
||||
EEx.eval_file "sample.eex", [bar: "baz"] #=> "foo baz"
|
||||
|
||||
"""
|
||||
@spec eval_file(String.t(), keyword, keyword) :: any
|
||||
|
||||
+25
-35
@@ -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
|
||||
@@ -41,40 +41,35 @@ defmodule EEx.Compiler do
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:expr, line, mark, chars, _} | rest], buffer, scope, state) do
|
||||
defp generate_buffer([{:expr, line, mark, chars} | rest], buffer, scope, state) do
|
||||
expr = Code.string_to_quoted!(chars, line: line, file: state.file)
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), expr)
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:start_expr, start_line, mark, chars, _} | rest], buffer, scope, state) do
|
||||
{contents, line, rest} = look_ahead_middle(rest, start_line, chars)
|
||||
defp generate_buffer([{:start_expr, start_line, mark, chars} | rest], buffer, scope, state) do
|
||||
{contents, line, rest} = look_ahead_text(rest, start_line, chars)
|
||||
|
||||
{contents, rest} =
|
||||
generate_buffer(
|
||||
rest,
|
||||
state.engine.handle_begin(buffer),
|
||||
[contents | scope],
|
||||
%{state | quoted: [], line: line, start_line: start_line}
|
||||
)
|
||||
generate_buffer(rest, state.engine.handle_begin(buffer), [contents | scope], %{
|
||||
state
|
||||
| quoted: [],
|
||||
line: line,
|
||||
start_line: start_line
|
||||
})
|
||||
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), contents)
|
||||
generate_buffer(rest, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:middle_expr, line, '', chars, _} | rest],
|
||||
buffer,
|
||||
[current | scope],
|
||||
state
|
||||
) do
|
||||
defp generate_buffer([{:middle_expr, line, '', chars} | rest], buffer, [current | scope], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
state = %{state | line: line}
|
||||
generate_buffer(rest, state.engine.handle_begin(buffer), [wrapped | scope], state)
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:middle_expr, line, modifier, chars, trimmed?} | t],
|
||||
[{:middle_expr, line, modifier, chars} | t],
|
||||
buffer,
|
||||
[_ | _] = scope,
|
||||
state
|
||||
@@ -83,43 +78,38 @@ defmodule EEx.Compiler do
|
||||
"unexpected beginning of EEx tag \"<%#{modifier}\" on \"<%#{modifier}#{chars}%>\", " <>
|
||||
"please remove \"#{modifier}\" accordingly"
|
||||
|
||||
:elixir_errors.erl_warn(line, state.file, message)
|
||||
generate_buffer([{:middle_expr, line, '', chars, trimmed?} | t], buffer, scope, state)
|
||||
:elixir_errors.warn(line, state.file, message)
|
||||
generate_buffer([{:middle_expr, line, '', chars} | t], buffer, scope, state)
|
||||
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
|
||||
# raise EEx.SyntaxError, message: message, file: state.file, line: line
|
||||
end
|
||||
|
||||
defp generate_buffer([{:middle_expr, line, _, chars, _} | _], _buffer, [], state) do
|
||||
defp generate_buffer([{:middle_expr, line, _, chars} | _], _buffer, [], state) do
|
||||
raise EEx.SyntaxError,
|
||||
message: "unexpected middle of expression <%#{chars}%>",
|
||||
file: state.file,
|
||||
line: line
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, '', chars, _} | rest], buffer, [current | _], state) do
|
||||
defp generate_buffer([{:end_expr, line, '', chars} | rest], buffer, [current | _], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
tuples = Code.string_to_quoted!(wrapped, line: state.start_line, file: state.file)
|
||||
buffer = insert_quoted(tuples, state.quoted)
|
||||
{buffer, rest}
|
||||
end
|
||||
|
||||
defp generate_buffer(
|
||||
[{:end_expr, line, modifier, chars, trimmed?} | t],
|
||||
buffer,
|
||||
[_ | _] = scope,
|
||||
state
|
||||
) do
|
||||
defp generate_buffer([{:end_expr, line, modifier, chars} | t], buffer, [_ | _] = scope, state) do
|
||||
message =
|
||||
"unexpected beginning of EEx tag \"<%#{modifier}\" on end of " <>
|
||||
"expression \"<%#{modifier}#{chars}%>\", please remove \"#{modifier}\" accordingly"
|
||||
|
||||
:elixir_errors.erl_warn(line, state.file, message)
|
||||
generate_buffer([{:end_expr, line, '', chars, trimmed?} | t], buffer, scope, state)
|
||||
:elixir_errors.warn(line, state.file, message)
|
||||
generate_buffer([{:end_expr, line, '', chars} | t], buffer, scope, state)
|
||||
# TODO: Make this an error on Elixir v2.0 since it accidentally worked previously.
|
||||
# raise EEx.SyntaxError, message: message, file: state.file, line: line
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, _, chars, _} | _], _buffer, [], state) do
|
||||
defp generate_buffer([{:end_expr, line, _, chars} | _], _buffer, [], state) do
|
||||
raise EEx.SyntaxError,
|
||||
message: "unexpected end of expression <%#{chars}%>",
|
||||
file: state.file,
|
||||
@@ -149,10 +139,10 @@ defmodule EEx.Compiler do
|
||||
{count, new_state}
|
||||
end
|
||||
|
||||
# Look middle expressions that immediatelly follow a start_expr
|
||||
# Look text ahead on expressions
|
||||
|
||||
defp look_ahead_middle(
|
||||
[{:text, text}, {:middle_expr, line, _, chars, _} | rest] = tokens,
|
||||
defp look_ahead_text(
|
||||
[{:text, text}, {:middle_expr, line, _, chars} | rest] = tokens,
|
||||
start,
|
||||
contents
|
||||
) do
|
||||
@@ -163,11 +153,11 @@ defmodule EEx.Compiler do
|
||||
end
|
||||
end
|
||||
|
||||
defp look_ahead_middle([{:middle_expr, line, _, chars, _} | rest], _start, contents) do
|
||||
defp look_ahead_text([{:middle_expr, line, _, chars} | rest], _start, contents) do
|
||||
{contents ++ chars, line, rest}
|
||||
end
|
||||
|
||||
defp look_ahead_middle(tokens, start, contents) do
|
||||
defp look_ahead_text(tokens, start, contents) do
|
||||
{contents, start, tokens}
|
||||
end
|
||||
|
||||
|
||||
+98
-115
@@ -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
|
||||
|
||||
"""
|
||||
@@ -127,7 +115,7 @@ defmodule EEx.Engine do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Raise on v2.0
|
||||
# TODO: Raise on 2.0
|
||||
@spec fetch_assign!(Access.t(), Access.key()) :: term | nil
|
||||
def fetch_assign!(assigns, key) do
|
||||
case Access.fetch(assigns, key) do
|
||||
@@ -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
|
||||
|
||||
@@ -24,12 +24,11 @@ defmodule EEx.SmartEngine do
|
||||
# sample.ex
|
||||
defmodule Sample do
|
||||
require EEx
|
||||
EEx.function_from_file(:def, :sample, "sample.eex", [:assigns])
|
||||
EEx.function_from_file :def, :sample, "sample.eex", [:assigns]
|
||||
end
|
||||
|
||||
# iex
|
||||
Sample.sample(a: 1, b: 2)
|
||||
#=> "3"
|
||||
Sample.sample(a: 1, b: 2) #=> "3"
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@@ -4,13 +4,9 @@ defmodule EEx.Tokenizer do
|
||||
@type content :: IO.chardata()
|
||||
@type line :: non_neg_integer
|
||||
@type marker :: '=' | '/' | '|' | ''
|
||||
@type trimmed? :: boolean
|
||||
@type token ::
|
||||
{:text, content}
|
||||
| {:expr | :start_expr | :middle_expr | :end_expr, line, marker, content, trimmed?}
|
||||
|
||||
@spaces [?\s, ?\t]
|
||||
@closing_brackets ')]}'
|
||||
| {:expr | :start_expr | :middle_expr | :end_expr, line, marker, content}
|
||||
|
||||
@doc """
|
||||
Tokenizes the given charlist or binary.
|
||||
@@ -18,10 +14,10 @@ defmodule EEx.Tokenizer do
|
||||
It returns {:ok, list} with the following tokens:
|
||||
|
||||
* `{:text, content}`
|
||||
* `{:expr, line, marker, content, trimmed?}`
|
||||
* `{:start_expr, line, marker, content, trimmed?}`
|
||||
* `{:middle_expr, line, marker, content, trimmed?}`
|
||||
* `{:end_expr, line, marker, content, trimmed?}`
|
||||
* `{:expr, line, marker, content}`
|
||||
* `{:start_expr, line, marker, content}`
|
||||
* `{:middle_expr, line, marker, content}`
|
||||
* `{:end_expr, line, marker, content}`
|
||||
|
||||
Or `{:error, line, error}` in case of errors.
|
||||
"""
|
||||
@@ -48,7 +44,7 @@ defmodule EEx.Tokenizer do
|
||||
error
|
||||
|
||||
{:ok, _, new_line, rest} ->
|
||||
{_, rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
{rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
tokenize(rest, new_line, opts, buffer, acc)
|
||||
end
|
||||
end
|
||||
@@ -62,9 +58,9 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
{:ok, expr, new_line, rest} ->
|
||||
token = token_name(expr)
|
||||
{trimmed?, rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
{rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
acc = tokenize_text(buffer, acc)
|
||||
final = {token, line, marker, Enum.reverse(expr), trimmed?}
|
||||
final = {token, line, marker, Enum.reverse(expr)}
|
||||
tokenize(rest, new_line, opts, [], [final | acc])
|
||||
end
|
||||
end
|
||||
@@ -114,28 +110,25 @@ defmodule EEx.Tokenizer do
|
||||
#
|
||||
# Start tokens finish with "do" and "fn ->"
|
||||
# Middle tokens are marked with "->" or keywords
|
||||
# End tokens contain only the end word and optionally
|
||||
# combinations of ")", "]" and "}".
|
||||
# End tokens contain only the end word and optionally ")"
|
||||
|
||||
defp token_name([h | t]) when h in @spaces do
|
||||
defp token_name([h | t]) when h in [?\s, ?\t, ?)] do
|
||||
token_name(t)
|
||||
end
|
||||
|
||||
defp token_name('od' ++ [h | rest]) when h in @spaces or h in @closing_brackets do
|
||||
case tokenize_rest(rest) do
|
||||
{:ok, [{:end, _} | _]} -> :middle_expr
|
||||
_ -> :start_expr
|
||||
end
|
||||
defp token_name('od' ++ [h | _]) when h in [?\s, ?\t, ?)] do
|
||||
:start_expr
|
||||
end
|
||||
|
||||
defp token_name('>-' ++ rest) do
|
||||
case tokenize_rest(rest) do
|
||||
{:ok, [{:end, _} | _]} ->
|
||||
:middle_expr
|
||||
rest = Enum.reverse(rest)
|
||||
|
||||
# Check if there is a "fn" token and, if so, it is not
|
||||
# followed by an "end" token. If this is the case, we
|
||||
# are on a start expr.
|
||||
# Tokenize the remaining passing check_terminators as
|
||||
# false, which relax the tokenizer to not error on
|
||||
# unmatched pairs. Then, we check if there is a "fn"
|
||||
# token and, if so, it is not followed by an "end"
|
||||
# token. If this is the case, we are on a start expr.
|
||||
case :elixir_tokenizer.tokenize(rest, 1, file: "eex", check_terminators: false) do
|
||||
{:ok, tokens} ->
|
||||
tokens = Enum.reverse(tokens)
|
||||
fn_index = fn_index(tokens)
|
||||
@@ -155,19 +148,10 @@ defmodule EEx.Tokenizer do
|
||||
defp token_name('retfa' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('hctac' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('eucser' ++ t), do: check_spaces(t, :middle_expr)
|
||||
defp token_name('dne' ++ t), do: check_spaces(t, :end_expr)
|
||||
|
||||
defp token_name(rest) do
|
||||
case Enum.drop_while(rest, &(&1 in @spaces or &1 in @closing_brackets)) do
|
||||
'dne' ++ t -> check_spaces(t, :end_expr)
|
||||
_ -> :expr
|
||||
end
|
||||
end
|
||||
|
||||
# Tokenize the remaining passing check_terminators as false,
|
||||
# which relax the tokenizer to not error on unmatched pairs.
|
||||
# If the tokens start with an "end" we have a middle expr.
|
||||
defp tokenize_rest(rest) do
|
||||
:elixir_tokenizer.tokenize(Enum.reverse(rest), 1, file: "eex", check_terminators: false)
|
||||
defp token_name(_) do
|
||||
:expr
|
||||
end
|
||||
|
||||
defp fn_index(tokens) do
|
||||
@@ -183,7 +167,7 @@ defmodule EEx.Tokenizer do
|
||||
end
|
||||
|
||||
defp check_spaces(string, token) do
|
||||
if Enum.all?(string, &(&1 in @spaces)) do
|
||||
if Enum.all?(string, &(&1 in [?\s, ?\t])) do
|
||||
token
|
||||
else
|
||||
:expr
|
||||
@@ -205,19 +189,24 @@ defmodule EEx.Tokenizer do
|
||||
# only itself and whitespace, trim the whitespace around it,
|
||||
# including the line break following it if there is one.
|
||||
defp trim_if_needed(rest, line, opts, buffer, acc) do
|
||||
with true <- opts[:trim],
|
||||
{true, new_buffer} <- trim_left(buffer, acc),
|
||||
{true, new_rest, new_line} <- trim_right(rest, line) do
|
||||
{true, new_rest, new_line, new_buffer}
|
||||
original = {rest, line, buffer}
|
||||
|
||||
if opts[:trim] do
|
||||
case {trim_left(buffer, acc), trim_right(rest, line)} do
|
||||
{{true, new_buffer}, {true, new_rest, new_line}} ->
|
||||
{new_rest, new_line, new_buffer}
|
||||
|
||||
_ ->
|
||||
original
|
||||
end
|
||||
else
|
||||
_ -> {false, rest, line, buffer}
|
||||
original
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_left(buffer, acc) do
|
||||
case {trim_whitespace(buffer), acc} do
|
||||
{[?\n | _] = trimmed_buffer, _} -> {true, trimmed_buffer}
|
||||
{[], [{_, _, _, _, true} | _]} -> {true, []}
|
||||
{[], []} -> {true, []}
|
||||
_ -> {false, buffer}
|
||||
end
|
||||
@@ -232,7 +221,7 @@ defmodule EEx.Tokenizer do
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_whitespace([h | t]) when h in @spaces do
|
||||
defp trim_whitespace([h | t]) when h == ?\s or h == ?\t do
|
||||
trim_whitespace(t)
|
||||
end
|
||||
|
||||
|
||||
@@ -29,19 +29,16 @@ defmodule EEx.SmartEngineTest do
|
||||
assert_eval("1\n2\n3\n", "<%= for x <- [1, 2, 3] do %><%= x %>\n<% end %>")
|
||||
end
|
||||
|
||||
test "preserves line numbers in assignments" do
|
||||
result = EEx.compile_string("foo\n<%= @hello %>", engine: EEx.SmartEngine)
|
||||
test "preserves line numbers" do
|
||||
result = EEx.compile_string("<%= @hello %>", engine: EEx.SmartEngine)
|
||||
|
||||
Macro.prewalk(result, fn
|
||||
{_left, meta, [_, :hello]} ->
|
||||
assert Keyword.get(meta, :line) == 2
|
||||
send(self(), :found)
|
||||
{_left, meta, _right} ->
|
||||
assert Keyword.get(meta, :line, 0) in [0, 1]
|
||||
|
||||
node ->
|
||||
node
|
||||
_ ->
|
||||
:ok
|
||||
end)
|
||||
|
||||
assert_received :found
|
||||
end
|
||||
|
||||
defp assert_eval(expected, actual, binding \\ []) do
|
||||
|
||||
@@ -13,28 +13,23 @@ defmodule EEx.TokenizerTest do
|
||||
end
|
||||
|
||||
test "strings with embedded code" do
|
||||
assert T.tokenize('foo <% bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '', ' bar ', false}]}
|
||||
assert T.tokenize('foo <% bar %>', 1) == {:ok, [{:text, 'foo '}, {:expr, 1, '', ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with embedded equals code" do
|
||||
assert T.tokenize('foo <%= bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '=', ' bar ', false}]}
|
||||
assert T.tokenize('foo <%= bar %>', 1) == {:ok, [{:text, 'foo '}, {:expr, 1, '=', ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with embedded slash code" do
|
||||
assert T.tokenize('foo <%/ bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '/', ' bar ', false}]}
|
||||
assert T.tokenize('foo <%/ bar %>', 1) == {:ok, [{:text, 'foo '}, {:expr, 1, '/', ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with embedded pipe code" do
|
||||
assert T.tokenize('foo <%| bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '|', ' bar ', false}]}
|
||||
assert T.tokenize('foo <%| bar %>', 1) == {:ok, [{:text, 'foo '}, {:expr, 1, '|', ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with more than one line" do
|
||||
assert T.tokenize('foo\n<%= bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo\n'}, {:expr, 2, '=', ' bar ', false}]}
|
||||
assert T.tokenize('foo\n<%= bar %>', 1) == {:ok, [{:text, 'foo\n'}, {:expr, 2, '=', ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with more than one line and expression with more than one line" do
|
||||
@@ -47,9 +42,9 @@ defmodule EEx.TokenizerTest do
|
||||
|
||||
exprs = [
|
||||
{:text, 'foo '},
|
||||
{:expr, 1, '=', ' bar\n\nbaz ', false},
|
||||
{:expr, 1, '=', ' bar\n\nbaz '},
|
||||
{:text, '\n'},
|
||||
{:expr, 4, '', ' foo ', false},
|
||||
{:expr, 4, '', ' foo '},
|
||||
{:text, '\n'}
|
||||
]
|
||||
|
||||
@@ -68,9 +63,9 @@ defmodule EEx.TokenizerTest do
|
||||
test "quotation with interpolation" do
|
||||
exprs = [
|
||||
{:text, 'a <% b '},
|
||||
{:expr, 1, '=', ' c ', false},
|
||||
{:expr, 1, '=', ' c '},
|
||||
{:text, ' '},
|
||||
{:expr, 1, '=', ' d ', false},
|
||||
{:expr, 1, '=', ' d '},
|
||||
{:text, ' e %> f'}
|
||||
]
|
||||
|
||||
@@ -104,9 +99,9 @@ defmodule EEx.TokenizerTest do
|
||||
test "strings with embedded do end" do
|
||||
exprs = [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' if true do ', false},
|
||||
{:start_expr, 1, '', ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:end_expr, 1, '', ' end ', false}
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <% if true do %>bar<% end %>', 1) == {:ok, exprs}
|
||||
@@ -115,50 +110,26 @@ defmodule EEx.TokenizerTest do
|
||||
test "strings with embedded -> end" do
|
||||
exprs = [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' cond do ', false},
|
||||
{:middle_expr, 1, '', ' false -> ', false},
|
||||
{:start_expr, 1, '', ' cond do '},
|
||||
{:middle_expr, 1, '', ' false -> '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, '', ' true -> ', false},
|
||||
{:middle_expr, 1, '', ' true -> '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, '', ' end ', false}
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1) ==
|
||||
{:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with multiple callbacks" do
|
||||
exprs = [
|
||||
{:start_expr, 1, '=', ' a fn -> ', false},
|
||||
{:text, 'foo'},
|
||||
{:middle_expr, 1, '', ' end, fn -> ', false},
|
||||
{:text, 'bar'},
|
||||
{:end_expr, 1, '', ' end ', false}
|
||||
]
|
||||
|
||||
assert T.tokenize('<%= a fn -> %>foo<% end, fn -> %>bar<% end %>', 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with callback followed by do block" do
|
||||
exprs = [
|
||||
{:start_expr, 1, '=', ' a fn -> ', false},
|
||||
{:text, 'foo'},
|
||||
{:middle_expr, 1, '', ' end do ', false},
|
||||
{:text, 'bar'},
|
||||
{:end_expr, 1, '', ' end ', false}
|
||||
]
|
||||
|
||||
assert T.tokenize('<%= a fn -> %>foo<% end do %>bar<% end %>', 1) == {:ok, exprs}
|
||||
end
|
||||
|
||||
test "strings with embedded keywords blocks" do
|
||||
exprs = [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' if true do ', false},
|
||||
{:start_expr, 1, '', ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, '', ' else ', false},
|
||||
{:middle_expr, 1, '', ' else '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, '', ' end ', false}
|
||||
{:end_expr, 1, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1) == {:ok, exprs}
|
||||
@@ -168,11 +139,11 @@ defmodule EEx.TokenizerTest do
|
||||
template = '\t<%= if true do %> \n TRUE \n <% else %>\n FALSE \n <% end %> '
|
||||
|
||||
exprs = [
|
||||
{:start_expr, 1, '=', ' if true do ', true},
|
||||
{:start_expr, 1, '=', ' if true do '},
|
||||
{:text, ' TRUE \n'},
|
||||
{:middle_expr, 3, '', ' else ', true},
|
||||
{:middle_expr, 3, '', ' else '},
|
||||
{:text, ' FALSE \n'},
|
||||
{:end_expr, 5, '', ' end ', true}
|
||||
{:end_expr, 5, '', ' end '}
|
||||
]
|
||||
|
||||
assert T.tokenize(template, 1, trim: true) == {:ok, exprs}
|
||||
@@ -189,7 +160,7 @@ defmodule EEx.TokenizerTest do
|
||||
test "trim mode with CRLF" do
|
||||
exprs = [
|
||||
{:text, '0\r\n'},
|
||||
{:expr, 2, '=', ' 12 ', true},
|
||||
{:expr, 2, '=', ' 12 '},
|
||||
{:text, '34'}
|
||||
]
|
||||
|
||||
@@ -199,7 +170,7 @@ defmodule EEx.TokenizerTest do
|
||||
test "trim mode set to false" do
|
||||
exprs = [
|
||||
{:text, ' '},
|
||||
{:expr, 1, '=', ' 12 ', false},
|
||||
{:expr, 1, '=', ' 12 '},
|
||||
{:text, ' \n'}
|
||||
]
|
||||
|
||||
|
||||
+15
-133
@@ -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
|
||||
@@ -84,18 +87,6 @@ defmodule EExTest do
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
end
|
||||
|
||||
test "trim mode with multiple lines" do
|
||||
string = """
|
||||
<%= "First line" %>
|
||||
<%= "Second line" %>
|
||||
<%= "Third line" %>
|
||||
<%= "Fourth line" %>
|
||||
"""
|
||||
|
||||
expected = "First lineSecond lineThird lineFourth line"
|
||||
assert_eval(expected, string, [], trim: true)
|
||||
end
|
||||
|
||||
test "embedded code" do
|
||||
assert_eval("foo bar", "foo <%= :bar %>")
|
||||
end
|
||||
@@ -112,12 +103,6 @@ defmodule EExTest do
|
||||
assert_eval("foo ", "foo <%= if false do %>bar<% end %>")
|
||||
end
|
||||
|
||||
test "embedded code with do preceeded by bracket" do
|
||||
assert_eval("foo bar", "foo <%= if {true}do %>bar<% end %>")
|
||||
assert_eval("foo bar", "foo <%= if (true)do %>bar<% end %>")
|
||||
assert_eval("foo bar", "foo <%= if [true]do %>bar<% end %>")
|
||||
end
|
||||
|
||||
test "embedded code with do end and expression" do
|
||||
assert_eval("foo bar", "foo <%= if true do %><%= :bar %><% end %>")
|
||||
end
|
||||
@@ -150,27 +135,7 @@ defmodule EExTest do
|
||||
)
|
||||
end
|
||||
|
||||
test "embedded code with end followed by bracket" do
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= Enum.map([1, 2, 3], fn x -> %> <%= 100 + x %> <% end) %>"
|
||||
)
|
||||
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= apply Enum, :map, [[1, 2, 3], fn x -> %> <%= 100 + x %> <% end] %>"
|
||||
)
|
||||
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= #{__MODULE__}.tuple_map {[1, 2, 3], fn x -> %> <%= 100 + x %> <% end} %>"
|
||||
)
|
||||
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= apply(Enum, :map, [[1, 2, 3], fn x -> %> <%= 100 + x %> <% end]) %>"
|
||||
)
|
||||
|
||||
test "embedded code with parentheses after end in end token" do
|
||||
assert_eval(
|
||||
" 101 102 103 ",
|
||||
"<%= Enum.map([1, 2, 3], (fn x -> %> <%= 100 + x %> <% end) ) %>"
|
||||
@@ -255,20 +220,6 @@ defmodule EExTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "error messages" do
|
||||
test "honor line numbers" do
|
||||
assert_raise EEx.SyntaxError, "nofile:99: missing token '%>'", fn ->
|
||||
EEx.compile_string("foo <%= bar", line: 99)
|
||||
end
|
||||
end
|
||||
|
||||
test "honor file names" do
|
||||
assert_raise EEx.SyntaxError, "my_file.eex:1: missing token '%>'", fn ->
|
||||
EEx.compile_string("foo <%= bar", file: "my_file.eex")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "environment" do
|
||||
test "respects line numbers" do
|
||||
expected = """
|
||||
@@ -394,52 +345,6 @@ defmodule EExTest do
|
||||
assert_eval(expected, string)
|
||||
end
|
||||
|
||||
test "inside multiple functions" do
|
||||
expected = """
|
||||
|
||||
A 1
|
||||
|
||||
B 2
|
||||
|
||||
A 3
|
||||
|
||||
"""
|
||||
|
||||
string = """
|
||||
<%= #{__MODULE__}.switching_map [1, 2, 3], fn x -> %>
|
||||
A <%= x %>
|
||||
<% end, fn x -> %>
|
||||
B <%= x %>
|
||||
<% end %>
|
||||
"""
|
||||
|
||||
assert_eval(expected, string)
|
||||
end
|
||||
|
||||
test "inside callback and do block" do
|
||||
expected = """
|
||||
|
||||
|
||||
A 1
|
||||
|
||||
B 2
|
||||
|
||||
A 3
|
||||
|
||||
"""
|
||||
|
||||
string = """
|
||||
<% require #{__MODULE__} %>
|
||||
<%= #{__MODULE__}.switching_macro [1, 2, 3], fn x -> %>
|
||||
A <%= x %>
|
||||
<% end do %>
|
||||
B <%= x %>
|
||||
<% end %>
|
||||
"""
|
||||
|
||||
assert_eval(expected, string)
|
||||
end
|
||||
|
||||
test "inside cond" do
|
||||
expected = """
|
||||
foo
|
||||
@@ -542,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
|
||||
|
||||
@@ -599,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
|
||||
@@ -617,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
|
||||
@@ -625,27 +530,4 @@ defmodule EExTest do
|
||||
defp assert_normalized_newline_equal(expected, actual) do
|
||||
assert String.replace(expected, "\r\n", "\n") == String.replace(actual, "\r\n", "\n")
|
||||
end
|
||||
|
||||
def tuple_map({list, callback}) do
|
||||
Enum.map(list, callback)
|
||||
end
|
||||
|
||||
def switching_map(list, a, b) do
|
||||
list
|
||||
|> Enum.with_index()
|
||||
|> Enum.map(fn
|
||||
{element, index} when rem(index, 2) == 0 -> a.(element)
|
||||
{element, index} when rem(index, 2) == 1 -> b.(element)
|
||||
end)
|
||||
end
|
||||
|
||||
defmacro switching_macro(list, a, do: block) do
|
||||
quote do
|
||||
b = fn var!(x) ->
|
||||
unquote(block)
|
||||
end
|
||||
|
||||
unquote(__MODULE__).switching_map(unquote(list), unquote(a), b)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+21
-28
@@ -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,13 +8,13 @@
|
||||
Atom,
|
||||
Base,
|
||||
Bitwise,
|
||||
Calendar,
|
||||
Calendar.ISO,
|
||||
Date,
|
||||
DateTime,
|
||||
Exception,
|
||||
Float,
|
||||
Function,
|
||||
Integer,
|
||||
Module,
|
||||
NaiveDateTime,
|
||||
Record,
|
||||
Regex,
|
||||
@@ -27,8 +23,8 @@
|
||||
Tuple,
|
||||
URI,
|
||||
Version,
|
||||
Version.Requirement
|
||||
],
|
||||
|
||||
"Collections & Enumerables": [
|
||||
Access,
|
||||
Date.Range,
|
||||
@@ -38,8 +34,9 @@
|
||||
Map,
|
||||
MapSet,
|
||||
Range,
|
||||
Stream
|
||||
Stream,
|
||||
],
|
||||
|
||||
"IO & System": [
|
||||
File,
|
||||
File.Stat,
|
||||
@@ -51,20 +48,20 @@
|
||||
Path,
|
||||
Port,
|
||||
StringIO,
|
||||
System
|
||||
System,
|
||||
],
|
||||
"Calendar": [
|
||||
Calendar,
|
||||
Calendar.ISO,
|
||||
Calendar.TimeZoneDatabase,
|
||||
Calendar.UTCOnlyTimeZoneDatabase
|
||||
|
||||
"Modules & Code": [
|
||||
Code,
|
||||
Kernel.ParallelCompiler,
|
||||
Macro,
|
||||
Macro.Env,
|
||||
Module,
|
||||
],
|
||||
|
||||
"Processes & Applications": [
|
||||
Agent,
|
||||
Application,
|
||||
Config,
|
||||
Config.Provider,
|
||||
Config.Reader,
|
||||
DynamicSupervisor,
|
||||
GenServer,
|
||||
Node,
|
||||
@@ -72,9 +69,10 @@
|
||||
Registry,
|
||||
Supervisor,
|
||||
Task,
|
||||
Task.Supervisor
|
||||
Task.Supervisor,
|
||||
],
|
||||
Protocols: [
|
||||
|
||||
"Protocols": [
|
||||
Collectable,
|
||||
Enumerable,
|
||||
Inspect,
|
||||
@@ -82,15 +80,10 @@
|
||||
Inspect.Opts,
|
||||
List.Chars,
|
||||
Protocol,
|
||||
String.Chars
|
||||
String.Chars,
|
||||
],
|
||||
"Code & Macros": [
|
||||
Code,
|
||||
Kernel.ParallelCompiler,
|
||||
Macro,
|
||||
Macro.Env
|
||||
],
|
||||
Deprecated: [
|
||||
|
||||
"Deprecated": [
|
||||
Behaviour,
|
||||
Dict,
|
||||
GenEvent,
|
||||
@@ -98,6 +91,6 @@
|
||||
HashSet,
|
||||
Set,
|
||||
Supervisor.Spec
|
||||
]
|
||||
],
|
||||
]
|
||||
]
|
||||
|
||||
@@ -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]).
|
||||
+158
-133
@@ -1,19 +1,27 @@
|
||||
defmodule Access do
|
||||
@moduledoc """
|
||||
Key-based access to data structures.
|
||||
Key-based access to data structures using the `data[key]` syntax.
|
||||
|
||||
The `Access` module defines a behaviour for dynamically accessing
|
||||
keys of any type in a data structure via the `data[key]` syntax.
|
||||
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.
|
||||
|
||||
`Access` supports keyword lists (`Keyword`) and maps (`Map`) out
|
||||
of the box. The key can be of any type and it returns `nil` if
|
||||
the key does not exist:
|
||||
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.
|
||||
|
||||
## Dynamic lookups
|
||||
|
||||
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]
|
||||
@@ -23,74 +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
|
||||
%{"john" => %{age: 28}, "meg" => %{age: 23}}
|
||||
|
||||
Furthermore, `Access` transparently ignores `nil` values:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:c][:unknown]
|
||||
nil
|
||||
|
||||
This works because accessing anything on a `nil` value, returns
|
||||
`nil` itself:
|
||||
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.
|
||||
|
||||
iex> nil[:a]
|
||||
nil
|
||||
## Static lookups
|
||||
|
||||
The access syntax can also be used with the `Kernel.put_in/2`,
|
||||
`Kernel.update_in/2` and `Kernel.get_and_update_in/2` macros
|
||||
to allow values to be set in nested data structures:
|
||||
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).
|
||||
|
||||
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
|
||||
iex> put_in(users["john"][:age], 28)
|
||||
%{"john" => %{age: 28}, "meg" => %{age: 23}}
|
||||
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:
|
||||
|
||||
> Attention! While the access syntax is allowed in maps via
|
||||
> `map[key]`, if your map is made of predefined atom keys,
|
||||
> you should prefer to access those atom keys with `map.key`
|
||||
> instead of `map[key]`, as `map.key` will raise if the key
|
||||
> is missing. This is important because, if a map has a predefined
|
||||
> set of keys and a key is missing, it is most likely a bug
|
||||
> in your software or a typo on the key name. For this reason,
|
||||
> because structs are predefined in nature, they only allow
|
||||
> the `struct.key` syntax and they do not allow the `struct[key]`
|
||||
> access syntax. See the `Map` module for more information.
|
||||
user = %User{name: "John"}
|
||||
user[:name]
|
||||
# ** (UndefinedFunctionError) undefined function User.fetch/2 (User does not implement the Access behaviour)
|
||||
|
||||
## Nested data structures
|
||||
Structs instead use the `user.name` syntax to access fields:
|
||||
|
||||
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`.
|
||||
user.name
|
||||
#=> "John"
|
||||
|
||||
For example, to update a map inside another map:
|
||||
The same `user.name` syntax can also be used by `Kernel.put_in/2`
|
||||
for updating structs fields:
|
||||
|
||||
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
|
||||
iex> put_in(users["john"].age, 28)
|
||||
%{"john" => %{age: 28}, "meg" => %{age: 23}}
|
||||
put_in user.name, "Mary"
|
||||
#=> %User{name: "Mary"}
|
||||
|
||||
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`.
|
||||
Differently from `user[:name]`, `user.name` is not extensible via
|
||||
a behaviour and is restricted only to structs and atom keys in maps.
|
||||
|
||||
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:
|
||||
As mentioned above, this works for atom keys in maps as well. Refer to the
|
||||
`Map` module for more information on this.
|
||||
|
||||
Summing up:
|
||||
|
||||
* `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
|
||||
|
||||
## Accessors
|
||||
|
||||
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 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
|
||||
|
||||
See the functions `key/1`, `key!/1`, `elem/1`, and `all/0` for
|
||||
some of the available accessors.
|
||||
"""
|
||||
|
||||
@type container :: keyword | struct | map
|
||||
@@ -130,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.
|
||||
|
||||
@@ -139,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`
|
||||
@@ -172,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}
|
||||
@@ -182,7 +247,7 @@ defmodule Access do
|
||||
unquote(exception)
|
||||
end
|
||||
|
||||
reraise exception, __STACKTRACE__
|
||||
reraise exception, stacktrace
|
||||
end
|
||||
end
|
||||
|
||||
@@ -192,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
|
||||
@@ -242,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)
|
||||
@@ -395,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:
|
||||
@@ -409,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)])
|
||||
@@ -447,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)])
|
||||
@@ -496,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)])
|
||||
@@ -518,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
|
||||
@@ -549,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])
|
||||
@@ -560,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]}
|
||||
|
||||
@@ -610,16 +648,10 @@ defmodule Access do
|
||||
iex> list = [%{name: "john"}, %{name: "mary"}]
|
||||
iex> get_in(list, [Access.at(1), :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"}]}
|
||||
iex> get_and_update_in(list, [Access.at(-1), :name], fn prev ->
|
||||
...> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"mary", [%{name: "john"}, %{name: "MARY"}]}
|
||||
|
||||
`at/1` can also be used to pop elements out of a list or
|
||||
a key inside of a list:
|
||||
@@ -635,19 +667,24 @@ 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"}]}
|
||||
|
||||
An error is raised for negative indexes:
|
||||
|
||||
iex> get_in([], [Access.at(-1)])
|
||||
** (FunctionClauseError) no function clause matching in Access.at/1
|
||||
|
||||
An error is raised if the accessed structure is not a list:
|
||||
|
||||
iex> get_in(%{}, [Access.at(1)])
|
||||
** (RuntimeError) Access.at/1 expected a list, got: %{}
|
||||
|
||||
"""
|
||||
@spec at(integer) :: access_fun(data :: list, get_value :: term)
|
||||
def at(index) when is_integer(index) do
|
||||
@spec at(non_neg_integer) :: access_fun(data :: list, get_value :: term)
|
||||
def at(index) when is_integer(index) and index >= 0 do
|
||||
fn op, data, next -> at(op, data, index, next) end
|
||||
end
|
||||
|
||||
@@ -670,17 +707,7 @@ defmodule Access do
|
||||
end
|
||||
end
|
||||
|
||||
defp get_and_update_at(list, index, next, updates) when index < 0 do
|
||||
list_length = length(list)
|
||||
|
||||
if list_length + index >= 0 do
|
||||
get_and_update_at(list, list_length + index, next, updates)
|
||||
else
|
||||
{nil, list}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_and_update_at([head | rest], index, next, updates) when index > 0 do
|
||||
defp get_and_update_at([head | rest], index, next, updates) do
|
||||
get_and_update_at(rest, index - 1, next, [head | updates])
|
||||
end
|
||||
|
||||
@@ -696,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}]}
|
||||
|
||||
@@ -718,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}]}
|
||||
|
||||
@@ -732,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
|
||||
|
||||
+45
-94
@@ -11,50 +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)
|
||||
#=> {:ok, #PID<0.123.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:
|
||||
|
||||
@@ -65,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
|
||||
@@ -79,60 +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
|
||||
|
||||
@@ -188,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,
|
||||
@@ -201,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__,
|
||||
@@ -227,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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+117
-291
@@ -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 operating system
|
||||
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
|
||||
@@ -333,6 +247,12 @@ defmodule Application do
|
||||
end
|
||||
end
|
||||
|
||||
@type app :: atom
|
||||
@type key :: atom
|
||||
@type value :: term
|
||||
@type state :: term
|
||||
@type start_type :: :permanent | :transient | :temporary
|
||||
|
||||
@application_keys [
|
||||
:description,
|
||||
:id,
|
||||
@@ -347,16 +267,6 @@ defmodule Application do
|
||||
:start_phases
|
||||
]
|
||||
|
||||
application_key_specs = Enum.reduce(@application_keys, &{:|, [], [&1, &2]})
|
||||
|
||||
@type app :: atom
|
||||
@type key :: atom
|
||||
@type application_key :: unquote(application_key_specs)
|
||||
@type value :: term
|
||||
@type state :: term
|
||||
@type start_type :: :normal | {:takeover, node} | {:failover, node}
|
||||
@type restart_type :: :permanent | :transient | :temporary
|
||||
|
||||
@doc """
|
||||
Returns the spec for `app`.
|
||||
|
||||
@@ -367,8 +277,8 @@ defmodule Application do
|
||||
Note the environment is not returned as it can be accessed via
|
||||
`fetch_env/2`. Returns `nil` if the application is not loaded.
|
||||
"""
|
||||
@spec spec(app) :: [{application_key, value}] | nil
|
||||
def spec(app) when is_atom(app) do
|
||||
@spec spec(app) :: [{key, value}] | nil
|
||||
def spec(app) do
|
||||
case :application.get_all_key(app) do
|
||||
{:ok, info} -> :lists.keydelete(:env, 1, info)
|
||||
:undefined -> nil
|
||||
@@ -382,8 +292,8 @@ defmodule Application do
|
||||
specification parameter does not exist, this function
|
||||
will raise. Returns `nil` if the application is not loaded.
|
||||
"""
|
||||
@spec spec(app, application_key) :: value | nil
|
||||
def spec(app, key) when is_atom(app) and key in @application_keys do
|
||||
@spec spec(app, key) :: value | nil
|
||||
def spec(app, key) when key in @application_keys do
|
||||
case :application.get_key(app, key) do
|
||||
{:ok, value} -> value
|
||||
:undefined -> nil
|
||||
@@ -409,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
|
||||
|
||||
@@ -418,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
|
||||
|
||||
@@ -465,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
|
||||
@@ -477,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
|
||||
@@ -516,54 +391,23 @@ 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
|
||||
|
||||
@doc """
|
||||
Puts the environment for multiple apps at the same time.
|
||||
|
||||
The given config should not:
|
||||
|
||||
* have the same application listed more than once
|
||||
* have the same key inside the same application listed more than once
|
||||
|
||||
If those conditions are not met, the behaviour is undefined
|
||||
(on Erlang/OTP 21 and earlier) or will raise (on Erlang/OTP 22
|
||||
and later).
|
||||
|
||||
It receives the same options as `put_env/4`. Returns `:ok`.
|
||||
"""
|
||||
@spec put_all_env([{app, [{key, value}]}], timeout: timeout, persistent: boolean) :: :ok
|
||||
def put_all_env(config, opts \\ []) when is_list(config) and is_list(opts) do
|
||||
# TODO: Remove function exported? check when we require Erlang/OTP 22+
|
||||
if function_exported?(:application, :set_env, 2) do
|
||||
:application.set_env(config, opts)
|
||||
else
|
||||
for app_keyword <- config,
|
||||
{app, keyword} = app_keyword,
|
||||
key_value <- keyword,
|
||||
{key, value} = key_value do
|
||||
:application.set_env(app, key, value, opts)
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the `key` from the given `app` environment.
|
||||
|
||||
It receives the same options as `put_env/4`. Returns `:ok`.
|
||||
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
|
||||
|
||||
@@ -577,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
|
||||
@@ -589,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
|
||||
@@ -628,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
|
||||
@@ -639,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
|
||||
|
||||
@@ -702,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
|
||||
@@ -741,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
|
||||
|
||||
@@ -37,8 +37,9 @@ defmodule Atom do
|
||||
:erlang.atom_to_list(atom)
|
||||
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
-20
@@ -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.
|
||||
"""
|
||||
@@ -28,7 +24,7 @@ defmodule Behaviour do
|
||||
do_defcallback(:defmacro, split_spec(spec, quote(do: Macro.t())))
|
||||
end
|
||||
|
||||
defp split_spec({:when, _, [{:"::", _, [spec, return]}, guard]}, _default) do
|
||||
defp split_spec({:when, _, [{:::, _, [spec, return]}, guard]}, _default) do
|
||||
{spec, return, guard}
|
||||
end
|
||||
|
||||
@@ -36,7 +32,7 @@ defmodule Behaviour do
|
||||
{spec, default, guard}
|
||||
end
|
||||
|
||||
defp split_spec({:"::", _, [spec, return]}, _default) do
|
||||
defp split_spec({:::, _, [spec, return]}, _default) do
|
||||
{spec, return, []}
|
||||
end
|
||||
|
||||
@@ -56,7 +52,7 @@ defmodule Behaviour do
|
||||
|
||||
defp do_callback(kind, name, args, return, guards) do
|
||||
fun = fn
|
||||
{:"::", _, [left, right]} ->
|
||||
{:::, _, [left, right]} ->
|
||||
ensure_not_default(left)
|
||||
ensure_not_default(right)
|
||||
left
|
||||
@@ -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
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
+25
-192
@@ -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,17 +302,9 @@ 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
|
||||
Calendar.ISO.date_to_string(year, month, day, format)
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: _} = date, format) when format in [:basic, :extended] do
|
||||
date
|
||||
|> convert!(Calendar.ISO)
|
||||
|> to_iso8601()
|
||||
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
|
||||
|
||||
@doc """
|
||||
@@ -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,119 +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
|
||||
|
||||
@@ -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
|
||||
|
||||
+178
-579
File diff suppressed because it is too large
Load Diff
+72
-466
@@ -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,8 +154,8 @@ defmodule Calendar.ISO do
|
||||
719_528
|
||||
end
|
||||
|
||||
def date_to_iso_days(year, month, day) when year in -9999..9999 do
|
||||
ensure_day_in_month!(year, month, day)
|
||||
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 -
|
||||
1
|
||||
@@ -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,11 +193,8 @@ defmodule Calendar.ISO do
|
||||
29
|
||||
iex> Calendar.ISO.days_in_month(2004, 4)
|
||||
30
|
||||
iex> Calendar.ISO.days_in_month(-1, 5)
|
||||
31
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec days_in_month(year, month) :: 28..31
|
||||
@impl true
|
||||
def days_in_month(year, month)
|
||||
@@ -272,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.
|
||||
|
||||
@@ -301,15 +219,12 @@ defmodule Calendar.ISO do
|
||||
true
|
||||
iex> Calendar.ISO.leap_year?(1900)
|
||||
false
|
||||
iex> Calendar.ISO.leap_year?(-4)
|
||||
true
|
||||
|
||||
"""
|
||||
@doc since: "1.3.0"
|
||||
@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 """
|
||||
@@ -321,185 +236,39 @@ 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
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@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
|
||||
ensure_day_in_month!(year, month, day)
|
||||
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.
|
||||
|
||||
By default, returns times formatted in the "extended" format,
|
||||
for human readability. It also supports the "basic" format
|
||||
by passing the `:basic` option.
|
||||
|
||||
## 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"
|
||||
|
||||
iex> Calendar.ISO.time_to_string(2, 2, 2, {2, 6}, :basic)
|
||||
"020202.000002"
|
||||
iex> Calendar.ISO.time_to_string(2, 2, 2, {2, 6}, :extended)
|
||||
"02:02:02.000002"
|
||||
|
||||
"""
|
||||
@impl true
|
||||
@doc since: "1.5.0"
|
||||
@spec time_to_string(
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond(),
|
||||
:basic | :extended
|
||||
) :: String.t()
|
||||
def time_to_string(hour, minute, second, microsecond, format \\ :extended)
|
||||
def time_to_string(hour, minute, second, microsecond) do
|
||||
time_to_string(hour, minute, second, microsecond, :extended)
|
||||
end
|
||||
|
||||
def time_to_string(hour, minute, second, {_, 0}, format) when format in [:basic, :extended] do
|
||||
def time_to_string(hour, minute, second, {_, 0}, format) do
|
||||
time_to_string_format(hour, minute, second, format)
|
||||
end
|
||||
|
||||
def time_to_string(hour, minute, second, {microsecond, precision}, format)
|
||||
when format in [:basic, :extended] do
|
||||
def time_to_string(hour, minute, second, {microsecond, precision}, format) do
|
||||
time_to_string_format(hour, minute, second, format) <>
|
||||
"." <> (microsecond |> zero_pad(6) |> binary_part(0, precision))
|
||||
end
|
||||
@@ -514,126 +283,32 @@ defmodule Calendar.ISO do
|
||||
|
||||
@doc """
|
||||
Converts the given date into a string.
|
||||
|
||||
By default, returns dates formatted in the "extended" format,
|
||||
for human readability. It also supports the "basic" format
|
||||
by passing the `:basic` option.
|
||||
|
||||
## 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"
|
||||
|
||||
iex> Calendar.ISO.date_to_string(2015, 2, 28, :basic)
|
||||
"20150228"
|
||||
iex> Calendar.ISO.date_to_string(-99, 1, 31, :basic)
|
||||
"-00990131"
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec date_to_string(year, month, day, :basic | :extended) :: String.t()
|
||||
@impl true
|
||||
def date_to_string(year, month, day, format \\ :extended)
|
||||
def date_to_string(year, month, day) do
|
||||
date_to_string(year, month, day, :extended)
|
||||
end
|
||||
|
||||
def date_to_string(year, month, day, :extended) do
|
||||
defp date_to_string(year, month, day, :extended) do
|
||||
zero_pad(year, 4) <> "-" <> zero_pad(month, 2) <> "-" <> zero_pad(day, 2)
|
||||
end
|
||||
|
||||
def date_to_string(year, month, day, :basic) do
|
||||
defp date_to_string(year, month, day, :basic) do
|
||||
zero_pad(year, 4) <> zero_pad(month, 2) <> zero_pad(day, 2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the datetime (without time zone) into a string.
|
||||
|
||||
By default, returns datetimes formatted in the "extended" format,
|
||||
for human readability. It also supports the "basic" format
|
||||
by passing the `:basic` option.
|
||||
|
||||
## 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"
|
||||
|
||||
iex> Calendar.ISO.naive_datetime_to_string(2015, 2, 28, 1, 2, 3, {4, 6}, :basic)
|
||||
"20150228 010203.000004"
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@impl true
|
||||
@spec naive_datetime_to_string(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond(),
|
||||
:basic | :extended
|
||||
) :: String.t()
|
||||
def naive_datetime_to_string(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
microsecond,
|
||||
format \\ :extended
|
||||
)
|
||||
when format in [:basic, :extended] do
|
||||
date_to_string(year, month, day, format) <>
|
||||
" " <> time_to_string(hour, minute, second, microsecond, format)
|
||||
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.
|
||||
|
||||
By default, returns datetimes formatted in the "extended" format,
|
||||
for human readability. It also supports the "basic" format
|
||||
by passing the `:basic` option.
|
||||
|
||||
## 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"
|
||||
|
||||
iex> time_zone = "Europe/Berlin"
|
||||
iex> Calendar.ISO.datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5}, time_zone, "CET", 3600, 0, :basic)
|
||||
"20170801 010203.00000+0100 CET Europe/Berlin"
|
||||
|
||||
Convers the datetime (with time zone) into a string.
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@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(),
|
||||
:basic | :extended
|
||||
) :: String.t()
|
||||
def datetime_to_string(
|
||||
year,
|
||||
month,
|
||||
@@ -645,76 +320,32 @@ defmodule Calendar.ISO do
|
||||
time_zone,
|
||||
zone_abbr,
|
||||
utc_offset,
|
||||
std_offset,
|
||||
format \\ :extended
|
||||
)
|
||||
when format in [:basic, :extended] do
|
||||
date_to_string(year, month, day, format) <>
|
||||
std_offset
|
||||
) do
|
||||
date_to_string(year, month, day) <>
|
||||
" " <>
|
||||
time_to_string(hour, minute, second, microsecond, format) <>
|
||||
offset_to_string(utc_offset, std_offset, time_zone, format) <>
|
||||
time_to_string(hour, minute, second, microsecond) <>
|
||||
offset_to_string(utc_offset, std_offset, time_zone) <>
|
||||
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
|
||||
|
||||
defp offset_to_string(utc, std, zone, format \\ :extended)
|
||||
defp offset_to_string(0, 0, "Etc/UTC", _format), do: "Z"
|
||||
|
||||
defp offset_to_string(utc, std, _zone, format) do
|
||||
@@ -739,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
|
||||
@@ -766,15 +393,24 @@ defmodule Calendar.ISO do
|
||||
end
|
||||
|
||||
defp precision_for_unit(unit) do
|
||||
case System.convert_time_unit(1, :second, unit) do
|
||||
1 -> 0
|
||||
10 -> 1
|
||||
100 -> 2
|
||||
1_000 -> 3
|
||||
10_000 -> 4
|
||||
100_000 -> 5
|
||||
_ -> 6
|
||||
end
|
||||
subsecond = div(System.convert_time_unit(1, :second, unit), 10)
|
||||
precision_for_unit(subsecond, 0)
|
||||
end
|
||||
|
||||
defp precision_for_unit(0, precision), do: precision
|
||||
defp precision_for_unit(_, 6), do: 6
|
||||
|
||||
defp precision_for_unit(number, precision),
|
||||
do: precision_for_unit(div(number, 10), precision + 1)
|
||||
|
||||
@doc false
|
||||
def date_to_iso8601(year, month, day, format \\ :extended) do
|
||||
date_to_string(year, month, day, format)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def time_to_iso8601(hour, minute, second, microsecond, format \\ :extended) do
|
||||
time_to_string(hour, minute, second, microsecond, format)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -874,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
|
||||
|
||||
@@ -925,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
|
||||
|
||||
@@ -956,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
|
||||
@@ -1030,16 +642,10 @@ 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)
|
||||
|
||||
{hour, minute, second}
|
||||
end
|
||||
|
||||
defp ensure_day_in_month!(year, month, day) do
|
||||
if day < 1 or day > days_in_month(year, month) do
|
||||
raise ArgumentError, "invalid date: #{date_to_string(year, month, day)}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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,17 +321,8 @@ defmodule NaiveDateTime do
|
||||
~D[2002-01-13]
|
||||
|
||||
"""
|
||||
@spec to_date(Calendar.naive_datetime()) :: Date.t()
|
||||
def to_date(%{
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
calendar: calendar,
|
||||
hour: _,
|
||||
minute: _,
|
||||
second: _,
|
||||
microsecond: _
|
||||
}) do
|
||||
@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
|
||||
|
||||
@@ -417,17 +338,16 @@ defmodule NaiveDateTime do
|
||||
~T[23:00:07]
|
||||
|
||||
"""
|
||||
@spec to_time(Calendar.naive_datetime()) :: Time.t()
|
||||
def to_time(%{
|
||||
year: _,
|
||||
month: _,
|
||||
day: _,
|
||||
calendar: calendar,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
}) do
|
||||
@spec to_time(t) :: Time.t()
|
||||
def to_time(%NaiveDateTime{} = naive_datetime) do
|
||||
%{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
calendar: calendar
|
||||
} = naive_datetime
|
||||
|
||||
%Time{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
@@ -446,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:
|
||||
@@ -478,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
|
||||
|
||||
@@ -534,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
|
||||
@@ -582,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} ->
|
||||
@@ -658,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.
|
||||
|
||||
@@ -703,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}
|
||||
|
||||
"""
|
||||
@@ -712,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 """
|
||||
@@ -732,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} ->
|
||||
@@ -772,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
|
||||
@@ -809,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}
|
||||
|
||||
@@ -871,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
|
||||
|
||||
+41
-100
@@ -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,23 +287,15 @@ 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_string(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)
|
||||
Calendar.ISO.time_to_iso8601(hour, minute, second, microsecond, format)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -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 beginning 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
|
||||
+193
-364
@@ -5,63 +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.
|
||||
"""
|
||||
|
||||
@available_compiler_options [
|
||||
:docs,
|
||||
:debug_info,
|
||||
:ignore_module_conflict,
|
||||
:relative_paths,
|
||||
:warnings_as_errors
|
||||
]
|
||||
|
||||
@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 on v1.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,
|
||||
@@ -69,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 on v1.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 """
|
||||
@@ -256,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.
|
||||
@@ -272,13 +221,6 @@ defmodule Code do
|
||||
expects a valid `Version` which is usually the minimum Elixir
|
||||
version supported by the project.
|
||||
|
||||
* `:force_do_end_blocks` (since v1.9.0) - when `true`, converts all
|
||||
inline usages of `do: ...`, `else: ...` and friends into `do/end`
|
||||
blocks. Defaults to `false`. Notice this option is convergent:
|
||||
once you set it to `true`, all keywords will be converted. If you
|
||||
set it to `false` later on, `do/end` blocks won't be converted
|
||||
back to keywords.
|
||||
|
||||
## Design principles
|
||||
|
||||
The formatter was designed under three principles.
|
||||
@@ -301,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
|
||||
@@ -424,14 +268,61 @@ defmodule Code do
|
||||
broken into multiple lines if they are followed by a newline in the
|
||||
opening bracket and preceded by a new line in the closing bracket
|
||||
|
||||
* Newlines before certain operators (such as the pipeline operators)
|
||||
and before other operators (such as comparison operators)
|
||||
* Pipeline operators, like `|>` and others with the same precedence,
|
||||
will span multiple lines if they spanned multiple lines in the input
|
||||
|
||||
The behaviours above are not guaranteed. We may remove or add new
|
||||
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
|
||||
@@ -468,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
|
||||
@@ -497,7 +387,7 @@ defmodule Code do
|
||||
# code
|
||||
end)
|
||||
|
||||
some_function_without_parens %{
|
||||
some_funtion_without_parens %{
|
||||
foo: :bar,
|
||||
baz: :bat
|
||||
}
|
||||
@@ -511,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
|
||||
|
||||
@@ -546,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)
|
||||
@@ -560,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)
|
||||
@@ -665,52 +553,11 @@ defmodule Code do
|
||||
when non-existing atoms are found by the tokenizer.
|
||||
Defaults to `false`.
|
||||
|
||||
* `:static_atom_encoder` - The static atom encoder function, see
|
||||
"The `:static_atom_encoder` function" section below. This option
|
||||
overrides the `:existing_atoms_only` behaviour for static atoms
|
||||
but `:existing_atoms_only` is still used for dynamic atoms, such
|
||||
as atoms with interpolations.
|
||||
|
||||
* `: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
|
||||
`Macro.to_string/2`, which converts a quoted form to a string/binary
|
||||
representation.
|
||||
|
||||
## The `:static_atom_encoder` function
|
||||
|
||||
When `static_atom_encoder: &my_encoder/2` is passed as an argument,
|
||||
`my_encoder/2` is called every time the tokenizer needs to create a
|
||||
"static" atom. Static atoms are atoms in the AST that function as
|
||||
aliases, remote calls, local calls, variable names, regular atoms
|
||||
and keyword lists.
|
||||
|
||||
The encoder function will receive the atom name (as a binary) and a
|
||||
keyword list with the current file, line and column. It must return
|
||||
`{:ok, token :: term} | {:error, reason :: binary}`.
|
||||
|
||||
The encoder function is supposed to create an atom from the given
|
||||
string. It is required to return either `{:ok, term}`, where term is
|
||||
an atom. It is possible to return something else than an atom,
|
||||
however, in that case the AST is no longer "valid" in that it cannot
|
||||
be used to compile or evaluate Elixir code. A use case for this is
|
||||
if you want to use the Elixir parser in a user-facing situation, but
|
||||
you don't want to exhaust the atom table.
|
||||
|
||||
The atom encoder is not called for *all* atoms that are present in
|
||||
the AST. It won't be invoked for the following atoms:
|
||||
|
||||
* operators (`:+`, `:-`, and so on)
|
||||
|
||||
* syntax keywords (`fn`, `do`, `else`, and so on)
|
||||
|
||||
* atoms containing interpolation (`:"\#{1 + 1} is two"`), as these
|
||||
atoms are constructed at runtime.
|
||||
|
||||
"""
|
||||
@spec string_to_quoted(List.Chars.t(), keyword) ::
|
||||
{:ok, Macro.t()} | {:error, {line :: pos_integer, term, term}}
|
||||
@@ -718,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
|
||||
|
||||
@@ -749,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}
|
||||
@@ -759,13 +602,32 @@ defmodule Code do
|
||||
eval_string(File.read!(file), [], file: file, line: 1)
|
||||
end
|
||||
|
||||
# TODO: Deprecate on v1.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, fn _, _ -> :ok end)
|
||||
:elixir_code_server.cast({:required, file})
|
||||
loaded = :elixir_compiler.file(file)
|
||||
:elixir_code_server.cast({:loaded, file})
|
||||
loaded
|
||||
end
|
||||
|
||||
@@ -773,45 +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)
|
||||
|
||||
case :elixir_code_server.call({:acquire, file}) do
|
||||
:required ->
|
||||
:loaded ->
|
||||
nil
|
||||
|
||||
{:queued, ref} ->
|
||||
receive do
|
||||
{:elixir_code_server, ^ref, :loaded} -> nil
|
||||
end
|
||||
|
||||
:proceed ->
|
||||
loaded = :elixir_compiler.file(file, fn _, _ -> :ok end)
|
||||
:elixir_code_server.cast({:required, file})
|
||||
loaded = :elixir_compiler.file(file)
|
||||
:elixir_code_server.cast({:loaded, file})
|
||||
loaded
|
||||
end
|
||||
end
|
||||
@@ -824,7 +688,8 @@ defmodule Code do
|
||||
## Examples
|
||||
|
||||
Code.compiler_options()
|
||||
#=> %{debug_info: true, docs: true, ...}
|
||||
#=> %{debug_info: true, docs: true,
|
||||
#=> warnings_as_errors: false, ignore_module_conflict: false}
|
||||
|
||||
"""
|
||||
@spec compiler_options() :: %{optional(atom) => boolean}
|
||||
@@ -835,38 +700,17 @@ 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
|
||||
|
||||
Code.available_compiler_options()
|
||||
#=> [:docs, :debug_info, ...]
|
||||
iex> Code.available_compiler_options
|
||||
[:docs, :debug_info, :ignore_module_conflict, :relative_paths, :warnings_as_errors]
|
||||
|
||||
"""
|
||||
@spec available_compiler_options() :: [atom]
|
||||
def available_compiler_options do
|
||||
@available_compiler_options
|
||||
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)
|
||||
[:docs, :debug_info, :ignore_module_conflict, :relative_paths, :warnings_as_errors]
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -904,14 +748,19 @@ defmodule Code do
|
||||
"""
|
||||
@spec compiler_options(Enumerable.t()) :: %{optional(atom) => boolean}
|
||||
def compiler_options(opts) do
|
||||
Enum.each(opts, fn
|
||||
{key, value} when key in @available_compiler_options ->
|
||||
if not is_boolean(value) do
|
||||
raise "compiler option #{inspect(key)} should be a boolean, got: #{inspect(value)}"
|
||||
end
|
||||
available = available_compiler_options()
|
||||
|
||||
{key, _} ->
|
||||
raise "unknown compiler option: #{inspect(key)}"
|
||||
Enum.each(opts, fn {key, value} ->
|
||||
cond do
|
||||
key not in available ->
|
||||
raise "unknown compiler option: #{inspect(key)}"
|
||||
|
||||
not is_boolean(value) ->
|
||||
raise "compiler option #{inspect(key)} should be a boolean, got: #{inspect(value)}"
|
||||
|
||||
true ->
|
||||
:ok
|
||||
end
|
||||
end)
|
||||
|
||||
:elixir_config.update(:compiler_options, &Enum.into(opts, &1))
|
||||
@@ -925,15 +774,11 @@ 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
|
||||
:elixir_compiler.string(to_charlist(string), file, fn _, _ -> :ok end)
|
||||
:elixir_compiler.string(to_charlist(string), file)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -946,27 +791,7 @@ defmodule Code do
|
||||
"""
|
||||
@spec compile_quoted(Macro.t(), binary) :: [{module, binary}]
|
||||
def compile_quoted(quoted, file \\ "nofile") when is_binary(file) do
|
||||
:elixir_compiler.quoted(quoted, file, fn _, _ -> :ok end)
|
||||
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`.
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@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), fn _, _ -> :ok end)
|
||||
:elixir_compiler.quoted(quoted, file)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1055,9 +880,6 @@ defmodule Code do
|
||||
If it succeeds in loading the module, it returns `{:module, module}`.
|
||||
If not, returns `{:error, reason}` with the error reason.
|
||||
|
||||
If the module being checked is currently in a compiler deadlock,
|
||||
this functions returns `{:error, :nofile}`.
|
||||
|
||||
Check `ensure_loaded/1` for more information on module loading
|
||||
and when to use `ensure_loaded/1` or `ensure_compiled/1`.
|
||||
"""
|
||||
@@ -1066,11 +888,9 @@ defmodule Code do
|
||||
def ensure_compiled(module) when is_atom(module) do
|
||||
case :code.ensure_loaded(module) do
|
||||
{:error, :nofile} = error ->
|
||||
if is_pid(:erlang.get(:elixir_compiler_pid)) do
|
||||
case Kernel.ErrorHandler.ensure_compiled(module, :module, :soft) do
|
||||
:found -> {:module, module}
|
||||
:not_found -> error
|
||||
end
|
||||
if is_pid(:erlang.get(:elixir_compiler_pid)) and
|
||||
Kernel.ErrorHandler.ensure_compiled(module, :module) do
|
||||
{:module, module}
|
||||
else
|
||||
error
|
||||
end
|
||||
@@ -1093,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: %{required(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
|
||||
|
||||
|
||||
+146
-231
@@ -28,7 +28,7 @@ defmodule Code.Formatter do
|
||||
@required_parens_logical_binary_operands [:||, :|||, :or, :&&, :&&&, :and]
|
||||
|
||||
# Operators with next break fits. = and :: do not consider new lines though
|
||||
@next_break_fits_operators [:<-, :==, :!=, :=~, :===, :!==, :<, :>, :<=, :>=, :=, :"::"]
|
||||
@next_break_fits_operators [:<-, :==, :!=, :=~, :===, :!==, :<, :>, :<=, :>=, :=, :::]
|
||||
|
||||
# Operators that always require parens on operands when they are the parent
|
||||
@required_parens_on_binary_operands [
|
||||
@@ -49,7 +49,7 @@ defmodule Code.Formatter do
|
||||
:<>
|
||||
]
|
||||
|
||||
@locals_without_parens [
|
||||
locals_without_parens = [
|
||||
# Special forms
|
||||
alias: 1,
|
||||
alias: 2,
|
||||
@@ -77,7 +77,6 @@ defmodule Code.Formatter do
|
||||
defmacro: 2,
|
||||
defmacrop: 1,
|
||||
defmacrop: 2,
|
||||
defmodule: 2,
|
||||
defdelegate: 2,
|
||||
defexception: 1,
|
||||
defoverridable: 1,
|
||||
@@ -99,6 +98,7 @@ defmodule Code.Formatter do
|
||||
defrecordp: 3,
|
||||
|
||||
# Testing
|
||||
all: :*,
|
||||
assert: 1,
|
||||
assert: 2,
|
||||
assert_in_delta: 3,
|
||||
@@ -110,8 +110,12 @@ defmodule Code.Formatter do
|
||||
assert_receive: 3,
|
||||
assert_received: 1,
|
||||
assert_received: 2,
|
||||
check: 1,
|
||||
check: 2,
|
||||
doctest: 1,
|
||||
doctest: 2,
|
||||
property: 1,
|
||||
property: 2,
|
||||
refute: 1,
|
||||
refute: 2,
|
||||
refute_in_delta: 3,
|
||||
@@ -134,7 +138,7 @@ defmodule Code.Formatter do
|
||||
import_config: 1
|
||||
]
|
||||
|
||||
@do_end_keywords [:rescue, :catch, :else, :after]
|
||||
@locals_without_parens MapSet.new(locals_without_parens)
|
||||
|
||||
@doc """
|
||||
Checks if two strings are equivalent.
|
||||
@@ -190,7 +194,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")
|
||||
@@ -198,12 +202,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
|
||||
@@ -225,7 +224,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
|
||||
@@ -238,8 +237,6 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp state(comments, opts) do
|
||||
force_do_end_blocks = Keyword.get(opts, :force_do_end_blocks, false)
|
||||
|
||||
rename_deprecated_at =
|
||||
if version = opts[:rename_deprecated_at] do
|
||||
case Version.parse(version) do
|
||||
@@ -253,10 +250,13 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
locals_without_parens =
|
||||
Keyword.get(opts, :locals_without_parens, []) ++ @locals_without_parens
|
||||
opts
|
||||
|> Keyword.get(:locals_without_parens, [])
|
||||
|> MapSet.new()
|
||||
|> MapSet.union(@locals_without_parens)
|
||||
|> MapSet.to_list()
|
||||
|
||||
%{
|
||||
force_do_end_blocks: force_do_end_blocks,
|
||||
locals_without_parens: locals_without_parens,
|
||||
operand_nesting: 2,
|
||||
rename_deprecated_at: rename_deprecated_at,
|
||||
@@ -362,24 +362,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
|
||||
|
||||
@@ -513,7 +513,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
|
||||
@@ -711,21 +711,14 @@ defmodule Code.Formatter do
|
||||
{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
|
||||
operand_to_algebra_with_comments(
|
||||
operands,
|
||||
meta,
|
||||
min_line,
|
||||
max_line,
|
||||
state,
|
||||
operand_to_algebra
|
||||
)
|
||||
end
|
||||
|
||||
defp binary_op_to_algebra(op, _, meta, left_arg, right_arg, context, state, _nesting)
|
||||
@@ -774,16 +767,13 @@ defmodule Code.Formatter do
|
||||
concat(concat(group(left), op_string), group(right))
|
||||
|
||||
true ->
|
||||
eol? = eol?(meta)
|
||||
|
||||
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
|
||||
|
||||
@@ -791,7 +781,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
# TODO: We can remove this workaround once we remove
|
||||
# ?rearrange_uop from the parser on v2.0.
|
||||
# ?rearrange_uop from the parser in Elixir v2.0.
|
||||
# (! left) in right
|
||||
# (not left) in right
|
||||
defp binary_operand_to_algebra(
|
||||
@@ -882,7 +872,7 @@ defmodule Code.Formatter do
|
||||
{docs, comments?, state} =
|
||||
quoted_to_algebra_with_comments(operands, [], min_line, max_line, 1, state, fun)
|
||||
|
||||
if comments? or eol?(meta) do
|
||||
if comments? or Keyword.get(meta, :eol, false) do
|
||||
{docs |> Enum.reduce(&line(&2, &1)) |> force_unfit(), state}
|
||||
else
|
||||
{docs |> Enum.reduce(&glue(&2, &1)), state}
|
||||
@@ -1053,12 +1043,8 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
# We can only rename functions in the same module because
|
||||
# introducing a new module may be wrong due to aliases.
|
||||
# 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
|
||||
@@ -1075,7 +1061,7 @@ defmodule Code.Formatter do
|
||||
defp local_to_algebra(fun, meta, args, context, state) when is_atom(fun) do
|
||||
skip_parens =
|
||||
cond do
|
||||
not Keyword.get(meta, :no_parens, false) -> :skip_if_only_do_end
|
||||
not Keyword.get(meta, :no_parens, false) -> :required
|
||||
local_without_parens?(fun, args, state) -> :skip_unless_many_args
|
||||
true -> :skip_if_do_end
|
||||
end
|
||||
@@ -1093,10 +1079,9 @@ defmodule Code.Formatter do
|
||||
{doc, state}
|
||||
end
|
||||
|
||||
# parens may be one of:
|
||||
# parens may one of:
|
||||
#
|
||||
# * :skip_unless_many_args - skips parens unless we are the argument context
|
||||
# * :skip_if_only_do_end - skip parens if we are do-end and the only arg
|
||||
# * :skip_if_do_end - skip parens if we are do-end
|
||||
# * :required - never skip parens
|
||||
#
|
||||
@@ -1110,18 +1095,13 @@ defmodule Code.Formatter do
|
||||
defp call_args_to_algebra(args, meta, context, parens, list_to_keyword?, state) do
|
||||
{rest, last} = split_last(args)
|
||||
|
||||
if blocks = do_end_blocks(last, state) do
|
||||
if blocks = do_end_blocks(last) do
|
||||
{call_doc, state} =
|
||||
case rest do
|
||||
[] when parens == :required ->
|
||||
{"() do", state}
|
||||
|
||||
[] ->
|
||||
{" do", state}
|
||||
|
||||
_ ->
|
||||
no_parens? = parens not in [:required, :skip_if_only_do_end]
|
||||
call_args_to_algebra_no_blocks(meta, rest, no_parens?, list_to_keyword?, " do", state)
|
||||
if rest == [] do
|
||||
{" do", state}
|
||||
else
|
||||
no_parens? = parens != :required
|
||||
call_args_to_algebra_no_blocks(meta, rest, no_parens?, list_to_keyword?, " do", state)
|
||||
end
|
||||
|
||||
{blocks_doc, state} = do_end_blocks_to_algebra(blocks, state)
|
||||
@@ -1150,110 +1130,79 @@ 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)
|
||||
to_algebra_fun = "ed_to_algebra(&1, context, &2)
|
||||
if left != [] and keyword? and skip_parens? and no_generators?(args) do
|
||||
call_args_to_algebra_with_no_parens_keywords(meta, left, right, context, extra, state)
|
||||
else
|
||||
args = if keyword?, do: left ++ right, else: left ++ [right]
|
||||
many_eol? = match?([_, _ | _], args) and Keyword.get(meta, :eol, false)
|
||||
join = if force_args?(args) or many_eol?, do: :line, else: :break
|
||||
|
||||
{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
|
||||
next_break_fits? = join == :break and next_break_fits?(right, state)
|
||||
last_arg_mode = if next_break_fits?, do: :next_break_fits, else: :none
|
||||
|
||||
{left_doc, _join, state} =
|
||||
args_to_algebra_with_comments(
|
||||
left,
|
||||
Keyword.delete(meta, :closing),
|
||||
skip_parens?,
|
||||
:force_comma,
|
||||
join,
|
||||
state,
|
||||
to_algebra_fun
|
||||
)
|
||||
{args_doc, _join, state} =
|
||||
args_to_algebra_with_comments(
|
||||
args,
|
||||
meta,
|
||||
skip_parens?,
|
||||
last_arg_mode,
|
||||
join,
|
||||
state,
|
||||
"ed_to_algebra(&1, context, &2)
|
||||
)
|
||||
|
||||
join = if force_args?(right) or force_args?(args) or many_eol?, do: :line, else: :break
|
||||
# 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)
|
||||
|
||||
{right_doc, _join, state} =
|
||||
args_to_algebra_with_comments(right, meta, false, :none, join, state, to_algebra_fun)
|
||||
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
|
||||
|
||||
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}
|
||||
if next_break_fits? do
|
||||
{next_break_fits(doc, :disabled), 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}
|
||||
{doc, state}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp call_args_to_algebra_with_no_parens_keywords(meta, left, right, context, extra, state) do
|
||||
to_algebra_fun = "ed_to_algebra(&1, context, &2)
|
||||
join = if force_args?(left), do: :line, else: :break
|
||||
|
||||
{left_doc, _join, state} =
|
||||
args_to_algebra_with_comments(left, meta, true, :force_comma, join, state, to_algebra_fun)
|
||||
|
||||
join = if force_args?(right) or force_args?(left ++ right), 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) |> 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
|
||||
@@ -1269,19 +1218,16 @@ defmodule Code.Formatter do
|
||||
not Enum.any?(args, &match?({:<-, _, [_, _]}, &1))
|
||||
end
|
||||
|
||||
defp do_end_blocks([{{:__block__, meta, [:do]}, _} | rest] = blocks, state) do
|
||||
if meta[:format] == :block or can_force_do_end_blocks?(rest, state) do
|
||||
defp do_end_blocks([{{:__block__, meta, [:do]}, _} | _] = blocks) do
|
||||
if meta[:format] == :block do
|
||||
blocks
|
||||
|> Enum.map(fn {{:__block__, meta, [key]}, value} -> {key, line(meta), value} end)
|
||||
|> do_end_blocks_with_range(end_line(meta))
|
||||
end
|
||||
end
|
||||
|
||||
defp do_end_blocks(_, _), do: nil
|
||||
|
||||
defp can_force_do_end_blocks?(rest, state) do
|
||||
state.force_do_end_blocks and
|
||||
Enum.all?(rest, fn {{:__block__, _, [key]}, _} -> key in @do_end_keywords end)
|
||||
defp do_end_blocks(_) do
|
||||
nil
|
||||
end
|
||||
|
||||
defp do_end_blocks_with_range([{key1, line1, value1}, {_, line2, _} = h | t], end_line) do
|
||||
@@ -1310,17 +1256,9 @@ 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
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
|
||||
entry when is_binary(entry) -> true
|
||||
_ -> false
|
||||
end)
|
||||
@@ -1334,23 +1272,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))
|
||||
@@ -1358,7 +1279,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp interpolation_to_algebra([entry | entries], escape, state, acc, last) do
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, meta, [quoted]}, {:binary, _, _}]} = entry
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, meta, [quoted]}, {:binary, _, _}]} = entry
|
||||
{doc, state} = block_to_algebra(quoted, line(meta), end_line(meta), state)
|
||||
doc = surround("\#{", doc, "}")
|
||||
interpolation_to_algebra(entries, escape, state, concat(acc, doc), last)
|
||||
@@ -1371,26 +1292,26 @@ defmodule Code.Formatter do
|
||||
## Sigils
|
||||
|
||||
defp maybe_sigil_to_algebra(fun, meta, args, state) do
|
||||
with <<"sigil_", name>> <- Atom.to_string(fun),
|
||||
[{:<<>>, _, entries}, modifiers] when is_list(modifiers) <- args,
|
||||
opening_terminator when not is_nil(opening_terminator) <- Keyword.get(meta, :terminator) do
|
||||
doc = <<?~, name, opening_terminator::binary>>
|
||||
case {Atom.to_string(fun), args} do
|
||||
{<<"sigil_", name>>, [{:<<>>, _, entries}, modifiers]} ->
|
||||
opening_terminator = Keyword.fetch!(meta, :terminator)
|
||||
doc = <<?~, name, opening_terminator::binary>>
|
||||
|
||||
if opening_terminator in [@double_heredoc, @single_heredoc] do
|
||||
closing_terminator = concat(opening_terminator, List.to_string(modifiers))
|
||||
if opening_terminator in [@double_heredoc, @single_heredoc] do
|
||||
closing_terminator = concat(opening_terminator, List.to_string(modifiers))
|
||||
|
||||
{doc, state} =
|
||||
entries
|
||||
|> prepend_heredoc_line()
|
||||
|> interpolation_to_algebra(:heredoc, state, doc, closing_terminator)
|
||||
{doc, state} =
|
||||
entries
|
||||
|> prepend_heredoc_line()
|
||||
|> interpolation_to_algebra(:heredoc, state, doc, closing_terminator)
|
||||
|
||||
{force_unfit(doc), state}
|
||||
else
|
||||
escape = closing_sigil_terminator(opening_terminator)
|
||||
closing_terminator = concat(escape, List.to_string(modifiers))
|
||||
interpolation_to_algebra(entries, escape, state, doc, closing_terminator)
|
||||
end
|
||||
|
||||
{force_unfit(doc), state}
|
||||
else
|
||||
escape = closing_sigil_terminator(opening_terminator)
|
||||
closing_terminator = concat(escape, List.to_string(modifiers))
|
||||
interpolation_to_algebra(entries, escape, state, doc, closing_terminator)
|
||||
end
|
||||
else
|
||||
_ ->
|
||||
:error
|
||||
end
|
||||
@@ -1406,7 +1327,7 @@ 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_break
|
||||
to_algebra_fun = &bitstring_segment_to_algebra(&1, &2, last)
|
||||
|
||||
{args_doc, join, state} =
|
||||
@@ -1427,7 +1348,7 @@ defmodule Code.Formatter do
|
||||
{bitstring_wrap_parens(doc, i, last), state}
|
||||
end
|
||||
|
||||
defp bitstring_segment_to_algebra({{:"::", _, [segment, spec]}, i}, state, last) 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)
|
||||
|
||||
@@ -1474,7 +1395,7 @@ defmodule Code.Formatter do
|
||||
## 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: :break
|
||||
fun = "ed_to_algebra(&1, :parens_arg, &2)
|
||||
|
||||
{args_doc, _join, state} =
|
||||
@@ -1484,7 +1405,7 @@ defmodule Code.Formatter do
|
||||
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: :break
|
||||
fun = "ed_to_algebra(&1, :parens_arg, &2)
|
||||
{left_doc, state} = fun.(left, state)
|
||||
|
||||
@@ -1501,7 +1422,7 @@ 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: :break
|
||||
fun = "ed_to_algebra(&1, :parens_arg, &2)
|
||||
|
||||
{args_doc, _join, state} =
|
||||
@@ -1512,7 +1433,7 @@ defmodule Code.Formatter do
|
||||
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 = "ed_to_algebra(&1, :parens_arg, &2)
|
||||
|
||||
{args_doc, join, state} =
|
||||
@@ -1795,7 +1716,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
|
||||
@@ -1853,7 +1774,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp add_max_line_to_last_clause([{op, meta, args}], max_line) do
|
||||
[{op, [closing: [line: max_line]] ++ meta, args}]
|
||||
[{op, [end_line: max_line] ++ meta, args}]
|
||||
end
|
||||
|
||||
defp add_max_line_to_last_clause([clause | clauses], max_line) do
|
||||
@@ -1861,19 +1782,13 @@ 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, :none, :break, 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
|
||||
@@ -2025,7 +1940,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
# TODO: We can remove this workaround once we remove
|
||||
# ?rearrange_uop from the parser on v2.0.
|
||||
# ?rearrange_uop from the parser in Elixir v2.0.
|
||||
defp wrap_in_parens_if_operator(doc, {:__block__, _, [expr]}) do
|
||||
wrap_in_parens_if_operator(doc, expr)
|
||||
end
|
||||
@@ -2131,7 +2046,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
|
||||
|
||||
@@ -2165,7 +2080,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)
|
||||
@@ -2241,11 +2156,11 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
defp line(meta) do
|
||||
meta[:line] || @max_line
|
||||
Keyword.get(meta, :line, @max_line)
|
||||
end
|
||||
|
||||
defp end_line(meta) do
|
||||
meta[:closing][:line] || @min_line
|
||||
Keyword.get(meta, :end_line, @min_line)
|
||||
end
|
||||
|
||||
## Algebra helpers
|
||||
|
||||
@@ -34,7 +34,7 @@ defmodule Code.Identifier do
|
||||
cond do
|
||||
op in [:<-, :\\] -> {:left, 40}
|
||||
op in [:when] -> {:right, 50}
|
||||
op in [:"::"] -> {:right, 60}
|
||||
op in [:::] -> {:right, 60}
|
||||
op in [:|] -> {:right, 70}
|
||||
op in [:=] -> {:right, 100}
|
||||
op in [:||, :|||, :or] -> {:left, 130}
|
||||
@@ -55,26 +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_operator` - all callable operators, such as `:<>`. Note
|
||||
* :callable_operators - all callable operators, such as `:<>`. Note
|
||||
operators such as `:..` are not callable because of ambiguity
|
||||
|
||||
* `:not_atomable` - callable operators that must be wrapped in quotes when
|
||||
defined as an atom. For example, `::` must be written as `:"::"` to avoid
|
||||
the ambiguity between the atom and the keyword identifier
|
||||
|
||||
* `: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
|
||||
@@ -84,9 +80,6 @@ defmodule Code.Identifier do
|
||||
atom in [:%, :%{}, :{}, :<<>>, :..., :.., :., :->] ->
|
||||
:not_callable
|
||||
|
||||
atom in [:"::"] ->
|
||||
:not_atomable
|
||||
|
||||
unary_op(atom) != :error or binary_op(atom) != :error ->
|
||||
:callable_operator
|
||||
|
||||
@@ -94,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
|
||||
@@ -150,7 +143,7 @@ defmodule Code.Identifier do
|
||||
type when type in [:callable_local, :callable_operator, :not_callable] ->
|
||||
":" <> binary
|
||||
|
||||
_ ->
|
||||
:other ->
|
||||
{escaped, _} = escape(binary, ?")
|
||||
IO.iodata_to_binary([?:, ?", escaped, ?"])
|
||||
end
|
||||
@@ -179,7 +172,7 @@ defmodule Code.Identifier do
|
||||
binary = Atom.to_string(atom)
|
||||
|
||||
case classify(atom) do
|
||||
type when type in [:callable_local, :callable_operator, :not_atomable] ->
|
||||
type when type in [:callable_local, :callable_operator] ->
|
||||
binary
|
||||
|
||||
type ->
|
||||
|
||||
@@ -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: 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
|
||||
@@ -21,9 +21,9 @@ defprotocol Collectable do
|
||||
shape where just the range limits are stored.
|
||||
|
||||
The `Collectable` module was designed to fill the gap left by the
|
||||
`Enumerable` protocol. `Collectable.into/1` can be seen as the opposite of
|
||||
`Enumerable.reduce/3`. If the functions in `Enumerable` are about taking values out,
|
||||
then `Collectable.into/1` is about collecting those values into a structure.
|
||||
`Enumerable` protocol. `into/1` can be seen as the opposite of
|
||||
`Enumerable.reduce/3`. If `Enumerable` is about taking values out,
|
||||
`Collectable.into/1` is about collecting those values into a structure.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,266 +0,0 @@
|
||||
defmodule Config do
|
||||
@moduledoc ~S"""
|
||||
A simple keyword-based configuration API.
|
||||
|
||||
## Example
|
||||
|
||||
This module is most commonly used to define application configuration,
|
||||
typically in `config/config.exs`:
|
||||
|
||||
import Config
|
||||
|
||||
config :some_app,
|
||||
key1: "value1",
|
||||
key2: "value2"
|
||||
|
||||
import_config "#{Mix.env()}.exs"
|
||||
|
||||
`import Config` will import the functions `config/2`, `config/3`
|
||||
and `import_config/1` to help you manage your configuration.
|
||||
|
||||
`config/2` and `config/3` are used to define key-value configuration
|
||||
for a given application. Once Mix starts, it will automatically
|
||||
evaluate the configuration file and persist the configuration above
|
||||
into `:some_app`'s application environment, which can be accessed in
|
||||
as follows:
|
||||
|
||||
"value1" = Application.fetch_env!(:some_app, :key1)
|
||||
|
||||
Finally, the line `import_config "#{Mix.env()}.exs"` will import other
|
||||
config files, based on the current Mix environment, such as
|
||||
`config/dev.exs` and `config/test.exs`.
|
||||
|
||||
`Config` also provides a low-level API for evaluating and reading
|
||||
configuration, under the `Config.Reader` module.
|
||||
|
||||
**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).
|
||||
|
||||
## Migrating from `use Mix.Config`
|
||||
|
||||
The `Config` module in Elixir was introduced in v1.9 as a replacement to
|
||||
`Mix.Config`, which was specific to Mix and has been deprecated.
|
||||
|
||||
You can leverage `Config` instead of `Mix.Config` in two steps. The first
|
||||
step is to replace `use Mix.Config` at the top of your config files by
|
||||
`import Config`.
|
||||
|
||||
The second is to make sure your `import_config/1` calls do not have a
|
||||
wildcard character. If so, you need to perform the wildcard lookup
|
||||
manually. For example, if you did:
|
||||
|
||||
import_config "../apps/*/config/config.exs"
|
||||
|
||||
It has to be replaced by:
|
||||
|
||||
for config <- "../apps/*/config/config.exs" |> Path.expand(__DIR__) |> Path.wildcard() do
|
||||
import_config config
|
||||
end
|
||||
|
||||
## config/releases.exs
|
||||
|
||||
If you are using releases, see `mix release`, there another configuration
|
||||
file called `config/releases.exs`. While `config/config.exs` and friends
|
||||
mentioned in the previous section are executed whenever you run a Mix
|
||||
command, including when you assemble a release, `config/releases.exs` is
|
||||
execute every time your production system boots. Since Mix is not available
|
||||
in a production system, `config/releases.exs` must not use any of the
|
||||
functions from Mix.
|
||||
"""
|
||||
|
||||
@config_key {__MODULE__, :config}
|
||||
@files_key {__MODULE__, :files}
|
||||
|
||||
defp get_config!() do
|
||||
Process.get(@config_key) || raise_improper_use!()
|
||||
end
|
||||
|
||||
defp put_config(value) do
|
||||
Process.put(@config_key, value)
|
||||
end
|
||||
|
||||
defp delete_config() do
|
||||
Process.delete(@config_key)
|
||||
end
|
||||
|
||||
defp get_files!() do
|
||||
Process.get(@files_key) || raise_improper_use!()
|
||||
end
|
||||
|
||||
defp put_files(value) do
|
||||
Process.put(@files_key, value)
|
||||
end
|
||||
|
||||
defp delete_files() do
|
||||
Process.delete(@files_key)
|
||||
end
|
||||
|
||||
defp raise_improper_use!() do
|
||||
raise "could not set configuration via Config. " <>
|
||||
"This usually means you are trying to execute a configuration file " <>
|
||||
"directly, instead of reading it with Config.Reader"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Configures the given `root_key`.
|
||||
|
||||
Keyword lists are always deep-merged.
|
||||
|
||||
## Examples
|
||||
|
||||
The given `opts` are merged into the existing configuration
|
||||
for the given `root_key`. Conflicting keys are overridden by the
|
||||
ones specified in `opts`. For example, the application
|
||||
configuration below
|
||||
|
||||
config :logger,
|
||||
level: :warn,
|
||||
backends: [:console]
|
||||
|
||||
config :logger,
|
||||
level: :info,
|
||||
truncate: 1024
|
||||
|
||||
will have a final configuration for `:logger` of:
|
||||
|
||||
[level: :info, backends: [:console], truncate: 1024]
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
def config(root_key, opts) when is_atom(root_key) and is_list(opts) do
|
||||
unless Keyword.keyword?(opts) do
|
||||
raise ArgumentError, "config/2 expected a keyword list, got: #{inspect(opts)}"
|
||||
end
|
||||
|
||||
get_config!()
|
||||
|> __merge__([{root_key, opts}])
|
||||
|> put_config()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Configures the given `key` for the given `root_key`.
|
||||
|
||||
Keyword lists are always deep merged.
|
||||
|
||||
## Examples
|
||||
|
||||
The given `opts` are merged into the existing values for `key`
|
||||
in the given `root_key`. Conflicting keys are overridden by the
|
||||
ones specified in `opts`. For example, the application
|
||||
configuration below
|
||||
|
||||
config :ecto, Repo,
|
||||
log_level: :warn,
|
||||
adapter: Ecto.Adapters.Postgres
|
||||
|
||||
config :ecto, Repo,
|
||||
log_level: :info,
|
||||
pool_size: 10
|
||||
|
||||
will have a final value of the configuration for the `Repo`
|
||||
key in the `:ecto` application of:
|
||||
|
||||
[log_level: :info, pool_size: 10, adapter: Ecto.Adapters.Postgres]
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
def config(root_key, key, opts) when is_atom(root_key) and is_atom(key) do
|
||||
get_config!()
|
||||
|> __merge__([{root_key, [{key, opts}]}])
|
||||
|> put_config()
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Imports configuration from the given file.
|
||||
|
||||
In case the file doesn't exist, an error is raised.
|
||||
|
||||
If file is a relative, it will be expanded relatively to the
|
||||
directory the current configuration file is in.
|
||||
|
||||
## Examples
|
||||
|
||||
This is often used to emulate configuration across environments:
|
||||
|
||||
import_config "#{Mix.env()}.exs"
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
defmacro import_config(file) do
|
||||
quote do
|
||||
Config.__import__!(Path.expand(unquote(file), __DIR__))
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __import__!(Path.t()) :: keyword()
|
||||
def __import__!(file) when is_binary(file) do
|
||||
current_files = get_files!()
|
||||
|
||||
if file in current_files do
|
||||
raise ArgumentError,
|
||||
"attempting to load configuration #{Path.relative_to_cwd(file)} recursively"
|
||||
end
|
||||
|
||||
put_files([file | current_files])
|
||||
Code.eval_file(file)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec __eval__!(Path.t(), [Path.t()]) :: {keyword, [Path.t()]}
|
||||
def __eval__!(file, imported_paths \\ []) when is_binary(file) and is_list(imported_paths) do
|
||||
previous_config = put_config([])
|
||||
previous_files = put_files(imported_paths)
|
||||
|
||||
try do
|
||||
{eval_config, _} = __import__!(Path.expand(file))
|
||||
|
||||
case get_config!() do
|
||||
[] when is_list(eval_config) ->
|
||||
{validate!(eval_config, file), get_files!()}
|
||||
|
||||
pdict_config ->
|
||||
{pdict_config, get_files!()}
|
||||
end
|
||||
after
|
||||
if previous_config, do: put_config(previous_config), else: delete_config()
|
||||
if previous_files, do: put_files(previous_files), else: delete_files()
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __merge__(config1, config2) when is_list(config1) and is_list(config2) do
|
||||
Keyword.merge(config1, config2, fn _, app1, app2 ->
|
||||
Keyword.merge(app1, app2, &deep_merge/3)
|
||||
end)
|
||||
end
|
||||
|
||||
defp deep_merge(_key, value1, value2) do
|
||||
if Keyword.keyword?(value1) and Keyword.keyword?(value2) do
|
||||
Keyword.merge(value1, value2, &deep_merge/3)
|
||||
else
|
||||
value2
|
||||
end
|
||||
end
|
||||
|
||||
defp validate!(config, file) do
|
||||
Enum.all?(config, fn
|
||||
{app, value} when is_atom(app) ->
|
||||
if Keyword.keyword?(value) do
|
||||
true
|
||||
else
|
||||
raise ArgumentError,
|
||||
"expected config for app #{inspect(app)} in #{Path.relative_to_cwd(file)} " <>
|
||||
"to return keyword list, got: #{inspect(value)}"
|
||||
end
|
||||
|
||||
_ ->
|
||||
false
|
||||
end)
|
||||
|
||||
config
|
||||
end
|
||||
end
|
||||
@@ -1,249 +0,0 @@
|
||||
defmodule Config.Provider do
|
||||
@moduledoc """
|
||||
Specifies a provider API that loads configuration during boot.
|
||||
|
||||
Config providers are typically used during releases to load
|
||||
external configuration while the system boots. This is done
|
||||
by starting the VM with the minimum amount of applications
|
||||
running, then invoking all of the providers, and then
|
||||
restarting the system. This requires a mutable configuration
|
||||
file on disk, as the results of the providers are written to
|
||||
the file system. For more information on runtime configuration,
|
||||
see `mix release`.
|
||||
|
||||
## Sample config provider
|
||||
|
||||
For example, imagine you need to load some configuration from
|
||||
a JSON file and load that into the system. Said configuration
|
||||
provider would look like:
|
||||
|
||||
defmodule JSONConfigProvider do
|
||||
@behaviour Config.Provider
|
||||
|
||||
# Let's pass the path to the JSON file as config
|
||||
def init(path) when is_binary(path), do: path
|
||||
|
||||
def load(config, path) do
|
||||
# We need to start any app we may depend on.
|
||||
{:ok, _} = Application.ensure_all_started(:jason)
|
||||
|
||||
json = path |> File.read!() |> Jason.decode!()
|
||||
|
||||
Config.Reader.merge(
|
||||
config,
|
||||
my_app: [
|
||||
some_value: json["my_app_some_value"],
|
||||
another_value: json["my_app_another_value"],
|
||||
]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Then when specifying your release, you can specify the provider:
|
||||
|
||||
config_providers: [{JSONConfigProvider, "/etc/config.json"}]
|
||||
|
||||
Now once the system boots, it will invoke the provider early in
|
||||
the boot process, save the merged configuration to the disk, and
|
||||
reboot the system with the new values in place.
|
||||
"""
|
||||
|
||||
@type config :: keyword
|
||||
@type state :: term
|
||||
|
||||
@typedoc """
|
||||
A path pointing to a configuration file.
|
||||
|
||||
Since configuration files are often accessed on target machines,
|
||||
it can be expressed either as:
|
||||
|
||||
* a binary representing an absolute path
|
||||
|
||||
* a tuple {:system, system_var, path} where the config is the
|
||||
concatenation of the `system_var` with the given `path`
|
||||
|
||||
"""
|
||||
@type config_path :: {:system, binary(), binary()} | binary()
|
||||
|
||||
@doc """
|
||||
Invoked when initializing a config provider.
|
||||
|
||||
A config provider is typically initialized on the machine
|
||||
where the system is assembled and not on the target machine.
|
||||
The `c:init/1` callback is useful to verify the arguments
|
||||
given to the provider and prepare the state that will be
|
||||
given to `c:load/2`.
|
||||
|
||||
Furthermore, because the state returned by `c:init/1` can
|
||||
be written to text-based config files, it should be
|
||||
restricted only to simple data types, such as integers,
|
||||
strings, atoms, tuples, maps, and lists. Entries such as
|
||||
PIDs, references, and functions cannot be serialized.
|
||||
"""
|
||||
@callback init(term) :: state
|
||||
|
||||
@doc """
|
||||
Loads configuration (typically during system boot).
|
||||
|
||||
It receives the current `config` and the `state` returned by
|
||||
`c:init/1`. Then you typically read the extra configuration
|
||||
from an external source and merge it into the received `config`.
|
||||
Merging should be done with `Config.Reader.merge/2`, as it
|
||||
performs deep merge. It should return the updated config.
|
||||
|
||||
Note that `c:load/2` is typically invoked very early in the
|
||||
boot process, therefore if you need to use an application
|
||||
in the provider, it is your responsibility to start it.
|
||||
"""
|
||||
@callback load(config, state) :: config
|
||||
|
||||
@doc false
|
||||
defstruct [:providers, :config_path, extra_config: [], prune_after_boot: false]
|
||||
|
||||
@doc """
|
||||
Validates a `t:config_path/0`.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec validate_config_path!(config_path) :: :ok
|
||||
def validate_config_path!({:system, name, path})
|
||||
when is_binary(name) and is_binary(path),
|
||||
do: :ok
|
||||
|
||||
def validate_config_path!(path) do
|
||||
if is_binary(path) and Path.type(path) != :relative do
|
||||
:ok
|
||||
else
|
||||
raise ArgumentError, """
|
||||
expected configuration path to be:
|
||||
|
||||
* a binary representing an absolute path
|
||||
* a tuple {:system, system_var, path} where the config is the \
|
||||
concatenation of the `system_var` with the given `path`
|
||||
|
||||
Got: #{inspect(path)}
|
||||
"""
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Resolves a `t:config_path/0` to an actual path.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec resolve_config_path!(config_path) :: binary
|
||||
def resolve_config_path!(path) when is_binary(path), do: path
|
||||
def resolve_config_path!({:system, name, path}), do: System.fetch_env!(name) <> path
|
||||
|
||||
@doc false
|
||||
def init(providers, config_path, opts \\ []) when is_list(providers) and is_list(opts) do
|
||||
validate_config_path!(config_path)
|
||||
providers = for {provider, init} <- providers, do: {provider, provider.init(init)}
|
||||
struct!(%Config.Provider{config_path: config_path, providers: providers}, opts)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def boot(app, key, restart_fun \\ &System.restart/0) do
|
||||
# The app with the config provider settings may not
|
||||
# have been loaded at this point, so make sure we load
|
||||
# its environment before querying it.
|
||||
_ = :application.load(app)
|
||||
|
||||
# The config provider typically runs very early in the
|
||||
# release process, so we need to make sure Elixir is started
|
||||
# before we go around running Elixir code.
|
||||
{:ok, _} = :application.ensure_all_started(:elixir)
|
||||
|
||||
case :application.get_env(app, key) do
|
||||
{:ok, %Config.Provider{} = provider} ->
|
||||
path = resolve_config_path!(provider.config_path)
|
||||
validate_no_cyclic_boot!(path)
|
||||
|
||||
read_config!(path)
|
||||
|> Config.__merge__([{app, [{key, booted_key(provider, path)}]} | provider.extra_config])
|
||||
|> run_providers(provider)
|
||||
|> write_config!(path)
|
||||
|
||||
restart_fun.()
|
||||
|
||||
{:ok, {:booted, path}} ->
|
||||
File.rm(path)
|
||||
:booted
|
||||
|
||||
{:ok, :booted} ->
|
||||
:booted
|
||||
|
||||
_ ->
|
||||
:skip
|
||||
end
|
||||
end
|
||||
|
||||
defp booted_key(%{prune_after_boot: true}, path), do: {:booted, path}
|
||||
defp booted_key(%{prune_after_boot: false}, _path), do: :booted
|
||||
|
||||
defp validate_no_cyclic_boot!(path) do
|
||||
if System.get_env("ELIXIR_CONFIG_PROVIDER_BOOTED") do
|
||||
bad_path_abort("Got infinite loop when running Config.Provider", path)
|
||||
else
|
||||
System.put_env("ELIXIR_CONFIG_PROVIDER_BOOTED", "1")
|
||||
end
|
||||
end
|
||||
|
||||
defp read_config!(path) do
|
||||
case :file.consult(path) do
|
||||
{:ok, [inner]} ->
|
||||
inner
|
||||
|
||||
{:error, reason} ->
|
||||
bad_path_abort(
|
||||
"Could not read runtime configuration due to reason: #{inspect(reason)}",
|
||||
path
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp run_providers(config, %{providers: providers}) do
|
||||
Enum.reduce(providers, config, fn {provider, state}, acc ->
|
||||
try do
|
||||
provider.load(acc, state)
|
||||
catch
|
||||
kind, error ->
|
||||
IO.puts(:stderr, "ERROR! Config provider #{inspect(provider)} failed with:")
|
||||
IO.puts(:stderr, Exception.format(kind, error, __STACKTRACE__))
|
||||
:erlang.raise(kind, error, __STACKTRACE__)
|
||||
else
|
||||
term when is_list(term) ->
|
||||
term
|
||||
|
||||
term ->
|
||||
abort("Expected provider #{inspect(provider)} to return a list, got: #{inspect(term)}")
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
defp write_config!(config, path) do
|
||||
contents = :io_lib.format("%% coding: utf-8~n~tw.~n", [config])
|
||||
|
||||
case File.write(path, contents, [:utf8]) do
|
||||
:ok ->
|
||||
:ok
|
||||
|
||||
{:error, reason} ->
|
||||
bad_path_abort(
|
||||
"Could not write runtime configuration due to reason: #{inspect(reason)}",
|
||||
path
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp bad_path_abort(msg, path) do
|
||||
abort(
|
||||
msg <>
|
||||
". Please make sure #{inspect(path)} is writable and accessible " <>
|
||||
"or choose a different path"
|
||||
)
|
||||
end
|
||||
|
||||
defp abort(msg) do
|
||||
IO.puts(:stderr, "ERROR! " <> msg)
|
||||
raise(msg)
|
||||
end
|
||||
end
|
||||
@@ -1,87 +0,0 @@
|
||||
defmodule Config.Reader do
|
||||
@moduledoc """
|
||||
API for reading config files defined with `Config`.
|
||||
|
||||
## As a provider
|
||||
|
||||
`Config.Reader` can also be used as a `Config.Provider`.
|
||||
When used as a provider, it expects a single argument:
|
||||
which the configuration path (as outlined in
|
||||
`t:Config.Provider.config_path/0`) for the configuration
|
||||
to be read and loaded during the system boot.
|
||||
"""
|
||||
|
||||
@behaviour Config.Provider
|
||||
|
||||
@impl true
|
||||
def init(path) do
|
||||
Config.Provider.validate_config_path!(path)
|
||||
path
|
||||
end
|
||||
|
||||
@impl true
|
||||
def load(config, path) do
|
||||
merge(config, path |> Config.Provider.resolve_config_path!() |> read!())
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the configuration file.
|
||||
|
||||
The same as `read_imports!/2` but only returns the configuration
|
||||
in the given file, without returning the imported paths.
|
||||
|
||||
It exists for convenience purposes. For example, you could
|
||||
invoke it inside your `mix.exs` to read some external data
|
||||
you decided to move to a configuration file:
|
||||
|
||||
releases: Config.Reader.read!("rel/releases.exs")
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec read!(Path.t(), [Path.t()]) :: keyword
|
||||
def read!(file, imported_paths \\ [])
|
||||
when is_binary(file) and is_list(imported_paths) do
|
||||
Config.__eval__!(file, imported_paths) |> elem(0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads the given configuration file alongside its imports.
|
||||
|
||||
It accepts a list of `imported_paths` that should raise if attempted
|
||||
to be imported again (to avoid recursive imports).
|
||||
|
||||
It returns a tuple with the configuration and the imported paths.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec read_imports!(Path.t(), [Path.t()]) :: {keyword, [Path.t()]}
|
||||
def read_imports!(file, imported_paths \\ [])
|
||||
when is_binary(file) and is_list(imported_paths) do
|
||||
Config.__eval__!(file, imported_paths)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Merges two configurations.
|
||||
|
||||
The configurations are merged together with the values in
|
||||
the second one having higher preference than the first in
|
||||
case of conflicts. In case both values are set to keyword
|
||||
lists, it deep merges them.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Config.Reader.merge([app: [k: :v1]], [app: [k: :v2]])
|
||||
[app: [k: :v2]]
|
||||
|
||||
iex> Config.Reader.merge([app: [k: [v1: 1, v2: 2]]], [app: [k: [v2: :a, v3: :b]]])
|
||||
[app: [k: [v1: 1, v2: :a, v3: :b]]]
|
||||
|
||||
iex> Config.Reader.merge([app1: []], [app2: []])
|
||||
[app1: [], app2: []]
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec merge(keyword, keyword) :: keyword
|
||||
def merge(config1, config2) when is_list(config1) and is_list(config2) do
|
||||
Config.__merge__(config1, config2)
|
||||
end
|
||||
end
|
||||
+4
-53
@@ -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,24 +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
|
||||
@@ -34,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
|
||||
@@ -42,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
|
||||
@@ -57,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
|
||||
@@ -70,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
|
||||
@@ -78,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
|
||||
@@ -93,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]
|
||||
@@ -134,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]
|
||||
@@ -151,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} ->
|
||||
@@ -162,7 +142,6 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def update!(dict, key, fun) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
@@ -173,7 +152,6 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def pop(dict, key, default \\ nil) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
@@ -184,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} ->
|
||||
@@ -195,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
|
||||
@@ -244,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)
|
||||
@@ -335,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)
|
||||
@@ -355,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)
|
||||
@@ -427,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)
|
||||
|
||||
@@ -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
|
||||
@@ -47,20 +47,18 @@ defmodule DynamicSupervisor do
|
||||
# Automatically defines child_spec/1
|
||||
use DynamicSupervisor
|
||||
|
||||
def start_link(init_arg) do
|
||||
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
|
||||
def start_link(arg) do
|
||||
DynamicSupervisor.start_link(__MODULE__, arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_init_arg) do
|
||||
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. A `@doc` annotation immediately preceding
|
||||
`use DynamicSupervisor` will be attached to the generated `child_spec/1`
|
||||
function.
|
||||
module-based supervisors.
|
||||
|
||||
## Name registration
|
||||
|
||||
@@ -78,20 +76,20 @@ defmodule DynamicSupervisor do
|
||||
defmodule MySupervisor do
|
||||
use Supervisor
|
||||
|
||||
def start_link(init_arg) do
|
||||
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
|
||||
def start_link(arg) do
|
||||
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
def start_child(foo, bar, baz) do
|
||||
# This will start child by calling MyWorker.start_link(init_arg, foo, bar, baz)
|
||||
# This will start child by calling MyWorker.start_link(initial_arg, foo, bar, baz)
|
||||
Supervisor.start_child(__MODULE__, [foo, bar, baz])
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(init_arg) do
|
||||
def init(initial_arg) do
|
||||
children = [
|
||||
# Or the deprecated: worker(MyWorker, [init_arg])
|
||||
%{id: MyWorker, start: {MyWorker, :start_link, [init_arg]}}
|
||||
# Or the deprecated: worker(MyWorker, [initial_arg])
|
||||
%{id: MyWorker, start: {MyWorker, :start_link, [initial_arg]})
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :simple_one_for_one)
|
||||
@@ -103,8 +101,8 @@ defmodule DynamicSupervisor do
|
||||
defmodule MySupervisor do
|
||||
use DynamicSupervisor
|
||||
|
||||
def start_link(init_arg) do
|
||||
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
|
||||
def start_link(arg) do
|
||||
DynamicSupervisor.start_link(__MODULE__, arg, name: __MODULE__)
|
||||
end
|
||||
|
||||
def start_child(foo, bar, baz) do
|
||||
@@ -115,10 +113,10 @@ defmodule DynamicSupervisor do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(init_arg) do
|
||||
def init(initial_arg) do
|
||||
DynamicSupervisor.init(
|
||||
strategy: :one_for_one,
|
||||
extra_arguments: [init_arg]
|
||||
extra_arguments: [initial_arg]
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -137,7 +135,7 @@ 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() :: %{
|
||||
@@ -172,7 +170,6 @@ defmodule DynamicSupervisor do
|
||||
| :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,
|
||||
@@ -191,18 +188,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 +201,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__,
|
||||
@@ -254,7 +242,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 +267,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
|
||||
@@ -302,15 +287,14 @@ defmodule DynamicSupervisor do
|
||||
|
||||
If the child process start function returns an error tuple or an erroneous
|
||||
value, or if it fails, the child specification is discarded and this function
|
||||
returns `{:error, error}` where `error` is the error or erroneous value
|
||||
returned from child process start function, or failure reason if it fails.
|
||||
returns `{:error, error}` where `error` is a term containing information about
|
||||
the error and child specification.
|
||||
|
||||
If the supervisor already has N children in a way that N exceeds the amount
|
||||
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) ::
|
||||
@spec start_child(Supervisor.supervisor(), :supervisor.child_spec() | {module, term} | module) ::
|
||||
on_start_child()
|
||||
def start_child(supervisor, {_, _, _, _, _, _} = child_spec) do
|
||||
validate_and_start_child(supervisor, child_spec)
|
||||
@@ -383,12 +367,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})
|
||||
@@ -405,7 +388,7 @@ defmodule DynamicSupervisor do
|
||||
|
||||
* `id` - it is always `:undefined` for dynamic supervisors
|
||||
|
||||
* `child` - the PID of the corresponding child process or the
|
||||
* `child` - the pid of the corresponding child process or the
|
||||
atom `:restarting` if the process is about to be restarted
|
||||
|
||||
* `type` - `:worker` or `:supervisor` as defined in the child
|
||||
@@ -414,7 +397,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()}
|
||||
]
|
||||
@@ -439,7 +421,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 +432,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.
|
||||
|
||||
@@ -487,7 +452,7 @@ defmodule DynamicSupervisor do
|
||||
|
||||
* `:strategy` - the restart strategy option. The only supported
|
||||
value is `:one_for_one` which means that no other child is
|
||||
terminated if a child process terminates. You can learn more
|
||||
terminate if a child process terminates. You can learn more
|
||||
about strategies in the `Supervisor` module docs.
|
||||
|
||||
* `:max_restarts` - the maximum number of restarts allowed in
|
||||
@@ -498,7 +463,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 +471,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,11 +496,11 @@ 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
|
||||
@@ -545,7 +509,7 @@ defmodule DynamicSupervisor do
|
||||
is_tuple(name) -> name
|
||||
end
|
||||
|
||||
state = %DynamicSupervisor{mod: mod, args: init_arg, name: name}
|
||||
state = %DynamicSupervisor{mod: mod, args: args, name: name}
|
||||
|
||||
case init(state, flags) do
|
||||
{:ok, state} -> {:ok, state}
|
||||
@@ -690,7 +654,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}
|
||||
@@ -750,8 +714,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}
|
||||
@@ -949,6 +913,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}
|
||||
|
||||
+292
-366
File diff suppressed because it is too large
Load Diff
+124
-321
@@ -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, true)
|
||||
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,25 +1029,11 @@ 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)} of type " <>
|
||||
value_type(value) <> maybe_description(description) <> maybe_available(protocol)
|
||||
"protocol #{inspect(protocol)} not implemented for #{inspect(value)}" <>
|
||||
maybe_description(description) <> maybe_available(protocol)
|
||||
end
|
||||
|
||||
defp value_type(%{__struct__: struct}), do: "#{inspect(struct)} (a struct)"
|
||||
defp value_type(value) when is_atom(value), do: "Atom"
|
||||
defp value_type(value) when is_bitstring(value), do: "BitString"
|
||||
defp value_type(value) when is_float(value), do: "Float"
|
||||
defp value_type(value) when is_function(value), do: "Function"
|
||||
defp value_type(value) when is_integer(value), do: "Integer"
|
||||
defp value_type(value) when is_list(value), do: "List"
|
||||
defp value_type(value) when is_map(value), do: "Map"
|
||||
defp value_type(value) when is_pid(value), do: "PID"
|
||||
defp value_type(value) when is_port(value), do: "Port"
|
||||
defp value_type(value) when is_reference(value), do: "Reference"
|
||||
defp value_type(value) when is_tuple(value), do: "Tuple"
|
||||
|
||||
defp maybe_description(""), do: ""
|
||||
defp maybe_description(description), do: ", " <> description
|
||||
|
||||
@@ -1174,8 +1043,7 @@ defmodule Protocol.UndefinedError do
|
||||
". There are no implementations for this protocol."
|
||||
|
||||
{:consolidated, types} ->
|
||||
". This protocol is implemented for the following type(s): " <>
|
||||
Enum.map_join(types, ", ", &inspect/1)
|
||||
". This protocol is implemented for: #{Enum.map_join(types, ", ", &inspect/1)}"
|
||||
|
||||
:not_consolidated ->
|
||||
""
|
||||
@@ -1184,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
|
||||
@@ -1286,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
|
||||
@@ -1304,25 +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))
|
||||
|
||||
location =
|
||||
case exception.on() do
|
||||
"" -> ""
|
||||
on -> ". #{on}"
|
||||
end
|
||||
|
||||
"could not #{exception.action} from #{inspect(exception.source)} to " <>
|
||||
"#{inspect(exception.destination)}#{location}: #{formatted}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule File.RenameError do
|
||||
defexception [:reason, :source, :destination, on: "", action: ""]
|
||||
|
||||
@impl true
|
||||
def message(exception) do
|
||||
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
|
||||
@@ -1340,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))
|
||||
|
||||
@@ -1352,7 +1144,6 @@ end
|
||||
defmodule ErlangError do
|
||||
defexception [:original]
|
||||
|
||||
@impl true
|
||||
def message(exception) do
|
||||
"Erlang error: #{inspect(exception.original)}"
|
||||
end
|
||||
@@ -1400,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
|
||||
|
||||
@@ -1429,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
|
||||
|
||||
@@ -1446,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
|
||||
|
||||
+136
-265
@@ -30,7 +30,7 @@ defmodule File do
|
||||
always treated as UTF-8. In particular, we expect that the
|
||||
shell and the operating system are configured to use UTF-8
|
||||
encoding. Binary filenames are considered raw and passed
|
||||
to the operating system as is.
|
||||
to the OS as is.
|
||||
|
||||
## API
|
||||
|
||||
@@ -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} ->
|
||||
@@ -373,8 +330,6 @@ defmodule File do
|
||||
machine
|
||||
* `:posix` - returns the time as integer seconds since epoch
|
||||
|
||||
Note: Since file times are stored in POSIX time format on most operating systems,
|
||||
it is faster to retrieve file information with the `time: :posix` option.
|
||||
"""
|
||||
@spec stat(Path.t(), stat_options) :: {:ok, File.Stat.t()} | {:error, posix}
|
||||
def stat(path, opts \\ []) do
|
||||
@@ -390,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} ->
|
||||
@@ -426,8 +381,6 @@ defmodule File do
|
||||
* `:local` - returns a `{date, time}` tuple using the machine time
|
||||
* `:posix` - returns the time as integer seconds since epoch
|
||||
|
||||
Note: Since file times are stored in POSIX time format on most operating systems,
|
||||
it is faster to retrieve file information with the `time: :posix` option.
|
||||
"""
|
||||
@spec lstat(Path.t(), stat_options) :: {:ok, File.Stat.t()} | {:error, posix}
|
||||
def lstat(path, opts \\ []) do
|
||||
@@ -443,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} ->
|
||||
@@ -475,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
|
||||
@@ -485,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} ->
|
||||
@@ -501,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}
|
||||
@@ -511,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 ->
|
||||
@@ -532,66 +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)`).
|
||||
|
||||
In Unix-like systems, changing the modification time may require
|
||||
you to be either `root` or the owner of the file. Having write
|
||||
access may not be enough. In those cases, touching the file the
|
||||
first time (to create it) will succeed, but touching an existing
|
||||
file with fail with `{:error, :eperm}`.
|
||||
|
||||
## 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
|
||||
@@ -608,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 ->
|
||||
@@ -641,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 ->
|
||||
@@ -695,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} ->
|
||||
@@ -722,18 +634,17 @@ defmodule File do
|
||||
|
||||
Returns `:ok` in case of success, `{:error, reason}` otherwise.
|
||||
|
||||
Note: The command `mv` in Unix systems behaves differently depending on
|
||||
whether `source` is a file and the `destination` is an existing directory.
|
||||
Note: The command `mv` in Unix systems behaves differently depending
|
||||
if `source` is a file and the `destination` is an existing directory.
|
||||
We have chosen to explicitly disallow this behaviour.
|
||||
|
||||
## 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
|
||||
@@ -741,54 +652,30 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
The same as `rename/2` but raises a `File.RenameError` exception if it fails.
|
||||
Returns `:ok` otherwise.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec rename!(Path.t(), Path.t()) :: :ok
|
||||
def rename!(source, destination) do
|
||||
case rename(source, destination) do
|
||||
:ok ->
|
||||
:ok
|
||||
|
||||
{:error, reason} ->
|
||||
raise File.RenameError,
|
||||
reason: reason,
|
||||
action: "rename",
|
||||
source: IO.chardata_to_string(source),
|
||||
destination: IO.chardata_to_string(destination)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Copies the contents in `source_file` to `destination_file` preserving its modes.
|
||||
|
||||
`source_file` and `destination_file` must be a file or a symbolic link to one,
|
||||
or in the case of destination, a path to a non-existent file. If either one of
|
||||
them is a directory, `{:error, :eisdir}` will be returned.
|
||||
Copies the contents in `source` to `destination` preserving its mode.
|
||||
|
||||
If a file already exists in the destination, it invokes a
|
||||
callback which should return `true` if the existing file
|
||||
should be overwritten, `false` otherwise. The callback defaults to return `true`.
|
||||
|
||||
The function returns `:ok` in case of success. Otherwise, it returns
|
||||
`{:error, reason}`.
|
||||
The function returns `:ok` in case of success, returns
|
||||
`{:error, reason}` otherwise.
|
||||
|
||||
If you want to copy contents from an IO device to another device
|
||||
or do a straight copy from a source to a destination without
|
||||
preserving modes, check `copy/3` instead.
|
||||
|
||||
Note: The command `cp` in Unix systems behaves differently depending on
|
||||
whether the destination is an existing directory or not. We have chosen to
|
||||
explicitly disallow copying to a destination which is a directory,
|
||||
and an error will be returned if tried.
|
||||
Note: The command `cp` in Unix systems behaves differently depending
|
||||
if `destination` is an existing directory or not. We have chosen to
|
||||
explicitly disallow this behaviour. If destination is a directory, an
|
||||
error will be returned.
|
||||
"""
|
||||
@spec cp(Path.t(), Path.t(), (Path.t(), Path.t() -> boolean)) :: :ok | {:error, posix}
|
||||
def cp(source_file, destination_file, callback \\ fn _, _ -> true end) do
|
||||
source_file = IO.chardata_to_string(source_file)
|
||||
destination_file = IO.chardata_to_string(destination_file)
|
||||
def cp(source, destination, callback \\ fn _, _ -> true end) do
|
||||
source = IO.chardata_to_string(source)
|
||||
destination = IO.chardata_to_string(destination)
|
||||
|
||||
case do_cp_file(source_file, destination_file, callback, []) do
|
||||
case do_cp_file(source, destination, callback, []) do
|
||||
{:error, reason, _} -> {:error, reason}
|
||||
_ -> :ok
|
||||
end
|
||||
@@ -801,12 +688,12 @@ 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
|
||||
def cp!(source_file, destination_file, callback \\ fn _, _ -> true end) do
|
||||
case cp(source_file, destination_file, callback) do
|
||||
@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 ->
|
||||
:ok
|
||||
|
||||
@@ -814,29 +701,26 @@ defmodule File do
|
||||
raise File.CopyError,
|
||||
reason: reason,
|
||||
action: "copy",
|
||||
source: IO.chardata_to_string(source_file),
|
||||
destination: IO.chardata_to_string(destination_file)
|
||||
source: IO.chardata_to_string(source),
|
||||
destination: IO.chardata_to_string(destination)
|
||||
end
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Copies the contents in `source` to `destination` recursively, maintaining the
|
||||
source directory structure and modes.
|
||||
|
||||
If `source` is a file or a symbolic link to it, `destination` must be a path
|
||||
to an existent file, a symbolic link to one, or a path to a non-existent file.
|
||||
|
||||
If `source` is a directory, or a symbolic link to it, then `destination` must
|
||||
be an existent `directory` or a symbolic link to one, or a path to a non-existent directory.
|
||||
Copies the contents in source to destination.
|
||||
|
||||
If the source is a file, it copies `source` to
|
||||
`destination`. If the `source` is a directory, it copies
|
||||
the contents inside source into the `destination` directory.
|
||||
`destination`. If the source is a directory, it copies
|
||||
the contents inside source into the destination.
|
||||
|
||||
If a file already exists in the destination, it invokes `callback`.
|
||||
`callback` must be a function that takes two arguments: `source` and `destination`.
|
||||
The callback should return `true` if the existing file should be overwritten and `false` otherwise.
|
||||
|
||||
If a directory already exists in the destination
|
||||
where a file is meant to be (or vice versa), this
|
||||
function will fail.
|
||||
|
||||
This function may fail while copying files,
|
||||
in such cases, it will leave the destination
|
||||
directory in a dirty state, where file which have already been copied
|
||||
@@ -846,23 +730,22 @@ defmodule File do
|
||||
success, `files_and_directories` lists all files and directories copied in no
|
||||
specific order. It returns `{:error, reason, file}` otherwise.
|
||||
|
||||
Note: The command `cp` in Unix systems behaves differently depending on
|
||||
whether `destination` is an existing directory or not. We have chosen to
|
||||
explicitly disallow this behaviour. If `source` is a `file` and `destination`
|
||||
is a directory, `{:error, :eisdir}` will be returned.
|
||||
Note: The command `cp` in Unix systems behaves differently
|
||||
depending if `destination` is an existing directory or not.
|
||||
We have chosen to explicitly disallow this behaviour.
|
||||
|
||||
## 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)) ::
|
||||
@@ -885,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} ->
|
||||
@@ -904,6 +787,8 @@ defmodule File do
|
||||
end
|
||||
end
|
||||
|
||||
# src may be a file or a directory, dest is definitely
|
||||
# a directory. Returns nil unless an error is found.
|
||||
defp do_cp_r(src, dest, callback, acc) when is_list(acc) do
|
||||
case :elixir_utils.read_link_type(src) do
|
||||
{:ok, :regular} ->
|
||||
@@ -1035,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)
|
||||
|
||||
@@ -1115,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 ->
|
||||
@@ -1131,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}
|
||||
|
||||
"""
|
||||
@@ -1153,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
|
||||
@@ -1181,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, []}
|
||||
|
||||
"""
|
||||
@@ -1244,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
|
||||
@@ -1275,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} ->
|
||||
@@ -1400,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)
|
||||
|
||||
@@ -1423,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} ->
|
||||
@@ -1441,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} ->
|
||||
@@ -1485,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} ->
|
||||
@@ -1509,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 ->
|
||||
@@ -1560,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} ->
|
||||
@@ -1610,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` .
|
||||
|
||||
@@ -1623,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
|
||||
@@ -1634,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)
|
||||
@@ -1648,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
|
||||
@@ -1673,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 ->
|
||||
@@ -1691,7 +1564,7 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Changes the group given by the group ID `gid`
|
||||
Changes the group given by the group id `gid`
|
||||
for a given `file`. Returns `:ok` on success, or
|
||||
`{:error, reason}` on failure.
|
||||
"""
|
||||
@@ -1701,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 ->
|
||||
@@ -1719,7 +1591,7 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Changes the owner given by the user ID `uid`
|
||||
Changes the owner given by the user id `uid`
|
||||
for a given `file`. Returns `:ok` on success,
|
||||
or `{:error, reason}` on failure.
|
||||
"""
|
||||
@@ -1729,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 ->
|
||||
@@ -1769,7 +1640,7 @@ defmodule File do
|
||||
[read_ahead: @read_ahead_size] ++ normalize_modes(rest, binary?)
|
||||
end
|
||||
|
||||
# TODO: Remove :char_list mode on v2.0
|
||||
# TODO: Remove :char_list mode by 2.0
|
||||
defp normalize_modes([mode | rest], _binary?) when mode in [:charlist, :char_list] do
|
||||
if mode == :char_list do
|
||||
IO.warn("the :char_list mode is deprecated, use :charlist")
|
||||
|
||||
@@ -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,27 +58,11 @@ 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() | integer(),
|
||||
mtime: :calendar.datetime() | integer(),
|
||||
ctime: :calendar.datetime() | integer(),
|
||||
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.
|
||||
"""
|
||||
@spec to_record(t()) :: :file.file_info()
|
||||
def to_record(%File.Stat{unquote_splicing(pairs)}) do
|
||||
{:file_info, unquote_splicing(vals)}
|
||||
end
|
||||
@@ -86,7 +70,6 @@ defmodule File.Stat do
|
||||
@doc """
|
||||
Converts a `:file_info` record into a `File.Stat`.
|
||||
"""
|
||||
@spec from_record(:file.file_info()) :: t()
|
||||
def from_record(file_info)
|
||||
|
||||
def from_record({:file_info, unquote_splicing(vals)}) do
|
||||
|
||||
@@ -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
|
||||
|
||||
+30
-56
@@ -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](https://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
|
||||
@@ -305,7 +288,7 @@ defmodule Float do
|
||||
num = rounding(rounding, sign, num, div)
|
||||
|
||||
# Convert back to float without loss
|
||||
# https://www.exploringbinary.com/correct-decimal-to-floating-point-using-big-integers/
|
||||
# http://www.exploringbinary.com/correct-decimal-to-floating-point-using-big-integers/
|
||||
den = power_of_10(precision)
|
||||
boundary = den <<< 52
|
||||
|
||||
@@ -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,18 +463,21 @@ defmodule Float do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(float))
|
||||
end
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
# (hard-deprecated in elixir_dispatch)
|
||||
@doc false
|
||||
@deprecated "Use Float.to_charlist/1 instead"
|
||||
def to_char_list(float), do: Float.to_charlist(float)
|
||||
|
||||
@doc false
|
||||
@deprecated "Use :erlang.float_to_list/2 instead"
|
||||
# TODO: Remove by 2.0
|
||||
# (hard-deprecated in elixir_dispatch)
|
||||
def to_char_list(float, options) do
|
||||
:erlang.float_to_list(float, expand_compact(options))
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use :erlang.float_to_binary/2 instead"
|
||||
# TODO: Remove by 2.0
|
||||
# (hard-deprecated in elixir_dispatch)
|
||||
def to_string(float, options) do
|
||||
:erlang.float_to_binary(float, expand_compact(options))
|
||||
end
|
||||
|
||||
@@ -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
|
||||
+16
-24
@@ -1,8 +1,10 @@
|
||||
defmodule GenEvent do
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
# 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
|
||||
@@ -41,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}
|
||||
@@ -83,15 +83,14 @@ 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__
|
||||
|
||||
deprecation_message =
|
||||
"the GenEvent module is deprecated, see its documentation for alternatives"
|
||||
|
||||
IO.warn(deprecation_message, Macro.Env.stacktrace(__CALLER__))
|
||||
:elixir_errors.warn(line, file, deprecation_message)
|
||||
|
||||
quote location: :keep do
|
||||
@behaviour :gen_event
|
||||
@@ -149,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)
|
||||
@@ -180,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
|
||||
@@ -193,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)
|
||||
|
||||
@@ -245,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
|
||||
@@ -273,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)
|
||||
@@ -324,7 +309,14 @@ defmodule GenEvent do
|
||||
|
||||
def init_it(starter, parent, name, _mod, _args, options) do
|
||||
Process.put(:"$initial_call", {__MODULE__, :init_it, 6})
|
||||
debug = :gen.debug_options(name, options)
|
||||
|
||||
debug =
|
||||
if function_exported?(:gen, :debug_options, 2) do
|
||||
:gen.debug_options(name, options)
|
||||
else
|
||||
:gen.debug_options(options)
|
||||
end
|
||||
|
||||
:proc_lib.init_ack(starter, {:ok, self()})
|
||||
loop(parent, name(name), [], debug, false)
|
||||
end
|
||||
@@ -837,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}
|
||||
|
||||
+186
-360
@@ -16,7 +16,7 @@ defmodule GenServer do
|
||||
|
||||
Let's start with a code example and then explore the available callbacks.
|
||||
Imagine we want a GenServer that works like a stack, allowing us to push
|
||||
and pop elements:
|
||||
and pop items:
|
||||
|
||||
defmodule Stack do
|
||||
use GenServer
|
||||
@@ -29,13 +29,13 @@ defmodule GenServer do
|
||||
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, element}, state) do
|
||||
{:noreply, [element | state]}
|
||||
def handle_cast({:push, item}, state) do
|
||||
{:noreply, [item | state]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -52,108 +52,40 @@ 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 element `:hello`). We can primarily
|
||||
representing the stack containing the item `:hello`). We can primarily
|
||||
interact with the server by sending two types of messages. **call**
|
||||
messages expect a reply from the server (and are therefore synchronous)
|
||||
while **cast** messages do not.
|
||||
|
||||
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 `c: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, element) do
|
||||
GenServer.cast(pid, {:push, element})
|
||||
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, element}, state) do
|
||||
{:noreply, [element | 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
|
||||
@@ -162,7 +94,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
|
||||
@@ -180,34 +112,76 @@ 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)
|
||||
|
||||
@impl true
|
||||
def handle_call(:pop, _from, [h | t]) do
|
||||
{:reply, h, t}
|
||||
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.
|
||||
|
||||
## 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.
|
||||
|
||||
@@ -218,100 +192,35 @@ 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
|
||||
|
||||
## Timeouts
|
||||
|
||||
The return value of `c:init/1` or any of the `handle_*` callbacks may include
|
||||
a timeout value in milliseconds; if not, `:infinity` is assumed.
|
||||
The timeout can be used to detect a lull in incoming messages.
|
||||
|
||||
If the process has no messages waiting when the timeout is set and the
|
||||
number of given milliseconds pass without any message arriving,
|
||||
then `handle_info/2` will be called with `:timeout` as the first argument.
|
||||
The timeout is cleared if any message is waiting or arrives before the
|
||||
given timeout.
|
||||
|
||||
Because a message may arrive before the timeout is set, even a timeout of `0`
|
||||
milliseconds is not guaranteed to execute. To take another action immediately
|
||||
and unconditionally, use a `:continue` instruction.
|
||||
|
||||
## 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:
|
||||
@@ -346,93 +255,67 @@ 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.
|
||||
|
||||
Returning `{:ok, state, timeout}` is similar to `{:ok, state}`,
|
||||
except that it also sets a timeout. See the "Timeouts" section
|
||||
in the module documentation for more information.
|
||||
Returning `{:ok, state, timeout}` is similar to `{:ok, state}`
|
||||
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
|
||||
@@ -443,9 +326,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
|
||||
@@ -462,8 +345,8 @@ defmodule GenServer do
|
||||
caller and continues the loop with new state `new_state`.
|
||||
|
||||
Returning `{:reply, reply, new_state, timeout}` is similar to
|
||||
`{:reply, reply, new_state}` except that it also sets a timeout.
|
||||
See the "Timeouts" section in the module documentation for more information.
|
||||
`{:reply, reply, new_state}` except `handle_info(:timeout, new_state)` will be
|
||||
called after `timeout` milliseconds if no messages are received.
|
||||
|
||||
Returning `{:reply, reply, new_state, :hibernate}` is similar to
|
||||
`{:reply, reply, new_state}` except the process is hibernated and will
|
||||
@@ -472,10 +355,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
|
||||
@@ -497,9 +376,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
|
||||
@@ -508,14 +387,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
|
||||
@@ -528,28 +408,25 @@ defmodule GenServer do
|
||||
|
||||
Returning `{:noreply, new_state}` continues the loop with new state `new_state`.
|
||||
|
||||
Returning `{:noreply, new_state, timeout}` is similar to `{:noreply, new_state}`
|
||||
except that it also sets a timeout. See the "Timeouts" section in the module
|
||||
documentation for more information.
|
||||
Returning `{:noreply, new_state, timeout}` is similar to
|
||||
`{:noreply, new_state}` except `handle_info(:timeout, new_state)` will be
|
||||
called after `timeout` milliseconds if no messages are received.
|
||||
|
||||
Returning `{:noreply, new_state, :hibernate}` is similar to
|
||||
`{: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
|
||||
|
||||
@@ -561,31 +438,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
|
||||
|
||||
@@ -605,20 +463,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` 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 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
|
||||
@@ -629,15 +480,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}
|
||||
@@ -660,13 +508,12 @@ 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}
|
||||
| {:error, reason :: term}
|
||||
when old_vsn: term | {:down, term}
|
||||
| {:down, term}
|
||||
when old_vsn: term
|
||||
|
||||
@doc """
|
||||
Invoked in some cases to retrieve a formatted version of the `GenServer` status.
|
||||
@@ -688,13 +535,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}
|
||||
@@ -711,17 +552,11 @@ 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()}]
|
||||
|
||||
@typedoc """
|
||||
The server reference.
|
||||
|
||||
This is either a plain PID or a value representing a registered name.
|
||||
See the "Name registration" section of this document for more information.
|
||||
"""
|
||||
@typedoc "The server reference"
|
||||
@type server :: pid | name | {atom, node}
|
||||
|
||||
@typedoc """
|
||||
@@ -737,18 +572,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)))
|
||||
@@ -756,7 +588,7 @@ defmodule GenServer do
|
||||
|
||||
defoverridable child_spec: 1
|
||||
|
||||
# TODO: Remove this on v2.0
|
||||
# TODO: Remove this on Elixir v2.0
|
||||
@before_compile GenServer
|
||||
|
||||
@doc false
|
||||
@@ -818,7 +650,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
|
||||
|
||||
@@ -830,20 +662,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.
|
||||
"""
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
: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
|
||||
@@ -857,7 +689,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.
|
||||
|
||||
@@ -869,7 +701,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
|
||||
@@ -880,10 +712,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
|
||||
@@ -897,8 +725,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 """
|
||||
@@ -907,27 +735,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
|
||||
@@ -992,8 +820,7 @@ defmodule GenServer do
|
||||
element.
|
||||
"""
|
||||
@spec call(server, term, timeout) :: term
|
||||
def call(server, request, timeout \\ 5000)
|
||||
when (is_integer(timeout) and timeout >= 0) or timeout == :infinity do
|
||||
def call(server, request, timeout \\ 5000) do
|
||||
case whereis(server) do
|
||||
nil ->
|
||||
exit({:noproc, {__MODULE__, :call, [server, request, timeout]}})
|
||||
@@ -1023,16 +850,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.
|
||||
|
||||
`server` can be any of the values described in the "Name registration"
|
||||
section of the documentation for this module.
|
||||
|
||||
Before Erlang/OTP 21, the call is going to block until a
|
||||
connection happens. This was done to guarantee ordering.
|
||||
Starting with Erlang/OTP 21, both Erlang and Elixir do
|
||||
not block the call.
|
||||
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)
|
||||
@@ -1158,8 +980,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 """
|
||||
|
||||
@@ -1,11 +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
|
||||
|
||||
@@ -22,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))
|
||||
@@ -47,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
|
||||
@@ -66,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}
|
||||
@@ -74,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)}}
|
||||
|
||||
@@ -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
|
||||
|
||||
+31
-112
@@ -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
|
||||
@@ -31,7 +29,7 @@ defprotocol Inspect do
|
||||
end
|
||||
end
|
||||
|
||||
The [`concat/1`](`Inspect.Algebra.concat/1`) function comes from `Inspect.Algebra` and it
|
||||
The `concat/1` function comes from `Inspect.Algebra` and it
|
||||
concatenates algebra documents together. In the example above,
|
||||
it is concatenating the string `"MapSet<"` (all strings are
|
||||
valid algebra documents that keep their formatting when pretty
|
||||
@@ -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,44 +46,17 @@ 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
|
||||
@fallback_to_any true
|
||||
|
||||
@doc """
|
||||
Converts `term` into an algebra document.
|
||||
|
||||
This function shouldn't be invoked directly, unless when implementing
|
||||
a custom `inspect_fun` to be given to `Inspect.Opts`. Everywhere else,
|
||||
`Inspect.Algebra.to_doc/2` should be preferred as it handles structs
|
||||
and exceptions.
|
||||
"""
|
||||
@spec inspect(t, Inspect.Opts.t()) :: Inspect.Algebra.t()
|
||||
def inspect(term, opts)
|
||||
end
|
||||
|
||||
@@ -169,7 +140,7 @@ defimpl Inspect, for: List do
|
||||
color("[]", :list, opts)
|
||||
end
|
||||
|
||||
# TODO: Remove :char_list and :as_char_lists handling on v2.0
|
||||
# TODO: Remove :char_list and :as_char_lists handling in 2.0
|
||||
def inspect(term, opts) do
|
||||
%Inspect.Opts{
|
||||
charlists: lists,
|
||||
@@ -316,20 +287,11 @@ end
|
||||
|
||||
defimpl Inspect, for: Regex do
|
||||
def inspect(regex, opts) do
|
||||
{escaped, _} =
|
||||
regex.source
|
||||
|> normalize(<<>>)
|
||||
|> Identifier.escape(?/, :infinity, &escape_map/1)
|
||||
|
||||
{escaped, _} = Identifier.escape(regex.source, ?/, :infinity, &escape_map/1)
|
||||
source = IO.iodata_to_binary(['~r/', escaped, ?/, regex.opts])
|
||||
color(source, :regex, opts)
|
||||
end
|
||||
|
||||
defp normalize(<<?\\, ?\\, rest::binary>>, acc), do: normalize(rest, <<acc::binary, ?\\, ?\\>>)
|
||||
defp normalize(<<?\\, ?/, rest::binary>>, acc), do: normalize(rest, <<acc::binary, ?/>>)
|
||||
defp normalize(<<char, rest::binary>>, acc), do: normalize(rest, <<acc::binary, char>>)
|
||||
defp normalize(<<>>, acc), do: acc
|
||||
|
||||
defp escape_map(?\a), do: '\\a'
|
||||
defp escape_map(?\f), do: '\\f'
|
||||
defp escape_map(?\n), do: '\\n'
|
||||
@@ -341,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
|
||||
|
||||
@@ -408,38 +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))
|
||||
name = Identifier.inspect_as_atom(unquote(module))
|
||||
unquote(inspect_module).inspect(map, name, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def inspect(%module{} = struct, opts) do
|
||||
try do
|
||||
module.__struct__
|
||||
@@ -449,23 +380,11 @@ defimpl Inspect, for: Any do
|
||||
dunder ->
|
||||
if :maps.keys(dunder) == :maps.keys(struct) do
|
||||
pruned = :maps.remove(:__exception__, :maps.remove(:__struct__, struct))
|
||||
Inspect.Map.inspect(pruned, Identifier.inspect_as_atom(module), opts)
|
||||
colorless_opts = %{opts | syntax_colors: []}
|
||||
Inspect.Map.inspect(pruned, Inspect.Atom.inspect(module, colorless_opts), opts)
|
||||
else
|
||||
Inspect.Map.inspect(struct, opts)
|
||||
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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -13,36 +13,28 @@ defmodule Inspect.Opts do
|
||||
When `:as_binaries` all binaries will be printed in bit syntax.
|
||||
|
||||
When the default `:infer`, the binary will be printed as a string if it
|
||||
is printable, otherwise in bit syntax. See `String.printable?/1` to learn
|
||||
when a string is printable.
|
||||
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.
|
||||
|
||||
When the default `:infer`, the list will be printed as a charlist if it
|
||||
is printable, otherwise as list. See `List.ascii_printable?/1` to learn
|
||||
when a charlist is printable.
|
||||
is printable, otherwise as list.
|
||||
|
||||
* `:limit` - limits the number of items that are inspected for tuples,
|
||||
* `: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 printable strings nor printable 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 characters that are inspected
|
||||
on printable strings and printable charlists. You can use `String.printable?/1`
|
||||
and `List.ascii_printable?/1` to check if a given string or charlist is
|
||||
printable. Defaults to 4096. If you don't want to limit the number of
|
||||
characters to a particular number, use `:infinity`.
|
||||
* `:printable_limit` - limits the number of bytes that are printed for strings
|
||||
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
|
||||
@@ -51,24 +43,17 @@ defmodule Inspect.Opts do
|
||||
* `:safe` - when `false`, failures while inspecting structs will be raised
|
||||
as errors instead of being wrapped in the `Inspect.Error` exception. This
|
||||
is useful when debugging failures and crashes for custom inspect
|
||||
implementations.
|
||||
implementations
|
||||
|
||||
* `:syntax_colors` - when set to a keyword list of colors the output is
|
||||
colorized. The keys are types and the values are the colors to use for
|
||||
* `:syntax_colors` - when set to a keyword list of colors the output will
|
||||
be colorized. The keys are types and the values are the colors to use for
|
||||
each type (for example, `[number: :red, atom: :blue]`). Types can include
|
||||
`:number`, `:atom`, `regex`, `:tuple`, `:map`, `:list`, and `:reset`.
|
||||
Colors can be any `t:IO.ANSI.ansidata/0` as accepted by `IO.ANSI.format/1`.
|
||||
|
||||
* `:inspect_fun` (since v1.9.0) - a function to build algebra documents,
|
||||
defaults to `Inspect.inspect/2`
|
||||
|
||||
* `:custom_options` (since v1.9.0) - a keyword list storing custom user-defined
|
||||
options. Useful when implementing the `Inspect` protocol for nested structs
|
||||
to pass the custom options through.
|
||||
|
||||
"""
|
||||
|
||||
# TODO: Remove :char_lists key on v2.0
|
||||
# TODO: Remove :char_lists key by 2.0
|
||||
defstruct structs: true,
|
||||
binaries: :infer,
|
||||
charlists: :infer,
|
||||
@@ -79,13 +64,11 @@ defmodule Inspect.Opts do
|
||||
base: :decimal,
|
||||
pretty: false,
|
||||
safe: true,
|
||||
syntax_colors: [],
|
||||
inspect_fun: &Inspect.inspect/2,
|
||||
custom_options: []
|
||||
syntax_colors: []
|
||||
|
||||
@type color_key :: atom
|
||||
|
||||
# TODO: Remove :char_lists key and :as_char_lists value on v2.0
|
||||
# TODO: Remove :char_lists key and :as_char_lists value by 2.0
|
||||
@type t :: %__MODULE__{
|
||||
structs: boolean,
|
||||
binaries: :infer | :as_binaries | :as_strings,
|
||||
@@ -97,9 +80,7 @@ defmodule Inspect.Opts do
|
||||
base: :decimal | :binary | :hex | :octal,
|
||||
pretty: boolean,
|
||||
safe: boolean,
|
||||
syntax_colors: [{color_key, IO.ANSI.ansidata()}],
|
||||
inspect_fun: (any, t -> Inspect.Algebra.t()),
|
||||
custom_options: keyword
|
||||
syntax_colors: [{color_key, IO.ANSI.ansidata()}]
|
||||
}
|
||||
end
|
||||
|
||||
@@ -120,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"
|
||||
@@ -129,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"]
|
||||
|
||||
@@ -164,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
|
||||
@@ -248,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,
|
||||
@@ -260,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
|
||||
|
||||
@@ -273,12 +270,14 @@ defmodule Inspect.Algebra do
|
||||
@spec to_doc(any, Inspect.Opts.t()) :: t
|
||||
def to_doc(term, opts)
|
||||
|
||||
def to_doc(%_{} = struct, %Inspect.Opts{inspect_fun: fun} = opts) do
|
||||
def to_doc(%_{} = struct, %Inspect.Opts{} = opts) do
|
||||
if opts.structs do
|
||||
try do
|
||||
fun.(struct, opts)
|
||||
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
|
||||
@@ -303,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)
|
||||
@@ -315,8 +314,8 @@ defmodule Inspect.Algebra do
|
||||
end
|
||||
end
|
||||
|
||||
def to_doc(arg, %Inspect.Opts{inspect_fun: fun} = opts) do
|
||||
fun.(arg, opts)
|
||||
def to_doc(arg, %Inspect.Opts{} = opts) do
|
||||
Inspect.inspect(arg, opts)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -328,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
|
||||
@@ -340,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
|
||||
@@ -375,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))
|
||||
@@ -427,13 +421,13 @@ defmodule Inspect.Algebra do
|
||||
defp simple?(other), do: is_binary(other)
|
||||
|
||||
@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
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Inspect.Algebra.container_doc/6 instead"
|
||||
# TODO: Deprecate on Elixir v1.8
|
||||
def surround_many(
|
||||
left,
|
||||
docs,
|
||||
@@ -453,7 +447,7 @@ defmodule Inspect.Algebra do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Inspect.Algebra.empty()
|
||||
iex> Inspect.Algebra.empty
|
||||
:doc_nil
|
||||
|
||||
"""
|
||||
@@ -488,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))
|
||||
@@ -527,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
|
||||
@@ -558,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
|
||||
@@ -612,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)
|
||||
@@ -659,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)
|
||||
@@ -669,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)
|
||||
@@ -700,10 +689,9 @@ defmodule Inspect.Algebra do
|
||||
to the document fitting. On the other hand, they are more expensive
|
||||
since each break needs to be re-evaluated.
|
||||
|
||||
This function is used by `container_doc/6` and friends to the
|
||||
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)
|
||||
@@ -716,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))
|
||||
@@ -755,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
|
||||
@@ -804,26 +789,23 @@ 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
|
||||
|
||||
@doc ~S"""
|
||||
Inserts a mandatory linebreak between two documents.
|
||||
|
||||
See `line/0`.
|
||||
See `line/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -845,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"]
|
||||
|
||||
@@ -896,11 +877,10 @@ defmodule Inspect.Algebra do
|
||||
# * flat_no_break - represents a document with breaks as flat not allowed to enter in break mode
|
||||
# * break_no_flat - represents a document with breaks as breaks not allowed to enter in flat mode
|
||||
#
|
||||
@typep mode :: :flat | :flat_no_break | :break | :break_no_flat
|
||||
@typep mode :: :flat | :flat_no_break | :break
|
||||
|
||||
@spec fits?(width :: integer(), column :: integer(), break? :: boolean(), entries) :: boolean()
|
||||
when entries:
|
||||
maybe_improper_list({integer(), mode(), t()}, {:tail, boolean(), entries} | [])
|
||||
when entries: [{integer(), mode(), t()}] | {:tail, boolean(), entries}
|
||||
|
||||
# We need at least a break to consider the document does not fit since a
|
||||
# large document without breaks has no option but fitting its current line.
|
||||
|
||||
+20
-26
@@ -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
|
||||
@@ -269,9 +265,6 @@ defmodule Integer do
|
||||
|
||||
defp count_digits_nosign(<<_::binary>>, _, count), do: count
|
||||
|
||||
# TODO: Remove Integer.to_string/1 once the minimum supported version is
|
||||
# Erlang/OTP 22, since it is covered by the now BIF Integer.to_string/2.
|
||||
# Please reapply commit 2622fd6b0aa419a983a899a1fbdb5deefba3d85d.
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `integer`.
|
||||
@@ -314,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"
|
||||
|
||||
"""
|
||||
@@ -323,9 +316,6 @@ defmodule Integer do
|
||||
:erlang.integer_to_binary(integer, base)
|
||||
end
|
||||
|
||||
# TODO: Remove Integer.to_charlist/1 once the minimum supported version is
|
||||
# Erlang/OTP 22, since it is covered by the now BIF Integer.to_charlist/2.
|
||||
# Please reapply commit 2622fd6b0aa419a983a899a1fbdb5deefba3d85d.
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation of the given `integer`.
|
||||
|
||||
@@ -366,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'
|
||||
|
||||
"""
|
||||
@@ -404,8 +394,8 @@ defmodule Integer do
|
||||
0
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec gcd(integer, integer) :: non_neg_integer
|
||||
@spec gcd(0, 0) :: 0
|
||||
@spec gcd(integer, integer) :: pos_integer
|
||||
def gcd(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
|
||||
gcd_positive(abs(integer1), abs(integer2))
|
||||
end
|
||||
@@ -414,11 +404,15 @@ defmodule Integer do
|
||||
defp gcd_positive(integer1, 0), do: integer1
|
||||
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
|
||||
|
||||
+55
-156
@@ -1,5 +1,5 @@
|
||||
defmodule IO do
|
||||
@moduledoc ~S"""
|
||||
@moduledoc """
|
||||
Functions handling input/output (IO).
|
||||
|
||||
Many functions in this module expect an IO device as an argument.
|
||||
@@ -7,10 +7,13 @@ defmodule IO do
|
||||
For convenience, Elixir provides `:stdio` and `:stderr` as
|
||||
shortcuts to Erlang's `:standard_io` and `:standard_error`.
|
||||
|
||||
The majority of the functions expect chardata. In case another type is given,
|
||||
functions will convert those types to string via the `String.Chars` protocol
|
||||
(as shown in typespecs). For more information on chardata, see the
|
||||
"IO data" section below.
|
||||
The majority of the functions expect chardata, i.e. strings or
|
||||
lists of characters and strings. In case another type is given,
|
||||
functions will convert to string via the `String.Chars` protocol
|
||||
(as shown in typespecs).
|
||||
|
||||
The functions starting with `bin` expect iodata as an argument,
|
||||
i.e. binaries or lists of bytes and binaries.
|
||||
|
||||
## IO devices
|
||||
|
||||
@@ -29,100 +32,17 @@ defmodule IO do
|
||||
was last accessed. The position of files can be changed using the
|
||||
`:file.position/2` function.
|
||||
|
||||
## IO data
|
||||
|
||||
IO data is a data type that can be used as a more efficient alternative to binaries
|
||||
in certain situations.
|
||||
|
||||
A term of type **IO data** is a binary or a list containing bytes (integers in `0..255`)
|
||||
or nested IO data. The type is recursive. Let's see an example of one of
|
||||
the possible IO data representing the binary `"hello"`:
|
||||
|
||||
[?h, "el", ["l", [?o]]]
|
||||
|
||||
The built-in `t:iodata/0` type is defined in terms of `t:iolist/0`. An IO list is
|
||||
the same as IO data but it doesn't allow for a binary at the top level (but binaries
|
||||
are still allowed in the list itself).
|
||||
|
||||
### Use cases for IO data
|
||||
|
||||
IO data exists because often you need to do many append operations
|
||||
on smaller chunks of binaries in order to create a bigger binary. However, in
|
||||
Erlang and Elixir concatenating binaries will copy the concatenated binaries
|
||||
into a new binary.
|
||||
|
||||
def email(username, domain) do
|
||||
username <> "@" <> domain
|
||||
end
|
||||
|
||||
In this function, creating the email address will copy the `username` and `domain`
|
||||
binaries. Now imagine you want to use the resulting email inside another binary:
|
||||
|
||||
def welcome_message(name, username, domain) do
|
||||
"Welcome #{name}, your email is: #{email(username, domain)}"
|
||||
end
|
||||
|
||||
IO.puts(welcome_message("Meg", "meg", "example.com"))
|
||||
#=> "Welcome Meg, your email is: meg@example.com"
|
||||
|
||||
Every time you concatenate binaries or use interpolation (`#{}`) you are making
|
||||
copies of those binaries. However, in many cases you don't need the complete
|
||||
binary while you create it, but only at the end to print it out or send it
|
||||
somewhere. In such cases, you can construct the binary by creating IO data:
|
||||
|
||||
def email(username, domain) do
|
||||
[username, ?@, domain]
|
||||
end
|
||||
|
||||
def welcome_message(name, username, domain) do
|
||||
["Welcome ", name, ", your email is: ", email(username, domain)]
|
||||
end
|
||||
|
||||
IO.puts(welcome_message("Meg", "meg", "example.com"))
|
||||
#=> "Welcome Meg, your email is: meg@example.com"
|
||||
|
||||
Building IO data is cheaper than concatenating binaries. Concatenating multiple
|
||||
pieces of IO data just means putting them together inside a list since IO data
|
||||
can be arbitrarily nested, and that's a cheap and efficient operation. Most of
|
||||
the IO-based APIs, such as `:gen_tcp`, `IO`, etc, receive IO data and write it
|
||||
to the socket directly without converting it to binary.
|
||||
|
||||
One drawback of IO data is that you can't do things like pattern match on the
|
||||
first part of a piece of IO data like you can with a binary, because you usually
|
||||
don't know the shape of the IO data. In those cases, you may need to convert it
|
||||
to a binary by calling `iodata_to_binary/1`, which is reasonably efficient
|
||||
since it's implemented natively in C. Other functionality, like computing the
|
||||
length of IO data, can be computed directly on the iodata by calling `iodata_length/1`.
|
||||
|
||||
### Chardata
|
||||
|
||||
Erlang and Elixir also have the idea of `t:chardata/0`. Chardata is very
|
||||
similar to IO data: the only difference is that integers in IO data represent
|
||||
bytes while integers in chardata represent Unicode codepoints. Bytes
|
||||
(`t:byte/0`) are integers in the `0..255` range, while Unicode codepoints
|
||||
(`t:char/0`) are integers in the range `0..0x10FFFF`. The `IO` module provides
|
||||
the `chardata_to_string/1` function for chardata as the "counter-part" of the
|
||||
`iodata_to_binary/1` function for IO data.
|
||||
|
||||
If you try to use `iodata_to_binary/1` on chardata, it will result in an
|
||||
argument error. For example, let's try to put a codepoint that is not
|
||||
representable with one byte, like `?π`, inside IO data:
|
||||
|
||||
iex> IO.iodata_to_binary(["The symbol for pi is: ", ?π])
|
||||
** (ArgumentError) argument error
|
||||
|
||||
If we use chardata instead, it will work as expected:
|
||||
|
||||
iex> IO.chardata_to_string(["The symbol for pi is: ", ?π])
|
||||
"The symbol for pi is: π"
|
||||
|
||||
"""
|
||||
|
||||
@type device :: atom | pid
|
||||
@type nodata :: {:error, term} | :eof
|
||||
@type chardata :: String.t() | maybe_improper_list(char | chardata, String.t() | [])
|
||||
@type chardata() :: :unicode.chardata()
|
||||
|
||||
defguardp is_iodata(data) when is_list(data) or is_binary(data)
|
||||
defmacrop is_iodata(data) do
|
||||
quote do
|
||||
is_list(unquote(data)) or is_binary(unquote(data))
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads from the IO `device`.
|
||||
@@ -221,44 +141,37 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `chardata` to the given `device`.
|
||||
Writes `item` to the given `device`.
|
||||
|
||||
By default, the `device` is the standard output.
|
||||
|
||||
## Examples
|
||||
|
||||
IO.write("sample")
|
||||
IO.write "sample"
|
||||
#=> sample
|
||||
|
||||
IO.write(:stderr, "error")
|
||||
IO.write :stderr, "error"
|
||||
#=> error
|
||||
|
||||
"""
|
||||
@spec write(device, chardata | String.Chars.t()) :: :ok
|
||||
def write(device \\ :stdio, chardata) do
|
||||
:io.put_chars(map_dev(device), to_chardata(chardata))
|
||||
def write(device \\ :stdio, item) do
|
||||
:io.put_chars(map_dev(device), to_chardata(item))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `iodata` to the given `device`.
|
||||
Writes `item` as a binary to the given `device`.
|
||||
No Unicode conversion happens.
|
||||
The operation is Unicode unsafe.
|
||||
|
||||
This operation is meant to be used with "raw" devices
|
||||
that are started without an encoding. The given `iodata`
|
||||
is written as is to the device, without conversion. For
|
||||
more information on IO data, see the "IO data" section in
|
||||
the module documentation.
|
||||
Check `write/2` for more information.
|
||||
|
||||
Use `write/2` for devices with encoding.
|
||||
|
||||
Important: do **not** use this function on IO devices in
|
||||
Unicode mode as it will write the wrong data. In particular,
|
||||
the standard IO device is set to Unicode by default, so writing
|
||||
to stdio with this function will likely result in the wrong data
|
||||
being sent down the wire.
|
||||
Note: do not use this function on IO devices in Unicode mode
|
||||
as it will return the wrong result.
|
||||
"""
|
||||
@spec binwrite(device, iodata) :: :ok | {:error, term}
|
||||
def binwrite(device \\ :stdio, iodata) when is_iodata(iodata) do
|
||||
:file.write(map_dev(device), iodata)
|
||||
def binwrite(device \\ :stdio, item) when is_iodata(item) do
|
||||
:file.write(map_dev(device), item)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -270,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
|
||||
|
||||
"""
|
||||
@@ -294,29 +207,22 @@ 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
|
||||
|
||||
"""
|
||||
@spec warn(chardata | String.Chars.t(), Exception.stacktrace()) :: :ok
|
||||
def warn(message, []) do
|
||||
message = [to_chardata(message), ?\n]
|
||||
:elixir_errors.io_warn(nil, nil, message, message)
|
||||
:elixir_errors.bare_warn(nil, nil, [to_chardata(message), ?\n])
|
||||
end
|
||||
|
||||
def warn(message, [{_, _, _, opts} | _] = stacktrace) do
|
||||
message = to_chardata(message)
|
||||
formatted_trace = Enum.map_join(stacktrace, "\n ", &Exception.format_stacktrace_entry(&1))
|
||||
message = [to_chardata(message), ?\n, " ", formatted_trace, ?\n]
|
||||
line = opts[:line]
|
||||
file = opts[:file]
|
||||
|
||||
:elixir_errors.io_warn(
|
||||
line,
|
||||
file && List.to_string(file),
|
||||
message,
|
||||
[message, ?\n, " ", formatted_trace, ?\n]
|
||||
)
|
||||
:elixir_errors.bare_warn(line, file && List.to_string(file), message)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -326,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
|
||||
|
||||
@@ -357,7 +263,7 @@ defmodule IO do
|
||||
|
||||
## Examples
|
||||
|
||||
IO.inspect(<<0, 1, 2>>, width: 40)
|
||||
IO.inspect <<0, 1, 2>>, width: 40
|
||||
|
||||
Prints:
|
||||
|
||||
@@ -365,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:
|
||||
|
||||
@@ -377,7 +283,7 @@ defmodule IO do
|
||||
|> IO.inspect(label: "before")
|
||||
|> Enum.map(&(&1 * 2))
|
||||
|> IO.inspect(label: "after")
|
||||
|> Enum.sum()
|
||||
|> Enum.sum
|
||||
|
||||
Prints:
|
||||
|
||||
@@ -409,7 +315,7 @@ defmodule IO do
|
||||
Gets a number of bytes from IO device `:stdio`.
|
||||
|
||||
If `:stdio` is a Unicode device, `count` implies
|
||||
the number of Unicode code points to be retrieved.
|
||||
the number of Unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
|
||||
See `IO.getn/3` for a description of return values.
|
||||
@@ -431,7 +337,7 @@ defmodule IO do
|
||||
Gets a number of bytes from the IO `device`.
|
||||
|
||||
If the IO `device` is a Unicode device, `count` implies
|
||||
the number of Unicode code points to be retrieved.
|
||||
the number of Unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
|
||||
It returns:
|
||||
@@ -468,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
|
||||
@@ -497,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()
|
||||
@@ -533,10 +439,8 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts chardata into a string.
|
||||
|
||||
For more information about chardata, see the ["Chardata"](#module-chardata)
|
||||
section in the module documentation.
|
||||
Converts chardata (a list of integers representing codepoints,
|
||||
lists and strings) into a string.
|
||||
|
||||
In case the conversion fails, it raises an `UnicodeConversionError`.
|
||||
If a string is given, it returns the string itself.
|
||||
@@ -553,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
|
||||
@@ -563,16 +467,14 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts IO data into a binary
|
||||
|
||||
Converts iodata (a list of integers representing bytes, lists
|
||||
and binaries) into a binary.
|
||||
The operation is Unicode unsafe.
|
||||
|
||||
Notice that this function treats integers in the given IO data as
|
||||
raw bytes and does not perform any kind of encoding conversion.
|
||||
If you want to convert from a charlist to a UTF-8-encoded string,
|
||||
use `chardata_to_string/1` instead. For more information about
|
||||
IO data and chardata, see the ["IO data"](#module-io-data) section in the
|
||||
module documentation.
|
||||
Notice that this function treats lists of integers as raw bytes
|
||||
and does not perform any kind of encoding conversion. If you want
|
||||
to convert from a charlist to a string (UTF-8 encoded), please
|
||||
use `chardata_to_string/1` instead.
|
||||
|
||||
If this function receives a binary, the same binary is returned.
|
||||
|
||||
@@ -592,15 +494,12 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec iodata_to_binary(iodata) :: binary
|
||||
def iodata_to_binary(iodata) do
|
||||
:erlang.iolist_to_binary(iodata)
|
||||
def iodata_to_binary(item) do
|
||||
:erlang.iolist_to_binary(item)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the size of an IO data.
|
||||
|
||||
For more information about IO data, see the ["IO data"](#module-io-data)
|
||||
section in the module documentation.
|
||||
Returns the size of an iodata.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -611,8 +510,8 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec iodata_length(iodata) :: non_neg_integer
|
||||
def iodata_length(iodata) do
|
||||
:erlang.iolist_size(iodata)
|
||||
def iodata_length(item) do
|
||||
:erlang.iolist_size(item)
|
||||
end
|
||||
|
||||
@doc false
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -237,7 +210,7 @@ defmodule IO.ANSI do
|
||||
The named sequences are represented by atoms.
|
||||
|
||||
An optional boolean parameter can be passed to enable or disable
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will be emitted.
|
||||
emitting actual ANSI codes. When `false`, no ANSI codes will emitted.
|
||||
By default checks if ANSI is enabled using the `enabled?/0` function.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -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,20 +474,22 @@ 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
|
||||
Regex.replace(~r{[a-z][a-z0-9\+\-\.]*://\S*}i, text, &String.replace(&1, "_", "\\_"))
|
||||
~r{https?\S*}
|
||||
|> Regex.recompile!()
|
||||
|> Regex.replace(text, &String.replace(&1, "_", "\\_"))
|
||||
end
|
||||
|
||||
defp remove_square_brackets_in_link(text) do
|
||||
Regex.replace(~r{\[([^\]]*?)\]\((.*?)\)}, text, "\\1 (\\2)")
|
||||
~r{\[(.*?)\]\((.*?)\)}
|
||||
|> Regex.recompile!()
|
||||
|> Regex.replace(text, "\\1 (\\2)")
|
||||
end
|
||||
|
||||
# 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 *
|
||||
@@ -599,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,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))
|
||||
|
||||
+670
-812
File diff suppressed because it is too large
Load Diff
@@ -5,7 +5,7 @@ defmodule Kernel.CLI do
|
||||
commands: [],
|
||||
output: ".",
|
||||
compile: [],
|
||||
no_halt: false,
|
||||
halt: true,
|
||||
compiler_options: [],
|
||||
errors: [],
|
||||
pa: [],
|
||||
@@ -21,7 +21,6 @@ defmodule Kernel.CLI do
|
||||
|
||||
{config, argv} = parse_argv(argv)
|
||||
System.argv(argv)
|
||||
System.no_halt(config.no_halt)
|
||||
|
||||
fun = fn _ ->
|
||||
errors = process_commands(config)
|
||||
@@ -32,7 +31,7 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
end
|
||||
|
||||
run(fun)
|
||||
run(fun, config.halt)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -43,10 +42,10 @@ defmodule Kernel.CLI do
|
||||
This function is used by Elixir's CLI and also
|
||||
by escripts generated by Elixir.
|
||||
"""
|
||||
def run(fun) do
|
||||
def run(fun, halt \\ true) do
|
||||
{ok_or_shutdown, status} = exec_fun(fun, {:ok, 0})
|
||||
|
||||
if ok_or_shutdown == :shutdown or not System.no_halt() do
|
||||
if ok_or_shutdown == :shutdown or halt do
|
||||
{_, status} = at_exit({ok_or_shutdown, status})
|
||||
|
||||
# Ensure Logger messages are flushed before halting
|
||||
@@ -59,25 +58,19 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses the CLI arguments. Made public for testing.
|
||||
"""
|
||||
@doc false
|
||||
def parse_argv(argv) do
|
||||
parse_argv(argv, @blank_config)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Process CLI commands. Made public for testing.
|
||||
"""
|
||||
@doc false
|
||||
def process_commands(config) do
|
||||
results = Enum.map(Enum.reverse(config.commands), &process_command(&1, config))
|
||||
errors = for {:error, msg} <- results, do: msg
|
||||
Enum.reverse(config.errors, errors)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Shared helper for error formatting on CLI tools.
|
||||
"""
|
||||
@doc false
|
||||
def format_error(kind, reason, stacktrace) do
|
||||
{blamed, stacktrace} = Exception.blame(kind, reason, stacktrace)
|
||||
|
||||
@@ -95,15 +88,6 @@ defmodule Kernel.CLI do
|
||||
[iodata, ?\n, Exception.format_stacktrace(prune_stacktrace(stacktrace))]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Function invoked across nodes for `--rpc-eval`.
|
||||
"""
|
||||
def rpc_eval(expr) do
|
||||
wrapper(fn -> :elixir.eval(to_charlist(expr), [], []) end)
|
||||
catch
|
||||
kind, reason -> {kind, reason, __STACKTRACE__}
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp at_exit(res) do
|
||||
@@ -132,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})
|
||||
@@ -191,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]
|
||||
|
||||
@@ -241,22 +226,13 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
|
||||
defp parse_shared(["--no-halt" | t], config) do
|
||||
parse_shared(t, %{config | no_halt: true})
|
||||
parse_shared(t, %{config | halt: false})
|
||||
end
|
||||
|
||||
defp parse_shared(["-e", h | t], config) do
|
||||
parse_shared(t, %{config | commands: [{:eval, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_shared(["--eval", h | t], config) do
|
||||
parse_shared(t, %{config | commands: [{:eval, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_shared(["--rpc-eval", node, h | t], config) do
|
||||
node = append_hostname(node)
|
||||
parse_shared(t, %{config | commands: [{:rpc_eval, node, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_shared(["-r", h | t], config) do
|
||||
parse_shared(t, %{config | commands: [{:require, h} | config.commands]})
|
||||
end
|
||||
@@ -265,15 +241,21 @@ defmodule Kernel.CLI do
|
||||
parse_shared(t, %{config | commands: [{:parallel_require, h} | config.commands]})
|
||||
end
|
||||
|
||||
defp parse_shared(list, config) do
|
||||
{list, config}
|
||||
@erl_arg_options ["--erl", "--sname", "--name", "--cookie"] ++
|
||||
["--logger-otp-reports", "--logger-sasl-reports"]
|
||||
|
||||
@erl_boolean_options ["--detached", "--hidden", "--werl"]
|
||||
|
||||
defp parse_shared([erl, _ | t], config) when erl in @erl_arg_options do
|
||||
parse_shared(t, config)
|
||||
end
|
||||
|
||||
defp append_hostname(node) do
|
||||
case :string.find(node, "@") do
|
||||
:nomatch -> node <> :string.find(Atom.to_string(node()), "@")
|
||||
_ -> node
|
||||
end
|
||||
defp parse_shared([erl | t], config) when erl in @erl_boolean_options do
|
||||
parse_shared(t, config)
|
||||
end
|
||||
|
||||
defp parse_shared(list, config) do
|
||||
{list, config}
|
||||
end
|
||||
|
||||
defp expand_code_path(path) do
|
||||
@@ -309,7 +291,7 @@ defmodule Kernel.CLI do
|
||||
shared_option?(list, config, &parse_argv(&1, &2))
|
||||
|
||||
_ ->
|
||||
if List.keymember?(config.commands, :eval, 0) do
|
||||
if Keyword.has_key?(config.commands, :eval) do
|
||||
{config, list}
|
||||
else
|
||||
{%{config | commands: [{:file, h} | config.commands]}, t}
|
||||
@@ -414,15 +396,6 @@ defmodule Kernel.CLI do
|
||||
wrapper(fn -> Code.eval_string(expr, []) end)
|
||||
end
|
||||
|
||||
defp process_command({:rpc_eval, node, expr}, _config) when is_binary(expr) do
|
||||
case :rpc.call(String.to_atom(node), __MODULE__, :rpc_eval, [expr]) do
|
||||
:ok -> :ok
|
||||
{:badrpc, {:EXIT, exit}} -> Process.exit(self(), exit)
|
||||
{:badrpc, reason} -> {:error, "--rpc-eval : RPC failed with reason #{inspect(reason)}"}
|
||||
{kind, error, stack} -> :erlang.raise(kind, error, stack)
|
||||
end
|
||||
end
|
||||
|
||||
defp process_command({:app, app}, _config) when is_binary(app) do
|
||||
case Application.ensure_all_started(String.to_atom(app)) do
|
||||
{:error, {app, reason}} ->
|
||||
|
||||
@@ -5,13 +5,13 @@ defmodule Kernel.ErrorHandler do
|
||||
|
||||
@spec undefined_function(module, atom, list) :: term
|
||||
def undefined_function(module, fun, args) do
|
||||
ensure_loaded(module) or ensure_compiled(module, :module, :raise)
|
||||
ensure_loaded(module) or ensure_compiled(module, :module)
|
||||
:error_handler.undefined_function(module, fun, args)
|
||||
end
|
||||
|
||||
@spec undefined_lambda(module, fun, list) :: term
|
||||
def undefined_lambda(module, fun, args) do
|
||||
ensure_loaded(module) or ensure_compiled(module, :module, :raise)
|
||||
ensure_loaded(module) or ensure_compiled(module, :module)
|
||||
:error_handler.undefined_lambda(module, fun, args)
|
||||
end
|
||||
|
||||
@@ -23,21 +23,21 @@ defmodule Kernel.ErrorHandler do
|
||||
end
|
||||
end
|
||||
|
||||
@spec ensure_compiled(module, atom, atom) :: :found | :not_found | :deadlock
|
||||
@spec ensure_compiled(module, atom) :: boolean
|
||||
# Never wait on nil because it should never be defined.
|
||||
def ensure_compiled(nil, _kind, _deadlock) do
|
||||
:not_found
|
||||
def ensure_compiled(nil, _kind) do
|
||||
false
|
||||
end
|
||||
|
||||
def ensure_compiled(module, kind, deadlock) do
|
||||
def ensure_compiled(module, kind) do
|
||||
parent = :erlang.get(:elixir_compiler_pid)
|
||||
ref = :erlang.make_ref()
|
||||
modules = :elixir_module.compiler_modules()
|
||||
send(parent, {:waiting, kind, self(), ref, module, modules, deadlock})
|
||||
send(parent, {:waiting, kind, self(), ref, module, :elixir_module.compiler_modules()})
|
||||
:erlang.garbage_collect(self())
|
||||
|
||||
receive do
|
||||
{^ref, value} -> value
|
||||
{^ref, :found} -> true
|
||||
{^ref, :not_found} -> false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,28 +12,44 @@ defmodule Kernel.LexicalTracker do
|
||||
@doc """
|
||||
Returns all remotes referenced in this lexical scope.
|
||||
"""
|
||||
def remote_references(pid) do
|
||||
:gen_server.call(pid, :remote_references, @timeout)
|
||||
def remote_references(arg) do
|
||||
:gen_server.call(to_pid(arg), :remote_references, @timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all remote dispatches in this lexical scope.
|
||||
"""
|
||||
def remote_dispatches(pid) do
|
||||
:gen_server.call(pid, :remote_dispatches, @timeout)
|
||||
def remote_dispatches(arg) 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
|
||||
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
|
||||
def stop(pid) do
|
||||
:gen_server.call(pid, :stop)
|
||||
:gen_server.cast(pid, :stop)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -109,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
|
||||
}
|
||||
@@ -142,12 +159,12 @@ defmodule Kernel.LexicalTracker do
|
||||
{:reply, {state.compile, state.runtime}, state}
|
||||
end
|
||||
|
||||
def handle_call({:read_cache, key}, _from, %{cache: cache} = state) do
|
||||
{:reply, :maps.get(key, cache), state}
|
||||
def handle_call(:dest, _from, state) do
|
||||
{:reply, state.dest, state}
|
||||
end
|
||||
|
||||
def handle_call(:stop, _from, state) do
|
||||
{:stop, :normal, :ok, state}
|
||||
def handle_call({:read_cache, key}, _from, %{cache: cache} = state) do
|
||||
{:reply, :maps.get(key, cache), state}
|
||||
end
|
||||
|
||||
def handle_cast({:write_cache, key, value}, %{cache: cache} = state) do
|
||||
@@ -159,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
|
||||
@@ -210,6 +227,10 @@ defmodule Kernel.LexicalTracker do
|
||||
{:noreply, %{state | directives: add_directive(state.directives, module, line, warn, :alias)}}
|
||||
end
|
||||
|
||||
def handle_cast(:stop, state) do
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, state) do
|
||||
{:noreply, state}
|
||||
|
||||
@@ -14,18 +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 ->
|
||||
send(parent, {:async, self()})
|
||||
: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)
|
||||
@@ -46,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
|
||||
@@ -67,18 +63,16 @@ defmodule Kernel.ParallelCompiler do
|
||||
* `:long_compilation_threshold` - the timeout (in seconds) after the
|
||||
`:each_long_compilation` callback is invoked; defaults to `15`
|
||||
|
||||
* `:dest` - the destination directory for the BEAM files. When using `compile/2`,
|
||||
* `:dest` - the destination directory for the BEAM files. When using `files/2`,
|
||||
this information is only used to properly annotate the BEAM files before
|
||||
they are loaded into memory. If you want a file to actually be written to
|
||||
`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
|
||||
@@ -91,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
|
||||
@@ -103,13 +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: 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,8 +110,8 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
end
|
||||
|
||||
# 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
|
||||
@@ -127,16 +120,16 @@ 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)
|
||||
|
||||
result =
|
||||
spawn_workers(files, 0, [], [], %{}, [], %{
|
||||
spawn_workers(files, [], [], [], [], %{
|
||||
dest: Keyword.get(options, :dest),
|
||||
each_cycle: Keyword.get(options, :each_cycle, fn -> [] end),
|
||||
each_file: Keyword.get(options, :each_file, fn _, _ -> :ok end) |> each_file(),
|
||||
each_file: Keyword.get(options, :each_file, fn _file -> :ok end),
|
||||
each_long_compilation: Keyword.get(options, :each_long_compilation, fn _file -> :ok end),
|
||||
each_module: Keyword.get(options, :each_module, fn _file, _module, _binary -> :ok end),
|
||||
output: output,
|
||||
@@ -162,189 +155,139 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
end
|
||||
|
||||
defp each_file(fun) when is_function(fun, 1), do: fn file, _ -> fun.(file) end
|
||||
defp each_file(fun) when is_function(fun, 2), do: fun
|
||||
|
||||
defp each_file(file, lexical, parent) do
|
||||
ref = Process.monitor(parent)
|
||||
send(parent, {:file_ok, self(), ref, file, lexical})
|
||||
|
||||
receive do
|
||||
^ref -> :ok
|
||||
{:DOWN, ^ref, _, _, _} -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
# We already have n=schedulers currently running, don't spawn new ones
|
||||
defp spawn_workers(
|
||||
queue,
|
||||
spawned,
|
||||
waiting,
|
||||
files,
|
||||
result,
|
||||
warnings,
|
||||
%{schedulers: schedulers} = state
|
||||
)
|
||||
when spawned - length(waiting) >= schedulers do
|
||||
wait_for_messages(queue, spawned, waiting, files, result, warnings, state)
|
||||
defp spawn_workers(files, waiting, queued, result, warnings, %{schedulers: schedulers} = state)
|
||||
when length(queued) - length(waiting) >= schedulers do
|
||||
wait_for_messages(files, waiting, queued, result, warnings, state)
|
||||
end
|
||||
|
||||
# Release waiting processes
|
||||
defp spawn_workers([{ref, found} | t], spawned, waiting, files, result, warnings, state) do
|
||||
defp spawn_workers([{ref, found} | t], waiting, queued, result, warnings, state) do
|
||||
waiting =
|
||||
case List.keytake(waiting, ref, 2) do
|
||||
{{_kind, pid, ^ref, _on, _defining, _deadlock}, waiting} ->
|
||||
{{_kind, pid, ^ref, _on, _defining}, waiting} ->
|
||||
send(pid, {ref, found})
|
||||
waiting
|
||||
|
||||
nil ->
|
||||
# In case the waiting process died (for example, it was an async process),
|
||||
# it will no longer be on the list. So we need to take it into account here.
|
||||
waiting
|
||||
end
|
||||
|
||||
spawn_workers(t, spawned, waiting, files, result, warnings, state)
|
||||
spawn_workers(t, waiting, queued, result, warnings, state)
|
||||
end
|
||||
|
||||
defp spawn_workers([file | queue], spawned, waiting, files, result, warnings, state) do
|
||||
defp spawn_workers([file | files], waiting, queued, result, warnings, state) do
|
||||
%{output: output, long_compilation_threshold: threshold, dest: dest} = state
|
||||
parent = self()
|
||||
file = Path.expand(file)
|
||||
|
||||
{pid, ref} =
|
||||
:erlang.spawn_monitor(fn ->
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
|
||||
try do
|
||||
case output do
|
||||
{:compile, path} ->
|
||||
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
|
||||
:erlang.put(:elixir_compiler_dest, path)
|
||||
:elixir_compiler.file_to_path(file, path, &each_file(&1, &2, parent))
|
||||
result =
|
||||
try do
|
||||
_ =
|
||||
case output do
|
||||
{:compile, path} ->
|
||||
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
|
||||
:elixir_compiler.file_to_path(file, path)
|
||||
|
||||
:compile ->
|
||||
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
|
||||
:erlang.put(:elixir_compiler_dest, dest)
|
||||
:elixir_compiler.file(file, &each_file(&1, &2, parent))
|
||||
:compile ->
|
||||
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
|
||||
:elixir_compiler.file(file, dest)
|
||||
|
||||
:require ->
|
||||
case :elixir_code_server.call({:acquire, file}) do
|
||||
:required ->
|
||||
send(parent, {:file_cancel, self()})
|
||||
|
||||
:proceed ->
|
||||
:elixir_compiler.file(file, &each_file(&1, &2, parent))
|
||||
:elixir_code_server.cast({:required, file})
|
||||
:require ->
|
||||
Code.require_file(file)
|
||||
end
|
||||
end
|
||||
catch
|
||||
kind, reason ->
|
||||
send(parent, {:file_error, self(), file, {kind, reason, __STACKTRACE__}})
|
||||
end
|
||||
|
||||
:ok
|
||||
catch
|
||||
kind, reason ->
|
||||
{kind, reason, System.stacktrace()}
|
||||
end
|
||||
|
||||
send(parent, {:file_done, self(), file, result})
|
||||
exit(:shutdown)
|
||||
end)
|
||||
|
||||
timer_ref = Process.send_after(self(), {:timed_out, pid}, threshold * 1000)
|
||||
files = [{pid, ref, file, timer_ref} | files]
|
||||
spawn_workers(queue, spawned + 1, waiting, files, result, warnings, state)
|
||||
queued = [{pid, ref, file, timer_ref} | queued]
|
||||
spawn_workers(files, waiting, queued, result, warnings, state)
|
||||
end
|
||||
|
||||
# No more queue, nothing waiting, this cycle is done
|
||||
defp spawn_workers([], 0, [], [], result, warnings, state) do
|
||||
# No more files, nothing waiting, queue is empty, this cycle is done
|
||||
defp spawn_workers([], [], [], result, warnings, state) do
|
||||
case state.each_cycle.() do
|
||||
[] ->
|
||||
modules = for {{:module, mod}, _} <- result, do: mod
|
||||
modules = for {:module, mod} <- result, do: mod
|
||||
warnings = Enum.reverse(warnings)
|
||||
{:ok, modules, warnings}
|
||||
|
||||
more ->
|
||||
spawn_workers(more, 0, [], [], result, warnings, state)
|
||||
spawn_workers(more, [], [], result, warnings, state)
|
||||
end
|
||||
end
|
||||
|
||||
# files x, waiting for x: POSSIBLE ERROR! Release processes so we get the failures
|
||||
# Queued x, waiting for x: POSSIBLE ERROR! Release processes so we get the failures
|
||||
defp spawn_workers([], waiting, queued, result, warnings, state)
|
||||
when length(waiting) == length(queued) do
|
||||
pending =
|
||||
for {pid, _, _, _} <- queued,
|
||||
entry = waiting_on_without_definition(waiting, pid),
|
||||
{_, _, ref, on, _} = entry,
|
||||
do: {on, {ref, :not_found}}
|
||||
|
||||
# Single entry, just release it.
|
||||
defp spawn_workers(
|
||||
[],
|
||||
1,
|
||||
[{_, pid, ref, _, _, _}] = waiting,
|
||||
[{pid, _, _, _}] = files,
|
||||
result,
|
||||
warnings,
|
||||
state
|
||||
) do
|
||||
spawn_workers([{ref, :not_found}], 1, waiting, files, result, warnings, state)
|
||||
end
|
||||
# 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
|
||||
# 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
|
||||
# sounds like a sane choice.
|
||||
pending
|
||||
|> Enum.group_by(&elem(&1, 0), &elem(&1, 1))
|
||||
|> Enum.sort_by(&length(elem(&1, 1)))
|
||||
|> case do
|
||||
[{_on, refs} | _] ->
|
||||
spawn_workers(refs, waiting, queued, result, warnings, state)
|
||||
|
||||
# Multiple entries, try to release modules.
|
||||
defp spawn_workers([], spawned, waiting, files, result, warnings, state)
|
||||
when length(waiting) == spawned do
|
||||
# There is potentially a deadlock. We will release modules with
|
||||
# the following order:
|
||||
#
|
||||
# 1. Code.ensure_compiled?/1 checks (deadlock = soft)
|
||||
# 2. Struct checks (deadlock = hard)
|
||||
# 3. Modules without a known definition
|
||||
# 4. Code invocation (deadlock = raise)
|
||||
#
|
||||
# In theory there is no difference between hard and raise, the
|
||||
# difference is where the raise is happening, inside the compiler
|
||||
# or in the caller.
|
||||
cond do
|
||||
deadlocked = deadlocked(waiting, :soft) || deadlocked(waiting, :hard) ->
|
||||
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, state)
|
||||
|
||||
without_definition = without_definition(waiting, files) ->
|
||||
spawn_workers(without_definition, spawned, waiting, files, result, warnings, state)
|
||||
|
||||
true ->
|
||||
errors = handle_deadlock(waiting, files)
|
||||
[] ->
|
||||
errors = handle_deadlock(waiting, queued)
|
||||
{:error, errors, warnings}
|
||||
end
|
||||
end
|
||||
|
||||
# No more queue, but spawned and length(waiting) do not match
|
||||
defp spawn_workers([], spawned, waiting, files, result, warnings, state) do
|
||||
wait_for_messages([], spawned, waiting, files, result, warnings, state)
|
||||
# No more files, but queue and waiting are not full or do not match
|
||||
defp spawn_workers([], waiting, queued, result, warnings, state) do
|
||||
wait_for_messages([], waiting, queued, result, warnings, state)
|
||||
end
|
||||
|
||||
# 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.
|
||||
defp without_definition(waiting, files) do
|
||||
nillify_empty(
|
||||
for {pid, _, _, _} <- files,
|
||||
{_, ^pid, ref, on, _, _} = List.keyfind(waiting, pid, 1),
|
||||
not Enum.any?(waiting, fn {_, _, _, _, defining, _} -> on in defining end),
|
||||
do: {ref, :not_found}
|
||||
)
|
||||
end
|
||||
defp waiting_on_without_definition(waiting, pid) do
|
||||
{_, ^pid, _, on, _} = entry = List.keyfind(waiting, pid, 1)
|
||||
|
||||
defp deadlocked(waiting, type) do
|
||||
nillify_empty(for {_, _, ref, _, _, ^type} <- waiting, do: {ref, :not_found})
|
||||
if Enum.any?(waiting, fn {_, _, _, _, defining} -> on in defining end) do
|
||||
nil
|
||||
else
|
||||
entry
|
||||
end
|
||||
end
|
||||
|
||||
defp nillify_empty([]), do: nil
|
||||
defp nillify_empty([_ | _] = list), do: list
|
||||
|
||||
# Wait for messages from child processes
|
||||
defp wait_for_messages(queue, spawned, waiting, files, result, warnings, state) do
|
||||
defp wait_for_messages(files, waiting, queued, result, warnings, state) do
|
||||
%{output: output} = state
|
||||
|
||||
receive do
|
||||
{:async, process} ->
|
||||
Process.monitor(process)
|
||||
wait_for_messages(queue, spawned + 1, waiting, files, result, warnings, state)
|
||||
|
||||
{:available, kind, module} ->
|
||||
{:struct_available, module} ->
|
||||
available =
|
||||
for {^kind, _, ref, ^module, _defining, _deadlock} <- waiting,
|
||||
for {:struct, _, ref, waiting_module, _defining} <- waiting,
|
||||
module == waiting_module,
|
||||
do: {ref, :found}
|
||||
|
||||
result = Map.put(result, {kind, module}, true)
|
||||
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
|
||||
result = [{:struct, module} | result]
|
||||
spawn_workers(available ++ files, waiting, queued, result, warnings, state)
|
||||
|
||||
{:module_available, child, ref, file, module, binary} ->
|
||||
state.each_module.(file, module, binary)
|
||||
@@ -353,77 +296,73 @@ defmodule Kernel.ParallelCompiler do
|
||||
send(child, {ref, :ack})
|
||||
|
||||
available =
|
||||
for {:module, _, ref, ^module, _defining, _deadlock} <- waiting,
|
||||
for {:module, _, ref, waiting_module, _defining} <- waiting,
|
||||
module == waiting_module,
|
||||
do: {ref, :found}
|
||||
|
||||
cancel_waiting_timer(files, child)
|
||||
result = Map.put(result, {:module, module}, true)
|
||||
spawn_workers(available ++ queue, spawned, waiting, files, result, warnings, state)
|
||||
cancel_waiting_timer(queued, child)
|
||||
|
||||
result = [{:module, module} | result]
|
||||
spawn_workers(available ++ files, waiting, queued, result, warnings, state)
|
||||
|
||||
# If we are simply requiring files, we do not add to waiting.
|
||||
{:waiting, _kind, child, ref, _on, _defining, _deadlock} when output == :require ->
|
||||
{:waiting, _kind, child, ref, _on, _defining} when output == :require ->
|
||||
send(child, {ref, :not_found})
|
||||
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
|
||||
spawn_workers(files, waiting, queued, result, warnings, state)
|
||||
|
||||
{:waiting, kind, child, ref, on, defining, deadlock?} ->
|
||||
# If we already got what we were waiting for, do not put it on waiting.
|
||||
{:waiting, kind, child, ref, on, defining} ->
|
||||
# Oops, we already got it, do not put it on waiting.
|
||||
# Alternatively, we're waiting on ourselves,
|
||||
# send :found so that we can crash with a better error.
|
||||
waiting =
|
||||
if Map.has_key?(result, {kind, on}) or on in defining do
|
||||
if :lists.any(&match?({^kind, ^on}, &1), result) or on in defining do
|
||||
send(child, {ref, :found})
|
||||
waiting
|
||||
else
|
||||
[{kind, child, ref, on, defining, deadlock?} | waiting]
|
||||
[{kind, child, ref, on, defining} | waiting]
|
||||
end
|
||||
|
||||
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
|
||||
spawn_workers(files, waiting, queued, result, warnings, state)
|
||||
|
||||
{:timed_out, child} ->
|
||||
case List.keyfind(files, child, 0) do
|
||||
{^child, _, file, _} -> state.each_long_compilation.(file)
|
||||
_ -> :ok
|
||||
case List.keyfind(queued, child, 0) do
|
||||
{^child, _, file, _} ->
|
||||
state.each_long_compilation.(file)
|
||||
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
|
||||
spawn_workers(queue, spawned, waiting, files, result, warnings, state)
|
||||
spawn_workers(files, waiting, queued, result, warnings, state)
|
||||
|
||||
{:warning, file, line, message} ->
|
||||
file = file && Path.absname(file)
|
||||
message = :unicode.characters_to_binary(message)
|
||||
warning = {file, line, message}
|
||||
wait_for_messages(queue, spawned, waiting, files, result, [warning | warnings], state)
|
||||
|
||||
{:file_ok, child_pid, ref, file, lexical} ->
|
||||
state.each_file.(file, lexical)
|
||||
send(child_pid, ref)
|
||||
cancel_waiting_timer(files, child_pid)
|
||||
wait_for_messages(files, waiting, queued, result, [warning | warnings], state)
|
||||
|
||||
{:file_done, child_pid, file, :ok} ->
|
||||
discard_down(child_pid)
|
||||
new_files = List.keydelete(files, child_pid, 0)
|
||||
state.each_file.(file)
|
||||
cancel_waiting_timer(queued, child_pid)
|
||||
|
||||
# Sometimes we may have spurious entries in the waiting list
|
||||
# because someone invoked try/rescue UndefinedFunctionError
|
||||
# Sometimes we may have spurious entries in the waiting
|
||||
# list because someone invoked try/rescue UndefinedFunctionError
|
||||
new_files = List.delete(files, child_pid)
|
||||
new_queued = List.keydelete(queued, child_pid, 0)
|
||||
new_waiting = List.keydelete(waiting, child_pid, 1)
|
||||
spawn_workers(queue, spawned - 1, new_waiting, new_files, result, warnings, state)
|
||||
spawn_workers(new_files, new_waiting, new_queued, result, warnings, state)
|
||||
|
||||
{:file_cancel, child_pid} ->
|
||||
cancel_waiting_timer(files, child_pid)
|
||||
{:file_done, child_pid, file, {kind, reason, stack}} ->
|
||||
discard_down(child_pid)
|
||||
new_files = List.keydelete(files, child_pid, 0)
|
||||
spawn_workers(queue, spawned - 1, waiting, new_files, result, warnings, state)
|
||||
|
||||
{:file_error, child_pid, file, {kind, reason, stack}} ->
|
||||
print_error(file, kind, reason, stack)
|
||||
cancel_waiting_timer(files, child_pid)
|
||||
discard_down(child_pid)
|
||||
files |> List.keydelete(child_pid, 0) |> terminate()
|
||||
cancel_waiting_timer(queued, child_pid)
|
||||
terminate(queued)
|
||||
{:error, [to_error(file, kind, reason, stack)], warnings}
|
||||
|
||||
{:DOWN, ref, :process, pid, reason} ->
|
||||
waiting = List.keydelete(waiting, pid, 1)
|
||||
|
||||
case handle_down(files, ref, reason) do
|
||||
:ok -> wait_for_messages(queue, spawned - 1, waiting, files, result, warnings, state)
|
||||
{:DOWN, ref, :process, _pid, reason} ->
|
||||
case handle_down(queued, ref, reason) do
|
||||
:ok -> wait_for_messages(files, waiting, queued, result, warnings, state)
|
||||
{:error, errors} -> {:error, errors, warnings}
|
||||
end
|
||||
end
|
||||
@@ -435,19 +374,15 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_down(_files, _ref, :normal) do
|
||||
defp handle_down(_queued, _ref, :normal) do
|
||||
:ok
|
||||
end
|
||||
|
||||
defp handle_down(files, ref, reason) do
|
||||
case List.keyfind(files, ref, 1) do
|
||||
{child_pid, ^ref, file, _timer_ref} ->
|
||||
defp handle_down(queued, ref, reason) do
|
||||
case List.keyfind(queued, ref, 1) do
|
||||
{_child, ^ref, file, _timer_ref} ->
|
||||
print_error(file, :exit, reason, [])
|
||||
|
||||
files
|
||||
|> List.keydelete(child_pid, 0)
|
||||
|> terminate()
|
||||
|
||||
terminate(queued)
|
||||
{:error, [to_error(file, :exit, reason, [])]}
|
||||
|
||||
_ ->
|
||||
@@ -455,17 +390,18 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_deadlock(waiting, files) do
|
||||
defp handle_deadlock(waiting, queued) do
|
||||
deadlock =
|
||||
for {pid, _, file, _} <- files do
|
||||
for {pid, _, file, _} <- queued 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)
|
||||
{Path.relative_to_cwd(file), on, description}
|
||||
|
||||
{file, on, description}
|
||||
end
|
||||
|
||||
IO.puts("""
|
||||
@@ -483,18 +419,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(files) do
|
||||
for {pid, _, _, _} <- files, do: Process.exit(pid, :kill)
|
||||
for {pid, _, _, _} <- files, do: discard_down(pid)
|
||||
:ok
|
||||
defp terminate(queued) do
|
||||
for {pid, _, _, _} <- queued do
|
||||
Process.exit(pid, :kill)
|
||||
end
|
||||
end
|
||||
|
||||
defp print_error(file, kind, reason, stack) do
|
||||
@@ -504,11 +437,12 @@ defmodule Kernel.ParallelCompiler do
|
||||
])
|
||||
end
|
||||
|
||||
defp cancel_waiting_timer(files, child_pid) do
|
||||
case List.keyfind(files, child_pid, 0) do
|
||||
defp cancel_waiting_timer(queued, child_pid) do
|
||||
case List.keyfind(queued, child_pid, 0) do
|
||||
{^child_pid, _ref, _file, timer_ref} ->
|
||||
Process.cancel_timer(timer_ref)
|
||||
# Let's flush the message in case it arrived before we canceled the timeout.
|
||||
# Let's flush the message in case it arrived before we canceled the
|
||||
# timeout.
|
||||
receive do
|
||||
{:timed_out, ^child_pid} -> :ok
|
||||
after
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
defmodule Kernel.ParallelRequire do
|
||||
# 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
|
||||
|
||||
@@ -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
|
||||
@@ -37,7 +37,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
## AST representation
|
||||
|
||||
Only two-element tuples are considered literals in Elixir and return themselves
|
||||
Only two-item tuples are considered literals in Elixir and return themselves
|
||||
when quoted. Therefore, all other tuples are represented in the AST as calls to
|
||||
the `:{}` special form.
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -192,7 +165,7 @@ defmodule Kernel.SpecialForms do
|
||||
iex> <<102, rest::binary>>
|
||||
"foo"
|
||||
|
||||
The `utf8`, `utf16`, and `utf32` types are for Unicode code points. They
|
||||
The `utf8`, `utf16`, and `utf32` types are for Unicode codepoints. They
|
||||
can also be applied to literal strings and charlists:
|
||||
|
||||
iex> <<"foo"::utf16>>
|
||||
@@ -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,14 +294,14 @@ 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)>>
|
||||
|
||||
def type(<<@png_signature, rest::binary>>), do: :png
|
||||
def type(<<@jpg_signature, rest::binary>>), do: :jpg
|
||||
def type(_), do: :unknown
|
||||
def type(_), do :unknown
|
||||
end
|
||||
|
||||
### Performance & Optimizations
|
||||
@@ -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
|
||||
@@ -486,11 +452,11 @@ defmodule Kernel.SpecialForms do
|
||||
defmacro unquote(:.)(left, right), do: error!([left, right])
|
||||
|
||||
@doc """
|
||||
`alias/2` is used to set up aliases, often useful with modules' names.
|
||||
`alias/2` is used to setup aliases, often useful with modules names.
|
||||
|
||||
## Examples
|
||||
|
||||
`alias/2` can be used to set up an alias for any module:
|
||||
`alias/2` can be used to setup an alias for any module:
|
||||
|
||||
defmodule Math do
|
||||
alias MyKeyword, as: Keyword
|
||||
@@ -503,10 +469,10 @@ defmodule Kernel.SpecialForms do
|
||||
In case one wants to access the original `Keyword`, it can be done
|
||||
by accessing `Elixir`:
|
||||
|
||||
Keyword.values #=> uses MyKeyword.values
|
||||
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
|
||||
@@ -542,7 +508,6 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
Both warning behaviours could be changed by explicitly
|
||||
setting the `:warn` option to `true` or `false`.
|
||||
|
||||
"""
|
||||
defmacro alias(module, opts), do: error!([module, opts])
|
||||
|
||||
@@ -567,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.
|
||||
|
||||
"""
|
||||
@@ -577,7 +542,7 @@ defmodule Kernel.SpecialForms do
|
||||
Imports functions and macros from other modules.
|
||||
|
||||
`import/2` allows one to easily access functions or macros from
|
||||
other modules without using the qualified name.
|
||||
others modules without using the qualified name.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -705,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.
|
||||
|
||||
@@ -784,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,
|
||||
@@ -804,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
|
||||
@@ -849,11 +778,23 @@ 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,
|
||||
there is no reason to define such a macro (and it would actually be
|
||||
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:
|
||||
|
||||
@@ -868,7 +809,7 @@ defmodule Kernel.SpecialForms do
|
||||
We can invoke it as:
|
||||
|
||||
import Math
|
||||
IO.puts("Got #{squared(5)}")
|
||||
IO.puts "Got #{squared(5)}"
|
||||
|
||||
At first, there is nothing in this example that actually reveals it is a
|
||||
macro. But what is happening is that, at compilation time, `squared(5)`
|
||||
@@ -878,10 +819,10 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
import Math
|
||||
my_number = fn ->
|
||||
IO.puts("Returning 5")
|
||||
IO.puts "Returning 5"
|
||||
5
|
||||
end
|
||||
IO.puts("Got #{squared(my_number.())}")
|
||||
IO.puts "Got #{squared(my_number.())}"
|
||||
|
||||
The example above will print:
|
||||
|
||||
@@ -915,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
|
||||
@@ -947,8 +888,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
import Math
|
||||
squared(5)
|
||||
x
|
||||
#=> ** (CompileError) undefined variable x or undefined function x/0
|
||||
x #=> ** (CompileError) undefined variable x or undefined function x/0
|
||||
|
||||
We can see that `x` did not leak to the user context. This happens
|
||||
because Elixir macros are hygienic, a topic we will discuss at length
|
||||
@@ -969,9 +909,8 @@ defmodule Kernel.SpecialForms do
|
||||
require Hygiene
|
||||
|
||||
a = 10
|
||||
Hygiene.no_interference()
|
||||
a
|
||||
#=> 10
|
||||
Hygiene.no_interference
|
||||
a #=> 10
|
||||
|
||||
In the example above, `a` returns 10 even if the macro
|
||||
is apparently setting it to 1 because variables defined
|
||||
@@ -990,12 +929,11 @@ defmodule Kernel.SpecialForms do
|
||||
require NoHygiene
|
||||
|
||||
a = 10
|
||||
NoHygiene.interference()
|
||||
a
|
||||
#=> 1
|
||||
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
|
||||
@@ -1011,8 +949,8 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
Hygiene.write()
|
||||
Hygiene.read()
|
||||
Hygiene.write
|
||||
Hygiene.read
|
||||
#=> ** (RuntimeError) undefined variable a or undefined function a/0
|
||||
|
||||
For such, you can explicitly pass the current module scope as
|
||||
@@ -1032,8 +970,8 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
ContextHygiene.write()
|
||||
ContextHygiene.read()
|
||||
ContextHygiene.write
|
||||
ContextHygiene.read
|
||||
#=> 1
|
||||
|
||||
## Hygiene in aliases
|
||||
@@ -1046,14 +984,13 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
defmacro no_interference do
|
||||
quote do
|
||||
M.new()
|
||||
M.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require Hygiene
|
||||
Hygiene.no_interference()
|
||||
#=> %{}
|
||||
Hygiene.no_interference #=> %{}
|
||||
|
||||
Notice that, even though the alias `M` is not available
|
||||
in the context the macro is expanded, the code above works
|
||||
@@ -1067,32 +1004,31 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
defmacro no_interference do
|
||||
quote do
|
||||
M.new()
|
||||
M.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require Hygiene
|
||||
alias SomethingElse, as: M
|
||||
Hygiene.no_interference()
|
||||
#=> %{}
|
||||
Hygiene.no_interference #=> %{}
|
||||
|
||||
In some cases, you want to access an alias or a module defined
|
||||
in the caller. For such, you can use the `alias!` macro:
|
||||
|
||||
defmodule Hygiene do
|
||||
# This will expand to Elixir.Nested.hello()
|
||||
# This will expand to Elixir.Nested.hello
|
||||
defmacro no_interference do
|
||||
quote do
|
||||
Nested.hello()
|
||||
Nested.hello
|
||||
end
|
||||
end
|
||||
|
||||
# This will expand to Nested.hello() for
|
||||
# This will expand to Nested.hello for
|
||||
# whatever is Nested in the caller
|
||||
defmacro interference do
|
||||
quote do
|
||||
alias!(Nested).hello()
|
||||
alias!(Nested).hello
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1103,10 +1039,10 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
|
||||
require Hygiene
|
||||
Hygiene.no_interference()
|
||||
Hygiene.no_interference
|
||||
#=> ** (UndefinedFunctionError) ...
|
||||
|
||||
Hygiene.interference()
|
||||
Hygiene.interference
|
||||
#=> "world"
|
||||
end
|
||||
|
||||
@@ -1128,8 +1064,7 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
Hygiene.return_length()
|
||||
#=> 3
|
||||
Hygiene.return_length #=> 3
|
||||
|
||||
Notice how `Hygiene.return_length/0` returns `3` even though the `Kernel.length/1`
|
||||
function is not imported. In fact, even if `return_length/0`
|
||||
@@ -1164,8 +1099,7 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
Lazy.return_length()
|
||||
#=> 5
|
||||
Lazy.return_length #=> 5
|
||||
|
||||
## Stacktrace information
|
||||
|
||||
@@ -1197,30 +1131,30 @@ 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
|
||||
|
||||
Elixir quote/unquote mechanisms provide a functionality called
|
||||
Elixir quote/unquote mechanisms provides a functionality called
|
||||
unquote fragments. Unquote fragments provide an easy way to generate
|
||||
functions on the fly. Consider this example:
|
||||
|
||||
kv = [foo: 1, bar: 2]
|
||||
Enum.each(kv, fn {k, v} ->
|
||||
Enum.each kv, fn {k, v} ->
|
||||
def unquote(k)(), do: unquote(v)
|
||||
end)
|
||||
end
|
||||
|
||||
In the example above, we have generated the functions `foo/0` and
|
||||
`bar/0` dynamically. Now, imagine that we want to convert this
|
||||
`bar/0` dynamically. Now, imagine that, we want to convert this
|
||||
functionality into a macro:
|
||||
|
||||
defmacro defkv(kv) do
|
||||
Enum.map(kv, fn {k, v} ->
|
||||
Enum.map kv, fn {k, v} ->
|
||||
quote do
|
||||
def unquote(k)(), do: unquote(v)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
We can invoke this macro as:
|
||||
@@ -1243,9 +1177,9 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
defmacro defkv(kv) do
|
||||
quote do
|
||||
Enum.each(unquote(kv), fn {k, v} ->
|
||||
Enum.each unquote(kv), fn {k, v} ->
|
||||
def unquote(k)(), do: unquote(v)
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1264,9 +1198,9 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
defmacro defkv(kv) do
|
||||
quote bind_quoted: [kv: kv] do
|
||||
Enum.each(kv, fn {k, v} ->
|
||||
Enum.each kv, fn {k, v} ->
|
||||
def unquote(k)(), do: unquote(v)
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1276,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
|
||||
|
||||
@@ -1369,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"]
|
||||
@@ -1389,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
|
||||
@@ -1409,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])
|
||||
|
||||
@@ -1470,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.
|
||||
@@ -1480,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:
|
||||
|
||||
@@ -1542,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])
|
||||
|
||||
@@ -1609,8 +1459,6 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
&local_function/1
|
||||
|
||||
See also `Function.capture/3`.
|
||||
|
||||
## Anonymous functions
|
||||
|
||||
The capture operator can also be used to partially apply
|
||||
@@ -1686,7 +1534,7 @@ defmodule Kernel.SpecialForms do
|
||||
it can be sure that it represents a call and the second argument
|
||||
in the list is an atom.
|
||||
|
||||
On the other hand, aliases hold some properties:
|
||||
On the other hand, aliases holds some properties:
|
||||
|
||||
1. The head element of aliases can be any term that must expand to
|
||||
an atom at compilation time.
|
||||
@@ -1735,7 +1583,7 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
#=> "This clause would match any value (x = 10)"
|
||||
|
||||
## Variable handling
|
||||
## Variables handling
|
||||
|
||||
Notice that variables bound in a clause "head" do not leak to the
|
||||
outer context:
|
||||
@@ -1745,8 +1593,7 @@ defmodule Kernel.SpecialForms do
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
value
|
||||
#=> unbound variable value
|
||||
value #=> unbound variable value
|
||||
|
||||
However, variables explicitly bound in the clause "body" are
|
||||
accessible from the outer context:
|
||||
@@ -1755,13 +1602,12 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
case lucky? do
|
||||
false -> value = 13
|
||||
true -> true
|
||||
true -> true
|
||||
end
|
||||
|
||||
value
|
||||
#=> 7 or 13
|
||||
value #=> 7 or 13
|
||||
|
||||
In the example above, `value` is going to be `7` or `13` depending on
|
||||
In the example above, value is going to be `7` or `13` depending on
|
||||
the value of `lucky?`. In case `value` has no previous value before
|
||||
case, clauses that do not explicitly bind a value have the variable
|
||||
bound to `nil`.
|
||||
@@ -1773,7 +1619,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
case 10 do
|
||||
^x -> "Won't match"
|
||||
_ -> "Will match"
|
||||
_ -> "Will match"
|
||||
end
|
||||
#=> "Will match"
|
||||
|
||||
@@ -1819,15 +1665,15 @@ defmodule Kernel.SpecialForms do
|
||||
do_something_that_may_fail(some_arg)
|
||||
rescue
|
||||
ArgumentError ->
|
||||
IO.puts("Invalid argument given")
|
||||
IO.puts "Invalid argument given"
|
||||
catch
|
||||
value ->
|
||||
IO.puts("Caught #{inspect(value)}")
|
||||
IO.puts "Caught #{inspect(value)}"
|
||||
else
|
||||
value ->
|
||||
IO.puts("Success! The result was #{inspect(value)}")
|
||||
IO.puts "Success! The result was #{inspect(value)}"
|
||||
after
|
||||
IO.puts("This is printed regardless if it failed or succeeded")
|
||||
IO.puts "This is printed regardless if it failed or succeed"
|
||||
end
|
||||
|
||||
The `rescue` clause is used to handle exceptions while the `catch`
|
||||
@@ -1836,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
|
||||
|
||||
@@ -1922,7 +1767,7 @@ defmodule Kernel.SpecialForms do
|
||||
throw(:some_value)
|
||||
catch
|
||||
thrown_value ->
|
||||
IO.puts("A value was thrown: #{inspect(thrown_value)}")
|
||||
IO.puts "A value was thrown: #{inspect(thrown_value)}"
|
||||
end
|
||||
|
||||
### Catching values of any kind
|
||||
@@ -1934,15 +1779,15 @@ defmodule Kernel.SpecialForms do
|
||||
try do
|
||||
exit(:shutdown)
|
||||
catch
|
||||
:exit, value ->
|
||||
IO.puts("Exited with value #{inspect(value)}")
|
||||
:exit, value
|
||||
IO.puts "Exited with value #{inspect(value)}"
|
||||
end
|
||||
|
||||
try do
|
||||
exit(:shutdown)
|
||||
catch
|
||||
kind, value when kind in [:exit, :throw] ->
|
||||
IO.puts("Caught exit or throw with value #{inspect(value)}")
|
||||
IO.puts "Caught exit or throw with value #{inspect(value)}"
|
||||
end
|
||||
|
||||
The `catch` clause also supports `:error` alongside `:exit` and `:throw` as
|
||||
@@ -2080,8 +1925,7 @@ defmodule Kernel.SpecialForms do
|
||||
_, _ -> :failed
|
||||
end
|
||||
|
||||
x
|
||||
#=> unbound variable "x"
|
||||
x #=> unbound variable "x"
|
||||
|
||||
In the example above, `x` cannot be accessed since it was defined
|
||||
inside the `try` clause. A common practice to address this issue
|
||||
@@ -2109,27 +1953,27 @@ 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")
|
||||
IO.puts :stderr, "Unexpected message received"
|
||||
end
|
||||
|
||||
An optional `after` clause can be given in case the message was not
|
||||
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")
|
||||
IO.puts :stderr, "Unexpected message received"
|
||||
after
|
||||
5000 ->
|
||||
IO.puts(:stderr, "No message in 5 seconds")
|
||||
IO.puts :stderr, "No message in 5 seconds"
|
||||
end
|
||||
|
||||
The `after` clause can be specified even if there are no match clauses.
|
||||
@@ -2137,16 +1981,16 @@ 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.
|
||||
|
||||
## Variable handling
|
||||
## Variables handling
|
||||
|
||||
The `receive/1` special form handles variables exactly as the `case/2`
|
||||
special macro. For more information, check the docs for `case/2`.
|
||||
|
||||
+864
-644
File diff suppressed because it is too large
Load Diff
@@ -25,7 +25,7 @@ defmodule Kernel.Utils do
|
||||
Callback for defdelegate.
|
||||
"""
|
||||
def defdelegate(fun, opts) when is_list(opts) do
|
||||
# TODO: Remove on v2.0
|
||||
# TODO: Remove by 2.0
|
||||
append_first? = Keyword.get(opts, :append_first, false)
|
||||
|
||||
{name, args} =
|
||||
@@ -114,7 +114,7 @@ defmodule Kernel.Utils do
|
||||
def announce_struct(module) do
|
||||
case :erlang.get(:elixir_compiler_pid) do
|
||||
:undefined -> :ok
|
||||
pid -> send(pid, {:available, :struct, module})
|
||||
pid -> send(pid, {:struct_available, module})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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,32 +157,27 @@ 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
|
||||
that checks for its presence in a guard, then unquotes the variable references as
|
||||
appropriate.
|
||||
|
||||
The following code
|
||||
The resulting transformation looks something like this:
|
||||
|
||||
expression = quote do: is_integer(value) and rem(value, 2) == 0
|
||||
variable_references = [value: Elixir]
|
||||
Kernel.Utils.defguard(expression, variable_references) |> Macro.to_string() |> IO.puts()
|
||||
> expression = quote do: is_integer(value) and rem(value, 2) == 0
|
||||
> variable_references = [value: Elixir]
|
||||
> Kernel.Utils.defguard(expression, variable_references) |> Macro.to_string |> IO.puts
|
||||
|
||||
would print a code similar to:
|
||||
|
||||
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
|
||||
|
||||
"""
|
||||
@@ -193,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
|
||||
@@ -230,36 +224,25 @@ 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
|
||||
{_, 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}
|
||||
case pair in refs and pair not in acc do
|
||||
true -> {var, [pair | 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: context_to_var(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
|
||||
@@ -272,6 +255,9 @@ defmodule Kernel.Utils do
|
||||
{:unquote, [], List.wrap(ast)}
|
||||
end
|
||||
|
||||
defp context_to_var(ref, ctx) when is_atom(ctx), do: {ref, [], ctx}
|
||||
defp context_to_var(ref, ctx) when is_integer(ctx), do: {ref, [counter: ctx], nil}
|
||||
|
||||
defp var_context(meta, kind) do
|
||||
case :lists.keyfind(:counter, 1, meta) do
|
||||
{:counter, counter} -> counter
|
||||
|
||||
+60
-106
@@ -2,7 +2,7 @@ defmodule Keyword do
|
||||
@moduledoc """
|
||||
A set of functions for working with keywords.
|
||||
|
||||
A keyword list is a list of two-element tuples where the first
|
||||
A keyword is a list of two-element tuples where the first
|
||||
element of the tuple is an atom and the second element
|
||||
can be any value.
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -67,10 +55,6 @@ defmodule Keyword do
|
||||
it comes to ordering. However, since a keyword list is simply a
|
||||
list, all the operations defined in `Enum` and `List` can be
|
||||
applied too, especially when ordering is required.
|
||||
|
||||
Most of the functions in this module work in linear time. This means
|
||||
that, the time it takes to perform an operation grows at the same
|
||||
rate as the length of the list.
|
||||
"""
|
||||
|
||||
@compile :inline_list_funcs
|
||||
@@ -88,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
|
||||
@@ -120,7 +104,7 @@ defmodule Keyword do
|
||||
def new, do: []
|
||||
|
||||
@doc """
|
||||
Creates a keyword list from an enumerable.
|
||||
Creates a keyword from an enumerable.
|
||||
|
||||
Duplicated entries are removed, the latest one prevails.
|
||||
Unlike `Enum.into(enumerable, [])`, `Keyword.new(enumerable)`
|
||||
@@ -141,7 +125,7 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a keyword list from an enumerable via the transformation function.
|
||||
Creates a keyword from an enumerable via the transformation function.
|
||||
|
||||
Duplicated entries are removed, the latest one prevails.
|
||||
Unlike `Enum.into(enumerable, [], fun)`,
|
||||
@@ -149,12 +133,12 @@ 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]
|
||||
|
||||
"""
|
||||
@spec new(Enum.t(), (term -> {key, value})) :: t
|
||||
def new(pairs, transform) when is_function(transform, 1) do
|
||||
def new(pairs, transform) do
|
||||
fun = fn el, acc ->
|
||||
{k, v} = transform.(el)
|
||||
put_new(acc, k, v)
|
||||
@@ -326,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
|
||||
@@ -386,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
|
||||
@@ -424,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]
|
||||
|
||||
"""
|
||||
@@ -442,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]
|
||||
|
||||
"""
|
||||
@@ -472,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`.
|
||||
@@ -510,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`.
|
||||
@@ -543,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`.
|
||||
@@ -632,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)]
|
||||
@@ -655,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
|
||||
@@ -706,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
|
||||
@@ -719,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
|
||||
|
||||
@@ -748,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]
|
||||
|
||||
@@ -770,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
|
||||
|
||||
@@ -792,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 """
|
||||
@@ -830,9 +789,8 @@ defmodule Keyword do
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
|
||||
"""
|
||||
@spec update!(t, key, (value -> value)) :: t
|
||||
def update!(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
|
||||
@spec update!(t, key, (value -> value)) :: t | no_return
|
||||
def update!(keywords, key, fun) do
|
||||
update!(keywords, key, fun, keywords)
|
||||
end
|
||||
|
||||
@@ -900,7 +858,7 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec split(t, [key]) :: {t, t}
|
||||
def split(keywords, keys) when is_list(keywords) and is_list(keys) do
|
||||
def split(keywords, keys) when is_list(keywords) do
|
||||
fun = fn {k, v}, {take, drop} ->
|
||||
case k in keys do
|
||||
true -> {[{k, v} | take], drop}
|
||||
@@ -928,7 +886,7 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec take(t, [key]) :: t
|
||||
def take(keywords, keys) when is_list(keywords) and is_list(keys) do
|
||||
def take(keywords, keys) when is_list(keywords) do
|
||||
:lists.filter(fn {k, _} -> k in keys end, keywords)
|
||||
end
|
||||
|
||||
@@ -946,20 +904,15 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec drop(t, [key]) :: t
|
||||
def drop(keywords, keys) when is_list(keywords) and is_list(keys) do
|
||||
def drop(keywords, keys) when is_list(keywords) do
|
||||
:lists.filter(fn {key, _} -> key not in keys end, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the first value for `key` and removes all associated entries in the keyword list.
|
||||
Returns and removes all values associated with `key` in the keyword list.
|
||||
|
||||
It returns a tuple where the first element is the first value for `key` and the
|
||||
second element is a keyword list with all entries associated with `key` removed.
|
||||
If the `key` is not present in the keyword list, `{default, keyword_list}` is
|
||||
returned.
|
||||
|
||||
If you don't want to remove all the entries associated with `key` use `pop_first/3`
|
||||
instead, that function will remove only the first entry.
|
||||
All duplicated keys are removed. See `pop_first/3` for
|
||||
removing only the first entry.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -974,7 +927,7 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec pop(t, key, value) :: {value, t}
|
||||
def pop(keywords, key, default \\ nil) when is_list(keywords) and is_atom(key) do
|
||||
def pop(keywords, key, default \\ nil) when is_list(keywords) do
|
||||
case fetch(keywords, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(keywords, key)}
|
||||
@@ -1008,7 +961,7 @@ defmodule Keyword do
|
||||
"""
|
||||
@spec pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
def pop_lazy(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
|
||||
when is_list(keywords) and is_function(fun, 0) do
|
||||
case fetch(keywords, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(keywords, key)}
|
||||
@@ -1036,7 +989,7 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@spec pop_first(t, key, value) :: {value, t}
|
||||
def pop_first(keywords, key, default \\ nil) when is_list(keywords) and is_atom(key) do
|
||||
def pop_first(keywords, key, default \\ nil) when is_list(keywords) do
|
||||
case :lists.keytake(key, 1, keywords) do
|
||||
{:value, {^key, value}, rest} -> {value, rest}
|
||||
false -> {default, keywords}
|
||||
@@ -1048,7 +1001,7 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.to_list(a: 1)
|
||||
iex> Keyword.to_list([a: 1])
|
||||
[a: 1]
|
||||
|
||||
"""
|
||||
@@ -1058,7 +1011,8 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Kernel.length/1 instead"
|
||||
# TODO: Remove on 2.0
|
||||
# (hard-deprecated in elixir_dispatch)
|
||||
def size(keyword) do
|
||||
length(keyword)
|
||||
end
|
||||
|
||||
+174
-353
@@ -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,34 +45,28 @@ 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]
|
||||
|
||||
Most of the functions in this module work in linear time. This means that,
|
||||
that the time it takes to perform an operation grows at the same rate as the
|
||||
length of the list. For example `length/1` and `last/1` will run in linear
|
||||
time because they need to iterate through every element of the list, but
|
||||
`first/1` will run in constant time because it only needs the first element.
|
||||
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
|
||||
|
||||
If a list is made of non-negative integers, where each integer represents a
|
||||
Unicode code point, the list can also be called a charlist. These integers
|
||||
must:
|
||||
|
||||
* be within the range `0..0x10FFFF` (`0..1_114_111`);
|
||||
* and be out of the range `0xD800..0xDFFF` (`55_296..57_343`), which is
|
||||
reserved in Unicode for UTF-16 surrogate pairs.
|
||||
|
||||
Elixir uses single quotes to define charlists:
|
||||
If a list is made of non-negative integers, it can also be called
|
||||
a charlist. Elixir uses single quotes to define charlists:
|
||||
|
||||
iex> 'héllo'
|
||||
[104, 233, 108, 108, 111]
|
||||
|
||||
In particular, charlists will be printed back by default in single
|
||||
quotes if they contain only printable ASCII characters:
|
||||
In particular, charlists may be printed back in single
|
||||
quotes if they contain only ASCII-printable codepoints:
|
||||
|
||||
iex> 'abc'
|
||||
'abc'
|
||||
@@ -95,28 +76,37 @@ 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 only printable ASCII
|
||||
characters with `ascii_printable?/2`.
|
||||
A list can be checked if it is made of printable ascii
|
||||
codepoints with `ascii_printable?/2`.
|
||||
|
||||
Improper lists are never deemed as charlists.
|
||||
## 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
|
||||
|
||||
@doc """
|
||||
Deletes the given `element` from the `list`. Returns a new list without
|
||||
the element.
|
||||
Deletes the given `item` from the `list`. Returns a new list without
|
||||
the item.
|
||||
|
||||
If the `element` occurs more than once in the `list`, just
|
||||
If the `item` occurs more than once in the `list`, just
|
||||
the first occurrence is removed.
|
||||
|
||||
## Examples
|
||||
@@ -124,47 +114,29 @@ defmodule List do
|
||||
iex> List.delete([:a, :b, :c], :a)
|
||||
[:b, :c]
|
||||
|
||||
iex> List.delete([:a, :b, :c], :d)
|
||||
[:a, :b, :c]
|
||||
|
||||
iex> List.delete([:a, :b, :b, :c], :b)
|
||||
[:a, :b, :c]
|
||||
|
||||
iex> List.delete([], :b)
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec delete([], any) :: []
|
||||
@spec delete([...], any) :: list
|
||||
def delete(list, element)
|
||||
def delete([element | list], element), do: list
|
||||
def delete([other | list], element), do: [other | delete(list, element)]
|
||||
def delete([], _element), do: []
|
||||
@spec delete(list, any) :: list
|
||||
def delete(list, item)
|
||||
def delete([item | list], item), do: list
|
||||
def delete([other | list], item), do: [other | delete(list, item)]
|
||||
def delete([], _item), do: []
|
||||
|
||||
@doc """
|
||||
Duplicates the given element `n` times in a list.
|
||||
|
||||
`n` is an integer greater than or equal to `0`.
|
||||
|
||||
If `n` is `0`, an empty list is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.duplicate("hello", 0)
|
||||
[]
|
||||
iex> List.duplicate("hello", 3)
|
||||
["hello", "hello", "hello"]
|
||||
|
||||
iex> List.duplicate("hi", 1)
|
||||
["hi"]
|
||||
|
||||
iex> List.duplicate("bye", 2)
|
||||
["bye", "bye"]
|
||||
|
||||
iex> List.duplicate([1, 2], 3)
|
||||
[[1, 2], [1, 2], [1, 2]]
|
||||
iex> List.duplicate([1, 2], 2)
|
||||
[[1, 2], [1, 2]]
|
||||
|
||||
"""
|
||||
@spec duplicate(any, 0) :: []
|
||||
@spec duplicate(elem, pos_integer) :: [elem, ...] when elem: var
|
||||
@spec duplicate(elem, non_neg_integer) :: [elem] when elem: var
|
||||
def duplicate(elem, n) do
|
||||
:lists.duplicate(n, elem)
|
||||
end
|
||||
@@ -172,16 +144,11 @@ defmodule List do
|
||||
@doc """
|
||||
Flattens the given `list` of nested lists.
|
||||
|
||||
Empty list elements are discarded.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.flatten([1, [[2], 3]])
|
||||
[1, 2, 3]
|
||||
|
||||
iex> List.flatten([[], [[], []]])
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec flatten(deep_list) :: list when deep_list: [any | deep_list]
|
||||
def flatten(list) do
|
||||
@@ -193,17 +160,11 @@ defmodule List do
|
||||
The list `tail` will be added at the end of
|
||||
the flattened list.
|
||||
|
||||
Empty list elements from `list` are discarded,
|
||||
but not the ones from `tail`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.flatten([1, [[2], 3]], [4, 5])
|
||||
[1, 2, 3, 4, 5]
|
||||
|
||||
iex> List.flatten([1, [], 2], [3, [], 4])
|
||||
[1, 2, 3, [], 4]
|
||||
|
||||
"""
|
||||
@spec flatten(deep_list, [elem]) :: [elem] when elem: var, deep_list: [elem | deep_list]
|
||||
def flatten(list, tail) do
|
||||
@@ -216,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
|
||||
|
||||
"""
|
||||
@@ -234,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
|
||||
|
||||
"""
|
||||
@@ -258,8 +219,7 @@ defmodule List do
|
||||
1
|
||||
|
||||
"""
|
||||
@spec first([]) :: nil
|
||||
@spec first([elem, ...]) :: elem when elem: var
|
||||
@spec first([elem]) :: nil | elem when elem: var
|
||||
def first([]), do: nil
|
||||
def first([head | _]), do: head
|
||||
|
||||
@@ -278,19 +238,16 @@ defmodule List do
|
||||
3
|
||||
|
||||
"""
|
||||
@spec last([]) :: nil
|
||||
@spec last([elem, ...]) :: elem when elem: var
|
||||
@spec last([elem]) :: nil | elem when elem: var
|
||||
def last([]), do: nil
|
||||
def last([head]), do: head
|
||||
def last([_ | tail]), do: last(tail)
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and returns the first tuple
|
||||
where the element at `position` in the tuple matches the
|
||||
where the item at `position` in the tuple matches the
|
||||
given `key`.
|
||||
|
||||
If no matching tuple is found, `default` is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.keyfind([a: 1, b: 2], :a, 0)
|
||||
@@ -310,7 +267,7 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and returns `true` if there is
|
||||
a tuple where the element at `position` in the tuple matches
|
||||
a tuple where the item at `position` in the tuple matches
|
||||
the given `key`.
|
||||
|
||||
## Examples
|
||||
@@ -331,17 +288,14 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and if the identified element 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
|
||||
@@ -349,7 +303,7 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and sorts the elements
|
||||
Receives a list of tuples and sorts the items
|
||||
at `position` of the tuples. The sort is stable.
|
||||
|
||||
## Examples
|
||||
@@ -367,10 +321,10 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a `list` of tuples and replaces the element
|
||||
identified by `key` at `position` with `new_tuple`.
|
||||
Receives a `list` of tuples and replaces the item
|
||||
identified by `key` at `position`.
|
||||
|
||||
If the element does not exist, it is added to the end of the `list`.
|
||||
If the item does not exist, it is added to the end of the `list`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -388,7 +342,7 @@ defmodule List do
|
||||
|
||||
@doc """
|
||||
Receives a `list` of tuples and deletes the first tuple
|
||||
where the element at `position` matches the
|
||||
where the item at `position` matches the
|
||||
given `key`. Returns the new list.
|
||||
|
||||
## Examples
|
||||
@@ -430,16 +384,16 @@ defmodule List do
|
||||
@spec keytake([tuple], any, non_neg_integer) :: {tuple, [tuple]} | nil
|
||||
def keytake(list, key, position) do
|
||||
case :lists.keytake(key, position + 1, list) do
|
||||
{:value, element, list} -> {element, list}
|
||||
{:value, item, list} -> {item, list}
|
||||
false -> nil
|
||||
end
|
||||
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
|
||||
|
||||
@@ -453,9 +407,7 @@ defmodule List do
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec wrap(term) :: maybe_improper_list()
|
||||
def wrap(term)
|
||||
|
||||
@spec wrap(list | any) :: list
|
||||
def wrap(list) when is_list(list) do
|
||||
list
|
||||
end
|
||||
@@ -489,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')
|
||||
@@ -522,58 +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, 0) :: true
|
||||
@spec ascii_printable?([], limit) :: true
|
||||
when limit: :infinity | pos_integer
|
||||
@spec ascii_printable?([...], limit) :: boolean
|
||||
when limit: :infinity | pos_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)
|
||||
# 7..13 is the range '\a\b\t\n\v\f\r'. 32..126 are ASCII printables.
|
||||
when is_integer(char) and
|
||||
((char >= 7 and char <= 13) or char == ?\e or (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?([], _counter), do: true
|
||||
defp ascii_printable_guarded?(_, _counter), do: false
|
||||
def ascii_printable?([?\n | rest], counter) do
|
||||
ascii_printable?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
def ascii_printable?([?\r | rest], counter) do
|
||||
ascii_printable?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
def ascii_printable?([?\t | rest], counter) do
|
||||
ascii_printable?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
def ascii_printable?([?\v | rest], counter) do
|
||||
ascii_printable?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
def ascii_printable?([?\b | rest], counter) do
|
||||
ascii_printable?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
def ascii_printable?([?\f | rest], counter) do
|
||||
ascii_printable?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
def ascii_printable?([?\e | rest], counter) do
|
||||
ascii_printable?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
def ascii_printable?([?\a | rest], counter) do
|
||||
ascii_printable?(rest, decrement(counter))
|
||||
end
|
||||
|
||||
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`.
|
||||
|
||||
@@ -596,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
|
||||
|
||||
@@ -634,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
|
||||
@@ -667,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
|
||||
@@ -719,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
|
||||
@@ -749,8 +676,7 @@ defmodule List do
|
||||
false
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec starts_with?(nonempty_list, nonempty_list) :: boolean
|
||||
@spec starts_with?(list, list) :: boolean
|
||||
@spec starts_with?(list, []) :: true
|
||||
@spec starts_with?([], nonempty_list) :: false
|
||||
def starts_with?(list, prefix)
|
||||
@@ -762,18 +688,15 @@ defmodule List do
|
||||
@doc """
|
||||
Converts a charlist to an atom.
|
||||
|
||||
Elixir supports conversions from charlists which contains any Unicode
|
||||
code point.
|
||||
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
|
||||
@@ -785,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
|
||||
code point.
|
||||
Currently Elixir does not support conversions from charlists
|
||||
which contains Unicode codepoints greater than 0xFF.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -796,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
|
||||
|
||||
@@ -874,18 +793,11 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a list of integers representing code points, lists or
|
||||
Converts a list of integers representing codepoints, lists or
|
||||
strings into a string.
|
||||
|
||||
To be converted to a string, a list must either be empty or only
|
||||
contain the following elements:
|
||||
|
||||
* strings
|
||||
* integers representing Unicode code points
|
||||
* a list containing one of these three elements
|
||||
|
||||
Notice that this function expects a list of integers representing
|
||||
UTF-8 code points. If you have a list of bytes, you must instead use
|
||||
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
|
||||
@@ -899,9 +811,6 @@ defmodule List do
|
||||
iex> List.to_string([0x0064, "ee", ['p']])
|
||||
"deep"
|
||||
|
||||
iex> List.to_string([])
|
||||
""
|
||||
|
||||
"""
|
||||
@spec to_string(:unicode.charlist()) :: String.t()
|
||||
def to_string(list) when is_list(list) do
|
||||
@@ -912,63 +821,10 @@ defmodule List do
|
||||
raise ArgumentError, """
|
||||
cannot convert the given list to a string.
|
||||
|
||||
To be converted to a string, a list must either be empty or only
|
||||
contain the following elements:
|
||||
To be converted to a string, a list must contain only:
|
||||
|
||||
* strings
|
||||
* integers representing Unicode code points
|
||||
* 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_binary(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 """
|
||||
Converts a list of integers representing code points, lists or
|
||||
strings into a charlist.
|
||||
|
||||
Notice that this function expects a list of integers representing
|
||||
UTF-8 code points. 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 code points
|
||||
* 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:
|
||||
@@ -976,7 +832,7 @@ defmodule List do
|
||||
#{inspect(list)}
|
||||
"""
|
||||
else
|
||||
result when is_list(result) ->
|
||||
result when is_binary(result) ->
|
||||
result
|
||||
|
||||
{:error, encoded, rest} ->
|
||||
@@ -1000,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
|
||||
@@ -1066,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
|
||||
|
||||
@@ -1151,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
|
||||
@@ -1165,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
|
||||
|
||||
@@ -1179,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
|
||||
@@ -1193,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
|
||||
|
||||
@@ -17,7 +17,8 @@ defprotocol List.Chars do
|
||||
def to_charlist(term)
|
||||
|
||||
@doc false
|
||||
@deprecated "Use List.Chars.to_charlist/1 instead"
|
||||
# TODO: Remove by 2.0
|
||||
# (hard-deprecated in elixir_dispatch)
|
||||
Kernel.def to_char_list(term) do
|
||||
__MODULE__.to_charlist(term)
|
||||
end
|
||||
|
||||
+64
-195
@@ -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,19 +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"
|
||||
@spec generate_arguments(0, context :: atom) :: []
|
||||
@spec generate_arguments(pos_integer, context) :: [{atom, [], context}, ...] when context: atom
|
||||
def generate_arguments(amount, context)
|
||||
|
||||
def generate_arguments(0, context) when is_atom(context), do: []
|
||||
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 """
|
||||
@@ -350,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})
|
||||
@@ -364,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
|
||||
|
||||
@@ -377,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 """
|
||||
@@ -489,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
|
||||
@@ -506,7 +433,7 @@ defmodule Macro do
|
||||
|
||||
In this setup, Elixir will escape the following: `\0`, `\a`, `\b`,
|
||||
`\d`, `\e`, `\f`, `\n`, `\r`, `\s`, `\t` and `\v`. Bytes can be
|
||||
given as hexadecimals via `\xNN` and Unicode code points as
|
||||
given as hexadecimals via `\xNN` and Unicode Codepoints as
|
||||
`\uNNNN` escapes.
|
||||
|
||||
This function is commonly used on sigil implementations
|
||||
@@ -535,7 +462,7 @@ defmodule Macro do
|
||||
## Map
|
||||
|
||||
The map must be a function. The function receives an integer
|
||||
representing the code point of the character it wants to unescape.
|
||||
representing the codepoint of the character it wants to unescape.
|
||||
Here is the default mapping function implemented by Elixir:
|
||||
|
||||
def unescape_map(unicode), do: true
|
||||
@@ -551,20 +478,20 @@ 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.
|
||||
|
||||
Hexadecimals and Unicode code points will be escaped if the map
|
||||
function returns `true` for `?x`. Unicode code points if the map
|
||||
Hexadecimals and Unicode codepoints will be escaped if the map
|
||||
function returns `true` for `?x`. Unicode codepoints if the map
|
||||
function returns `true` for `?u`.
|
||||
|
||||
## Examples
|
||||
|
||||
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()
|
||||
@@ -573,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 """
|
||||
@@ -613,11 +532,11 @@ 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
|
||||
def to_string({var, _, context} = ast, fun) when is_atom(var) and is_atom(context) do
|
||||
def to_string({var, _, atom} = ast, fun) when is_atom(atom) do
|
||||
fun.(ast, Atom.to_string(var))
|
||||
end
|
||||
|
||||
@@ -762,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)
|
||||
@@ -812,10 +727,9 @@ defmodule Macro do
|
||||
Kernel.inspect(value, limit: :infinity, printable_limit: :infinity)
|
||||
end
|
||||
|
||||
defp bitpart_to_string({:"::", _, [left, right]} = ast, fun) do
|
||||
defp bitpart_to_string({:::, _, [left, right]} = ast, fun) do
|
||||
result =
|
||||
op_to_string(left, fun, :"::", :left) <>
|
||||
"::" <> bitmods_to_string(right, fun, :"::", :right)
|
||||
op_to_string(left, fun, :::, :left) <> "::" <> bitmods_to_string(right, fun, :::, :right)
|
||||
|
||||
fun.(ast, result)
|
||||
end
|
||||
@@ -848,7 +762,7 @@ defmodule Macro do
|
||||
# Check if we have an interpolated string.
|
||||
defp interpolated?({:<<>>, _, [_ | _] = parts}) do
|
||||
Enum.all?(parts, fn
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [_]}, {:binary, _, _}]} -> true
|
||||
binary when is_binary(binary) -> true
|
||||
_ -> false
|
||||
end)
|
||||
@@ -861,12 +775,12 @@ defmodule Macro do
|
||||
defp interpolate({:<<>>, _, parts}, fun) do
|
||||
parts =
|
||||
Enum.map_join(parts, "", fn
|
||||
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [arg]}, {:binary, _, _}]} ->
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [arg]}, {:binary, _, _}]} ->
|
||||
"\#{" <> to_string(arg, fun) <> "}"
|
||||
|
||||
binary when is_binary(binary) ->
|
||||
binary = inspect_no_limit(binary)
|
||||
binary_part(binary, 1, byte_size(binary) - 2)
|
||||
:binary.part(binary, 1, byte_size(binary) - 2)
|
||||
end)
|
||||
|
||||
<<?", parts::binary, ?">>
|
||||
@@ -1138,15 +1052,13 @@ 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
|
||||
itself. This function does not traverse the AST, only the root
|
||||
node is expanded.
|
||||
|
||||
`expand_once/2` performs the expansion just once. Check `expand/2`
|
||||
to perform expansion until the node can no longer be expanded.
|
||||
itself. Notice that `expand_once/2` performs the expansion just
|
||||
once and it is not recursive. Check `expand/2` for expansion
|
||||
until the node can no longer be expanded.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1175,7 +1087,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]}
|
||||
|
||||
@@ -1198,7 +1110,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
|
||||
@@ -1255,7 +1167,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
|
||||
@@ -1269,7 +1181,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
|
||||
@@ -1285,15 +1197,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}
|
||||
|
||||
@@ -1316,7 +1222,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 ->
|
||||
@@ -1328,59 +1234,23 @@ 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, "ed_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.
|
||||
|
||||
Note this function does not traverse the AST, only the root
|
||||
node is expanded.
|
||||
|
||||
This function uses `expand_once/2` under the hood. Check
|
||||
it out for more information and examples.
|
||||
"""
|
||||
def expand(ast, env) do
|
||||
expand_until({ast, true}, env)
|
||||
def expand(tree, env) do
|
||||
expand_until({tree, true}, env)
|
||||
end
|
||||
|
||||
defp expand_until({ast, true}, env) do
|
||||
expand_until(do_expand_once(ast, env), env)
|
||||
defp expand_until({tree, true}, env) do
|
||||
expand_until(do_expand_once(tree, env), env)
|
||||
end
|
||||
|
||||
defp expand_until({ast, false}, _env) do
|
||||
ast
|
||||
defp expand_until({tree, false}, _env) do
|
||||
tree
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1396,29 +1266,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)
|
||||
@@ -1464,15 +1333,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"
|
||||
|
||||
"""
|
||||
|
||||
+26
-60
@@ -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 :: %{optional({variable, var_version}) => non_neg_integer | false}
|
||||
@typep current_vars :: %{optional(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__,
|
||||
@@ -85,17 +81,15 @@ defmodule Macro.Env do
|
||||
aliases: aliases,
|
||||
functions: functions,
|
||||
macros: macros,
|
||||
macro_aliases: macro_aliases,
|
||||
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 """
|
||||
|
||||
+49
-92
@@ -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,12 +80,16 @@ defmodule Map do
|
||||
iex> %{map | three: 3}
|
||||
** (KeyError) key :three not found
|
||||
|
||||
The functions in this module that need to find a specific key work in logarithmic time.
|
||||
This means that the time it takes to find keys grows as the map grows, but it's not
|
||||
directly proportional to the map size. In comparison to finding an element in a list,
|
||||
it performs better because lists have a linear time complexity. Some functions,
|
||||
such as `keys/1` and `values/1`, run in linear time because they need to get to every
|
||||
element in the map.
|
||||
## 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
|
||||
@@ -106,8 +99,6 @@ defmodule Map do
|
||||
@doc """
|
||||
Returns all keys from `map`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.keys(%{a: 1, b: 2})
|
||||
@@ -120,8 +111,6 @@ defmodule Map do
|
||||
@doc """
|
||||
Returns all values from `map`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.values(%{a: 1, b: 2})
|
||||
@@ -137,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})
|
||||
@@ -155,7 +142,7 @@ defmodule Map do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.new()
|
||||
iex> Map.new
|
||||
%{}
|
||||
|
||||
"""
|
||||
@@ -171,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}
|
||||
|
||||
"""
|
||||
@@ -212,15 +199,13 @@ defmodule Map do
|
||||
|> :maps.from_list()
|
||||
end
|
||||
|
||||
defp new_transform([element | rest], fun, acc) do
|
||||
new_transform(rest, fun, [fun.(element) | acc])
|
||||
defp new_transform([item | rest], fun, acc) do
|
||||
new_transform(rest, fun, [fun.(item) | acc])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns whether the given `key` exists in the given `map`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.has_key?(%{a: 1}, :a)
|
||||
@@ -228,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)
|
||||
@@ -238,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)
|
||||
@@ -247,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)
|
||||
@@ -258,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)
|
||||
@@ -268,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
|
||||
@@ -300,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} ->
|
||||
@@ -320,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)
|
||||
@@ -330,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)
|
||||
@@ -384,20 +364,13 @@ defmodule Map do
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec take(map, [key]) :: map
|
||||
@spec take(map, Enumerable.t()) :: map
|
||||
def take(map, keys)
|
||||
|
||||
def take(map, keys) when is_map(map) and is_list(keys) do
|
||||
take(keys, map, _acc = [])
|
||||
end
|
||||
|
||||
def take(map, keys) when is_map(map) do
|
||||
IO.warn(
|
||||
"Map.take/2 with an Enumerable of keys that is not a list is deprecated. " <>
|
||||
" Use a list of keys instead."
|
||||
)
|
||||
|
||||
take(map, Enum.to_list(keys))
|
||||
keys
|
||||
|> Enum.to_list()
|
||||
|> take(map, [])
|
||||
end
|
||||
|
||||
def take(non_map, _keys) do
|
||||
@@ -422,9 +395,8 @@ defmodule Map do
|
||||
Gets the value for a specific `key` in `map`.
|
||||
|
||||
If `key` is present in `map` with value `value`, then `value` is
|
||||
returned. Otherwise, `default` is returned.
|
||||
|
||||
If `default` is not provided, `nil` is used.
|
||||
returned. Otherwise, `default` is returned (which is `nil` unless
|
||||
specified otherwise).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -491,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)
|
||||
@@ -500,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
|
||||
@@ -511,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)
|
||||
@@ -520,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)
|
||||
@@ -684,30 +654,23 @@ defmodule Map do
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec drop(map, [key]) :: map
|
||||
@spec drop(map, Enumerable.t()) :: map
|
||||
def drop(map, keys)
|
||||
|
||||
def drop(map, keys) when is_map(map) and is_list(keys) do
|
||||
drop_keys(keys, map)
|
||||
end
|
||||
|
||||
def drop(map, keys) when is_map(map) do
|
||||
IO.warn(
|
||||
"Map.drop/2 with an Enumerable of keys that is not a list is deprecated. " <>
|
||||
" Use a list of keys instead."
|
||||
)
|
||||
|
||||
drop(map, Enum.to_list(keys))
|
||||
keys
|
||||
|> Enum.to_list()
|
||||
|> drop_list(map)
|
||||
end
|
||||
|
||||
def drop(non_map, keys) do
|
||||
:erlang.error({:badmap, non_map}, [non_map, keys])
|
||||
end
|
||||
|
||||
defp drop_keys([], acc), do: acc
|
||||
defp drop_list([], acc), do: acc
|
||||
|
||||
defp drop_keys([key | rest], acc) do
|
||||
drop_keys(rest, delete(acc, key))
|
||||
defp drop_list([key | rest], acc) do
|
||||
drop_list(rest, delete(acc, key))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -724,20 +687,13 @@ defmodule Map do
|
||||
{%{a: 1, c: 3}, %{b: 2}}
|
||||
|
||||
"""
|
||||
@spec split(map, [key]) :: {map, map}
|
||||
@spec split(map, Enumerable.t()) :: {map, map}
|
||||
def split(map, keys)
|
||||
|
||||
def split(map, keys) when is_map(map) and is_list(keys) do
|
||||
split(keys, [], map)
|
||||
end
|
||||
|
||||
def split(map, keys) when is_map(map) do
|
||||
IO.warn(
|
||||
"Map.split/2 with an Enumerable of keys that is not a list is deprecated. " <>
|
||||
" Use a list of keys instead."
|
||||
)
|
||||
|
||||
split(map, Enum.to_list(keys))
|
||||
keys
|
||||
|> Enum.to_list()
|
||||
|> split([], map)
|
||||
end
|
||||
|
||||
def split(non_map, keys) do
|
||||
@@ -788,7 +744,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`.
|
||||
@@ -852,7 +808,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)
|
||||
@@ -920,7 +876,8 @@ defmodule Map do
|
||||
def equal?(term, other), do: :erlang.error({:badmap, term}, [term, other])
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Kernel.map_size/1 instead"
|
||||
# TODO: Remove on 2.0
|
||||
# (hard-deprecated in elixir_dispatch)
|
||||
def size(map) do
|
||||
map_size(map)
|
||||
end
|
||||
|
||||
+11
-16
@@ -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")
|
||||
@@ -30,10 +30,6 @@ defmodule MapSet do
|
||||
|
||||
`MapSet`s can also be constructed starting from other collection-type data
|
||||
structures: for example, see `MapSet.new/1` or `Enum.into/2`.
|
||||
|
||||
`MapSet` is built on top of `Map`, this means that they share many properties,
|
||||
including logarithmic time complexity. See the documentation for `Map` for more
|
||||
information on its execution time complexity.
|
||||
"""
|
||||
|
||||
# MapSets have an underlying Map. MapSet elements are keys of said map,
|
||||
@@ -45,7 +41,7 @@ defmodule MapSet do
|
||||
@opaque t(value) :: %__MODULE__{map: %{optional(value) => []}}
|
||||
@type t :: t(term)
|
||||
|
||||
# TODO: Remove version key on v2.0
|
||||
# TODO: Remove version key on Elixir 2.0
|
||||
defstruct map: %{}, version: 2
|
||||
|
||||
@doc """
|
||||
@@ -53,7 +49,7 @@ defmodule MapSet do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.new()
|
||||
iex> MapSet.new
|
||||
#MapSet<[]>
|
||||
|
||||
"""
|
||||
@@ -108,16 +104,16 @@ defmodule MapSet do
|
||||
:maps.from_list(acc)
|
||||
end
|
||||
|
||||
defp new_from_list([element | rest], acc) do
|
||||
new_from_list(rest, [{element, @dummy_value} | acc])
|
||||
defp new_from_list([item | rest], acc) do
|
||||
new_from_list(rest, [{item, @dummy_value} | acc])
|
||||
end
|
||||
|
||||
defp new_from_list_transform([], _fun, acc) do
|
||||
:maps.from_list(acc)
|
||||
end
|
||||
|
||||
defp new_from_list_transform([element | rest], fun, acc) do
|
||||
new_from_list_transform(rest, fun, [{fun.(element), @dummy_value} | acc])
|
||||
defp new_from_list_transform([item | rest], fun, acc) do
|
||||
new_from_list_transform(rest, fun, [{fun.(item), @dummy_value} | acc])
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -152,7 +148,7 @@ defmodule MapSet do
|
||||
def difference(map_set1, map_set2)
|
||||
|
||||
# If the first set is less than twice the size of the second map,
|
||||
# it is fastest to re-accumulate elements in the first set that are not
|
||||
# it is fastest to re-accumulate items in the first set that are not
|
||||
# present in the second set.
|
||||
def difference(%MapSet{map: map1}, %MapSet{map: map2})
|
||||
when map_size(map1) < map_size(map2) * 2 do
|
||||
@@ -165,7 +161,7 @@ defmodule MapSet do
|
||||
end
|
||||
|
||||
# If the second set is less than half the size of the first set, it's fastest
|
||||
# to simply iterate through each element in the second set, deleting them from
|
||||
# to simply iterate through each item in the second set, deleting them from
|
||||
# the first set.
|
||||
def difference(%MapSet{map: map1} = map_set, %MapSet{map: map2}) do
|
||||
%{map_set | map: Map.drop(map1, Map.keys(map2))}
|
||||
@@ -214,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
|
||||
|
||||
@@ -402,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
|
||||
|
||||
+379
-653
File diff suppressed because it is too large
Load Diff
@@ -4,25 +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}, line, macro_dispatch?}`
|
||||
# * `{: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
|
||||
|
||||
@defmacros [:defmacro, :defmacrop]
|
||||
@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), kind in @defmacros})
|
||||
put_edge(d, {name, i}, {name, arity})
|
||||
end
|
||||
|
||||
:ok
|
||||
@@ -31,51 +54,58 @@ defmodule Module.LocalsTracker do
|
||||
@doc """
|
||||
Adds a local dispatch from-to the given target.
|
||||
"""
|
||||
def add_local({_set, bag}, from, to, meta, macro_dispatch?)
|
||||
when is_tuple(from) and is_tuple(to) and is_boolean(macro_dispatch?) do
|
||||
put_edge(bag, {:local, from}, {to, get_line(meta), macro_dispatch?})
|
||||
: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), kind in @defmacros})
|
||||
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,52 +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, _, meta, _} <- all_defined,
|
||||
{local, line, macro_dispatch?} <- out_neighbours(bag, {:local, pair}),
|
||||
error = undefined_local_error(set, local, macro_dispatch?),
|
||||
do: {build_meta(line, meta), local, error}
|
||||
|
||||
:lists.usort(undefined)
|
||||
end
|
||||
|
||||
defp undefined_local_error(set, local, true) do
|
||||
case :ets.member(set, {:def, local}) do
|
||||
true -> false
|
||||
false -> :undefined_function
|
||||
end
|
||||
end
|
||||
|
||||
defp undefined_local_error(set, local, false) do
|
||||
try do
|
||||
if :ets.lookup_element(set, {:def, local}, 2) in @defmacros do
|
||||
:incorrect_dispatch
|
||||
else
|
||||
false
|
||||
end
|
||||
catch
|
||||
_, _ -> :undefined_function
|
||||
end
|
||||
end
|
||||
|
||||
defp unreachable(reachable, reattached, private) do
|
||||
for {tuple, kind, _, _} <- private,
|
||||
not reachable?(tuple, kind, reachable, reattached),
|
||||
@@ -193,51 +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, _meta), do: []
|
||||
|
||||
# We need to transform any file annotation in the function
|
||||
# definition into a keep annotation that is used by the
|
||||
# error handling system in order to respect line/file.
|
||||
defp build_meta(line, meta) do
|
||||
case Keyword.get(meta, :file) do
|
||||
{file, _} -> [keep: {file, line}]
|
||||
_ -> [line: line]
|
||||
end
|
||||
end
|
||||
|
||||
## 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)
|
||||
|
||||
@@ -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]
|
||||
@@ -250,7 +246,6 @@ defmodule Node do
|
||||
|
||||
This function will raise `FunctionClauseError` if the given `node` is not alive.
|
||||
"""
|
||||
@spec set_cookie(t, atom) :: true
|
||||
def set_cookie(node \\ Node.self(), cookie) when is_atom(cookie) do
|
||||
:erlang.set_cookie(node, cookie)
|
||||
end
|
||||
@@ -260,7 +255,6 @@ defmodule Node do
|
||||
|
||||
Returns the cookie if the node is alive, otherwise `:nocookie`.
|
||||
"""
|
||||
@spec get_cookie() :: atom
|
||||
def get_cookie() do
|
||||
:erlang.get_cookie()
|
||||
end
|
||||
|
||||
+129
-169
@@ -1,29 +1,6 @@
|
||||
defmodule OptionParser do
|
||||
@moduledoc """
|
||||
Functions for parsing command line arguments.
|
||||
|
||||
When calling a command, it's possible to pass command line options
|
||||
to modify what the command does. In this documentation, those are
|
||||
called "switches", in other situations they may be called "flags"
|
||||
or simply "options". A switch can be given a value, also called an
|
||||
"argument".
|
||||
|
||||
The main function in this module is `parse/2`, which parses a list
|
||||
of command line options and arguments into a keyword list:
|
||||
|
||||
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()]
|
||||
@@ -57,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.
|
||||
@@ -76,25 +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 and their types. Any switch
|
||||
in `argv` that is not specified in the list is returned in the
|
||||
invalid options list. This is the preferred way to parse options.
|
||||
|
||||
* `:switches` - defines switches and their types. This function
|
||||
* `: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
|
||||
@@ -121,7 +95,7 @@ defmodule OptionParser do
|
||||
Switches can be specified with modifiers, which change how
|
||||
they behave. The following modifiers are supported:
|
||||
|
||||
* `:keep` - keeps duplicated elements instead of overriding them;
|
||||
* `:keep` - keeps duplicated items instead of overriding them;
|
||||
works with all types except `:count`. Specifying `switch_name: :keep`
|
||||
assumes the type of `:switch_name` will be `:string`.
|
||||
|
||||
@@ -136,53 +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**. For example:
|
||||
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])
|
||||
# ... then somewhere in the same module you access it ...
|
||||
{opts, _, _} = OptionParser.parse(["--option-parser-example"])
|
||||
opts[:option_parser_example]
|
||||
|
||||
In other words, Elixir will 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
|
||||
@@ -192,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])
|
||||
@@ -213,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"], [], []}
|
||||
|
||||
"""
|
||||
@@ -253,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}
|
||||
@@ -279,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"], []}
|
||||
|
||||
"""
|
||||
@@ -308,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}
|
||||
@@ -355,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)}
|
||||
@@ -385,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`
|
||||
|
||||
@@ -419,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) ->
|
||||
@@ -442,21 +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
|
||||
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
|
||||
|
||||
@@ -464,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}
|
||||
@@ -493,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"]
|
||||
@@ -512,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} -> []
|
||||
@@ -604,7 +543,6 @@ defmodule OptionParser do
|
||||
{strict, true}
|
||||
|
||||
true ->
|
||||
IO.warn("not passing the :switches or :strict option to OptionParser is deprecated")
|
||||
{[], false}
|
||||
end
|
||||
|
||||
@@ -631,7 +569,7 @@ defmodule OptionParser do
|
||||
|
||||
:count in kinds ->
|
||||
case value do
|
||||
nil -> {false, 1}
|
||||
1 -> {false, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
|
||||
@@ -671,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]) ->
|
||||
@@ -687,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
|
||||
@@ -697,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
|
||||
@@ -707,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}
|
||||
|
||||
@@ -748,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
|
||||
|
||||
@@ -766,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)
|
||||
|
||||
+26
-41
@@ -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 """
|
||||
@@ -95,8 +94,6 @@ defmodule Path do
|
||||
absname(absname_join(name), cwd)
|
||||
end
|
||||
|
||||
@slash [?/, ?\\]
|
||||
|
||||
# Joins a list
|
||||
defp absname_join([name1, name2 | rest]), do: absname_join([absname_join(name1, name2) | rest])
|
||||
|
||||
@@ -112,11 +109,6 @@ defmodule Path do
|
||||
do_absname_join(rest, relativename, [?:, uc_letter + ?a - ?A], :win32)
|
||||
end
|
||||
|
||||
defp do_absname_join(<<c1, c2, rest::binary>>, relativename, [], :win32)
|
||||
when c1 in @slash and c2 in @slash do
|
||||
do_absname_join(rest, relativename, '//', :win32)
|
||||
end
|
||||
|
||||
defp do_absname_join(<<?\\, rest::binary>>, relativename, result, :win32),
|
||||
do: do_absname_join(<<?/, rest::binary>>, relativename, result, :win32)
|
||||
|
||||
@@ -158,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 """
|
||||
@@ -179,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 """
|
||||
@@ -261,6 +252,8 @@ defmodule Path do
|
||||
defp unix_pathtype([list | rest]) when is_list(list), do: unix_pathtype(list ++ rest)
|
||||
defp unix_pathtype(relative), do: {:relative, relative}
|
||||
|
||||
@slash [?/, ?\\]
|
||||
|
||||
defp win32_pathtype([list | rest]) when is_list(list), do: win32_pathtype(list ++ rest)
|
||||
|
||||
defp win32_pathtype([char, list | rest]) when is_list(list),
|
||||
@@ -406,9 +399,6 @@ defmodule Path do
|
||||
iex> Path.dirname("/foo/bar/")
|
||||
"/foo/bar"
|
||||
|
||||
iex> Path.dirname("bar.ex")
|
||||
"."
|
||||
|
||||
"""
|
||||
@spec dirname(t) :: binary
|
||||
def dirname(path) do
|
||||
@@ -602,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.
|
||||
|
||||
@@ -626,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
|
||||
|
||||
@@ -692,9 +672,14 @@ defmodule Path do
|
||||
|
||||
defp resolve_home(rest) do
|
||||
case {rest, major_os_type()} do
|
||||
{"\\" <> _, :win32} -> System.user_home!() <> rest
|
||||
{"/" <> _, _} -> System.user_home!() <> rest
|
||||
_ -> "~" <> rest
|
||||
{"\\" <> _, :win32} ->
|
||||
System.user_home!() <> rest
|
||||
|
||||
{"/" <> _, _} ->
|
||||
System.user_home!() <> rest
|
||||
|
||||
_ ->
|
||||
rest
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+18
-25
@@ -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}
|
||||
@@ -89,7 +89,7 @@ defmodule Port do
|
||||
{#Port<0.1444>, {:data, "hello\n"}}
|
||||
|
||||
`:spawn` will retrieve the program name from the argument and traverse your
|
||||
operating system `$PATH` environment variable looking for a matching program.
|
||||
OS `$PATH` environment variable looking for a matching program.
|
||||
|
||||
Although the above is handy, it means it is impossible to invoke an executable
|
||||
that has whitespaces on its name or in any of its arguments. For those reasons,
|
||||
@@ -117,7 +117,7 @@ defmodule Port do
|
||||
reimplementing core part of the Runtime System, such as the `:user` and
|
||||
`:shell` processes.
|
||||
|
||||
## Zombie operating system processes
|
||||
## Zombie OS 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 """
|
||||
@@ -226,7 +222,6 @@ defmodule Port do
|
||||
|
||||
For more information, see `:erlang.port_info/1`.
|
||||
"""
|
||||
@spec info(port) :: keyword | nil
|
||||
def info(port) do
|
||||
nillify(:erlang.port_info(port))
|
||||
end
|
||||
@@ -262,16 +257,15 @@ defmodule Port do
|
||||
where:
|
||||
|
||||
* `ref` is a monitor reference returned by this function;
|
||||
* `object` is either the `port` being monitored (when monitoring by port ID)
|
||||
* `object` is either the `port` being monitored (when monitoring by port id)
|
||||
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
|
||||
@@ -283,11 +277,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
@@ -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.
|
||||
"""
|
||||
|
||||
+198
-333
@@ -1,241 +1,15 @@
|
||||
defmodule Protocol do
|
||||
@moduledoc ~S"""
|
||||
Reference and functions for working with protocols.
|
||||
|
||||
A protocol specifies an API that should be defined by its
|
||||
implementations. A protocol is defined with `Kernel.defprotocol/2`
|
||||
and its implementations with `Kernel.defimpl/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
In Elixir, we have two verbs for checking how many items there
|
||||
are in a data structure: `length` and `size`. `length` means the
|
||||
information must be computed. For example, `length(list)` needs to
|
||||
traverse the whole list to calculate its length. On the other hand,
|
||||
`tuple_size(tuple)` and `byte_size(binary)` do not depend on the
|
||||
tuple and binary size as the size information is precomputed in
|
||||
the data structure.
|
||||
|
||||
Although Elixir includes specific functions such as `tuple_size`,
|
||||
`binary_size` and `map_size`, sometimes we want to be able to
|
||||
retrieve the size of a data structure regardless of its type.
|
||||
In Elixir we can write polymorphic code, i.e. code that works
|
||||
with different shapes/types, by using protocols. A size protocol
|
||||
could be implemented as follows:
|
||||
|
||||
defprotocol Size do
|
||||
@doc "Calculates the size (and not the length!) of a data structure"
|
||||
def size(data)
|
||||
end
|
||||
|
||||
Now that the protocol can be implemented for every data structure
|
||||
the protocol may have a compliant implementation for:
|
||||
|
||||
defimpl Size, for: BitString do
|
||||
def size(binary), do: byte_size(binary)
|
||||
end
|
||||
|
||||
defimpl Size, for: Map do
|
||||
def size(map), do: map_size(map)
|
||||
end
|
||||
|
||||
defimpl Size, for: Tuple do
|
||||
def size(tuple), do: tuple_size(tuple)
|
||||
end
|
||||
|
||||
Notice we didn't implement it for lists as we don't have the
|
||||
`size` information on lists, rather its value needs to be
|
||||
computed with `length`.
|
||||
|
||||
It is possible to implement protocols for all Elixir types:
|
||||
|
||||
* Structs (see below)
|
||||
* `Tuple`
|
||||
* `Atom`
|
||||
* `List`
|
||||
* `BitString`
|
||||
* `Integer`
|
||||
* `Float`
|
||||
* `Function`
|
||||
* `PID`
|
||||
* `Map`
|
||||
* `Port`
|
||||
* `Reference`
|
||||
* `Any` (see below)
|
||||
|
||||
## Protocols and Structs
|
||||
|
||||
The real benefit of protocols comes when mixed with structs.
|
||||
For instance, Elixir ships with many data types implemented as
|
||||
structs, like `MapSet`. We can implement the `Size` protocol
|
||||
for those types as well:
|
||||
|
||||
defimpl Size, for: MapSet do
|
||||
def size(map_set), do: MapSet.size(map_set)
|
||||
end
|
||||
|
||||
When implementing a protocol for a struct, the `:for` option can
|
||||
be omitted if the `defimpl` call is inside the module that defines
|
||||
the struct:
|
||||
|
||||
defmodule User do
|
||||
defstruct [:email, :name]
|
||||
|
||||
defimpl Size do
|
||||
# two fields
|
||||
def size(%User{}), do: 2
|
||||
end
|
||||
end
|
||||
|
||||
If a protocol implementation is not found for a given type,
|
||||
invoking the protocol will raise unless it is configured to
|
||||
fall back to `Any`. Conveniences for building implementations
|
||||
on top of existing ones are also available, look at `defstruct/1`
|
||||
for more information about deriving
|
||||
protocols.
|
||||
|
||||
## Fallback to `Any`
|
||||
|
||||
In some cases, it may be convenient to provide a default
|
||||
implementation for all types. This can be achieved by setting
|
||||
the `@fallback_to_any` attribute to `true` in the protocol
|
||||
definition:
|
||||
|
||||
defprotocol Size do
|
||||
@fallback_to_any true
|
||||
def size(data)
|
||||
end
|
||||
|
||||
The `Size` protocol can now be implemented for `Any`:
|
||||
|
||||
defimpl Size, for: Any do
|
||||
def size(_), do: 0
|
||||
end
|
||||
|
||||
Although the implementation above is arguably not a reasonable
|
||||
one. For example, it makes no sense to say a PID or an integer
|
||||
have a size of `0`. That's one of the reasons why `@fallback_to_any`
|
||||
is an opt-in behaviour. For the majority of protocols, raising
|
||||
an error when a protocol is not implemented is the proper behaviour.
|
||||
|
||||
## Multiple implementations
|
||||
|
||||
Protocols can also be implemented for multiple types at once:
|
||||
|
||||
defprotocol Reversible do
|
||||
def reverse(term)
|
||||
end
|
||||
|
||||
defimpl Reversible, for: [Map, List] do
|
||||
def reverse(term), do: Enum.reverse(term)
|
||||
end
|
||||
|
||||
Inside `defimpl/2`, you can use `@protocol` to access the protocol
|
||||
being implemented and `@for` to access the module it is being
|
||||
defined for.
|
||||
|
||||
## Types
|
||||
|
||||
Defining a protocol automatically defines a type named `t`, which
|
||||
can be used as follows:
|
||||
|
||||
@spec print_size(Size.t()) :: :ok
|
||||
def print_size(data) do
|
||||
result =
|
||||
case Size.size(data) do
|
||||
0 -> "data has no items"
|
||||
1 -> "data has one item"
|
||||
n -> "data has #{n} items"
|
||||
end
|
||||
|
||||
IO.puts(result)
|
||||
end
|
||||
|
||||
The `@spec` above expresses that all types allowed to implement the
|
||||
given protocol are valid argument types for the given function.
|
||||
|
||||
## Reflection
|
||||
|
||||
Any protocol module contains three extra functions:
|
||||
|
||||
* `__protocol__/1` - returns the protocol information. The function takes
|
||||
one of the following atoms:
|
||||
|
||||
* `:consolidated?` - returns whether the protocol is consolidated
|
||||
|
||||
* `:functions` - returns keyword list of protocol functions and their arities
|
||||
|
||||
* `:impls` - if consolidated, returns `{:consolidated, modules}` with the list of modules
|
||||
implementing the protocol, otherwise `:not_consolidated`
|
||||
|
||||
* `:module` - the protocol module atom name
|
||||
|
||||
* `impl_for/1` - receives a structure and returns the module that
|
||||
implements the protocol for the structure, `nil` otherwise
|
||||
|
||||
* `impl_for!/1` - same as above but raises an error if an implementation is
|
||||
not found
|
||||
|
||||
For example, for the `Enumerable` protocol we have:
|
||||
|
||||
iex> Enumerable.__protocol__(:functions)
|
||||
[count: 1, member?: 2, reduce: 3, slice: 1]
|
||||
|
||||
iex> Enumerable.impl_for([])
|
||||
Enumerable.List
|
||||
|
||||
iex> Enumerable.impl_for(42)
|
||||
nil
|
||||
|
||||
## Consolidation
|
||||
|
||||
In order to cope with code loading in development, protocols in
|
||||
Elixir provide a slow implementation of protocol dispatching specific
|
||||
to development.
|
||||
|
||||
In order to speed up dispatching in production environments, where
|
||||
all implementations are known up-front, Elixir provides a feature
|
||||
called protocol consolidation. Consolidation directly links protocols
|
||||
to their implementations in a way that invoking a function from a
|
||||
consolidated protocol is equivalent to invoking two remote functions.
|
||||
|
||||
Protocol consolidation is applied by default to all Mix projects during
|
||||
compilation. This may be an issue during test. For instance, if you want
|
||||
to implement a protocol during test, the implementation will have no
|
||||
effect, as the protocol has already been consolidated. One possible
|
||||
solution is to include compilation directories that are specific to your
|
||||
test environment in your mix.exs:
|
||||
|
||||
def project do
|
||||
...
|
||||
elixirc_paths: elixirc_paths(Mix.env())
|
||||
...
|
||||
end
|
||||
|
||||
defp elixirc_paths(:test), do: ["lib", "test/support"]
|
||||
defp elixirc_paths(_), do: ["lib"]
|
||||
|
||||
And then you can define the implementations specific to the test environment
|
||||
inside `test/support/some_file.ex`.
|
||||
|
||||
Another approach is to disable protocol consolidation during tests in your
|
||||
mix.exs:
|
||||
|
||||
def project do
|
||||
...
|
||||
consolidate_protocols: Mix.env() != :test
|
||||
...
|
||||
end
|
||||
|
||||
Although doing so is not recommended as it may affect your test suite
|
||||
performance.
|
||||
|
||||
Finally note all protocols are compiled with `debug_info` set to `true`,
|
||||
regardless of the option set by `elixirc` compiler. The debug info is
|
||||
used for consolidation and it may be removed after consolidation.
|
||||
@moduledoc """
|
||||
Functions for working with protocols.
|
||||
"""
|
||||
|
||||
@doc false
|
||||
@doc """
|
||||
Defines a new protocol function.
|
||||
|
||||
Protocols do not allow functions to be defined directly, instead, the
|
||||
regular `Kernel.def/*` macros are replaced by this macro which
|
||||
defines the protocol functions with the appropriate callbacks.
|
||||
"""
|
||||
defmacro def(signature)
|
||||
|
||||
defmacro def({_, _, args}) when args == [] or is_atom(args) do
|
||||
@@ -248,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]
|
||||
@@ -268,9 +42,9 @@ defmodule Protocol do
|
||||
impl_for!(term).unquote(name)(unquote_splicing(call_args))
|
||||
end
|
||||
|
||||
# Copy spec as callback if possible,
|
||||
# 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
|
||||
@@ -284,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
|
||||
@@ -311,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
|
||||
@@ -351,7 +125,7 @@ defmodule Protocol do
|
||||
## Examples
|
||||
|
||||
defprotocol Derivable do
|
||||
def ok(arg)
|
||||
def ok(a)
|
||||
end
|
||||
|
||||
defimpl Derivable, for: Any do
|
||||
@@ -373,10 +147,13 @@ defmodule Protocol do
|
||||
defmodule ImplStruct do
|
||||
@derive [Derivable]
|
||||
defstruct a: 0, b: 0
|
||||
end
|
||||
|
||||
Derivable.ok(%ImplStruct{})
|
||||
{:ok, %ImplStruct{a: 0, b: 0}, %ImplStruct{a: 0, b: 0}, []}
|
||||
defimpl Sample do
|
||||
def ok(struct) do
|
||||
Unknown.undefined(struct)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Explicit derivations can now be called via `__deriving__`:
|
||||
|
||||
@@ -391,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
|
||||
|
||||
@@ -408,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
|
||||
@@ -437,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
|
||||
@@ -506,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
|
||||
@@ -525,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}
|
||||
@@ -562,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
|
||||
@@ -646,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
|
||||
@@ -690,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)
|
||||
@@ -698,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))
|
||||
@@ -710,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
|
||||
|
||||
@@ -720,15 +567,14 @@ defmodule Protocol do
|
||||
target = Module.concat(__MODULE__, mod)
|
||||
|
||||
Kernel.def impl_for(data) when :erlang.unquote(guard)(data) do
|
||||
try do
|
||||
unquote(target).__impl__(:target)
|
||||
rescue
|
||||
UndefinedFunctionError ->
|
||||
unquote(any_impl_for)
|
||||
case Code.ensure_compiled?(unquote(target)) and
|
||||
function_exported?(unquote(target), :__impl__, 1) do
|
||||
true -> unquote(target).__impl__(:target)
|
||||
false -> unquote(any_impl_for)
|
||||
end
|
||||
end
|
||||
end,
|
||||
built_in
|
||||
builtin
|
||||
)
|
||||
|
||||
# Define a catch-all impl_for/1 clause to pacify Dialyzer (since
|
||||
@@ -757,18 +603,16 @@ defmodule Protocol do
|
||||
Kernel.defp struct_impl_for(struct) do
|
||||
target = Module.concat(__MODULE__, struct)
|
||||
|
||||
try do
|
||||
target.__impl__(:target)
|
||||
rescue
|
||||
UndefinedFunctionError ->
|
||||
unquote(any_impl_for)
|
||||
case Code.ensure_compiled?(target) and function_exported?(target, :__impl__, 1) do
|
||||
true -> target.__impl__(:target)
|
||||
false -> unquote(any_impl_for)
|
||||
end
|
||||
end
|
||||
|
||||
# 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
|
||||
|
||||
@@ -780,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)
|
||||
@@ -844,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) ->
|
||||
@@ -866,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)
|
||||
|
||||
@@ -902,16 +751,32 @@ defmodule Protocol do
|
||||
"implement protocols after compilation or during tests, check the " <>
|
||||
"\"Consolidation\" section in the documentation for Kernel.defprotocol/2"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
end
|
||||
|
||||
: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,
|
||||
|
||||
+13
-43
@@ -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,35 +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 = _range1, first2..last2 = _range2) 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
|
||||
@@ -100,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
@@ -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
|
||||
|
||||
+52
-116
@@ -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
|
||||
@@ -33,6 +33,19 @@ defmodule Regex do
|
||||
|
||||
~r/(?<foo>.)(?<bar>.)/.source == ~r/(?<foo>.)(?<bar>.)/.source
|
||||
|
||||
## Precompilation
|
||||
|
||||
Regular expressions built with sigil are precompiled and stored in `.beam`
|
||||
files. This may be a problem if you are precompiling Elixir to run in
|
||||
different OTP releases, as OTP releases may update the underlying regular
|
||||
expression engine at any time.
|
||||
|
||||
For such reasons, we always recommend precompiling Elixir projects using
|
||||
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.
|
||||
|
||||
## Modifiers
|
||||
|
||||
The modifiers available when creating a Regex are:
|
||||
@@ -45,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
|
||||
@@ -66,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
|
||||
|
||||
@@ -80,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
|
||||
|
||||
@@ -90,56 +103,6 @@ defmodule Regex do
|
||||
|
||||
* `list(binary)` - a list of named captures to capture
|
||||
|
||||
## Character classes
|
||||
|
||||
Regex supports several built in named character classes. These are used by
|
||||
enclosing the class name in `[: :]` inside a group. For example:
|
||||
|
||||
iex> String.match?("123", ~r/^[[:alnum:]]+$/)
|
||||
true
|
||||
iex> String.match?("123 456", ~r/^[[:alnum:][:blank:]]+$/)
|
||||
true
|
||||
|
||||
The supported class names are:
|
||||
|
||||
* alnum - Letters and digits
|
||||
* alpha - Letters
|
||||
* ascii - Character codes 0-127
|
||||
* blank - Space or tab only
|
||||
* cntrl - Control characters
|
||||
* digit - Decimal digits (same as \\d)
|
||||
* graph - Printing characters, excluding space
|
||||
* lower - Lowercase letters
|
||||
* print - Printing characters, including space
|
||||
* punct - Printing characters, excluding letters, digits, and space
|
||||
* space - Whitespace (the same as \s from PCRE 8.34)
|
||||
* upper - Uppercase letters
|
||||
* word - "Word" characters (same as \w)
|
||||
* xdigit - Hexadecimal digits
|
||||
|
||||
Note the behaviour of those classes may change according to the Unicode
|
||||
and other modifiers:
|
||||
|
||||
iex> String.match?("josé", ~r/^[[:lower:]]+$/)
|
||||
false
|
||||
iex> String.match?("josé", ~r/^[[:lower:]]+$/u)
|
||||
true
|
||||
|
||||
## Precompilation
|
||||
|
||||
Regular expressions built with sigil are precompiled and stored in `.beam`
|
||||
files. Precompiled regexes will be checked in runtime and may work slower
|
||||
between operating systems and OTP releases. This is rarely a problem, as most Elixir code
|
||||
shared during development is compiled on the target (such as dependencies,
|
||||
archives, and escripts) and, when running in production, the code must either
|
||||
be compiled on the target (via `mix compile` or similar) or released on the
|
||||
host (via `mix releases` or similar) with a matching OTP, OS and architecture
|
||||
as as the target.
|
||||
|
||||
If you know you are running on a different system that the current one and
|
||||
you are doing multiple matches with the regex, you can manually invoke
|
||||
`Regex.recompile/1` or `Regex.recompile!/1` to perform a runtime version
|
||||
check and recompile the regex if necessary.
|
||||
"""
|
||||
|
||||
defstruct re_pattern: nil, source: "", opts: "", re_version: ""
|
||||
@@ -154,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.
|
||||
@@ -164,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
|
||||
|
||||
@@ -189,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}}
|
||||
@@ -203,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}"
|
||||
@@ -216,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}
|
||||
|
||||
_ ->
|
||||
@@ -234,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
|
||||
@@ -246,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 """
|
||||
@@ -265,8 +229,8 @@ defmodule Regex do
|
||||
|
||||
"""
|
||||
@spec match?(t, String.t()) :: boolean
|
||||
def match?(%Regex{} = regex, string) when is_binary(string) do
|
||||
safe_run(regex, string, [{:capture, :none}]) == :match
|
||||
def match?(%Regex{re_pattern: compiled}, string) when is_binary(string) do
|
||||
:re.run(string, compiled, [{:capture, :none}]) == :match
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -293,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.
|
||||
|
||||
@@ -313,11 +276,11 @@ defmodule Regex do
|
||||
@spec run(t, binary, [term]) :: nil | [binary] | [{integer, integer}]
|
||||
def run(regex, string, options \\ [])
|
||||
|
||||
def run(%Regex{} = regex, string, options) when is_binary(string) do
|
||||
def run(%Regex{re_pattern: compiled}, string, options) when is_binary(string) do
|
||||
return = Keyword.get(options, :return, :binary)
|
||||
captures = Keyword.get(options, :capture, :all)
|
||||
|
||||
case safe_run(regex, string, [{:capture, captures, return}]) do
|
||||
case :re.run(string, compiled, [{:capture, captures, return}]) do
|
||||
:nomatch -> nil
|
||||
:match -> []
|
||||
{:match, results} -> results
|
||||
@@ -325,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
|
||||
|
||||
@@ -398,17 +358,7 @@ defmodule Regex do
|
||||
|
||||
"""
|
||||
@spec names(t) :: [String.t()]
|
||||
def names(%Regex{re_pattern: compiled, re_version: version, source: source}) do
|
||||
re_pattern =
|
||||
case version() do
|
||||
^version ->
|
||||
compiled
|
||||
|
||||
_ ->
|
||||
{:ok, recompiled} = :re.compile(source)
|
||||
recompiled
|
||||
end
|
||||
|
||||
def names(%Regex{re_pattern: re_pattern}) do
|
||||
{:namelist, names} = :re.inspect(re_pattern, :namelist)
|
||||
names
|
||||
end
|
||||
@@ -422,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.
|
||||
|
||||
@@ -441,36 +390,22 @@ 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 \\ [])
|
||||
|
||||
def scan(%Regex{} = regex, string, options) when is_binary(string) do
|
||||
def scan(%Regex{re_pattern: compiled}, string, options) when is_binary(string) do
|
||||
return = Keyword.get(options, :return, :binary)
|
||||
captures = Keyword.get(options, :capture, :all)
|
||||
options = [{:capture, captures, return}, :global]
|
||||
|
||||
case safe_run(regex, string, options) do
|
||||
case :re.run(string, compiled, options) do
|
||||
:match -> []
|
||||
:nomatch -> []
|
||||
{:match, results} -> results
|
||||
end
|
||||
end
|
||||
|
||||
defp safe_run(
|
||||
%Regex{re_pattern: compiled, source: source, re_version: version, opts: compile_opts},
|
||||
string,
|
||||
options
|
||||
) do
|
||||
case version() do
|
||||
^version -> :re.run(string, compiled, options)
|
||||
_ -> :re.run(string, source, translate_options(compile_opts, options))
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Splits the given target based on the given pattern and in the given number of
|
||||
parts.
|
||||
@@ -497,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")
|
||||
@@ -530,11 +465,11 @@ defmodule Regex do
|
||||
end
|
||||
end
|
||||
|
||||
def split(%Regex{} = regex, string, opts)
|
||||
def split(%Regex{re_pattern: compiled}, string, opts)
|
||||
when is_binary(string) and is_list(opts) do
|
||||
on = Keyword.get(opts, :on, :first)
|
||||
|
||||
case safe_run(regex, string, [:global, capture: on]) do
|
||||
case :re.run(string, compiled, [:global, capture: on]) do
|
||||
{:match, matches} ->
|
||||
index = parts_to_index(Keyword.get(opts, :parts, :infinity))
|
||||
trim = Keyword.get(opts, :trim, false)
|
||||
@@ -652,15 +587,15 @@ 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
|
||||
|
||||
defp do_replace(%Regex{} = regex, string, replacement, options) do
|
||||
defp do_replace(%Regex{re_pattern: compiled}, string, replacement, options) do
|
||||
opts = if Keyword.get(options, :global) != false, do: [:global], else: []
|
||||
opts = [{:capture, :all, :index} | opts]
|
||||
|
||||
case safe_run(regex, string, opts) do
|
||||
case :re.run(string, compiled, opts) do
|
||||
:nomatch ->
|
||||
string
|
||||
|
||||
@@ -844,6 +779,7 @@ defmodule Regex do
|
||||
|
||||
defp translate_options(<<?m, t::binary>>, acc), do: translate_options(t, [:multiline | acc])
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
defp translate_options(<<?r, t::binary>>, acc) do
|
||||
IO.warn("the /r modifier in regular expressions is deprecated, please use /U instead")
|
||||
translate_options(t, [:ungreedy | acc])
|
||||
|
||||
+85
-352
@@ -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}
|
||||
@@ -78,14 +62,14 @@ defmodule Registry do
|
||||
Now, an entity interested in dispatching events for a given key may call
|
||||
`dispatch/3` passing in the key and a callback. This callback will be invoked
|
||||
with a list of all the values registered under the requested key, alongside
|
||||
the PID of the process that registered each value, in the form of `{pid,
|
||||
the pid of the process that registered each value, in the form of `{pid,
|
||||
value}` tuples. In our example, `value` will be the `{module, function}` tuple
|
||||
in the code above:
|
||||
|
||||
Registry.dispatch(Registry.DispatcherTest, "hello", fn entries ->
|
||||
for {pid, {module, function}} <- entries, do: apply(module, function, [pid])
|
||||
end)
|
||||
# Prints #PID<...> where the PID is for the process that called register/3 above
|
||||
# Prints #PID<...> where the pid is for the process that called register/3 above
|
||||
#=> :ok
|
||||
|
||||
Dispatching happens in the process that calls `dispatch/3` either serially or
|
||||
@@ -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)
|
||||
@@ -161,7 +138,7 @@ defmodule Registry do
|
||||
in the function documentation.
|
||||
|
||||
However, keep in mind those cases are typically not an issue. After all, a
|
||||
process referenced by a PID may crash at any time, including between getting
|
||||
process referenced by a pid may crash at any time, including between getting
|
||||
the value from the registry and sending it a message. Many parts of the standard
|
||||
library are designed to cope with that, such as `Process.monitor/1` which will
|
||||
deliver the `:DOWN` message immediately if the monitored process is already dead
|
||||
@@ -194,28 +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] | []
|
||||
|
||||
@typedoc "A pattern used to representing the output format part of a match spec"
|
||||
@type body :: [atom | tuple]
|
||||
|
||||
@typedoc "A full match spec used when selecting objects in the registry"
|
||||
@type spec :: [{match_pattern, guards, body}]
|
||||
|
||||
## 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)
|
||||
@@ -234,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
|
||||
@@ -270,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
|
||||
|
||||
@@ -305,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,
|
||||
@@ -322,17 +275,11 @@ defmodule Registry do
|
||||
"expected :keys to be given and be one of :unique or :duplicate, got: #{inspect(keys)}"
|
||||
end
|
||||
|
||||
name =
|
||||
case Keyword.fetch(options, :name) do
|
||||
{:ok, name} when is_atom(name) ->
|
||||
name
|
||||
name = Keyword.get(options, :name)
|
||||
|
||||
{:ok, other} ->
|
||||
raise ArgumentError, "expected :name to be an atom, got: #{inspect(other)}"
|
||||
|
||||
:error ->
|
||||
raise ArgumentError, "expected :name option to be present"
|
||||
end
|
||||
unless is_atom(name) do
|
||||
raise ArgumentError, "expected :name to be given and to be an atom, got: #{inspect(name)}"
|
||||
end
|
||||
|
||||
meta = Keyword.get(options, :meta, [])
|
||||
|
||||
@@ -363,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
|
||||
@@ -374,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),
|
||||
@@ -393,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,8 +386,8 @@ defmodule Registry do
|
||||
for the given `registry`.
|
||||
|
||||
The list of `entries` is a non-empty list of two-element tuples where
|
||||
the first element is the PID and the second element is the value
|
||||
associated to the PID. If there are no entries for the given key,
|
||||
the first element is the pid and the second element is the value
|
||||
associated to the pid. If there are no entries for the given key,
|
||||
the callback is never invoked.
|
||||
|
||||
If the registry is partitioned, the callback is invoked multiple times
|
||||
@@ -447,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
|
||||
@@ -540,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)
|
||||
@@ -562,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
|
||||
@@ -592,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.
|
||||
|
||||
@@ -611,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, :_, :_})
|
||||
@@ -622,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, :"$_"}]}]
|
||||
@@ -663,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)
|
||||
@@ -674,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)
|
||||
@@ -683,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)
|
||||
@@ -691,7 +635,7 @@ defmodule Registry do
|
||||
|
||||
keys =
|
||||
try do
|
||||
spec = [{{pid, :"$1", :"$2", :_}, [], [{{:"$1", :"$2"}}]}]
|
||||
spec = [{{pid, :"$1", :"$2"}, [], [{{:"$1", :"$2"}}]}]
|
||||
:ets.select(pid_ets, spec)
|
||||
catch
|
||||
:error, :badarg -> []
|
||||
@@ -734,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"]
|
||||
@@ -745,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())
|
||||
@@ -756,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()
|
||||
@@ -768,8 +711,8 @@ defmodule Registry do
|
||||
# Remove first from the key_ets because in case of crashes
|
||||
# the pid_ets will still be able to clean up. The last step is
|
||||
# to clean if we have no more entries.
|
||||
true = __unregister__(key_ets, {key, {self, :_}}, 1)
|
||||
true = __unregister__(pid_ets, {self, key, key_ets, :_}, 2)
|
||||
true = :ets.match_delete(key_ets, {key, {self, :_}})
|
||||
true = :ets.delete_object(pid_ets, {self, key, key_ets})
|
||||
|
||||
unlink_if_unregistered(pid_server, pid_ets, self)
|
||||
|
||||
@@ -788,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"]
|
||||
@@ -803,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)
|
||||
@@ -815,10 +758,7 @@ defmodule Registry do
|
||||
["hello", "hello"]
|
||||
iex> Registry.lookup(Registry.DuplicateUnregisterMatchTest, "hello")
|
||||
[{self(), :world_b}, {self(), :world_c}]
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec unregister_match(registry, key, match_pattern, guards) :: :ok
|
||||
def unregister_match(registry, key, pattern, guards \\ []) when is_list(guards) do
|
||||
self = self()
|
||||
|
||||
@@ -831,7 +771,8 @@ defmodule Registry do
|
||||
# the pid_ets will still be able to clean up. The last step is
|
||||
# to clean if we have no more entries.
|
||||
|
||||
# Here we want to count all entries for this pid under this key, regardless of pattern.
|
||||
# Here we want to count all entries for this pid under this key, regardless
|
||||
# of pattern.
|
||||
underscore_guard = {:"=:=", {:element, 1, :"$_"}, {:const, key}}
|
||||
total_spec = [{{:_, {self, :_}}, [underscore_guard], [true]}]
|
||||
total = :ets.select_count(key_ets, total_spec)
|
||||
@@ -842,7 +783,8 @@ defmodule Registry do
|
||||
case :ets.select_delete(key_ets, delete_spec) do
|
||||
# We deleted everything, we can just delete the object
|
||||
^total ->
|
||||
true = __unregister__(pid_ets, {self, key, key_ets, :_}, 2)
|
||||
true = :ets.delete_object(pid_ets, {self, key, key_ets})
|
||||
|
||||
unlink_if_unregistered(pid_server, pid_ets, self)
|
||||
|
||||
for listener <- listeners do
|
||||
@@ -857,12 +799,11 @@ defmodule Registry do
|
||||
# duplicate_bag tables will remove every entry, but we only want to
|
||||
# remove those we have deleted. The solution is to introduce a temp_entry
|
||||
# that indicates how many keys WILL be remaining after the delete operation.
|
||||
counter = System.unique_integer()
|
||||
remaining = total - deleted
|
||||
temp_entry = {self, key, {key_ets, remaining}, counter}
|
||||
temp_entry = {self, key, {key_ets, remaining}}
|
||||
true = :ets.insert(pid_ets, temp_entry)
|
||||
true = __unregister__(pid_ets, {self, key, key_ets, :_}, 2)
|
||||
real_keys = List.duplicate({self, key, key_ets, counter}, remaining)
|
||||
true = :ets.delete_object(pid_ets, {self, key, key_ets})
|
||||
real_keys = List.duplicate({self, key, key_ets}, remaining)
|
||||
true = :ets.insert(pid_ets, real_keys)
|
||||
# We've recreated the real remaining key entries, so we can now delete
|
||||
# our temporary entry.
|
||||
@@ -880,11 +821,11 @@ defmodule Registry do
|
||||
lookup.
|
||||
|
||||
This function returns `{:ok, owner}` or `{:error, reason}`.
|
||||
The `owner` is the PID in the registry partition responsible for
|
||||
the PID. The owner is automatically linked to the caller.
|
||||
The `owner` is the pid in the registry partition responsible for
|
||||
the pid. The owner is automatically linked to the caller.
|
||||
|
||||
If the registry has unique keys, it will return `{:ok, owner}` unless
|
||||
the key is already associated to a PID, in which case it returns
|
||||
the key is already associated to a pid, in which case it returns
|
||||
`{:error, {:already_registered, pid}}`.
|
||||
|
||||
If the registry has duplicate keys, multiple registrations from the
|
||||
@@ -894,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()}}
|
||||
@@ -903,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()
|
||||
@@ -919,13 +859,11 @@ 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)
|
||||
|
||||
counter = System.unique_integer()
|
||||
true = :ets.insert(pid_ets, {self, key, key_ets, counter})
|
||||
true = :ets.insert(pid_ets, {self, key, key_ets})
|
||||
|
||||
case register_key(kind, pid_server, key_ets, key, {key, {self, value}}) do
|
||||
{:ok, _} = ok ->
|
||||
@@ -936,11 +874,10 @@ defmodule Registry do
|
||||
ok
|
||||
|
||||
{:error, {:already_registered, ^self}} = error ->
|
||||
true = :ets.delete_object(pid_ets, {self, key, key_ets, counter})
|
||||
error
|
||||
|
||||
{:error, _} = error ->
|
||||
true = :ets.delete_object(pid_ets, {self, key, key_ets, counter})
|
||||
true = :ets.delete_object(pid_ets, {self, key, key_ets})
|
||||
unlink_if_unregistered(pid_server, pid_ets, self)
|
||||
error
|
||||
end
|
||||
@@ -973,20 +910,19 @@ defmodule Registry do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads registry metadata given on `start_link/1`.
|
||||
Reads registry metadata given on `start_link/3`.
|
||||
|
||||
Atoms and tuples are allowed as keys.
|
||||
|
||||
## 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
|
||||
@@ -1007,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)
|
||||
@@ -1018,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
|
||||
@@ -1030,194 +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
|
||||
|
||||
@doc """
|
||||
Select key, pid, and values registered using full match specs.
|
||||
|
||||
The `spec` consists of a list of three part tuples, in the shape of `[{match_pattern, guards, body}]`.
|
||||
|
||||
The first part, the match pattern, must be a tuple that will match the structure of the
|
||||
the data stored in the registry, which is `{key, pid, value}`. 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. This can be combined
|
||||
like `{:"$1", :_, :_}`.
|
||||
|
||||
The second part, the guards, is a list of conditions that allow filtering the results.
|
||||
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.
|
||||
|
||||
The third part, the body, is a list of shapes of the returned entries. Like guards, you have access to
|
||||
assigned variables like `:"$1"`, which you can combine with hardcoded values to freely shape entries
|
||||
Note that tuples have to be wrapped in an additional tuple. To get a result format like
|
||||
`%{key: key, pid: pid, value: value}`, assuming you bound those variables in order in the match part,
|
||||
you would provide a body like `[%{key: :"$1", pid: :"$2", value: :"$3"}]`. Like guards, you can use
|
||||
some operations like `:element` to modify the output format.
|
||||
|
||||
Do not use special match variables `:"$_"` and `:"$$"`, because they might not work as expected.
|
||||
|
||||
Note that for large registries with many partitions this will be costly as it builds the result by
|
||||
concatenating all the partitions.
|
||||
|
||||
## Examples
|
||||
|
||||
This example shows how to get everything from the registry.
|
||||
|
||||
iex> Registry.start_link(keys: :unique, name: Registry.SelectAllTest)
|
||||
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "hello", :value)
|
||||
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "world", :value)
|
||||
iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :"$2", :"$3"}, [], [{{:"$1", :"$2", :"$3"}}]}])
|
||||
[{"world", self(), :value}, {"hello", self(), :value}]
|
||||
|
||||
Get all keys in the registry.
|
||||
|
||||
iex> Registry.start_link(keys: :unique, name: Registry.SelectAllTest)
|
||||
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "hello", :value)
|
||||
iex> {:ok, _} = Registry.register(Registry.SelectAllTest, "world", :value)
|
||||
iex> Registry.select(Registry.SelectAllTest, [{{:"$1", :_, :_}, [], [:"$1"]}])
|
||||
["world", "hello"]
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec select(registry, spec) :: [term]
|
||||
def select(registry, spec)
|
||||
when is_atom(registry) and is_list(spec) do
|
||||
spec =
|
||||
for part <- spec do
|
||||
case part do
|
||||
{{key, pid, value}, guards, select} ->
|
||||
{{key, {pid, value}}, guards, select}
|
||||
|
||||
_ ->
|
||||
raise ArgumentError,
|
||||
"invalid match specification in Registry.select/2: #{inspect(spec)}"
|
||||
end
|
||||
end
|
||||
|
||||
case key_info!(registry) do
|
||||
{_kind, partitions, nil} ->
|
||||
Enum.flat_map(0..(partitions - 1), fn partition_index ->
|
||||
:ets.select(key_ets!(registry, partition_index), spec)
|
||||
end)
|
||||
|
||||
{_kind, 1, key_ets} ->
|
||||
:ets.select(key_ets, spec)
|
||||
end
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
@compile {:inline, hash: 2}
|
||||
@@ -1282,24 +1029,6 @@ defmodule Registry do
|
||||
Process.unlink(pid_server)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __unregister__(table, match, pos) do
|
||||
key = :erlang.element(pos, match)
|
||||
|
||||
# We need to perform an element comparison if we have an special atom key.
|
||||
if is_atom(key) and reserved_atom?(Atom.to_string(key)) do
|
||||
match = :erlang.setelement(pos, match, :_)
|
||||
guard = {:"=:=", {:element, pos, :"$_"}, {:const, key}}
|
||||
:ets.select_delete(table, [{match, [guard], [true]}]) >= 0
|
||||
else
|
||||
:ets.match_delete(table, match)
|
||||
end
|
||||
end
|
||||
|
||||
defp reserved_atom?("_"), do: true
|
||||
defp reserved_atom?("$" <> _), do: true
|
||||
defp reserved_atom?(_), do: false
|
||||
end
|
||||
|
||||
defmodule Registry.Supervisor do
|
||||
@@ -1327,12 +1056,12 @@ defmodule Registry.Supervisor do
|
||||
end
|
||||
|
||||
# Unique registries have their key partition hashed by key.
|
||||
# This means that, if a PID partition crashes, it may have
|
||||
# This means that, if a pid partition crashes, it may have
|
||||
# entries from all key partitions, so we need to crash all.
|
||||
defp strategy_for_kind(:unique), do: :one_for_all
|
||||
|
||||
# Duplicate registries have both key and pid partitions hashed
|
||||
# by pid. This means that, if a PID partition crashes, all of
|
||||
# by pid. This means that, if a pid partition crashes, all of
|
||||
# its associated entries are in its sibling table, so we crash one.
|
||||
defp strategy_for_kind(:duplicate), do: :one_for_one
|
||||
end
|
||||
@@ -1340,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
|
||||
@@ -1429,10 +1158,10 @@ defmodule Registry.Partition do
|
||||
def handle_info({:EXIT, pid, _reason}, ets) do
|
||||
entries = :ets.take(ets, pid)
|
||||
|
||||
for {_pid, key, key_ets, _counter} <- entries 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
|
||||
|
||||
@@ -1441,7 +1170,7 @@ defmodule Registry.Partition do
|
||||
end
|
||||
|
||||
try do
|
||||
Registry.__unregister__(key_ets, {key, {pid, :_}}, 1)
|
||||
:ets.match_delete(key_ets, {key, {pid, :_}})
|
||||
catch
|
||||
:error, :badarg -> :badarg
|
||||
end
|
||||
@@ -1449,4 +1178,8 @@ defmodule Registry.Partition do
|
||||
|
||||
{:noreply, ets}
|
||||
end
|
||||
|
||||
def handle_info(msg, state) do
|
||||
super(msg, state)
|
||||
end
|
||||
end
|
||||
|
||||
+5
-17
@@ -1,17 +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
|
||||
|
||||
message = "Use the MapSet module for working with sets"
|
||||
# TODO: Remove by 2.0
|
||||
# (hard-deprecated in elixir_dispatch)
|
||||
|
||||
defmacrop target(set) do
|
||||
quote do
|
||||
@@ -22,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)
|
||||
@@ -42,7 +39,6 @@ defmodule Set do
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def disjoint?(set1, set2) do
|
||||
target1 = target(set1)
|
||||
target2 = target(set2)
|
||||
@@ -60,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)
|
||||
@@ -82,7 +77,6 @@ defmodule Set do
|
||||
end
|
||||
end
|
||||
|
||||
@deprecated message
|
||||
def intersection(set1, set2) do
|
||||
target1 = target(set1)
|
||||
target2 = target(set2)
|
||||
@@ -97,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)
|
||||
@@ -124,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)
|
||||
|
||||
+156
-203
@@ -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
|
||||
elements one by one during enumeration is called a stream. For example,
|
||||
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
|
||||
@@ -22,9 +21,9 @@ defmodule Stream do
|
||||
[3, 5, 7]
|
||||
|
||||
Notice we started with a range and then we created a stream that is
|
||||
meant to multiply each element in the range by 2. At this point, no
|
||||
meant to multiply each item in the range by 2. At this point, no
|
||||
computation was done. Only when `Enum.map/2` is called we actually
|
||||
enumerate over each element in the range, multiplying it by 2 and adding 1.
|
||||
enumerate over each item in the range, multiplying it by 2 and adding 1.
|
||||
We say the functions in `Stream` are *lazy* and the functions in `Enum`
|
||||
are *eager*.
|
||||
|
||||
@@ -46,7 +45,7 @@ defmodule Stream do
|
||||
6
|
||||
#=> [2, 4, 6]
|
||||
|
||||
Notice that we first printed each element in the list, then multiplied each
|
||||
Notice that we first printed each item in the list, then multiplied each
|
||||
element by 2 and finally printed each new value. In this example, the list
|
||||
was enumerated three times. Let's see an example with streams:
|
||||
|
||||
@@ -63,8 +62,8 @@ defmodule Stream do
|
||||
6
|
||||
#=> [2, 4, 6]
|
||||
|
||||
Although the end result is the same, the order in which the elements were
|
||||
printed changed! With streams, we print the first element and then print
|
||||
Although the end result is the same, the order in which the items were
|
||||
printed changed! With streams, we print the first item and then print
|
||||
its double. In this example, the list was enumerated just once!
|
||||
|
||||
That's what we meant when we said earlier that streams are composable,
|
||||
@@ -72,13 +71,6 @@ defmodule Stream do
|
||||
effectively composing the streams and keeping them lazy. The computations
|
||||
are only performed when you call a function from the `Enum` module.
|
||||
|
||||
Like with `Enum`, the functions in this module work in linear time. This
|
||||
means that, the time it takes to perform an operation grows at the same
|
||||
rate as the length of the list. This is expected on operations such as
|
||||
`Stream.map/2`. After all, if we want to traverse every element on a
|
||||
stream, the longer the stream, the more elements we need to traverse,
|
||||
and the longer it will take.
|
||||
|
||||
## Creating Streams
|
||||
|
||||
There are many functions in Elixir's standard library that return
|
||||
@@ -102,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
|
||||
@@ -132,19 +121,13 @@ defmodule Stream do
|
||||
|
||||
## Transformers
|
||||
|
||||
# Deprecate on v1.7
|
||||
@doc false
|
||||
@deprecated "Use Stream.chunk_every/2 instead"
|
||||
def chunk(enum, n), do: chunk(enum, n, n, nil)
|
||||
|
||||
# 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
|
||||
|
||||
@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
|
||||
@@ -152,12 +135,11 @@ 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, [])
|
||||
|
||||
@doc """
|
||||
Streams the enumerable in chunks, containing `count` elements each,
|
||||
Streams the enumerable in chunks, containing `count` items each,
|
||||
where each new chunk starts `step` elements into the enumerable.
|
||||
|
||||
`step` is optional and, if not passed, defaults to `count`, i.e.
|
||||
@@ -173,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 \\ [])
|
||||
@@ -207,7 +188,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec chunk_by(Enumerable.t(), (element -> any)) :: Enumerable.t()
|
||||
def chunk_by(enum, fun) when is_function(fun, 1) do
|
||||
def chunk_by(enum, fun) do
|
||||
R.chunk_by(&chunk_while/4, enum, fun)
|
||||
end
|
||||
|
||||
@@ -224,11 +205,11 @@ defmodule Stream do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> chunk_fun = fn element, acc ->
|
||||
...> if rem(element, 2) == 0 do
|
||||
...> {:cont, Enum.reverse([element | acc]), []}
|
||||
iex> chunk_fun = fn i, acc ->
|
||||
...> if rem(i, 2) == 0 do
|
||||
...> {:cont, Enum.reverse([i | acc]), []}
|
||||
...> else
|
||||
...> {:cont, [element | acc]}
|
||||
...> {:cont, [i | acc]}
|
||||
...> end
|
||||
...> end
|
||||
iex> after_fun = fn
|
||||
@@ -240,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,
|
||||
@@ -248,8 +228,7 @@ defmodule Stream do
|
||||
(acc -> {:cont, chunk, acc} | {:cont, acc})
|
||||
) :: Enumerable.t()
|
||||
when chunk: any
|
||||
def chunk_while(enum, acc, chunk_fun, after_fun)
|
||||
when is_function(chunk_fun, 2) and is_function(after_fun, 1) do
|
||||
def chunk_while(enum, acc, chunk_fun, after_fun) do
|
||||
lazy(
|
||||
enum,
|
||||
[acc | after_fun],
|
||||
@@ -262,9 +241,9 @@ defmodule Stream do
|
||||
fn entry, acc(head, [acc | after_fun], tail) ->
|
||||
case callback.(entry, acc) do
|
||||
{:cont, emit, acc} ->
|
||||
# If we emit an element and then we have to halt,
|
||||
# If we emit an item and then we have to halt,
|
||||
# we need to disable the after_fun callback to
|
||||
# avoid emitting even more elements.
|
||||
# avoid emitting even more items.
|
||||
case next(fun, emit, [head | tail]) do
|
||||
{:halt, [head | tail]} -> {:halt, acc(head, [acc | &{:cont, &1}], tail)}
|
||||
{command, [head | tail]} -> {command, acc(head, [acc | after_fun], tail)}
|
||||
@@ -291,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]
|
||||
|
||||
"""
|
||||
@@ -310,21 +289,21 @@ 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}]
|
||||
|
||||
"""
|
||||
@spec dedup_by(Enumerable.t(), (element -> term)) :: Enumerable.t()
|
||||
def dedup_by(enum, fun) when is_function(fun, 1) do
|
||||
def dedup_by(enum, fun) do
|
||||
lazy(enum, nil, fn f1 -> R.dedup(fun, f1) end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily drops the next `n` elements from the enumerable.
|
||||
Lazily drops the next `n` items from the enumerable.
|
||||
|
||||
If a negative `n` is given, it will drop the last `n` elements from
|
||||
If a negative `n` is given, it will drop the last `n` items from
|
||||
the collection. Note that the mechanism by which this is implemented
|
||||
will delay the emission of any element until `n` additional elements have
|
||||
will delay the emission of any item until `n` additional items have
|
||||
been emitted by the enum.
|
||||
|
||||
## Examples
|
||||
@@ -338,7 +317,7 @@ defmodule Stream do
|
||||
[1, 2, 3, 4, 5]
|
||||
|
||||
"""
|
||||
@spec drop(Enumerable.t(), integer) :: Enumerable.t()
|
||||
@spec drop(Enumerable.t(), non_neg_integer) :: Enumerable.t()
|
||||
def drop(enum, n) when is_integer(n) and n >= 0 do
|
||||
lazy(enum, n, fn f1 -> R.drop(f1) end)
|
||||
end
|
||||
@@ -370,9 +349,9 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that drops every `nth` element from the enumerable.
|
||||
Creates a stream that drops every `nth` item from the enumerable.
|
||||
|
||||
The first element is always dropped, unless `nth` is 0.
|
||||
The first item is always dropped, unless `nth` is 0.
|
||||
|
||||
`nth` must be a non-negative integer.
|
||||
|
||||
@@ -412,18 +391,18 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec drop_while(Enumerable.t(), (element -> as_boolean(term))) :: Enumerable.t()
|
||||
def drop_while(enum, fun) when is_function(fun, 1) do
|
||||
def drop_while(enum, fun) do
|
||||
lazy(enum, true, fn f1 -> R.drop_while(fun, f1) end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Executes the given function for each element.
|
||||
Executes the given function for each item.
|
||||
|
||||
Useful for adding side effects (like printing) to a stream.
|
||||
|
||||
## 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
|
||||
@@ -434,7 +413,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec each(Enumerable.t(), (element -> term)) :: Enumerable.t()
|
||||
def each(enum, fun) when is_function(fun, 1) do
|
||||
def each(enum, fun) do
|
||||
lazy(enum, fn f1 ->
|
||||
fn x, acc ->
|
||||
fun.(x)
|
||||
@@ -451,17 +430,17 @@ 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]]
|
||||
|
||||
"""
|
||||
@spec flat_map(Enumerable.t(), (element -> Enumerable.t())) :: Enumerable.t()
|
||||
def flat_map(enum, mapper) when is_function(mapper, 1) do
|
||||
def flat_map(enum, mapper) do
|
||||
transform(enum, nil, fn val, nil -> {mapper.(val), nil} end)
|
||||
end
|
||||
|
||||
@@ -471,18 +450,19 @@ 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]
|
||||
|
||||
"""
|
||||
@spec filter(Enumerable.t(), (element -> as_boolean(term))) :: Enumerable.t()
|
||||
def filter(enum, fun) when is_function(fun, 1) do
|
||||
def filter(enum, fun) do
|
||||
lazy(enum, fn f1 -> R.filter(fun, f1) end)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Stream.filter/2 + Stream.map/2 instead"
|
||||
# TODO: Remove on 2.0
|
||||
# (hard-deprecated in elixir_dispatch)
|
||||
def filter_map(enum, filter, mapper) do
|
||||
lazy(enum, fn f1 -> R.filter_map(filter, mapper, f1) end)
|
||||
end
|
||||
@@ -493,7 +473,7 @@ defmodule Stream do
|
||||
|
||||
The values emitted are an increasing counter starting at `0`.
|
||||
This operation will block the caller by the given interval
|
||||
every time a new element is streamed.
|
||||
every time a new item is streamed.
|
||||
|
||||
Do not use this function to generate a sequence of numbers.
|
||||
If blocking the caller process is not necessary, use
|
||||
@@ -506,7 +486,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec interval(non_neg_integer) :: Enumerable.t()
|
||||
def interval(n) when is_integer(n) and n >= 0 do
|
||||
def interval(n) do
|
||||
unfold(0, fn count ->
|
||||
Process.sleep(n)
|
||||
{count, count + 1}
|
||||
@@ -520,7 +500,7 @@ defmodule Stream do
|
||||
is delayed until the stream is executed. See `run/1` for an example.
|
||||
"""
|
||||
@spec into(Enumerable.t(), Collectable.t(), (term -> term)) :: Enumerable.t()
|
||||
def into(enum, collectable, transform \\ fn x -> x end) when is_function(transform, 1) do
|
||||
def into(enum, collectable, transform \\ fn x -> x end) do
|
||||
&do_into(enum, collectable, transform, &1, &2)
|
||||
end
|
||||
|
||||
@@ -541,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)}
|
||||
@@ -559,50 +540,47 @@ 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]
|
||||
|
||||
"""
|
||||
@spec map(Enumerable.t(), (element -> any)) :: Enumerable.t()
|
||||
def map(enum, fun) when is_function(fun, 1) do
|
||||
def map(enum, fun) do
|
||||
lazy(enum, fn f1 -> R.map(fun, f1) end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that will apply the given function on
|
||||
every `nth` element from the enumerable.
|
||||
every `nth` item from the enumerable.
|
||||
|
||||
The first element is always passed to the given function.
|
||||
The first item is always passed to the given function.
|
||||
|
||||
`nth` must be a non-negative integer.
|
||||
|
||||
## 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) when is_integer(nth) and nth >= 0 and is_function(fun, 1) do
|
||||
map_every_after_guards(enum, nth, fun)
|
||||
end
|
||||
def map_every(enum, nth, fun)
|
||||
|
||||
defp map_every_after_guards(enum, 1, fun), do: map(enum, fun)
|
||||
defp map_every_after_guards(enum, 0, _fun), do: %Stream{enum: enum}
|
||||
defp map_every_after_guards([], _nth, _fun), do: %Stream{enum: []}
|
||||
def map_every(enum, 1, fun), do: map(enum, fun)
|
||||
def map_every(enum, 0, _fun), do: %Stream{enum: enum}
|
||||
def map_every([], _nth, _fun), do: %Stream{enum: []}
|
||||
|
||||
defp map_every_after_guards(enum, nth, fun) do
|
||||
def map_every(enum, nth, fun) when is_integer(nth) and nth > 0 do
|
||||
lazy(enum, nth, fn f1 -> R.map_every(nth, fun, f1) end)
|
||||
end
|
||||
|
||||
@@ -612,13 +590,13 @@ 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]
|
||||
|
||||
"""
|
||||
@spec reject(Enumerable.t(), (element -> as_boolean(term))) :: Enumerable.t()
|
||||
def reject(enum, fun) when is_function(fun, 1) do
|
||||
def reject(enum, fun) do
|
||||
lazy(enum, fn f1 -> R.reject(fun, f1) end)
|
||||
end
|
||||
|
||||
@@ -633,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
|
||||
@@ -661,7 +639,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec scan(Enumerable.t(), (element, acc -> any)) :: Enumerable.t()
|
||||
def scan(enum, fun) when is_function(fun, 2) do
|
||||
def scan(enum, fun) do
|
||||
lazy(enum, :first, fn f1 -> R.scan2(fun, f1) end)
|
||||
end
|
||||
|
||||
@@ -678,12 +656,12 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec scan(Enumerable.t(), acc, (element, acc -> any)) :: Enumerable.t()
|
||||
def scan(enum, acc, fun) when is_function(fun, 2) do
|
||||
def scan(enum, acc, fun) do
|
||||
lazy(enum, acc, fn f1 -> R.scan3(fun, f1) end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily takes the next `count` elements from the enumerable and stops
|
||||
Lazily takes the next `count` items from the enumerable and stops
|
||||
enumeration.
|
||||
|
||||
If a negative `count` is given, the last `count` values will be taken.
|
||||
@@ -708,26 +686,21 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec take(Enumerable.t(), integer) :: Enumerable.t()
|
||||
def take(enum, count) when is_integer(count) do
|
||||
take_after_guards(enum, count)
|
||||
end
|
||||
def take(_enum, 0), do: %Stream{enum: []}
|
||||
def take([], _count), do: %Stream{enum: []}
|
||||
|
||||
defp take_after_guards(_enum, 0), do: %Stream{enum: []}
|
||||
|
||||
defp take_after_guards([], _count), do: %Stream{enum: []}
|
||||
|
||||
defp take_after_guards(enum, count) when count > 0 do
|
||||
def take(enum, count) when is_integer(count) and count > 0 do
|
||||
lazy(enum, count, fn f1 -> R.take(f1) end)
|
||||
end
|
||||
|
||||
defp take_after_guards(enum, count) when count < 0 do
|
||||
def take(enum, count) when is_integer(count) and count < 0 do
|
||||
&Enumerable.reduce(Enum.take(enum, count), &1, &2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that takes every `nth` element from the enumerable.
|
||||
Creates a stream that takes every `nth` item from the enumerable.
|
||||
|
||||
The first element is always included, unless `nth` is 0.
|
||||
The first item is always included, unless `nth` is 0.
|
||||
|
||||
`nth` must be a non-negative integer.
|
||||
|
||||
@@ -747,15 +720,11 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec take_every(Enumerable.t(), non_neg_integer) :: Enumerable.t()
|
||||
def take_every(enum, nth) when is_integer(nth) and nth >= 0 do
|
||||
take_every_after_guards(enum, nth)
|
||||
end
|
||||
def take_every(enum, nth)
|
||||
def take_every(_enum, 0), do: %Stream{enum: []}
|
||||
def take_every([], _nth), do: %Stream{enum: []}
|
||||
|
||||
defp take_every_after_guards(_enum, 0), do: %Stream{enum: []}
|
||||
|
||||
defp take_every_after_guards([], _nth), do: %Stream{enum: []}
|
||||
|
||||
defp take_every_after_guards(enum, nth) do
|
||||
def take_every(enum, nth) when is_integer(nth) and nth > 0 do
|
||||
lazy(enum, nth, fn f1 -> R.take_every(nth, f1) end)
|
||||
end
|
||||
|
||||
@@ -771,7 +740,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec take_while(Enumerable.t(), (element -> as_boolean(term))) :: Enumerable.t()
|
||||
def take_while(enum, fun) when is_function(fun, 1) do
|
||||
def take_while(enum, fun) do
|
||||
lazy(enum, fn f1 -> R.take_while(fun, f1) end)
|
||||
end
|
||||
|
||||
@@ -779,23 +748,23 @@ defmodule Stream do
|
||||
Creates a stream that emits a single value after `n` milliseconds.
|
||||
|
||||
The value emitted is `0`. This operation will block the caller by
|
||||
the given time until the element is streamed.
|
||||
the given time until the item is streamed.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Stream.timer(10) |> Enum.to_list()
|
||||
iex> Stream.timer(10) |> Enum.to_list
|
||||
[0]
|
||||
|
||||
"""
|
||||
@spec timer(non_neg_integer) :: Enumerable.t()
|
||||
def timer(n) when is_integer(n) and n >= 0 do
|
||||
def timer(n) do
|
||||
take(interval(n), 1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Transforms an existing stream.
|
||||
|
||||
It expects an accumulator and a function that receives each stream element
|
||||
It expects an accumulator and a function that receives each stream item
|
||||
and an accumulator, and must return a tuple containing a new stream
|
||||
(often a list) with the new accumulator or a tuple with `:halt` as first
|
||||
element and the accumulator as second.
|
||||
@@ -822,7 +791,7 @@ defmodule Stream do
|
||||
@spec transform(Enumerable.t(), acc, fun) :: Enumerable.t()
|
||||
when fun: (element, acc -> {Enumerable.t(), acc} | {:halt, acc}),
|
||||
acc: any
|
||||
def transform(enum, acc, reducer) when is_function(reducer, 2) do
|
||||
def transform(enum, acc, reducer) do
|
||||
&do_transform(enum, fn -> acc end, reducer, &1, &2, nil)
|
||||
end
|
||||
|
||||
@@ -839,8 +808,7 @@ defmodule Stream do
|
||||
@spec transform(Enumerable.t(), (() -> acc), fun, (acc -> term)) :: Enumerable.t()
|
||||
when fun: (element, acc -> {Enumerable.t(), acc} | {:halt, acc}),
|
||||
acc: any
|
||||
def transform(enum, start_fun, reducer, after_fun)
|
||||
when is_function(start_fun, 0) and is_function(reducer, 2) and is_function(after_fun, 1) do
|
||||
def transform(enum, start_fun, reducer, after_fun) do
|
||||
&do_transform(enum, start_fun, reducer, &1, &2, after_fun)
|
||||
end
|
||||
|
||||
@@ -876,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)
|
||||
@@ -898,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)
|
||||
@@ -927,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)
|
||||
@@ -952,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]} ->
|
||||
@@ -995,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 elements 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]
|
||||
|
||||
"""
|
||||
@@ -1009,14 +982,15 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Use Stream.uniq_by/2 instead"
|
||||
# TODO: Remove on 2.0
|
||||
# (hard-deprecated in elixir_dispatch)
|
||||
def uniq(enum, fun) do
|
||||
uniq_by(enum, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that only emits elements if they are unique, by removing the
|
||||
elements for which function `fun` returned duplicate elements.
|
||||
elements for which function `fun` returned duplicate items.
|
||||
|
||||
The function `fun` maps every element to a term which is used to
|
||||
determine if two elements are duplicates.
|
||||
@@ -1024,24 +998,24 @@ 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 elements 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}]
|
||||
|
||||
"""
|
||||
@spec uniq_by(Enumerable.t(), (element -> term)) :: Enumerable.t()
|
||||
def uniq_by(enum, fun) when is_function(fun, 1) do
|
||||
def uniq_by(enum, fun) do
|
||||
lazy(enum, %{}, fn f1 -> R.uniq_by(fun, f1) end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream where each element in the enumerable will
|
||||
Creates a stream where each item in the enumerable will
|
||||
be wrapped in a tuple alongside its index.
|
||||
|
||||
If an `offset` is given, we will index from the given offset instead of from zero.
|
||||
@@ -1058,7 +1032,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec with_index(Enumerable.t(), integer) :: Enumerable.t()
|
||||
def with_index(enum, offset \\ 0) when is_integer(offset) do
|
||||
def with_index(enum, offset \\ 0) do
|
||||
lazy(enum, offset, fn f1 -> R.with_index(f1) end)
|
||||
end
|
||||
|
||||
@@ -1108,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}]
|
||||
|
||||
"""
|
||||
@@ -1126,12 +1100,12 @@ 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(enumerables) :: Enumerable.t() when enumerables: [Enumerable.t()] | Enumerable.t()
|
||||
@spec zip([Enumerable.t()]) :: Enumerable.t()
|
||||
@spec zip(Enumerable.t()) :: Enumerable.t()
|
||||
def zip(enumerables) do
|
||||
&prepare_zip(enumerables, &1, &2)
|
||||
end
|
||||
@@ -1141,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)
|
||||
@@ -1168,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)
|
||||
@@ -1182,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
|
||||
@@ -1205,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
|
||||
@@ -1220,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
|
||||
@@ -1258,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
|
||||
|
||||
@@ -1278,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})
|
||||
|
||||
@@ -1293,30 +1262,18 @@ 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]
|
||||
|
||||
"""
|
||||
@spec iterate(element, (element -> element)) :: Enumerable.t()
|
||||
def iterate(start_value, next_fun) when is_function(next_fun, 1) do
|
||||
def iterate(start_value, next_fun) do
|
||||
unfold({:ok, start_value}, fn
|
||||
{:ok, value} ->
|
||||
{value, {:next, value}}
|
||||
@@ -1339,7 +1296,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@spec repeatedly((() -> element)) :: Enumerable.t()
|
||||
def repeatedly(generator_fun) when is_function(generator_fun, 0) do
|
||||
def repeatedly(generator_fun) do
|
||||
&do_repeatedly(generator_fun, &1, &2)
|
||||
end
|
||||
|
||||
@@ -1365,7 +1322,7 @@ defmodule Stream do
|
||||
Successive values are generated by calling `next_fun` with the
|
||||
previous accumulator (the initial value being the result returned
|
||||
by `start_fun`) and it must return a tuple containing a list
|
||||
of elements to be emitted and the next accumulator. The enumeration
|
||||
of items to be emitted and the next accumulator. The enumeration
|
||||
finishes if it returns `{:halt, acc}`.
|
||||
|
||||
As the name says, this function is useful to stream values from
|
||||
@@ -1373,22 +1330,19 @@ 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)) ::
|
||||
Enumerable.t()
|
||||
def resource(start_fun, next_fun, after_fun)
|
||||
when is_function(start_fun, 0) and is_function(next_fun, 1) and is_function(after_fun, 1) do
|
||||
def resource(start_fun, next_fun, after_fun) do
|
||||
&do_resource(start_fun.(), next_fun, &1, &2, after_fun)
|
||||
end
|
||||
|
||||
@@ -1411,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)
|
||||
@@ -1436,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)
|
||||
@@ -1455,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)
|
||||
@@ -1488,15 +1445,12 @@ 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]
|
||||
|
||||
"""
|
||||
@spec unfold(acc, (acc -> {element, acc} | nil)) :: Enumerable.t()
|
||||
def unfold(next_acc, next_fun) when is_function(next_fun, 1) do
|
||||
def unfold(next_acc, next_fun) do
|
||||
&do_unfold(next_acc, next_fun, &1, &2)
|
||||
end
|
||||
|
||||
@@ -1520,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
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user