Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a1fb6c0d0e | ||
|
|
4f4aad2cd4 | ||
|
|
d0f5236c8e | ||
|
|
2b95813d98 | ||
|
|
eb5679bfcc | ||
|
|
e38ae98c3a | ||
|
|
71a537b950 | ||
|
|
7087149bfa | ||
|
|
b4b1947a22 | ||
|
|
a33d9116ab | ||
|
|
7d744ebc84 | ||
|
|
3e6c6986f2 | ||
|
|
f167716f53 | ||
|
|
0cdf6d28b1 | ||
|
|
e4c1e3d262 | ||
|
|
3f6313906d | ||
|
|
0089a40a33 | ||
|
|
549745fa5e | ||
|
|
ac41358b5a | ||
|
|
0db2ad6953 | ||
|
|
8ca85ed2a8 | ||
|
|
af64fe7e98 | ||
|
|
28cf40fe03 | ||
|
|
d38a189907 | ||
|
|
2b338092b6 | ||
|
|
557b779bd4 | ||
|
|
ece40480f6 | ||
|
|
a77445c46b | ||
|
|
4d55fa6814 | ||
|
|
b9d6f4f9c1 | ||
|
|
b0cf76c74c | ||
|
|
7841fbb8e0 | ||
|
|
f5773fed42 | ||
|
|
e2b8ab9a0d | ||
|
|
1dc6d10f30 | ||
|
|
e7138fb2c6 | ||
|
|
04ecc05adc | ||
|
|
8aab53b941 | ||
|
|
07c276ab5c | ||
|
|
89e3a81adc | ||
|
|
9752f1ce05 | ||
|
|
06e5ec2d4c | ||
|
|
b6d3f29b69 | ||
|
|
bfdaf3f8d1 | ||
|
|
bc26f10040 | ||
|
|
52868e556d | ||
|
|
04c45b031a | ||
|
|
d410466ce4 | ||
|
|
fe3c263599 | ||
|
|
7558c9a023 | ||
|
|
0d30dbfd24 | ||
|
|
793bbe6e3c | ||
|
|
3318d33d2e | ||
|
|
b6d7769674 | ||
|
|
dc3a56c44f | ||
|
|
ea9cb84eb1 | ||
|
|
b7b7282a22 | ||
|
|
d3d1833d47 | ||
|
|
7d427719fe | ||
|
|
9812b74a2c | ||
|
|
aebc7e2fe1 | ||
|
|
f0aeb7136f | ||
|
|
d92a450637 | ||
|
|
afb4667cd0 | ||
|
|
eb50a09679 | ||
|
|
2f8ecec32c | ||
|
|
4354938168 | ||
|
|
b06d82c93e | ||
|
|
3dbe8a6661 | ||
|
|
6932e25159 | ||
|
|
22abd6a48e | ||
|
|
05f5073cd1 | ||
|
|
18d96c2fd5 | ||
|
|
ea04ec8f81 | ||
|
|
0a5d633a5e | ||
|
|
7392d736c4 | ||
|
|
1164784b8e | ||
|
|
6694ddbd2f | ||
|
|
85a4d55efb | ||
|
|
9b238e0316 | ||
|
|
ca82388792 | ||
|
|
b33dd12d86 | ||
|
|
c36b2c9070 | ||
|
|
ac75cdbe05 |
+15
-18
@@ -5,15 +5,15 @@ 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-19.0
|
||||
- OTP_RELEASE=OTP-19.1
|
||||
- OTP_RELEASE=OTP-19.2
|
||||
- OTP_RELEASE=OTP-19.3
|
||||
- OTP_RELEASE=OTP-20.0
|
||||
- OTP_RELEASE=OTP-20.1
|
||||
- OTP_RELEASE=OTP-20.2
|
||||
- OTP_RELEASE=OTP-20.3
|
||||
- OTP_RELEASE=OTP-21.0
|
||||
- OTP_RELEASE=maint
|
||||
- OTP_RELEASE=master
|
||||
|
||||
@@ -31,17 +31,14 @@ install:
|
||||
- PATH=$(pwd)/otp/bin:$PATH
|
||||
|
||||
script:
|
||||
- make compile
|
||||
- rm -rf .git
|
||||
- ELIXIRC_OPTS="--warnings-as-errors" ERLC_OPTS="+warning_as_errors" make compile
|
||||
- make test
|
||||
- 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
|
||||
|
||||
+221
-206
@@ -1,295 +1,310 @@
|
||||
# Changelog for Elixir v1.9
|
||||
# Changelog for Elixir v1.7
|
||||
|
||||
## Releases
|
||||
Elixir v1.7 is the last release to support Erlang/OTP 19. We recommend everyone to migrate to Erlang/OTP 20+.
|
||||
|
||||
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.
|
||||
## Documentation metadata
|
||||
|
||||
You can start a new project and assemble a release for it in three easy steps:
|
||||
Elixir v1.7 implements [EEP 48](http://erlang.org/eep/eeps/eep-0048.html). EEP 48 aims to bring documentation interoperability across all languages running on the Erlang VM. The documentation format proposed by EEP 48 also supports metadata, which is now fully exposed to Elixir developers:
|
||||
|
||||
$ mix new my_app
|
||||
$ cd my_app
|
||||
$ MIX_ENV=prod mix release
|
||||
```elixir
|
||||
@moduledoc "A brand new module"
|
||||
@moduledoc authors: ["Jane", "Mary"], since: "1.4.0"
|
||||
```
|
||||
|
||||
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:
|
||||
Passing metadata is supported on `@doc`, `@moduledoc` and `@typedoc`.
|
||||
|
||||
* `bin/my_app start`, `bin/my_app start_iex`, `bin/my_app restart`, and `bin/my_app stop` - for general management of the release
|
||||
To access the new documentation format, developers should use `Code.fetch_docs/1`. The old documentation format is no longer available and the old `Code.get_docs/2` function will return `nil` accordingly.
|
||||
|
||||
* `bin/my_app rpc COMMAND` and `bin/my_app remote` - for running commands on the running system or to connect to the running system
|
||||
Tools like IEx and ExDoc have been updated to leverage the new format and show relevant metadata to users. While Elixir allows any metadata to be given, those tools currently exhibit only `:deprecated` and `:since`. Other keys may be shown in the future.
|
||||
|
||||
* `bin/my_app eval COMMAND` - to start a fresh system that runs a single command and then shuts down
|
||||
## The `__STACKTRACE__` construct
|
||||
|
||||
* `bin/my_app daemon` and `bin/my_app daemon_iex` - to start the system as a daemon on Unix-like systems
|
||||
Erlang/OTP 21.0 introduces a new way to retrieve the stacktrace that is lexically scoped and no longer relies on side-effects like `System.stacktrace/0` does. Before one would write:
|
||||
|
||||
* `bin/my_app install` - to install the system as a service on Windows machines
|
||||
```elixir
|
||||
try do
|
||||
... something that may fail ...
|
||||
rescue
|
||||
e ->
|
||||
log(e, System.stacktrace())
|
||||
reraise(e, System.stacktrace())
|
||||
end
|
||||
```
|
||||
|
||||
### Why releases?
|
||||
In Elixir v1.7, this can be written as:
|
||||
|
||||
Releases allow developers to precompile and package all of their code and the runtime into a single unit. The benefits of releases are:
|
||||
```elixir
|
||||
try do
|
||||
... something that may fail ...
|
||||
rescue
|
||||
e ->
|
||||
log(e, __STACKTRACE__)
|
||||
reraise(e, __STACKTRACE__)
|
||||
end
|
||||
```
|
||||
|
||||
* 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 change may also yield performance improvements in the future, since the lexical scope allows us to track precisely when a stacktrace is used and we no longer need to keep references to stacktrace entries after the `try` construct finishes.
|
||||
|
||||
* Configuration and customization. Releases give developers fine grained control over system configuration and the VM flags used to start the system.
|
||||
Other parts of the exception system have been improved. For example, more information is provided in certain occurrences of `ArgumentError`, `ArithmeticError` and `KeyError` messages.
|
||||
|
||||
* 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.
|
||||
## Erlang/OTP logger integration
|
||||
|
||||
* Multiple releases. You can assemble different releases with different configuration per application or even with different applications altogether.
|
||||
Erlang/OTP 21 includes a new `:logger` module. Elixir v1.7 fully integrates with the new `:logger` and leverages its metadata system. The `Logger.Translator` mechanism has also been improved to export metadata, allowing custom Logger backends to leverage information such as:
|
||||
|
||||
### Hooks and Configuration
|
||||
* `:crash_reason` - a two-element tuple with the throw/error/exit reason as first argument and the stacktrace as second
|
||||
|
||||
Releases also provide built-in hooks for configuring almost every need of the production system:
|
||||
* `:initial_call` - the initial call that started the process
|
||||
|
||||
* `config/config.exs` (and `config/prod.exs`) - provides build-time application configuration, which is executed when the release is assembled
|
||||
* `:registered_name` - the process registered name as an atom
|
||||
|
||||
* `config/releases.exs` - provides runtime application configuration. It is executed every time the release boots and is further extensible via config providers
|
||||
We recommend Elixir libraries that previously hooked into Erlang's `:error_logger` to hook into `Logger` instead, in order to support all current and future Erlang/OTP versions.
|
||||
|
||||
* `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
|
||||
## Other Logger improvements
|
||||
|
||||
* `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
|
||||
Previously, Logger macros such as `debug`, `info`, and so on would always evaluate their arguments, even when nothing would be logged. From Elixir v1.7, the arguments are only evaluated when the message is logged.
|
||||
|
||||
We have written extensive documentation on releases, so we recommend checking it out for more information.
|
||||
The Logger configuration system also accepts a new option called `:compile_time_purge_matching` that allows you to remove log calls with specific compile-time metadata. For example, to remove all logger calls from application `:foo` with level lower than `:info`, as well as remove all logger calls from `Bar.foo/3`, you can use the following configuration:
|
||||
|
||||
## Configuration overhaul
|
||||
```elixir
|
||||
config :logger,
|
||||
compile_time_purge_matching: [
|
||||
[application: :foo, level_lower_than: :info],
|
||||
[module: Bar, function: "foo/3"]
|
||||
]
|
||||
```
|
||||
|
||||
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`.
|
||||
## ExUnit improvements
|
||||
|
||||
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.
|
||||
ExUnit has also seen its own share of improvements. Assertions such as `assert some_fun(arg1, arg2, arg3)` will now include the value of each argument in the failure report:
|
||||
|
||||
## Other enhancements
|
||||
```
|
||||
1) test function call arguments (TestOneOfEach)
|
||||
lib/ex_unit/examples/one_of_each.exs:157
|
||||
Expected truthy, got false
|
||||
code: assert some_vars(1 + 2, 3 + 4)
|
||||
arguments:
|
||||
|
||||
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.
|
||||
# 1
|
||||
3
|
||||
|
||||
## v1.9.4 (2019-11-05)
|
||||
# 2
|
||||
7
|
||||
|
||||
stacktrace:
|
||||
lib/ex_unit/examples/one_of_each.exs:158: (test)
|
||||
```
|
||||
|
||||
Furthermore, failures in doctests are now colored and diffed.
|
||||
|
||||
On the `mix test` side of things, there is a new `--failed` flag that runs all tests that failed the last time they ran. Finally, coverage reports generated with `mix test --cover` include a summary out of the box:
|
||||
|
||||
```
|
||||
Generating cover results ...
|
||||
|
||||
Percentage | Module
|
||||
-----------|--------------------------
|
||||
100.00% | Plug.Exception.Any
|
||||
100.00% | Plug.Adapters.Cowboy2.Stream
|
||||
100.00% | Collectable.Plug.Conn
|
||||
100.00% | Plug.Crypto.KeyGenerator
|
||||
100.00% | Plug.Parsers
|
||||
100.00% | Plug.Head
|
||||
100.00% | Plug.Router.Utils
|
||||
100.00% | Plug.RequestId
|
||||
... | ...
|
||||
-----------|--------------------------
|
||||
77.19% | Total
|
||||
```
|
||||
|
||||
## v1.7.4 (2018-10-24)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Kernel] Expand `left..right` at compile time in more cases, which leads to improved performance under different scenarios, especially on `x in left..right` expressions
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix deps.loadpaths] Add `--no-load-deps` flag. This is useful for Rebar 3 compatibility
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Calendar] Fix for converting from negative iso days on New Year in a leap year
|
||||
* [Kernel] Ensure `@spec`, `@callback`, `@type` and friends can be read accordingly
|
||||
* [Module] Avoid warnings when using Module.eval_quoted in the middle of existing definitions
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix archive.build] Unload previous archive versions before building
|
||||
* [mix format] Expand paths so mix format `path\for\windows.ex` works
|
||||
* [mix test] Ensure that `--cover` displays correct coverage in an umbrella app
|
||||
|
||||
## v1.7.3 (2018-08-24)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Mix
|
||||
#### ExUnit
|
||||
|
||||
* [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
|
||||
* [ExUnit.Assertions] Do not attempt to expand `try/1` as it is a special form
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix release] Add :tar option for releases to create a tarball
|
||||
* [mix compile.app] Do not include applications with `runtime: false` as a runtime dependency for applications coming from Hex
|
||||
|
||||
### 2. Bug fixes
|
||||
## v1.7.2 (2018-08-05)
|
||||
|
||||
#### 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
|
||||
### 1. 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
|
||||
* [DateTime] Take negative years into account in `DateTime.from_iso8601/1`
|
||||
* [Kernel] Do not emit warnings for repeated docs over different clauses due to false positives
|
||||
|
||||
#### 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
|
||||
* [mix compile] Properly mark top-level dependencies as optional and as runtime. This fixes a bug where Mix attempted to start optional dependencies of a package when those optional dependencies were not available
|
||||
* [mix compile] Avoid deadlock when a config has a timestamp later than current time
|
||||
* [mix help] Show task and alias help when both are available
|
||||
* [mix test] Do not fail suite if there are no test files
|
||||
|
||||
## v1.9.1 (2019-07-18)
|
||||
## v1.7.1 (2018-07-26)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix format] Print relative paths in `--check-formatted` output
|
||||
* [mix release] Support included applications
|
||||
|
||||
### 2. Bug fixes
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Code] Fix formatter wrongly removing nested parens in nested calls
|
||||
* [Calendar] Work-around a Dialyzer bug that causes it to loop for a long time, potentially indefinitely
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Do not crash translator on poorly formatted supervisor names
|
||||
|
||||
#### Mix
|
||||
|
||||
* [mix compile] Raise readable error for mismatched sources during compilation
|
||||
* [mix release] Preserve UTF8 encoding in release config files
|
||||
|
||||
## v1.9.0 (2019-06-24)
|
||||
## v1.7.0 (2018-07-25)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Allow more complex mixed expressions when tokenizing
|
||||
|
||||
#### 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.ISO] Support negative dates in `Calendar.ISO`
|
||||
* [Calendar] Add `Calendar.months_in_year/1` callback
|
||||
* [Code] Add `Code.compile_file/2` that compiles files without leaving footprints on the system
|
||||
* [Code] Add `Code.purge_compiler_modules/0` that purges any compiler module left behind. This is useful for live systems dynamically compiling code
|
||||
* [Code] Add `Code.fetch_docs/1` that returns docs in the [EEP 48](http://erlang.org/eep/eeps/eep-0048.html) format
|
||||
* [Date] Add `Date.months_in_year/1` function
|
||||
* [DynamicSupervisor] Use the name of the `DynamicSupervisor` as the ID whenever possible
|
||||
* [Exception] Provide "did you mean" suggestions on KeyError
|
||||
* [Exception] Provide more information on ArithmeticError on Erlang/OTP 21+
|
||||
* [Function] Add `Function` module with `capture/3`, `info/1` and `info/2` functions
|
||||
* [GenServer] Support the new `handle_continue/2` callback on Erlang/OTP 21+
|
||||
* [IO.ANSI] Add cursor movement to `IO.ANSI`
|
||||
* [Kernel] Support adding arbitrary documentation metadata by passing a keyword list to `@doc`, `@moduledoc` and `@typedoc`
|
||||
* [Kernel] Introduce `__STACKTRACE__` to retrieve the current stacktrace inside `catch`/`rescue` (this will be a requirement for Erlang/OTP 21+)
|
||||
* [Kernel] Raise on unsafe variables in order to allow us to better track unused variables
|
||||
* [Kernel] Warn when using `length` to check if a list is not empty on guards
|
||||
* [Kernel] Add hints on mismatched `do`/`end` and others pairs
|
||||
* [Kernel] Warn when comparing structs using the `>`, `<`, `>=` and `<=` operators
|
||||
* [Kernel] Warn on unsupported nested comparisons such as `x < y < z`
|
||||
* [Kernel] Warn if redefining documentation across clauses of the same definition
|
||||
* [Kernel] Warn on unnecessary quotes around atoms, keywords and calls
|
||||
* [Macro] Add `Macro.special_form?/2` and `Macro.operator?/2` that returns `true` if the given name/arity is a special form or operator respectively
|
||||
* [Macro.Env] Add `Macro.Env.vars/1` and `Macro.Env.has_var?/2` that gives access to environment data without accessing private fields
|
||||
* [Regex] Include endianness in the regex version. This allows regexes to be recompiled when an archive is installed in a system with a different endianness
|
||||
* [Registry] Add `Registry.count/1` and `Registry.count_match/4`
|
||||
* [String] Update to Unicode 11
|
||||
* [StringIO] Add `StringIO.open/3`
|
||||
* [System] Use ISO 8601 in `System.build_info/0`
|
||||
|
||||
#### 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.Assertion] Print the arguments in error reports when asserting on a function call. For example, if `assert is_list(arg)` fails, the argument will be shown in the report
|
||||
* [ExUnit.Diff] Improve diffing of lists when one list is a subset of the other
|
||||
* [ExUnit.DocTest] Show colored diffs on failed doctests
|
||||
* [ExUnit.Formatter] Excluded tests, via the `--exclude` and `--only` flags, are now shown as "Excluded" in reports. Tests skipped via `@tag :skip` are now exclusively shown as "Skipped" and in yellow
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.CLI] Copy ticktime from remote node on IEx `--remsh`
|
||||
* [IEx.CLI] Automatically add a host on node given to `--remsh`
|
||||
* [IEx.Helpers] Add `use_if_available/2`
|
||||
* [IEx.Helpers] Allow `force: true` option in `recompile/1`
|
||||
* [IEx.Helpers] Add `:allocators` pane to `runtime_info/1`
|
||||
* [IEx.Helpers] Show documentation metadata in `h/1` helpers
|
||||
|
||||
#### 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] Ensure nil metadata is always pruned
|
||||
* [Logger] Only evaluate Logger macro arguments when the message will be logged
|
||||
* [Logger] Add `:compile_time_purge_matching` to purge logger calls that match certain compile time metadata, such as module names and application names
|
||||
* [Logger] Log to `:stderr` if a backend fails and there are no other backends
|
||||
* [Logger] Allow translators to return custom metadata
|
||||
* [Logger] Return `:crash_reason`, `:initial_call` and `:registered_name` as metadata in crash reports coming from Erlang/OTP
|
||||
|
||||
#### 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 archive.install] Add support for the Hex organization via `--organization`
|
||||
* [mix archive.uninstall] Support `--force` flag
|
||||
* [mix compile] Improve support for external build tools such as `rebar`
|
||||
* [mix deps] Include `override: true` in rebar dependencies to make the behaviour closer to how rebar3 works (although diverged deps are still marked as diverged)
|
||||
* [mix escript.install] Add support for the Hex organization via `--organization`
|
||||
* [mix escript.uninstall] Support `--force` flag
|
||||
* [mix help] Also list aliases
|
||||
* [mix local] Use ipv6 with auto fallback to ipv4 when downloading data
|
||||
* [mix profile] Allow all profiling tasks to run programatically
|
||||
* [mix test] Add `--failed` option that only runs previously failed tests
|
||||
* [mix test] Print coverage summary by default when the `--cover` flag is given
|
||||
* [Mix.Project] Add `Mix.Project.clear_deps_cache/0`
|
||||
* [Mix.Project] Add `Mix.Project.config_mtime/0` that caches the config mtime values to avoid filesystem access
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Consistently trim newlines when you have a single EEx expression per line on multiple lines
|
||||
|
||||
#### 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
|
||||
* [IO.ANSI.Docs] Fix table column alignment when converting docs to ANSI escapes
|
||||
* [Code] Ensure `string_to_quoted` returns error tuples instead of raising in certain constructs
|
||||
* [Code.Formatter] Consistently format keyword lists in function calls with and without parens
|
||||
* [Code.Formatter] Do not break after `->` when there are only comments and one-line clauses
|
||||
* [File] Allow the `:trim_bom` option to be used with `:encoding`
|
||||
* [Kernel] Raise on unsafe variables as some of the code emitted with unsafe variables would not correctly propagate variables or would disable tail call optimization semantics
|
||||
* [Kernel] Do not crash on dynamic sizes in binary generators with collectable into in comprehensions
|
||||
* [Kernel] Do not crash on literals with non-unary size in binary generators with collectable into in comprehensions
|
||||
* [Task] Improve error reports and exit reasons for failed tasks on Erlang/OTP 20+
|
||||
|
||||
#### 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.Case] Raise proper error if `@tag` and `@moduletag` are used before `use ExUnit.Case`
|
||||
* [ExUnit.Case] Raise proper error if `@describetag` is used outside of `describe/2` blocks
|
||||
* [ExUnit.DocTest] Emit proper assertion error on doctests with invalid UTF-8
|
||||
|
||||
#### 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 archive.install] Fetch optional dependencies when installing an archive from Git/Hex
|
||||
* [mix compile] Properly track config files in umbrella projects and recompile when any relevant umbrella configuration changes
|
||||
* [mix deps] Ensure the same dependency from different SCMs are tagged as diverged when those SCMs are remote and non-remote
|
||||
* [mix deps] Ensure we re-run dependency resolution when overriding a skipped dep in umbrella
|
||||
* [mix deps.compile] Perform clean builds for dependencies on outdated locks to avoid old modules from affecting future compilation
|
||||
* [mix escript.install] Fetch optional dependencies when installing an escript from Git/Hex
|
||||
|
||||
### 3. Soft-deprecations (no warnings emitted)
|
||||
|
||||
#### Mix
|
||||
#### Elixir
|
||||
|
||||
* [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
|
||||
* [Code] Deprecate `Code.load_file/2` in favor of `Code.compile_file/2`
|
||||
* [Code] Deprecate `Code.loaded_files/0` in favor of `Code.required_files/0`
|
||||
* [Code] Deprecate `Code.unload_files/1` in favor of `Code.unrequire_files/1`
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] `compile_time_purge_level` is deprecated in favor of `compile_time_purge_matching`
|
||||
|
||||
### 4. Hard-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
|
||||
* [Code] `Code.get_docs/2` is deprecated in favor of `Code.fetch_docs/1`
|
||||
* [Enum] `Enum.chunk/2/3/4` is deprecated in favor of `Enum.chunk_every/2/3/4` - notice `chunk_every` does not discard incomplete chunks by default
|
||||
* [GenServer] Warn if `super` is used in any of the GenServer callbacks
|
||||
* [Kernel] `not left in right` is ambiguous and is deprecated in favor of `left not in right`
|
||||
* [Kernel] Warn on confusing operator sequences, such as `1+++1` meaning `1 ++ +1` or `........` meaning `... .. ...`
|
||||
* [OptionParser] Deprecate dynamic option parser mode that depended on atoms to be previously loaded and therefore behaved inconsistently
|
||||
* [Stream] `Stream.chunk/2/3/4` is deprecated in favor of `Stream.chunk_every/2/3/4` - notice `chunk_every` does not discard incomplete chunks by default
|
||||
|
||||
#### Mix
|
||||
## v1.6
|
||||
|
||||
* [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.6 releases can be found [in the v1.6 branch](https://github.com/elixir-lang/elixir/blob/v1.6/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
|
||||
|
||||
+2
-4
@@ -1,9 +1,8 @@
|
||||
### 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
|
||||
@@ -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,9 +1,8 @@
|
||||
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.7/
|
||||
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))
|
||||
@@ -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 unicode app 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/OTP 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,22 +45,10 @@ 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
|
||||
@@ -127,29 +116,6 @@ 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:
|
||||
rm -rf ebin
|
||||
rm -rf lib/*/ebin
|
||||
@@ -175,7 +141,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
|
||||
|
||||
@@ -227,7 +193,7 @@ Precompiled.zip: build_man compile
|
||||
|
||||
zips: Precompiled.zip Docs.zip
|
||||
@ echo ""
|
||||
@ echo "### Checksums"
|
||||
@ 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:"
|
||||
@@ -264,7 +230,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 +254,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 +279,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/OTP version
|
||||
(Elixir requires Erlang/OTP 19.0 or later). You can check your Erlang/OTP 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,20 +69,20 @@ 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`.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -158,8 +147,7 @@ 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.
|
||||
|
||||
+3
-7
@@ -20,9 +20,7 @@
|
||||
|
||||
9. Publish new zips with `make zips`, upload `Precompiled.zip` and `Docs.zip` to GitHub Releases, and include SHAs+CHANGELOG
|
||||
|
||||
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`
|
||||
|
||||
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
|
||||
10. Add the release to `elixir.csv` and `_data/elixir-versions.yml` files in `elixir-lang/elixir-lang.github.com`
|
||||
|
||||
## Creating a new vMAJOR.MINOR branch
|
||||
|
||||
@@ -30,7 +28,7 @@
|
||||
|
||||
1. Set `CANONICAL=` in /Makefile
|
||||
|
||||
2. Update tables in /SECURITY.md and "Compatibility and Deprecations"
|
||||
2. Update **all** tables in "Compatibility and Deprecations"
|
||||
|
||||
3. Commit "Prepare vMAJOR.MINOR for release"
|
||||
|
||||
@@ -40,6 +38,4 @@
|
||||
|
||||
2. Start new /CHANGELOG.md
|
||||
|
||||
3. Update tables in "Compatibility and Deprecations"
|
||||
|
||||
4. Commit "Start vMAJOR.MINOR+1"
|
||||
3. Commit "Start vMAJOR.MINOR+1"
|
||||
-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
|
||||
+62
-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,17 @@ 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
|
||||
ERL_PATH="$ERL_EXEC"
|
||||
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
|
||||
|
||||
+7
-10
@@ -43,8 +43,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
|
||||
|
||||
@@ -106,7 +105,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 +141,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 +167,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 +176,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)
|
||||
@@ -209,8 +207,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'}
|
||||
]
|
||||
|
||||
|
||||
+3
-124
@@ -84,18 +84,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 +100,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 +132,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 +217,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 +342,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
|
||||
@@ -599,7 +501,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 +519,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 +527,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
|
||||
|
||||
@@ -8,10 +8,12 @@
|
||||
warn_unused_record,
|
||||
warn_deprecated_function,
|
||||
warn_obsolete_guard,
|
||||
strict_validation,
|
||||
warn_exported_vars,
|
||||
%% warn_export_vars,
|
||||
%% warn_missing_spec,
|
||||
%% warn_untyped_record,
|
||||
warnings_as_errors,
|
||||
%% warnings_as_errors,
|
||||
debug_info,
|
||||
{outdir, "ebin/"}
|
||||
]}.
|
||||
|
||||
+9
-21
@@ -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,14 @@
|
||||
Atom,
|
||||
Base,
|
||||
Bitwise,
|
||||
Calendar,
|
||||
Calendar.ISO,
|
||||
Date,
|
||||
DateTime,
|
||||
Exception,
|
||||
Float,
|
||||
Function,
|
||||
Integer,
|
||||
Module,
|
||||
NaiveDateTime,
|
||||
Record,
|
||||
Regex,
|
||||
@@ -26,8 +23,7 @@
|
||||
Time,
|
||||
Tuple,
|
||||
URI,
|
||||
Version,
|
||||
Version.Requirement
|
||||
Version
|
||||
],
|
||||
"Collections & Enumerables": [
|
||||
Access,
|
||||
@@ -53,18 +49,16 @@
|
||||
StringIO,
|
||||
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,
|
||||
@@ -84,12 +78,6 @@
|
||||
Protocol,
|
||||
String.Chars
|
||||
],
|
||||
"Code & Macros": [
|
||||
Code,
|
||||
Kernel.ParallelCompiler,
|
||||
Macro,
|
||||
Macro.Env
|
||||
],
|
||||
Deprecated: [
|
||||
Behaviour,
|
||||
Dict,
|
||||
|
||||
+108
-68
@@ -2,12 +2,46 @@ defmodule Access do
|
||||
@moduledoc """
|
||||
Key-based access to data structures.
|
||||
|
||||
The `Access` module defines a behaviour for dynamically accessing
|
||||
keys of any type in a data structure via the `data[key]` syntax.
|
||||
Elixir supports three main key-value constructs: keywords,
|
||||
maps, and structs. It also supports two mechanisms to access those keys:
|
||||
by brackets (via `data[key]`) and by dot-syntax (via `data.field`).
|
||||
|
||||
`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:
|
||||
In the next section we will briefly recap the key-value constructs and then
|
||||
discuss the access mechanisms.
|
||||
|
||||
## Key-value constructs
|
||||
|
||||
Elixir provides three main key-value constructs, summarized below:
|
||||
|
||||
* keyword lists - they are lists of two-element tuples where
|
||||
the first element is an atom. Commonly written in the
|
||||
`[key: value]` syntax, they support only atom keys. Keyword
|
||||
lists are used almost exclusively to pass options to functions
|
||||
and macros. They keep the user ordering and allow duplicate
|
||||
keys. See the `Keyword` module.
|
||||
|
||||
* maps - they are the "go to" key-value data structure in Elixir.
|
||||
They are capable of supporting billions of keys of any type. They are
|
||||
written using the `%{key => value}` syntax and also support the
|
||||
`%{key: value}` syntax when the keys are atoms. They do not
|
||||
have any specified ordering and do not allow duplicate keys.
|
||||
See the `Map` module.
|
||||
|
||||
* structs - they are named maps with a pre-determined set of keys.
|
||||
They are defined with `defstruct/1` and written using the
|
||||
`%StructName{key: value}` syntax.
|
||||
|
||||
## Key-based accessors
|
||||
|
||||
Elixir provides two mechanisms to access data structures by key,
|
||||
described next.
|
||||
|
||||
### Bracket-based access
|
||||
|
||||
The `data[key]` syntax is used to access data structures with a
|
||||
dynamic number of keys, such as keywords and maps. The key can
|
||||
be of any type. The bracket-based access syntax returns `nil`
|
||||
if the key does not exist:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:a]
|
||||
@@ -25,72 +59,89 @@ defmodule Access do
|
||||
|
||||
This syntax is very convenient as it can be nested arbitrarily:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:c][:unknown]
|
||||
nil
|
||||
|
||||
This works because accessing anything on a `nil` value, returns
|
||||
`nil` itself:
|
||||
|
||||
iex> nil[:a]
|
||||
nil
|
||||
|
||||
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:
|
||||
|
||||
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
|
||||
iex> put_in(users["john"][:age], 28)
|
||||
%{"john" => %{age: 28}, "meg" => %{age: 23}}
|
||||
|
||||
> 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.
|
||||
Furthermore, the bracket-based access syntax transparently ignores
|
||||
`nil` values. When trying to access anything on a `nil` value, `nil`
|
||||
is returned:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:c][:unknown]
|
||||
nil
|
||||
|
||||
iex> nil[:a]
|
||||
nil
|
||||
|
||||
Internally, `data[key]` translates to `Access.get(term, key, nil)`.
|
||||
Developers interested in implementing their own key-value data
|
||||
structures can implement the `Access` behaviour to provide the
|
||||
bracket-based access syntax. `Access` requires the key comparison
|
||||
to be implemented using the `===/2` operator.
|
||||
|
||||
### Dot-based syntax
|
||||
|
||||
The `data.field` syntax is used exclusively to access atom fields
|
||||
in maps and structs. If the accessed field does not exist, an error is
|
||||
raised. This is a deliberate decision: since all of the
|
||||
fields in a struct are pre-determined, structs support only the
|
||||
dot-based syntax and not the access one.
|
||||
|
||||
Imagine a struct named `User` with a `:name` field. The following would raise:
|
||||
|
||||
user = %User{name: "John"}
|
||||
user[:name]
|
||||
# ** (UndefinedFunctionError) undefined function User.fetch/2 (User does not implement the Access behaviour)
|
||||
|
||||
Instead we should use the `user.name` syntax to access fields:
|
||||
|
||||
user.name
|
||||
#=> "John"
|
||||
|
||||
Differently from `user[:name]`, `user.name` is not extensible via
|
||||
a behaviour and is restricted only to structs and atom keys in maps.
|
||||
|
||||
### Summing up
|
||||
|
||||
The bracket-based syntax, `user[:name]`, is used by dynamic structures,
|
||||
is extensible and returns nil on misisng keys.
|
||||
|
||||
The dot-based syntax, `user.name`, is used exclusively to access atom
|
||||
keys in maps and structs, and it raises on missing keys.
|
||||
|
||||
## Nested data structures
|
||||
|
||||
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`.
|
||||
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`.
|
||||
|
||||
For example, to update a map inside another map:
|
||||
|
||||
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
|
||||
iex> put_in(users["john"].age, 28)
|
||||
%{"john" => %{age: 28}, "meg" => %{age: 23}}
|
||||
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
|
||||
iex> put_in(users["john"].age, 28)
|
||||
%{"john" => %{age: 28}, "meg" => %{age: 23}}
|
||||
|
||||
This module provides convenience functions for traversing other
|
||||
structures, like tuples and lists. These functions can be used
|
||||
in all the `Access`-related functions and macros in `Kernel`.
|
||||
|
||||
For instance, given a user map with the `:name` and `:languages`
|
||||
keys, here is how to deeply traverse the map and convert all
|
||||
language names to uppercase:
|
||||
For instance, given a user map with the `: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}
|
||||
]
|
||||
}
|
||||
%{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.
|
||||
See the functions `key/1`, `key!/1`, `elem/1`, and `all/0` for some of the
|
||||
available accessors.
|
||||
"""
|
||||
|
||||
@type container :: keyword | struct | map
|
||||
@@ -560,7 +611,7 @@ 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 ->
|
||||
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 +661,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)}
|
||||
...> 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:
|
||||
@@ -640,14 +685,19 @@ defmodule Access do
|
||||
...> 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 +720,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
|
||||
|
||||
|
||||
+43
-92
@@ -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
|
||||
|
||||
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:
|
||||
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:
|
||||
|
||||
* `:id` - the child specification identifier, defaults to the current module
|
||||
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
|
||||
* `:restart` - when the 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,7 +142,7 @@ 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"
|
||||
def child_spec(arg) do
|
||||
@@ -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,8 +23,8 @@ defmodule Application do
|
||||
`mix.exs`, and others are generated by Mix itself.
|
||||
|
||||
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`.
|
||||
documentation of `Mix.Tasks.Compile.App`, available as well by running `mix
|
||||
help compile.app`.
|
||||
|
||||
## The application environment
|
||||
|
||||
@@ -66,8 +66,8 @@ defmodule Application do
|
||||
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 environment can be overriden via the `-config` option of
|
||||
`erl`, as well as command-line flags, as we are going to see below.
|
||||
|
||||
## The application callback module
|
||||
|
||||
@@ -135,7 +135,7 @@ defmodule Application do
|
||||
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`.
|
||||
`-Application` flags passed to `erl`.
|
||||
|
||||
Loading an application *does not* load its modules.
|
||||
|
||||
@@ -197,9 +197,9 @@ defmodule Application do
|
||||
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.
|
||||
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.
|
||||
|
||||
## Tooling
|
||||
|
||||
@@ -296,7 +296,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 +306,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 +322,13 @@ defmodule Application do
|
||||
end
|
||||
end
|
||||
|
||||
@type app :: atom
|
||||
@type key :: atom
|
||||
@type value :: term
|
||||
@type state :: term
|
||||
@type start_type :: :normal | {:takeover, node} | {:failover, node}
|
||||
@type restart_type :: :permanent | :transient | :temporary
|
||||
|
||||
@application_keys [
|
||||
:description,
|
||||
:id,
|
||||
@@ -347,16 +343,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 +353,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 +368,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 +395,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
|
||||
|
||||
@@ -432,12 +418,12 @@ defmodule Application do
|
||||
|
||||
config :my_app, Databases.RepoOne,
|
||||
# A database configuration
|
||||
ip: "localhost",
|
||||
ip: "localhost"
|
||||
port: 5433
|
||||
|
||||
config :my_app, Databases.RepoTwo,
|
||||
# Another database configuration (for the same OTP app)
|
||||
ip: "localhost",
|
||||
ip: "localhost"
|
||||
port: 20717
|
||||
|
||||
config :my_app, my_app_databases: [Databases.RepoOne, Databases.RepoTwo]
|
||||
@@ -452,10 +438,10 @@ defmodule Application do
|
||||
**Important:** if you are writing a library to be used by other developers,
|
||||
it is generally recommended to avoid the application environment, as the
|
||||
application environment is effectively a global storage. For more information,
|
||||
read our [library guidelines](library-guidelines.html).
|
||||
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 +451,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 +463,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 +502,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
|
||||
|
||||
@@ -639,7 +594,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
|
||||
|
||||
@@ -722,11 +677,11 @@ defmodule Application do
|
||||
@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
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ defmodule Atom do
|
||||
:erlang.atom_to_list(atom)
|
||||
end
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
@deprecated "Use Atom.to_charlist/1 instead"
|
||||
@spec to_char_list(atom) :: charlist
|
||||
|
||||
@@ -28,7 +28,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 +36,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 +56,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
|
||||
|
||||
@@ -66,7 +66,6 @@ defmodule Bitwise do
|
||||
1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro bnot(expr) do
|
||||
quote(do: :erlang.bnot(unquote(expr)))
|
||||
end
|
||||
@@ -80,7 +79,6 @@ defmodule Bitwise do
|
||||
1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro ~~~expr do
|
||||
quote(do: :erlang.bnot(unquote(expr)))
|
||||
end
|
||||
@@ -92,7 +90,6 @@ defmodule Bitwise do
|
||||
1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro band(left, right) do
|
||||
quote(do: :erlang.band(unquote(left), unquote(right)))
|
||||
end
|
||||
@@ -104,7 +101,6 @@ defmodule Bitwise do
|
||||
1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro left &&& right do
|
||||
quote(do: :erlang.band(unquote(left), unquote(right)))
|
||||
end
|
||||
@@ -116,7 +112,6 @@ defmodule Bitwise do
|
||||
11
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro bor(left, right) do
|
||||
quote(do: :erlang.bor(unquote(left), unquote(right)))
|
||||
end
|
||||
@@ -128,7 +123,6 @@ defmodule Bitwise do
|
||||
11
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro left ||| right do
|
||||
quote(do: :erlang.bor(unquote(left), unquote(right)))
|
||||
end
|
||||
@@ -140,7 +134,6 @@ defmodule Bitwise do
|
||||
10
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro bxor(left, right) do
|
||||
quote(do: :erlang.bxor(unquote(left), unquote(right)))
|
||||
end
|
||||
@@ -152,7 +145,6 @@ defmodule Bitwise do
|
||||
10
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro left ^^^ right do
|
||||
quote(do: :erlang.bxor(unquote(left), unquote(right)))
|
||||
end
|
||||
@@ -170,7 +162,6 @@ defmodule Bitwise do
|
||||
-1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro bsl(left, right) do
|
||||
quote(do: :erlang.bsl(unquote(left), unquote(right)))
|
||||
end
|
||||
@@ -188,7 +179,6 @@ defmodule Bitwise do
|
||||
-1
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro left <<< right do
|
||||
quote(do: :erlang.bsl(unquote(left), unquote(right)))
|
||||
end
|
||||
@@ -206,7 +196,6 @@ defmodule Bitwise do
|
||||
-4
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro bsr(left, right) do
|
||||
quote(do: :erlang.bsr(unquote(left), unquote(right)))
|
||||
end
|
||||
@@ -224,7 +213,6 @@ defmodule Bitwise do
|
||||
-4
|
||||
|
||||
"""
|
||||
@doc guard: true
|
||||
defmacro left >>> right do
|
||||
quote(do: :erlang.bsr(unquote(left), unquote(right)))
|
||||
end
|
||||
|
||||
+12
-71
@@ -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,28 +112,6 @@ 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.
|
||||
"""
|
||||
@@ -155,27 +134,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.
|
||||
@@ -299,22 +258,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
|
||||
|
||||
@@ -4,7 +4,7 @@ defmodule Date do
|
||||
|
||||
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:
|
||||
[`~D`](`Kernel.sigil_D/2`) sigil:
|
||||
|
||||
iex> ~D[2000-01-01]
|
||||
~D[2000-01-01]
|
||||
@@ -25,7 +25,7 @@ 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
|
||||
|
||||
@@ -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(),
|
||||
@@ -248,8 +248,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")
|
||||
@@ -343,7 +341,7 @@ defmodule Date do
|
||||
|
||||
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)
|
||||
Calendar.ISO.date_to_iso8601(year, month, day, format)
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: _} = date, format) when format in [:basic, :extended] do
|
||||
@@ -467,7 +465,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
|
||||
@@ -558,20 +556,9 @@ defmodule Date do
|
||||
"""
|
||||
@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 """
|
||||
@@ -652,114 +639,13 @@ defmodule Date do
|
||||
|
||||
"""
|
||||
@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
|
||||
|
||||
+143
-496
@@ -12,25 +12,26 @@ defmodule DateTime do
|
||||
are structural and based on the DateTime struct fields. For proper
|
||||
comparison between datetimes, use the `compare/2` function.
|
||||
|
||||
The functions on this module work with the `DateTime` struct as well
|
||||
as any struct that contains the same fields as the `DateTime` struct.
|
||||
Such functions expect `t:Calendar.datetime/0` in their typespecs
|
||||
(instead of `t:t/0`).
|
||||
|
||||
Developers should avoid creating the `DateTime` struct directly
|
||||
and instead rely on the functions provided by this module as
|
||||
well as the ones in third-party calendar libraries.
|
||||
well as the ones in 3rd party calendar libraries.
|
||||
|
||||
## Time zone database
|
||||
## Where are my functions?
|
||||
|
||||
Many functions in this module require a time zone database.
|
||||
By default, it uses the default time zone database returned by
|
||||
`Calendar.get_time_zone_database/0`, which defaults to
|
||||
`Calendar.UTCOnlyTimeZoneDatabase` which only handles "Etc/UTC"
|
||||
datetimes and returns `{:error, :utc_only_time_zone_database}`
|
||||
for any other time zone.
|
||||
You will notice this module only contains conversion
|
||||
functions as well as functions that work on UTC. This
|
||||
is because a proper `DateTime` implementation requires a
|
||||
time zone database which currently is not provided as part
|
||||
of Elixir.
|
||||
|
||||
Other time zone databases (including ones provided by packages)
|
||||
can be configure as default either via configuration:
|
||||
|
||||
config :elixir, :time_zone_database, CustomTimeZoneDatabase
|
||||
|
||||
or by calling `Calendar.put_time_zone_database/1`.
|
||||
Such may be addressed in upcoming versions, meanwhile,
|
||||
use 3rd party packages to provide `DateTime` building and
|
||||
similar functionality with time zone backing.
|
||||
"""
|
||||
|
||||
@enforce_keys [:year, :month, :day, :hour, :minute, :second] ++
|
||||
@@ -97,17 +98,17 @@ defmodule DateTime do
|
||||
|
||||
iex> {:ok, datetime} = DateTime.from_unix(1_464_096_368)
|
||||
iex> datetime
|
||||
~U[2016-05-24 13:26:08Z]
|
||||
#DateTime<2016-05-24 13:26:08Z>
|
||||
|
||||
iex> {:ok, datetime} = DateTime.from_unix(1_432_560_368_868_569, :microsecond)
|
||||
iex> datetime
|
||||
~U[2015-05-25 13:26:08.868569Z]
|
||||
#DateTime<2015-05-25 13:26:08.868569Z>
|
||||
|
||||
The unit can also be an integer as in `t:System.time_unit/0`:
|
||||
|
||||
iex> {:ok, datetime} = DateTime.from_unix(143_256_036_886_856, 1024)
|
||||
iex> datetime
|
||||
~U[6403-03-17 07:05:22.320312Z]
|
||||
#DateTime<6403-03-17 07:05:22.320Z>
|
||||
|
||||
Negative Unix times are supported, up to -62167219200 seconds,
|
||||
which is equivalent to "0000-01-01T00:00:00Z" or 0 Gregorian seconds.
|
||||
@@ -152,20 +153,17 @@ defmodule DateTime do
|
||||
|
||||
# An easy way to get the Unix epoch is passing 0 to this function
|
||||
iex> DateTime.from_unix!(0)
|
||||
~U[1970-01-01 00:00:00Z]
|
||||
#DateTime<1970-01-01 00:00:00Z>
|
||||
|
||||
iex> DateTime.from_unix!(1_464_096_368)
|
||||
~U[2016-05-24 13:26:08Z]
|
||||
#DateTime<2016-05-24 13:26:08Z>
|
||||
|
||||
iex> DateTime.from_unix!(1_432_560_368_868_569, :microsecond)
|
||||
~U[2015-05-25 13:26:08.868569Z]
|
||||
|
||||
iex> DateTime.from_unix!(143_256_036_886_856, 1024)
|
||||
~U[6403-03-17 07:05:22.320312Z]
|
||||
#DateTime<2015-05-25 13:26:08.868569Z>
|
||||
|
||||
"""
|
||||
@spec from_unix!(integer, :native | System.time_unit(), Calendar.calendar()) :: t
|
||||
def from_unix!(integer, unit \\ :second, calendar \\ Calendar.ISO) do
|
||||
def from_unix!(integer, unit \\ :second, calendar \\ Calendar.ISO) when is_atom(unit) do
|
||||
case from_unix(integer, unit, calendar) do
|
||||
{:ok, datetime} ->
|
||||
datetime
|
||||
@@ -178,147 +176,21 @@ defmodule DateTime do
|
||||
@doc """
|
||||
Converts the given `NaiveDateTime` to `DateTime`.
|
||||
|
||||
It expects a time zone to put the `NaiveDateTime` in.
|
||||
If the time zone is "Etc/UTC", it always succeeds. Otherwise,
|
||||
the NaiveDateTime is checked against the time zone database
|
||||
given as `time_zone_database`. See the "Time zone database"
|
||||
section in the module documentation.
|
||||
It expects a time zone to put the NaiveDateTime in.
|
||||
Currently it only supports "Etc/UTC" as time zone.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> DateTime.from_naive(~N[2016-05-24 13:26:08.003], "Etc/UTC")
|
||||
{:ok, ~U[2016-05-24 13:26:08.003Z]}
|
||||
|
||||
When the datetime is ambiguous - for instance during changing from summer
|
||||
to winter time - the two possible valid datetimes are returned. First the one
|
||||
that happens first, then the one that happens after.
|
||||
|
||||
iex> {:ambiguous, first_dt, second_dt} = DateTime.from_naive(~N[2018-10-28 02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> first_dt
|
||||
#DateTime<2018-10-28 02:30:00+02:00 CEST Europe/Copenhagen>
|
||||
iex> second_dt
|
||||
#DateTime<2018-10-28 02:30:00+01:00 CET Europe/Copenhagen>
|
||||
|
||||
When there is a gap in wall time - for instance in spring when the clocks are
|
||||
turned forward - the latest valid datetime just before the gap and the first
|
||||
valid datetime just after the gap.
|
||||
|
||||
iex> {:gap, just_before, just_after} = DateTime.from_naive(~N[2019-03-31 02:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> just_before
|
||||
#DateTime<2019-03-31 01:59:59.999999+01:00 CET Europe/Copenhagen>
|
||||
iex> just_after
|
||||
#DateTime<2019-03-31 03:00:00+02:00 CEST Europe/Copenhagen>
|
||||
|
||||
Most of the time there is one, and just one, valid datetime for a certain
|
||||
date and time in a certain time zone.
|
||||
|
||||
iex> {:ok, datetime} = DateTime.from_naive(~N[2018-07-28 12:30:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> {:ok, datetime} = DateTime.from_naive(~N[2016-05-24 13:26:08.003], "Etc/UTC")
|
||||
iex> datetime
|
||||
#DateTime<2018-07-28 12:30:00+02:00 CEST Europe/Copenhagen>
|
||||
#DateTime<2016-05-24 13:26:08.003Z>
|
||||
|
||||
This function accepts any map or struct that contains at least the same fields as a `NaiveDateTime`
|
||||
struct. The most common example of that is a `DateTime`. In this case the information about the time
|
||||
zone of that `DateTime` is completely ignored. This is the same principle as passing a `DateTime` to
|
||||
`Date.to_iso8601/2`. `Date.to_iso8601/2` extracts only the date-specific fields (calendar, year,
|
||||
month and day) of the given structure and ignores all others.
|
||||
|
||||
This way if you have a `DateTime` in one time zone, you can get the same wall time in another time zone.
|
||||
For instance if you have 2018-08-24 10:00:00 in Copenhagen and want a `DateTime` for 2018-08-24 10:00:00
|
||||
in UTC you can do:
|
||||
|
||||
iex> cph_datetime = DateTime.from_naive!(~N[2018-08-24 10:00:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> {:ok, utc_datetime} = DateTime.from_naive(cph_datetime, "Etc/UTC", FakeTimeZoneDatabase)
|
||||
iex> utc_datetime
|
||||
~U[2018-08-24 10:00:00Z]
|
||||
|
||||
If instead you want a `DateTime` for the same point time in a different time zone see the
|
||||
`DateTime.shift_zone/3` function which would convert 2018-08-24 10:00:00 in Copenhagen
|
||||
to 2018-08-24 08:00:00 in UTC.
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec from_naive(
|
||||
Calendar.naive_datetime(),
|
||||
Calendar.time_zone(),
|
||||
Calendar.time_zone_database()
|
||||
) ::
|
||||
{:ok, t}
|
||||
| {:ambiguous, t, t}
|
||||
| {:gap, t, t}
|
||||
| {:error,
|
||||
:incompatible_calendars | :time_zone_not_found | :utc_only_time_zone_database}
|
||||
|
||||
def from_naive(
|
||||
naive_datetime,
|
||||
time_zone,
|
||||
time_zone_database \\ Calendar.get_time_zone_database()
|
||||
)
|
||||
|
||||
def from_naive(naive_datetime, "Etc/UTC", _) do
|
||||
utc_period = %{std_offset: 0, utc_offset: 0, zone_abbr: "UTC"}
|
||||
{:ok, from_naive_with_period(naive_datetime, "Etc/UTC", utc_period)}
|
||||
end
|
||||
|
||||
def from_naive(%{calendar: Calendar.ISO} = naive_datetime, time_zone, time_zone_database) do
|
||||
case time_zone_database.time_zone_periods_from_wall_datetime(naive_datetime, time_zone) do
|
||||
{:ok, period} ->
|
||||
{:ok, from_naive_with_period(naive_datetime, time_zone, period)}
|
||||
|
||||
{:ambiguous, first_period, second_period} ->
|
||||
first_datetime = from_naive_with_period(naive_datetime, time_zone, first_period)
|
||||
second_datetime = from_naive_with_period(naive_datetime, time_zone, second_period)
|
||||
{:ambiguous, first_datetime, second_datetime}
|
||||
|
||||
{:gap, {first_period, first_period_until_wall}, {second_period, second_period_from_wall}} ->
|
||||
# `until_wall` is not valid, but any time just before is.
|
||||
# So by subtracting a second and adding .999999 seconds
|
||||
# we get the last microsecond just before.
|
||||
before_naive =
|
||||
first_period_until_wall
|
||||
|> Map.put(:microsecond, {999_999, 6})
|
||||
|> NaiveDateTime.add(-1)
|
||||
|
||||
after_naive = second_period_from_wall
|
||||
|
||||
latest_datetime_before = from_naive_with_period(before_naive, time_zone, first_period)
|
||||
first_datetime_after = from_naive_with_period(after_naive, time_zone, second_period)
|
||||
{:gap, latest_datetime_before, first_datetime_after}
|
||||
|
||||
{:error, _} = error ->
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
def from_naive(%{calendar: calendar} = naive_datetime, time_zone, time_zone_database)
|
||||
when calendar != Calendar.ISO do
|
||||
# For non-ISO calendars, convert to ISO, create ISO DateTime, and then
|
||||
# convert to original calendar
|
||||
iso_result =
|
||||
with {:ok, in_iso} <- NaiveDateTime.convert(naive_datetime, Calendar.ISO) do
|
||||
from_naive(in_iso, time_zone, time_zone_database)
|
||||
end
|
||||
|
||||
case iso_result do
|
||||
{:ok, dt} ->
|
||||
convert(dt, calendar)
|
||||
|
||||
{:ambiguous, dt1, dt2} ->
|
||||
with {:ok, dt1converted} <- convert(dt1, calendar),
|
||||
{:ok, dt2converted} <- convert(dt2, calendar),
|
||||
do: {:ambiguous, dt1converted, dt2converted}
|
||||
|
||||
{:gap, dt1, dt2} ->
|
||||
with {:ok, dt1converted} <- convert(dt1, calendar),
|
||||
{:ok, dt2converted} <- convert(dt2, calendar),
|
||||
do: {:gap, dt1converted, dt2converted}
|
||||
|
||||
{:error, _} = error ->
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
defp from_naive_with_period(naive_datetime, time_zone, period) do
|
||||
%{std_offset: std_offset, utc_offset: utc_offset, zone_abbr: zone_abbr} = period
|
||||
@spec from_naive(NaiveDateTime.t(), Calendar.time_zone()) :: {:ok, t}
|
||||
def from_naive(naive_datetime, time_zone)
|
||||
|
||||
def from_naive(%NaiveDateTime{} = naive_datetime, "Etc/UTC") do
|
||||
%{
|
||||
calendar: calendar,
|
||||
hour: hour,
|
||||
@@ -330,7 +202,7 @@ defmodule DateTime do
|
||||
day: day
|
||||
} = naive_datetime
|
||||
|
||||
%DateTime{
|
||||
datetime = %DateTime{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
@@ -339,171 +211,40 @@ defmodule DateTime do
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
std_offset: std_offset,
|
||||
utc_offset: utc_offset,
|
||||
zone_abbr: zone_abbr,
|
||||
time_zone: time_zone
|
||||
std_offset: 0,
|
||||
utc_offset: 0,
|
||||
zone_abbr: "UTC",
|
||||
time_zone: "Etc/UTC"
|
||||
}
|
||||
|
||||
{:ok, datetime}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given `NaiveDateTime` to `DateTime`.
|
||||
|
||||
It expects a time zone to put the NaiveDateTime in.
|
||||
If the time zone is "Etc/UTC", it always succeeds. Otherwise,
|
||||
the NaiveDateTime is checked against the time zone database
|
||||
given as `time_zone_database`. See the "Time zone database"
|
||||
section in the module documentation.
|
||||
Currently it only supports "Etc/UTC" as time zone.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC")
|
||||
~U[2016-05-24 13:26:08.003Z]
|
||||
|
||||
iex> DateTime.from_naive!(~N[2018-05-24 13:26:08.003], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
#DateTime<2018-05-24 13:26:08.003+02:00 CEST Europe/Copenhagen>
|
||||
#DateTime<2016-05-24 13:26:08.003Z>
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec from_naive!(
|
||||
NaiveDateTime.t(),
|
||||
Calendar.time_zone(),
|
||||
Calendar.time_zone_database()
|
||||
) :: t
|
||||
def from_naive!(
|
||||
naive_datetime,
|
||||
time_zone,
|
||||
time_zone_database \\ Calendar.get_time_zone_database()
|
||||
) do
|
||||
case from_naive(naive_datetime, time_zone, time_zone_database) do
|
||||
@spec from_naive!(NaiveDateTime.t(), Calendar.time_zone()) :: t
|
||||
def from_naive!(naive_datetime, time_zone) do
|
||||
case from_naive(naive_datetime, time_zone) do
|
||||
{:ok, datetime} ->
|
||||
datetime
|
||||
|
||||
{:ambiguous, dt1, dt2} ->
|
||||
raise ArgumentError,
|
||||
"cannot convert #{inspect(naive_datetime)} to datetime because such " <>
|
||||
"instant is ambiguous in time zone #{time_zone} as there is an overlap " <>
|
||||
"between #{inspect(dt1)} and #{inspect(dt2)}"
|
||||
|
||||
{:gap, dt1, dt2} ->
|
||||
raise ArgumentError,
|
||||
"cannot convert #{inspect(naive_datetime)} to datetime because such " <>
|
||||
"instant does not exist in time zone #{time_zone} as there is a gap " <>
|
||||
"between #{inspect(dt1)} and #{inspect(dt2)}"
|
||||
|
||||
{:error, reason} ->
|
||||
raise ArgumentError,
|
||||
"cannot convert #{inspect(naive_datetime)} to datetime, reason: #{inspect(reason)}"
|
||||
"cannot parse #{inspect(naive_datetime)} to datetime, reason: #{inspect(reason)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Changes the time zone of a `DateTime`.
|
||||
|
||||
Returns a `DateTime` for the same point in time, but instead at
|
||||
the time zone provided. It assumes that `DateTime` is valid and
|
||||
exists in the given time zone and calendar.
|
||||
|
||||
By default, it uses the default time zone database returned by
|
||||
`Calendar.get_time_zone_database/0`, which defaults to
|
||||
`Calendar.UTCOnlyTimeZoneDatabase` which only handles "Etc/UTC" datetimes.
|
||||
Other time zone databases can be passed as argument or set globally.
|
||||
See the "Time zone database" section in the module docs.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> cph_datetime = DateTime.from_naive!(~N[2018-07-16 12:00:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> {:ok, pacific_datetime} = DateTime.shift_zone(cph_datetime, "America/Los_Angeles", FakeTimeZoneDatabase)
|
||||
iex> pacific_datetime
|
||||
#DateTime<2018-07-16 03:00:00-07:00 PDT America/Los_Angeles>
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec shift_zone(t, Calendar.time_zone(), Calendar.time_zone_database()) ::
|
||||
{:ok, t} | {:error, :time_zone_not_found | :utc_only_time_zone_database}
|
||||
def shift_zone(datetime, time_zone, time_zone_database \\ Calendar.get_time_zone_database())
|
||||
|
||||
def shift_zone(%{time_zone: time_zone} = datetime, time_zone, _) do
|
||||
{:ok, datetime}
|
||||
end
|
||||
|
||||
def shift_zone(datetime, time_zone, time_zone_database) do
|
||||
%{
|
||||
std_offset: std_offset,
|
||||
utc_offset: utc_offset,
|
||||
calendar: calendar,
|
||||
microsecond: {_, precision}
|
||||
} = datetime
|
||||
|
||||
datetime
|
||||
|> to_iso_days()
|
||||
|> apply_tz_offset(utc_offset + std_offset)
|
||||
|> shift_zone_for_iso_days_utc(calendar, precision, time_zone, time_zone_database)
|
||||
end
|
||||
|
||||
defp shift_zone_for_iso_days_utc(iso_days_utc, calendar, precision, time_zone, time_zone_db) do
|
||||
case time_zone_db.time_zone_period_from_utc_iso_days(iso_days_utc, time_zone) do
|
||||
{:ok, %{std_offset: std_offset, utc_offset: utc_offset, zone_abbr: zone_abbr}} ->
|
||||
{year, month, day, hour, minute, second, {microsecond_without_precision, _}} =
|
||||
iso_days_utc
|
||||
|> apply_tz_offset(-(utc_offset + std_offset))
|
||||
|> calendar.naive_datetime_from_iso_days()
|
||||
|
||||
datetime = %DateTime{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: {microsecond_without_precision, precision},
|
||||
std_offset: std_offset,
|
||||
utc_offset: utc_offset,
|
||||
zone_abbr: zone_abbr,
|
||||
time_zone: time_zone
|
||||
}
|
||||
|
||||
{:ok, datetime}
|
||||
|
||||
{:error, _} = error ->
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current datetime in the provided time zone.
|
||||
|
||||
By default, it uses the default time_zone returned by
|
||||
`Calendar.get_time_zone_database/0`, which defaults to
|
||||
`Calendar.UTCOnlyTimeZoneDatabase` which only handles "Etc/UTC" datetimes.
|
||||
Other time zone databases can be passed as argument or set globally.
|
||||
See the "Time zone database" section in the module docs.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, datetime} = DateTime.now("Etc/UTC")
|
||||
iex> datetime.time_zone
|
||||
"Etc/UTC"
|
||||
iex> DateTime.now("Europe/Copenhagen")
|
||||
{:error, :utc_only_time_zone_database}
|
||||
iex> DateTime.now("not a real time zone name", FakeTimeZoneDatabase)
|
||||
{:error, :time_zone_not_found}
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec now(Calendar.time_zone(), Calendar.time_zone_database()) ::
|
||||
{:ok, t} | {:error, :time_zone_not_found | :utc_only_time_zone_database}
|
||||
def now(time_zone, time_zone_database \\ Calendar.get_time_zone_database())
|
||||
|
||||
def now("Etc/UTC", _) do
|
||||
{:ok, utc_now()}
|
||||
end
|
||||
|
||||
def now(time_zone, time_zone_database) do
|
||||
shift_zone(utc_now(), time_zone, time_zone_database)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given `datetime` to Unix time.
|
||||
|
||||
@@ -556,18 +297,19 @@ defmodule DateTime do
|
||||
~N[2000-02-29 23:00:07.0]
|
||||
|
||||
"""
|
||||
@spec to_naive(Calendar.datetime()) :: NaiveDateTime.t()
|
||||
def to_naive(%{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
time_zone: _
|
||||
}) do
|
||||
@spec to_naive(t) :: NaiveDateTime.t()
|
||||
def to_naive(%DateTime{} = datetime) do
|
||||
%DateTime{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond
|
||||
} = datetime
|
||||
|
||||
%NaiveDateTime{
|
||||
year: year,
|
||||
month: month,
|
||||
@@ -595,18 +337,9 @@ defmodule DateTime do
|
||||
~D[2000-02-29]
|
||||
|
||||
"""
|
||||
@spec to_date(Calendar.datetime()) :: Date.t()
|
||||
def to_date(%{
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
calendar: calendar,
|
||||
hour: _,
|
||||
minute: _,
|
||||
second: _,
|
||||
microsecond: _,
|
||||
time_zone: _
|
||||
}) do
|
||||
@spec to_date(t) :: Date.t()
|
||||
def to_date(%DateTime{} = datetime) do
|
||||
%{year: year, month: month, day: day, calendar: calendar} = datetime
|
||||
%Date{year: year, month: month, day: day, calendar: calendar}
|
||||
end
|
||||
|
||||
@@ -625,18 +358,11 @@ defmodule DateTime do
|
||||
~T[23:00:07.0]
|
||||
|
||||
"""
|
||||
@spec to_time(Calendar.datetime()) :: Time.t()
|
||||
def to_time(%{
|
||||
year: _,
|
||||
month: _,
|
||||
day: _,
|
||||
calendar: calendar,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
time_zone: _
|
||||
}) do
|
||||
@spec to_time(t) :: Time.t()
|
||||
def to_time(%DateTime{} = datetime) do
|
||||
%{hour: hour, minute: minute, second: second, microsecond: microsecond, calendar: calendar} =
|
||||
datetime
|
||||
|
||||
%Time{
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
@@ -740,43 +466,48 @@ defmodule DateTime do
|
||||
As specified in the standard, the separator "T" may be omitted if
|
||||
desired as there is no ambiguity within this function.
|
||||
|
||||
The year parsed by this function is limited to four digits and,
|
||||
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
|
||||
hour of the next day, this notation is not supported by Elixir.
|
||||
Note leap seconds are not supported by the built-in Calendar.ISO.
|
||||
Time representations with reduced accuracy are not supported.
|
||||
|
||||
Note that while ISO 8601 allows datetimes to specify 24:00:00 as the
|
||||
zero hour of the next day, this notation is not supported by Elixir.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, datetime, 0} = DateTime.from_iso8601("2015-01-23T23:50:07Z")
|
||||
iex> datetime
|
||||
~U[2015-01-23 23:50:07Z]
|
||||
#DateTime<2015-01-23 23:50:07Z>
|
||||
|
||||
iex> {:ok, datetime, 9000} = DateTime.from_iso8601("2015-01-23T23:50:07.123+02:30")
|
||||
iex> datetime
|
||||
~U[2015-01-23 21:20:07.123Z]
|
||||
#DateTime<2015-01-23 21:20:07.123Z>
|
||||
|
||||
iex> {:ok, datetime, 9000} = DateTime.from_iso8601("2015-01-23T23:50:07,123+02:30")
|
||||
iex> datetime
|
||||
~U[2015-01-23 21:20:07.123Z]
|
||||
#DateTime<2015-01-23 21:20:07.123Z>
|
||||
|
||||
iex> {:ok, datetime, 0} = DateTime.from_iso8601("-2015-01-23T23:50:07Z")
|
||||
iex> datetime
|
||||
~U[-2015-01-23 23:50:07Z]
|
||||
#DateTime<-2015-01-23 23:50:07Z>
|
||||
|
||||
iex> {:ok, datetime, 9000} = DateTime.from_iso8601("-2015-01-23T23:50:07,123+02:30")
|
||||
iex> datetime
|
||||
~U[-2015-01-23 21:20:07.123Z]
|
||||
#DateTime<-2015-01-23 21:20:07.123Z>
|
||||
|
||||
iex> DateTime.from_iso8601("2015-01-23P23:50:07")
|
||||
{:error, :invalid_format}
|
||||
iex> DateTime.from_iso8601("2015-01-23 23:50:07A")
|
||||
{:error, :invalid_format}
|
||||
iex> DateTime.from_iso8601("2015-01-23T23:50:07")
|
||||
{:error, :missing_offset}
|
||||
iex> DateTime.from_iso8601("2015-01-23 23:50:61")
|
||||
{:error, :invalid_time}
|
||||
iex> DateTime.from_iso8601("2015-01-32 23:50:07")
|
||||
{:error, :invalid_date}
|
||||
|
||||
iex> DateTime.from_iso8601("2015-01-23T23:50:07.123-00:00")
|
||||
{:error, :invalid_format}
|
||||
iex> DateTime.from_iso8601("2015-01-23T23:50:07.123-00:60")
|
||||
{:error, :invalid_format}
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@@ -796,14 +527,14 @@ defmodule DateTime do
|
||||
[match_date, guard_date, read_date] = Calendar.ISO.__match_date__()
|
||||
[match_time, guard_time, read_time] = Calendar.ISO.__match_time__()
|
||||
|
||||
defp raw_from_iso8601(string, calendar, is_year_negative) do
|
||||
defp raw_from_iso8601(string, calendar, is_negative_datetime) do
|
||||
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
|
||||
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
|
||||
{microsecond, rest} <- Calendar.ISO.parse_microsecond(rest),
|
||||
{offset, ""} <- Calendar.ISO.parse_offset(rest) do
|
||||
{year, month, day} = unquote(read_date)
|
||||
{hour, minute, second} = unquote(read_time)
|
||||
year = if is_year_negative, do: -year, else: year
|
||||
year = if is_negative_datetime, do: -year, else: year
|
||||
|
||||
cond do
|
||||
not calendar.valid_date?(year, month, day) ->
|
||||
@@ -834,36 +565,22 @@ defmodule DateTime do
|
||||
{:error, :missing_offset}
|
||||
|
||||
true ->
|
||||
day_fraction = Calendar.ISO.time_to_day_fraction(hour, minute, second, {0, 0})
|
||||
{_, precision} = microsecond
|
||||
|
||||
{{year, month, day}, {hour, minute, second, _}} =
|
||||
case apply_tz_offset({0, day_fraction}, offset) do
|
||||
{0, day_fraction} ->
|
||||
{{year, month, day}, Calendar.ISO.time_from_day_fraction(day_fraction)}
|
||||
datetime =
|
||||
Calendar.ISO.naive_datetime_to_iso_days(
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
microsecond
|
||||
)
|
||||
|> apply_tz_offset(offset)
|
||||
|> from_iso_days("Etc/UTC", "UTC", 0, 0, calendar, precision)
|
||||
|
||||
{extra_days, day_fraction} ->
|
||||
base_days = Calendar.ISO.date_to_iso_days(year, month, day)
|
||||
|
||||
{Calendar.ISO.date_from_iso_days(base_days + extra_days),
|
||||
Calendar.ISO.time_from_day_fraction(day_fraction)}
|
||||
end
|
||||
|
||||
datetime = %DateTime{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
std_offset: 0,
|
||||
utc_offset: 0,
|
||||
zone_abbr: "UTC",
|
||||
time_zone: "Etc/UTC"
|
||||
}
|
||||
|
||||
{:ok, datetime, offset}
|
||||
{:ok, %{datetime | microsecond: microsecond}, offset}
|
||||
end
|
||||
else
|
||||
_ -> {:error, :invalid_format}
|
||||
@@ -956,8 +673,8 @@ defmodule DateTime do
|
||||
@doc since: "1.4.0"
|
||||
@spec compare(Calendar.datetime(), Calendar.datetime()) :: :lt | :eq | :gt
|
||||
def compare(
|
||||
%{utc_offset: utc_offset1, std_offset: std_offset1} = datetime1,
|
||||
%{utc_offset: utc_offset2, std_offset: std_offset2} = datetime2
|
||||
%{calendar: _, utc_offset: utc_offset1, std_offset: std_offset1} = datetime1,
|
||||
%{calendar: _, utc_offset: utc_offset2, std_offset: std_offset2} = datetime2
|
||||
) do
|
||||
{days1, {parts1, ppd1}} =
|
||||
datetime1
|
||||
@@ -970,13 +687,13 @@ defmodule DateTime do
|
||||
|> apply_tz_offset(utc_offset2 + std_offset2)
|
||||
|
||||
# Ensure fraction tuples have same denominator.
|
||||
first = {days1, parts1 * ppd2}
|
||||
second = {days2, parts2 * ppd1}
|
||||
iso_days1 = {days1, parts1 * ppd2}
|
||||
iso_days2 = {days2, parts2 * ppd1}
|
||||
|
||||
cond do
|
||||
first > second -> :gt
|
||||
first < second -> :lt
|
||||
true -> :eq
|
||||
case {iso_days1, iso_days2} do
|
||||
{first, second} when first > second -> :gt
|
||||
{first, second} when first < second -> :lt
|
||||
_ -> :eq
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1005,7 +722,7 @@ defmodule DateTime do
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec diff(Calendar.datetime(), Calendar.datetime(), System.time_unit()) :: integer()
|
||||
@spec diff(Calendar.datetime(), Calendar.datetime()) :: integer()
|
||||
def diff(
|
||||
%{utc_offset: utc_offset1, std_offset: std_offset1} = datetime1,
|
||||
%{utc_offset: utc_offset2, std_offset: std_offset2} = datetime2,
|
||||
@@ -1019,80 +736,10 @@ defmodule DateTime do
|
||||
naive_diff + System.convert_time_unit(offset_diff, :second, unit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Adds a specified amount of time to a `DateTime`.
|
||||
|
||||
Accepts an `amount_to_add` in any `unit` available from `t:System.time_unit/0`.
|
||||
Negative values will move backwards in time.
|
||||
|
||||
Takes changes such as summer time/DST into account. This means that adding time
|
||||
can cause the wall time to "go backwards" during "fall back" during autumn.
|
||||
Adding just a few seconds to a datetime just before "spring forward" can cause wall
|
||||
time to increase by more than an hour.
|
||||
|
||||
Fractional second precision stays the same in a similar way to `NaiveDateTime.add/2`.
|
||||
|
||||
### Examples
|
||||
|
||||
iex> dt = DateTime.from_naive!(~N[2018-11-15 10:00:00], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> dt |> DateTime.add(3600, :second, FakeTimeZoneDatabase)
|
||||
#DateTime<2018-11-15 11:00:00+01:00 CET Europe/Copenhagen>
|
||||
|
||||
iex> DateTime.add(~U[2018-11-15 10:00:00Z], 3600, :second)
|
||||
~U[2018-11-15 11:00:00Z]
|
||||
|
||||
When adding 3 seconds just before "spring forward" we go from 1:59:59 to 3:00:02
|
||||
|
||||
iex> dt = DateTime.from_naive!(~N[2019-03-31 01:59:59.123], "Europe/Copenhagen", FakeTimeZoneDatabase)
|
||||
iex> dt |> DateTime.add(3, :second, FakeTimeZoneDatabase)
|
||||
#DateTime<2019-03-31 03:00:02.123+02:00 CEST Europe/Copenhagen>
|
||||
|
||||
"""
|
||||
@doc since: "1.8.0"
|
||||
@spec add(Calendar.datetime(), integer, System.time_unit(), Calendar.time_zone_database()) ::
|
||||
t()
|
||||
def add(
|
||||
datetime,
|
||||
amount_to_add,
|
||||
unit \\ :second,
|
||||
time_zone_database \\ Calendar.get_time_zone_database()
|
||||
)
|
||||
when is_integer(amount_to_add) do
|
||||
%{
|
||||
utc_offset: utc_offset,
|
||||
std_offset: std_offset,
|
||||
calendar: calendar,
|
||||
microsecond: {_, precision}
|
||||
} = datetime
|
||||
|
||||
ppd = System.convert_time_unit(86400, :second, unit)
|
||||
total_offset = System.convert_time_unit(utc_offset + std_offset, :second, unit)
|
||||
|
||||
result =
|
||||
datetime
|
||||
|> to_iso_days()
|
||||
# Subtract total offset in order to get UTC and add the integer for the addition
|
||||
|> Calendar.ISO.add_day_fraction_to_iso_days(amount_to_add - total_offset, ppd)
|
||||
|> shift_zone_for_iso_days_utc(calendar, precision, datetime.time_zone, time_zone_database)
|
||||
|
||||
case result do
|
||||
{:ok, result_datetime} ->
|
||||
result_datetime
|
||||
|
||||
{:error, error} ->
|
||||
raise ArgumentError,
|
||||
"cannot add #{amount_to_add} #{unit} to #{inspect(datetime)} (with time zone " <>
|
||||
"database #{inspect(time_zone_database)}), reason: #{inspect(error)}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the given datetime with the microsecond field truncated to the given
|
||||
precision (`:microsecond`, `millisecond` or `:second`).
|
||||
|
||||
The given datetime is returned unchanged if it already has lower precision than
|
||||
the given precision.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dt1 = %DateTime{year: 2017, month: 11, day: 7, zone_abbr: "CET",
|
||||
@@ -1115,15 +762,11 @@ defmodule DateTime do
|
||||
|
||||
"""
|
||||
@doc since: "1.6.0"
|
||||
@spec truncate(Calendar.datetime(), :microsecond | :millisecond | :second) :: t()
|
||||
@spec truncate(t(), :microsecond | :millisecond | :second) :: t()
|
||||
def truncate(%DateTime{microsecond: microsecond} = datetime, precision) do
|
||||
%{datetime | microsecond: Calendar.truncate(microsecond, precision)}
|
||||
end
|
||||
|
||||
def truncate(%{} = datetime_map, precision) do
|
||||
truncate(from_map(datetime_map), precision)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a given `datetime` from one calendar to another.
|
||||
|
||||
@@ -1151,12 +794,40 @@ defmodule DateTime do
|
||||
@spec convert(Calendar.datetime(), Calendar.calendar()) ::
|
||||
{:ok, t} | {:error, :incompatible_calendars}
|
||||
|
||||
def convert(%DateTime{calendar: calendar} = datetime, calendar) do
|
||||
{:ok, datetime}
|
||||
end
|
||||
# Keep it multiline for proper function clause errors.
|
||||
def convert(
|
||||
%{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
time_zone: time_zone,
|
||||
zone_abbr: zone_abbr,
|
||||
utc_offset: utc_offset,
|
||||
std_offset: std_offset
|
||||
},
|
||||
calendar
|
||||
) do
|
||||
datetime = %DateTime{
|
||||
calendar: calendar,
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
hour: hour,
|
||||
minute: minute,
|
||||
second: second,
|
||||
microsecond: microsecond,
|
||||
time_zone: time_zone,
|
||||
zone_abbr: zone_abbr,
|
||||
utc_offset: utc_offset,
|
||||
std_offset: std_offset
|
||||
}
|
||||
|
||||
def convert(%{calendar: calendar} = datetime, calendar) do
|
||||
{:ok, from_map(datetime)}
|
||||
{:ok, datetime}
|
||||
end
|
||||
|
||||
def convert(%{calendar: dt_calendar, microsecond: {_, precision}} = datetime, calendar) do
|
||||
@@ -1195,7 +866,7 @@ defmodule DateTime do
|
||||
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec convert!(Calendar.datetime(), Calendar.calendar()) :: t
|
||||
@spec convert!(Calendar.datetime(), Calendar.calendar()) :: t | no_return
|
||||
def convert!(datetime, calendar) do
|
||||
case convert(datetime, calendar) do
|
||||
{:ok, value} ->
|
||||
@@ -1227,6 +898,10 @@ defmodule DateTime do
|
||||
%{time_zone: time_zone, zone_abbr: zone_abbr, utc_offset: utc_offset, std_offset: std_offset} =
|
||||
datetime
|
||||
|
||||
from_iso_days(iso_days, time_zone, zone_abbr, utc_offset, std_offset, calendar, precision)
|
||||
end
|
||||
|
||||
defp from_iso_days(iso_days, time_zone, zone_abbr, utc_offset, std_offset, calendar, precision) do
|
||||
{year, month, day, hour, minute, second, {microsecond, _}} =
|
||||
calendar.naive_datetime_from_iso_days(iso_days)
|
||||
|
||||
@@ -1246,30 +921,10 @@ defmodule DateTime do
|
||||
}
|
||||
end
|
||||
|
||||
defp apply_tz_offset(iso_days, 0) do
|
||||
iso_days
|
||||
end
|
||||
|
||||
defp apply_tz_offset(iso_days, offset) do
|
||||
Calendar.ISO.add_day_fraction_to_iso_days(iso_days, -offset, 86400)
|
||||
end
|
||||
|
||||
defp from_map(%{} = datetime_map) do
|
||||
%DateTime{
|
||||
year: datetime_map.year,
|
||||
month: datetime_map.month,
|
||||
day: datetime_map.day,
|
||||
hour: datetime_map.hour,
|
||||
minute: datetime_map.minute,
|
||||
second: datetime_map.second,
|
||||
microsecond: datetime_map.microsecond,
|
||||
time_zone: datetime_map.time_zone,
|
||||
zone_abbr: datetime_map.zone_abbr,
|
||||
utc_offset: datetime_map.utc_offset,
|
||||
std_offset: datetime_map.std_offset
|
||||
}
|
||||
end
|
||||
|
||||
defimpl String.Chars do
|
||||
def to_string(datetime) do
|
||||
%{
|
||||
@@ -1319,7 +974,7 @@ defmodule DateTime do
|
||||
std_offset: std_offset
|
||||
} = datetime
|
||||
|
||||
formatted =
|
||||
"#DateTime<" <>
|
||||
Calendar.ISO.datetime_to_string(
|
||||
year,
|
||||
month,
|
||||
@@ -1332,15 +987,7 @@ defmodule DateTime do
|
||||
zone_abbr,
|
||||
utc_offset,
|
||||
std_offset
|
||||
)
|
||||
|
||||
case datetime do
|
||||
%{utc_offset: 0, std_offset: 0, time_zone: "Etc/UTC"} ->
|
||||
"~U[" <> formatted <> "]"
|
||||
|
||||
_ ->
|
||||
"#DateTime<" <> formatted <> ">"
|
||||
end
|
||||
) <> ">"
|
||||
end
|
||||
|
||||
def inspect(datetime, opts) do
|
||||
|
||||
+61
-241
@@ -26,7 +26,7 @@ defmodule Calendar.ISO do
|
||||
|
||||
@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
|
||||
@@ -37,11 +37,6 @@ defmodule Calendar.ISO do
|
||||
|
||||
@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
|
||||
@@ -180,12 +175,8 @@ defmodule Calendar.ISO do
|
||||
@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,13 +188,9 @@ 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
|
||||
@doc since: "1.5.0"
|
||||
def date_to_iso_days(0, 1, 1) do
|
||||
0
|
||||
end
|
||||
@@ -213,7 +200,7 @@ defmodule Calendar.ISO do
|
||||
end
|
||||
|
||||
def date_to_iso_days(year, month, day) when year in -9999..9999 do
|
||||
ensure_day_in_month!(year, month, day)
|
||||
true = day <= days_in_month(year, month)
|
||||
|
||||
days_in_previous_years(year) + days_before_month(month) + leap_day_offset(year, month) + day -
|
||||
1
|
||||
@@ -260,7 +247,6 @@ defmodule Calendar.ISO do
|
||||
31
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec days_in_month(year, month) :: 28..31
|
||||
@impl true
|
||||
def days_in_month(year, month)
|
||||
@@ -305,7 +291,6 @@ defmodule Calendar.ISO do
|
||||
true
|
||||
|
||||
"""
|
||||
@doc since: "1.3.0"
|
||||
@spec leap_year?(year) :: boolean()
|
||||
@impl true
|
||||
def leap_year?(year) when is_integer(year) do
|
||||
@@ -337,137 +322,16 @@ defmodule Calendar.ISO do
|
||||
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})
|
||||
@@ -477,29 +341,23 @@ defmodule Calendar.ISO do
|
||||
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
|
||||
Calendar.microsecond()
|
||||
) :: String.t()
|
||||
def time_to_string(hour, minute, second, microsecond, format \\ :extended)
|
||||
@impl true
|
||||
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
|
||||
@@ -515,10 +373,6 @@ 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)
|
||||
@@ -528,32 +382,24 @@ defmodule Calendar.ISO do
|
||||
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()
|
||||
@spec date_to_string(year, month, day) :: 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})
|
||||
@@ -561,11 +407,7 @@ defmodule Calendar.ISO do
|
||||
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,
|
||||
@@ -574,51 +416,27 @@ defmodule Calendar.ISO do
|
||||
Calendar.hour(),
|
||||
Calendar.minute(),
|
||||
Calendar.second(),
|
||||
Calendar.microsecond(),
|
||||
:basic | :extended
|
||||
Calendar.microsecond()
|
||||
) :: 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)
|
||||
iex> Calendar.ISO.datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5}, "Europe/Berlin", "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)
|
||||
iex> Calendar.ISO.datetime_to_string(2017, 8, 1, 1, 2, 3, {4, 5}, "Europe/Berlin", "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)
|
||||
iex> Calendar.ISO.datetime_to_string(2015, 2, 28, 1, 2, 3, {4, 5}, "America/Los_Angeles", "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)
|
||||
iex> Calendar.ISO.datetime_to_string(2015, 2, 28, 1, 2, 3, {4, 5}, "America/Los_Angeles", "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"
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@impl true
|
||||
@spec datetime_to_string(
|
||||
year,
|
||||
@@ -631,8 +449,7 @@ defmodule Calendar.ISO do
|
||||
Calendar.time_zone(),
|
||||
Calendar.zone_abbr(),
|
||||
Calendar.utc_offset(),
|
||||
Calendar.std_offset(),
|
||||
:basic | :extended
|
||||
Calendar.std_offset()
|
||||
) :: String.t()
|
||||
def datetime_to_string(
|
||||
year,
|
||||
@@ -645,14 +462,12 @@ 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
|
||||
|
||||
@@ -681,32 +496,30 @@ defmodule Calendar.ISO do
|
||||
|
||||
@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.
|
||||
Note that leap seconds are considered valid, but the use of 24:00:00 as the
|
||||
zero hour of the day is considered invalid.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.valid_time?(10, 50, 25, {3006, 6})
|
||||
true
|
||||
iex> Calendar.ISO.valid_time?(23, 59, 60, {0, 0})
|
||||
false
|
||||
true
|
||||
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()) ::
|
||||
@spec valid_time?(Calendar.hour(), Calendar.minute(), Calendar.secon(), 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.
|
||||
See `c:Calendar.day_rollover_relative_to_midlight_utc/0` for documentation.
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@impl true
|
||||
@@ -715,6 +528,7 @@ defmodule Calendar.ISO 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
|
||||
@@ -741,7 +555,7 @@ defmodule Calendar.ISO 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
|
||||
:binary.copy("0", count - byte_size(num)) <> num
|
||||
end
|
||||
|
||||
defp zero_pad(val, count) do
|
||||
@@ -766,15 +580,25 @@ 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
|
||||
@doc since: "1.5.0"
|
||||
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
|
||||
@@ -872,13 +696,15 @@ defmodule Calendar.ISO do
|
||||
end
|
||||
|
||||
@doc false
|
||||
@doc since: "1.5.0"
|
||||
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
|
||||
|
||||
@doc false
|
||||
@doc since: "1.5.0"
|
||||
def add_day_fraction_to_iso_days({days, {parts, ppd}}, add, ppd) do
|
||||
normalize_iso_days(days, parts + add, ppd)
|
||||
end
|
||||
@@ -1036,10 +862,4 @@ defmodule Calendar.ISO do
|
||||
|
||||
{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
|
||||
|
||||
@@ -5,7 +5,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:
|
||||
[`~N`](`Kernel.sigil_N/2`) sigil:
|
||||
|
||||
iex> ~N[2000-01-01 23:00:07]
|
||||
~N[2000-01-01 23:00:07]
|
||||
@@ -28,9 +28,14 @@ defmodule NaiveDateTime do
|
||||
`NaiveDateTime` is not validated against a time zone, such errors
|
||||
would go unnoticed.
|
||||
|
||||
The functions of 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.
|
||||
as the ones in 3rd party calendar libraries.
|
||||
|
||||
## Comparing naive date times
|
||||
|
||||
@@ -67,7 +72,7 @@ defmodule NaiveDateTime do
|
||||
calendar: Calendar.ISO
|
||||
]
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
@type t :: %NaiveDateTime{
|
||||
year: Calendar.year(),
|
||||
month: Calendar.month(),
|
||||
day: Calendar.day(),
|
||||
@@ -141,11 +146,13 @@ 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}
|
||||
@@ -228,9 +235,11 @@ 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`.
|
||||
Accepts an `integer` in any `unit` available from `t:System.time_unit/0`.
|
||||
Negative values will move backwards in time.
|
||||
|
||||
This operation is only possible if both calendars are convertible to `Calendar.ISO`.
|
||||
|
||||
## Examples
|
||||
|
||||
# adds seconds by default
|
||||
@@ -258,29 +267,17 @@ defmodule NaiveDateTime do
|
||||
iex> NaiveDateTime.add(~N[0000-01-01 00:00:00], 63_579_428_950)
|
||||
~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
|
||||
|
||||
@@ -311,13 +308,13 @@ defmodule NaiveDateTime do
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec diff(Calendar.naive_datetime(), Calendar.naive_datetime(), System.time_unit()) :: integer
|
||||
@spec diff(t, t, System.time_unit()) :: integer
|
||||
def diff(
|
||||
%{calendar: calendar1} = naive_datetime1,
|
||||
%{calendar: calendar2} = naive_datetime2,
|
||||
%NaiveDateTime{} = naive_datetime1,
|
||||
%NaiveDateTime{} = naive_datetime2,
|
||||
unit \\ :second
|
||||
) do
|
||||
if not Calendar.compatible_calendars?(calendar1, calendar2) do
|
||||
if not Calendar.compatible_calendars?(naive_datetime1.calendar, naive_datetime2.calendar) do
|
||||
raise ArgumentError,
|
||||
"cannot calculate the difference between #{inspect(naive_datetime1)} and " <>
|
||||
"#{inspect(naive_datetime2)} because their calendars are not compatible " <>
|
||||
@@ -333,9 +330,6 @@ defmodule NaiveDateTime do
|
||||
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.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> NaiveDateTime.truncate(~N[2017-11-06 00:23:51.123456], :microsecond)
|
||||
@@ -354,31 +348,6 @@ defmodule NaiveDateTime 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)
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a `NaiveDateTime` into a `Date`.
|
||||
|
||||
@@ -391,17 +360,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 +377,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,
|
||||
@@ -485,10 +444,10 @@ defmodule NaiveDateTime do
|
||||
As specified in the standard, the separator "T" may be omitted if
|
||||
desired as there is no ambiguity within this function.
|
||||
|
||||
The year parsed by this function is limited to four digits and,
|
||||
while ISO 8601 allows datetimes to specify 24:00:00 as the zero
|
||||
hour of the next day, this notation is not supported by Elixir.
|
||||
Note leap seconds are not supported by the built-in Calendar.ISO.
|
||||
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
|
||||
|
||||
@@ -558,8 +517,8 @@ defmodule NaiveDateTime 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)
|
||||
with {:ok, utc_date} <- new(year, month, day, hour, min, sec, microsec, Calendar.ISO) do
|
||||
convert(utc_date, calendar)
|
||||
end
|
||||
else
|
||||
_ -> {:error, :invalid_format}
|
||||
@@ -582,7 +541,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} ->
|
||||
@@ -712,8 +671,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 +691,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} ->
|
||||
|
||||
@@ -4,7 +4,7 @@ defmodule Time do
|
||||
|
||||
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:
|
||||
[`~T`](`Kernel.sigil_T/2`) sigil:
|
||||
|
||||
iex> ~T[23:00:07.001]
|
||||
~T[23:00:07.001]
|
||||
@@ -25,7 +25,7 @@ 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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -80,10 +78,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 +88,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}
|
||||
@@ -184,7 +187,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
|
||||
|
||||
@@ -301,7 +303,7 @@ defmodule Time do
|
||||
microsecond: microsecond
|
||||
} = time
|
||||
|
||||
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
|
||||
Calendar.ISO.time_to_iso8601(hour, minute, second, microsecond, format)
|
||||
end
|
||||
|
||||
def to_iso8601(%{calendar: _} = time, format) when format in [:extended, :basic] do
|
||||
@@ -403,9 +405,12 @@ defmodule Time do
|
||||
@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 +421,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.
|
||||
|
||||
@@ -607,33 +597,7 @@ defmodule Time do
|
||||
"""
|
||||
@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 +609,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)
|
||||
|
||||
@@ -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
|
||||
+59
-105
@@ -26,18 +26,10 @@ defmodule Code do
|
||||
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
|
||||
file, without tracking. `eval_file/2` should be used when you are intested on
|
||||
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.
|
||||
|
||||
@@ -54,7 +46,7 @@ defmodule Code do
|
||||
:elixir_code_server.call(:required)
|
||||
end
|
||||
|
||||
# TODO: Deprecate on v1.9
|
||||
# TODO: Deprecate me on 1.9
|
||||
@doc false
|
||||
def loaded_files do
|
||||
required_files()
|
||||
@@ -86,7 +78,7 @@ defmodule Code do
|
||||
:elixir_code_server.cast({:unrequire_files, files})
|
||||
end
|
||||
|
||||
# TODO: Deprecate on v1.9
|
||||
# TODO: Deprecate me on 1.9
|
||||
@doc false
|
||||
def unload_files(files) do
|
||||
unrequire_files(files)
|
||||
@@ -272,13 +264,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.
|
||||
@@ -424,14 +409,14 @@ 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.
|
||||
### 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
|
||||
@@ -665,12 +650,6 @@ 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`.
|
||||
@@ -680,37 +659,6 @@ defmodule Code do
|
||||
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}}
|
||||
@@ -749,7 +697,7 @@ 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
|
||||
While `require_file/2` and `compile_file/2` returns the loaded modules and their
|
||||
bytecode, `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`).
|
||||
"""
|
||||
@@ -759,12 +707,12 @@ defmodule Code do
|
||||
eval_string(File.read!(file), [], file: file, line: 1)
|
||||
end
|
||||
|
||||
# TODO: Deprecate on v1.9
|
||||
# TODO: Deprecate me on 1.9
|
||||
@doc false
|
||||
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)
|
||||
loaded = :elixir_compiler.file(file)
|
||||
:elixir_code_server.cast({:required, file})
|
||||
loaded
|
||||
end
|
||||
@@ -784,7 +732,7 @@ defmodule Code do
|
||||
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
|
||||
filenames. Finally, if you would like to get the result of evaluating file rather
|
||||
than the modules defined in it, see `eval_file/2`.
|
||||
|
||||
## Examples
|
||||
@@ -795,7 +743,7 @@ defmodule Code do
|
||||
List.first(modules)
|
||||
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
|
||||
|
||||
If the file has been required, it returns `nil`:
|
||||
If the code has been required, it returns `nil`:
|
||||
|
||||
Code.require_file("eex_test.exs", "../eex/test")
|
||||
#=> nil
|
||||
@@ -805,12 +753,18 @@ defmodule Code do
|
||||
def require_file(file, relative_to \\ nil) when is_binary(file) do
|
||||
file = find_file(file, relative_to)
|
||||
|
||||
# TODO: Simply block until :required or :proceed once load_file is removed in 2.0
|
||||
case :elixir_code_server.call({:acquire, file}) do
|
||||
:required ->
|
||||
nil
|
||||
|
||||
{:queued, ref} ->
|
||||
receive do
|
||||
{:elixir_code_server, ^ref, :required} -> nil
|
||||
end
|
||||
|
||||
:proceed ->
|
||||
loaded = :elixir_compiler.file(file, fn _, _ -> :ok end)
|
||||
loaded = :elixir_compiler.file(file)
|
||||
:elixir_code_server.cast({:required, file})
|
||||
loaded
|
||||
end
|
||||
@@ -824,7 +778,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,17 +790,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
|
||||
[:docs, :debug_info, :ignore_module_conflict, :relative_paths, :warnings_as_errors]
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -854,11 +809,11 @@ defmodule Code do
|
||||
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
|
||||
may be unable to reclaim those modules, keeping an unecessary 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
|
||||
old compiler module names to be resued. If there are any processes running
|
||||
any code from such modules, they will be terminated too.
|
||||
|
||||
It returns `{:ok, number_of_modules_purged}`.
|
||||
@@ -904,14 +859,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))
|
||||
@@ -933,7 +893,7 @@ defmodule Code do
|
||||
"""
|
||||
@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,7 +906,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)
|
||||
:elixir_compiler.quoted(quoted, file)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -963,10 +923,9 @@ defmodule Code do
|
||||
|
||||
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.file(find_file(file, relative_to))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1055,9 +1014,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 +1022,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,7 +1047,7 @@ 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.
|
||||
|
||||
@@ -1118,20 +1072,19 @@ defmodule Code do
|
||||
"""
|
||||
@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]}
|
||||
{:docs_v1, anno, beam_language, format, module_doc :: doc, metadata,
|
||||
docs :: [{{kind, name, arity}, anno, signature, doc, metadata}]}
|
||||
| {: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},
|
||||
| future_formats
|
||||
when anno: :erl_anno.anno(),
|
||||
beam_language: atom,
|
||||
format: binary,
|
||||
doc: %{binary => binary} | :none | :hidden,
|
||||
kind: atom,
|
||||
name: atom,
|
||||
signature: [binary],
|
||||
metadata: map
|
||||
def fetch_docs(module_or_path)
|
||||
|
||||
metadata: map,
|
||||
future_formats: term
|
||||
def fetch_docs(module) when is_atom(module) do
|
||||
case :code.get_object_code(module) do
|
||||
{_module, bin, _beam_path} -> do_fetch_docs(bin)
|
||||
@@ -1139,8 +1092,8 @@ defmodule Code do
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_docs(path) when is_binary(path) do
|
||||
do_fetch_docs(String.to_charlist(path))
|
||||
def fetch_docs(binpath) when is_binary(binpath) do
|
||||
do_fetch_docs(String.to_charlist(binpath))
|
||||
end
|
||||
|
||||
@docs_chunk 'Docs'
|
||||
@@ -1168,7 +1121,8 @@ defmodule Code do
|
||||
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"
|
||||
@doc 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
|
||||
|
||||
@@ -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.
|
||||
@@ -238,8 +242,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 +255,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 +367,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
|
||||
|
||||
@@ -774,16 +779,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 +793,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 +884,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 +1055,10 @@ 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 +1075,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 +1093,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 +1109,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)
|
||||
@@ -1151,7 +1145,7 @@ defmodule Code.Formatter do
|
||||
end
|
||||
|
||||
args = if keyword?, do: left ++ right, else: left ++ [right]
|
||||
many_eol? = match?([_, _ | _], args) and eol?(meta)
|
||||
many_eol? = match?([_, _ | _], args) and Keyword.get(meta, :eol, false)
|
||||
no_generators? = no_generators?(args)
|
||||
to_algebra_fun = "ed_to_algebra(&1, context, &2)
|
||||
|
||||
@@ -1162,7 +1156,7 @@ defmodule Code.Formatter do
|
||||
{left_doc, _join, state} =
|
||||
args_to_algebra_with_comments(
|
||||
left,
|
||||
Keyword.delete(meta, :closing),
|
||||
Keyword.delete(meta, :end_line),
|
||||
skip_parens?,
|
||||
:force_comma,
|
||||
join,
|
||||
@@ -1269,19 +1263,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 +1301,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 +1317,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 +1324,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 +1337,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 +1372,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 +1393,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 +1440,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 +1450,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 +1467,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 +1478,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 +1761,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 +1819,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
|
||||
@@ -2025,7 +1991,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 +2097,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 +2131,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 +2207,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}
|
||||
@@ -60,13 +60,9 @@ defmodule Code.Identifier do
|
||||
* `: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
|
||||
@@ -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 ->
|
||||
|
||||
@@ -18,7 +18,7 @@ defmodule Code.Typespec do
|
||||
uniq: true,
|
||||
do: {var, {:var, meta, nil}}
|
||||
|
||||
spec = {:"::", meta, [body, typespec_to_quoted(result)]}
|
||||
spec = {:::, meta, [body, typespec_to_quoted(result)]}
|
||||
|
||||
if vars == [] do
|
||||
spec
|
||||
@@ -28,7 +28,7 @@ defmodule Code.Typespec do
|
||||
end
|
||||
|
||||
def spec_to_quoted(name, {:type, line, :fun, []}) when is_atom(name) do
|
||||
{:"::", [line: line], [{name, [line: line], []}, quote(do: term)]}
|
||||
{:::, [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
|
||||
@@ -52,7 +52,7 @@ defmodule Code.Typespec do
|
||||
args = for arg <- args, do: typespec_to_quoted(arg)
|
||||
|
||||
when_args = [
|
||||
{:"::", meta, [{name, [line: line], args}, typespec_to_quoted(result)]},
|
||||
{:::, meta, [{name, [line: line], args}, typespec_to_quoted(result)]},
|
||||
guards ++ vars
|
||||
]
|
||||
|
||||
@@ -152,23 +152,37 @@ defmodule Code.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: Do not rely on abstract_code when OTP 20+ support is dropped (v1.8).
|
||||
# We should then be able to simplify this code and use `with`.
|
||||
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 get_module_and_beam(module) do
|
||||
{module, binary} ->
|
||||
case :beam_lib.chunks(binary, [:debug_info]) do
|
||||
{:ok, {_, [debug_info: {:debug_info_v1, backend, data}]}} ->
|
||||
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
|
||||
_ ->
|
||||
case backend.debug_info(:erlang_v1, module, data, []) do
|
||||
{:ok, abstract_code} -> {:ok, abstract_code}
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
_ ->
|
||||
case :beam_lib.chunks(binary, [:abstract_code]) do
|
||||
{:ok, {_, [{:abstract_code, {_raw_abstract_v1, abstract_code}}]}} ->
|
||||
{:ok, abstract_code}
|
||||
|
||||
_ ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
@@ -329,7 +343,7 @@ defmodule Code.Typespec do
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:var, line, var}) do
|
||||
{erl_to_ex_var(var), [line: line], nil}
|
||||
{erl_to_ex_var(var), line, nil}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:op, line, op, arg}) do
|
||||
@@ -341,7 +355,7 @@ defmodule Code.Typespec do
|
||||
end
|
||||
|
||||
defp typespec_to_quoted({:ann_type, line, [var, type]}) do
|
||||
{:"::", [line: line], [typespec_to_quoted(var), typespec_to_quoted(type)]}
|
||||
{:::, [line: line], [typespec_to_quoted(var), typespec_to_quoted(type)]}
|
||||
end
|
||||
|
||||
defp typespec_to_quoted(
|
||||
|
||||
@@ -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
|
||||
@@ -18,6 +18,8 @@ defmodule Dict do
|
||||
message =
|
||||
"Use the Map module for working with maps or the Keyword module for working with keyword lists"
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
@deprecated message
|
||||
defmacro __using__(_) do
|
||||
# Use this import to guarantee proper code expansion
|
||||
@@ -293,7 +295,7 @@ defmodule Dict do
|
||||
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
|
||||
|
||||
@@ -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
|
||||
@@ -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,
|
||||
@@ -211,14 +208,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__,
|
||||
@@ -282,8 +277,8 @@ defmodule DynamicSupervisor do
|
||||
"""
|
||||
@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 """
|
||||
@@ -302,15 +297,15 @@ 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)
|
||||
@@ -405,7 +400,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
|
||||
@@ -467,13 +462,13 @@ defmodule DynamicSupervisor do
|
||||
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 +482,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
|
||||
@@ -532,11 +527,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 +540,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}
|
||||
@@ -750,8 +745,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}
|
||||
|
||||
+198
-242
File diff suppressed because it is too large
Load Diff
+11
-86
@@ -150,7 +150,7 @@ 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, parses beam files, evaluates code and
|
||||
so on.
|
||||
|
||||
If the exception module implements the optional `c:blame/2`
|
||||
@@ -186,6 +186,8 @@ 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]) ::
|
||||
@@ -208,7 +210,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.scope(meta)
|
||||
|
||||
{erl_args, scope} =
|
||||
:elixir_erl_clauses.match(&:elixir_erl_pass.translate_args/2, ex_args, scope)
|
||||
@@ -788,10 +790,6 @@ 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
|
||||
@@ -856,7 +854,7 @@ defmodule CondClauseError do
|
||||
|
||||
@impl true
|
||||
def message(_exception) do
|
||||
"no cond clause evaluated to a truthy value"
|
||||
"no cond clause evaluated to a true value"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -946,8 +944,7 @@ defmodule UndefinedFunctionError do
|
||||
end
|
||||
|
||||
defp hint(module, function, arity, true) do
|
||||
behaviour_hint(module, function, arity) <>
|
||||
hint_for_loaded_module(module, function, arity, nil)
|
||||
hint_for_loaded_module(module, function, arity, nil)
|
||||
end
|
||||
|
||||
defp hint(_module, _function, _arity, _loaded?) do
|
||||
@@ -973,10 +970,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 +994,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,18 +1005,6 @@ 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
|
||||
@@ -1148,23 +1105,10 @@ defmodule Protocol.UndefinedError do
|
||||
|
||||
@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 +1118,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 ->
|
||||
""
|
||||
@@ -1190,7 +1133,7 @@ defmodule KeyError do
|
||||
def message(exception = %{message: nil}), do: message(exception.key, exception.term)
|
||||
def message(%{message: message}), do: message
|
||||
|
||||
defp message(key, term) do
|
||||
def message(key, term) do
|
||||
message = "key #{inspect(key)} not found"
|
||||
|
||||
if term != nil do
|
||||
@@ -1319,24 +1262,6 @@ defmodule File.CopyError do
|
||||
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))
|
||||
|
||||
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.LinkError do
|
||||
defexception [:reason, :existing, :new, action: ""]
|
||||
|
||||
|
||||
+133
-233
@@ -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
|
||||
|
||||
@@ -114,35 +114,20 @@ defmodule File do
|
||||
| {:read_ahead, pos_integer | false}
|
||||
| {: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 +136,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 +150,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 +177,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 +205,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 +268,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 +306,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 +339,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 +354,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 +390,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 +405,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} ->
|
||||
@@ -485,11 +447,11 @@ 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 +463,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 +473,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 +494,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
|
||||
@@ -615,11 +543,12 @@ defmodule File do
|
||||
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
|
||||
@spec ln!(Path.t(), Path.t()) :: :ok | no_return
|
||||
def ln!(existing, new) do
|
||||
case ln(existing, new) do
|
||||
:ok ->
|
||||
@@ -648,10 +577,11 @@ defmodule File do
|
||||
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
|
||||
@spec ln_s!(Path.t(), Path.t()) :: :ok | no_return
|
||||
def ln_s!(existing, new) do
|
||||
case ln_s(existing, new) do
|
||||
:ok ->
|
||||
@@ -695,11 +625,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,17 +652,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}
|
||||
@@ -741,54 +671,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 +707,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 +720,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 +749,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 +787,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 +806,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 +939,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 +1018,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 ->
|
||||
@@ -1153,8 +1055,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 +1082,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 +1145,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 +1176,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 +1301,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 +1324,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 +1343,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 +1386,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 +1410,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 +1461,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} ->
|
||||
@@ -1634,6 +1536,7 @@ 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
|
||||
@@ -1648,7 +1551,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 +1576,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 +1593,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 +1603,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 +1620,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 +1630,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 +1669,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
|
||||
|
||||
+28
-44
@@ -36,7 +36,7 @@ defmodule Float do
|
||||
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/)
|
||||
* [What Every Programmer Should Know About Floating-Point Arithmetic](http://floating-point-gui.de/)
|
||||
|
||||
"""
|
||||
|
||||
@@ -149,10 +149,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 +192,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 +248,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 +296,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 +373,6 @@ defmodule Float do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.ratio(0.0)
|
||||
{0, 1}
|
||||
iex> Float.ratio(3.14)
|
||||
{7070651414971679, 2251799813685248}
|
||||
iex> Float.ratio(-3.14)
|
||||
@@ -399,35 +388,27 @@ defmodule Float do
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec ratio(float) :: {integer, pos_integer}
|
||||
def ratio(0.0), do: {0, 1}
|
||||
|
||||
@spec ratio(float) :: {pos_integer | neg_integer, pos_integer}
|
||||
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 +423,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}
|
||||
@@ -493,16 +474,19 @@ defmodule Float do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use Float.to_charlist/1 instead"
|
||||
def to_char_list(float), do: Float.to_charlist(float)
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use :erlang.float_to_list/2 instead"
|
||||
def to_char_list(float, options) do
|
||||
:erlang.float_to_list(float, expand_compact(options))
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use :erlang.float_to_binary/2 instead"
|
||||
def to_string(float, options) do
|
||||
:erlang.float_to_binary(float, expand_compact(options))
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
defmodule GenEvent do
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
# Functions from this module are deprecated in elixir_dispatch.
|
||||
|
||||
@moduledoc """
|
||||
@@ -88,10 +90,12 @@ defmodule GenEvent do
|
||||
@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
|
||||
@@ -324,7 +328,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
|
||||
|
||||
+94
-180
@@ -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
|
||||
@@ -34,8 +34,8 @@ defmodule GenServer do
|
||||
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
|
||||
|
||||
@@ -54,16 +54,14 @@ defmodule GenServer do
|
||||
|
||||
We start our `Stack` by calling `start_link/2`, 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
|
||||
|
||||
@@ -83,8 +81,8 @@ defmodule GenServer do
|
||||
GenServer.start_link(__MODULE__, default)
|
||||
end
|
||||
|
||||
def push(pid, element) do
|
||||
GenServer.cast(pid, {:push, element})
|
||||
def push(pid, item) do
|
||||
GenServer.cast(pid, {:push, item})
|
||||
end
|
||||
|
||||
def pop(pid) do
|
||||
@@ -104,8 +102,8 @@ defmodule GenServer do
|
||||
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
|
||||
|
||||
@@ -113,45 +111,25 @@ defmodule GenServer do
|
||||
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
|
||||
## use GenServer and callbacks
|
||||
|
||||
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:
|
||||
There are 7 callbacks to be implemented when you use a `GenServer`.
|
||||
The only required callback is `init/1`.
|
||||
|
||||
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:
|
||||
`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:
|
||||
|
||||
* `:id` - the child specification identifier, defaults to the current module
|
||||
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
|
||||
* `:restart` - when the 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
|
||||
|
||||
@@ -180,8 +158,7 @@ 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
|
||||
@@ -218,51 +195,28 @@ 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
|
||||
@@ -346,48 +300,29 @@ 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
|
||||
|
||||
@@ -395,10 +330,10 @@ defmodule GenServer do
|
||||
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)
|
||||
|
||||
"""
|
||||
|
||||
@@ -406,14 +341,14 @@ defmodule GenServer do
|
||||
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
|
||||
@@ -443,7 +378,7 @@ 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}}
|
||||
| :ignore
|
||||
@@ -462,8 +397,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
|
||||
@@ -515,7 +450,7 @@ defmodule GenServer do
|
||||
{:reply, reply, new_state}
|
||||
| {:reply, reply, new_state, timeout | :hibernate | {:continue, term}}
|
||||
| {:noreply, new_state}
|
||||
| {:noreply, new_state, timeout | :hibernate | {:continue, term}}
|
||||
| {:noreply, new_state, timeout | :hibernate, {:continue, term}}
|
||||
| {:stop, reason, reply, new_state}
|
||||
| {:stop, reason, new_state}
|
||||
when reply: term, new_state: term, reason: term
|
||||
@@ -528,9 +463,9 @@ 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
|
||||
@@ -605,20 +540,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
|
||||
@@ -634,7 +562,7 @@ defmodule GenServer do
|
||||
receiving a `GenServer`'s exit signal and do not need to be closed manually
|
||||
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.
|
||||
@@ -666,7 +594,8 @@ defmodule GenServer do
|
||||
@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.
|
||||
@@ -711,17 +640,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 +660,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 +676,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
|
||||
@@ -830,20 +750,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 +777,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.
|
||||
|
||||
@@ -880,10 +800,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 +813,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,23 +823,23 @@ 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, """
|
||||
@@ -992,8 +908,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 +938,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 +1068,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 """
|
||||
|
||||
@@ -7,6 +7,8 @@ defmodule HashDict do
|
||||
|
||||
@moduledoc deprecated: "Use Map instead"
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
use Dict
|
||||
|
||||
@node_bitmap 0b111
|
||||
|
||||
+10
-90
@@ -20,7 +20,7 @@ defprotocol Inspect do
|
||||
## 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 +31,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 +39,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 +48,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 +142,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 +289,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'
|
||||
@@ -408,38 +372,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 +381,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,29 +13,24 @@ 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. If you don't want to limit
|
||||
the number of items to a particular number, use `:infinity`.
|
||||
|
||||
* `: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. If you don't want to limit the number of items
|
||||
to a particular number, use `:infinity`.
|
||||
|
||||
* `:pretty` - if set to `true` enables pretty printing, defaults to `false`.
|
||||
|
||||
@@ -51,24 +46,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 +67,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 +83,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
|
||||
|
||||
@@ -164,7 +148,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
|
||||
@@ -273,10 +257,10 @@ 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 ->
|
||||
# Because we try to raise a nice error message in case
|
||||
@@ -315,8 +299,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 +312,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,22 +324,18 @@ 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> 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> 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> 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! ...]"
|
||||
|
||||
@@ -363,7 +343,7 @@ defmodule Inspect.Algebra do
|
||||
@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 +355,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 +407,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,
|
||||
@@ -700,7 +680,7 @@ 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"
|
||||
@@ -755,24 +735,22 @@ 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)
|
||||
@@ -809,8 +787,7 @@ defmodule Inspect.Algebra do
|
||||
...> Inspect.Algebra.concat(
|
||||
...> "Hughes",
|
||||
...> Inspect.Algebra.line()
|
||||
...> ),
|
||||
...> "Wadler"
|
||||
...> ), "Wadler"
|
||||
...> )
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["Hughes", "\n", "Wadler"]
|
||||
@@ -823,7 +800,7 @@ defmodule Inspect.Algebra do
|
||||
@doc ~S"""
|
||||
Inserts a mandatory linebreak between two documents.
|
||||
|
||||
See `line/0`.
|
||||
See `line/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -845,10 +822,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 +872,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.
|
||||
|
||||
@@ -4,7 +4,7 @@ defmodule Integer do
|
||||
|
||||
Some functions that work on integers are found in `Kernel`:
|
||||
|
||||
* `abs/1`
|
||||
* `abs/2`
|
||||
* `div/2`
|
||||
* `max/2`
|
||||
* `min/2`
|
||||
@@ -158,7 +158,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
|
||||
|
||||
@@ -269,9 +269,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`.
|
||||
@@ -323,9 +320,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`.
|
||||
|
||||
@@ -405,7 +399,8 @@ defmodule Integer do
|
||||
|
||||
"""
|
||||
@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,10 +409,12 @@ 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
|
||||
@doc false
|
||||
@deprecated "Use Integer.to_charlist/1 instead"
|
||||
def to_char_list(integer), do: Integer.to_charlist(integer)
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
@deprecated "Use Integer.to_charlist/2 instead"
|
||||
def to_char_list(integer, base), do: Integer.to_charlist(integer, base)
|
||||
|
||||
+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
|
||||
|
||||
@@ -237,7 +237,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
|
||||
|
||||
@@ -56,7 +56,7 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prints documentation metadata (only `delegate_to`, `deprecated`, `guard`, and `since` for now).
|
||||
Prints documentation metadata (only `since` and `deprecated` for now).
|
||||
|
||||
See `default_options/0` for docs on the supported options.
|
||||
"""
|
||||
@@ -66,7 +66,7 @@ defmodule IO.ANSI.Docs do
|
||||
print_each_metadata(metadata, options) && IO.write("\n")
|
||||
end
|
||||
|
||||
@metadata_filter [:deprecated, :guard, :since]
|
||||
@metadata_filter [:deprecated, :since]
|
||||
|
||||
defp print_each_metadata(metadata, options) do
|
||||
Enum.reduce(metadata, false, fn
|
||||
@@ -75,13 +75,6 @@ defmodule IO.ANSI.Docs do
|
||||
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)
|
||||
@@ -98,7 +91,7 @@ defmodule IO.ANSI.Docs do
|
||||
@doc """
|
||||
Prints the documentation body.
|
||||
|
||||
In addition to the printing string, takes a set of `options`
|
||||
In addition to the printing string, takes a set of options
|
||||
defined in `default_options/0`.
|
||||
"""
|
||||
@spec print(String.t(), keyword) :: :ok
|
||||
@@ -524,20 +517,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 +594,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
|
||||
|
||||
|
||||
+494
-527
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
|
||||
@@ -241,22 +225,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 +240,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 +290,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 +395,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,15 +12,22 @@ 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
|
||||
|
||||
defp to_pid(pid) when is_pid(pid), do: pid
|
||||
|
||||
defp to_pid(mod) when is_atom(mod) do
|
||||
{set, _} = :elixir_module.data_tables(mod)
|
||||
:ets.lookup_element(set, {:elixir, :lexical_tracker}, 2)
|
||||
end
|
||||
|
||||
# Internal API
|
||||
@@ -33,7 +40,7 @@ defmodule Kernel.LexicalTracker do
|
||||
|
||||
@doc false
|
||||
def stop(pid) do
|
||||
:gen_server.call(pid, :stop)
|
||||
:gen_server.cast(pid, :stop)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -146,10 +153,6 @@ defmodule Kernel.LexicalTracker do
|
||||
{:reply, :maps.get(key, cache), state}
|
||||
end
|
||||
|
||||
def handle_call(:stop, _from, state) do
|
||||
{:stop, :normal, :ok, state}
|
||||
end
|
||||
|
||||
def handle_cast({:write_cache, key, value}, %{cache: cache} = state) do
|
||||
{:noreply, %{state | cache: :maps.put(key, value, cache)}}
|
||||
end
|
||||
@@ -159,7 +162,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 +213,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}
|
||||
|
||||
@@ -22,7 +22,6 @@ defmodule Kernel.ParallelCompiler do
|
||||
{: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)
|
||||
@@ -46,7 +45,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,7 +66,7 @@ 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.
|
||||
@@ -91,7 +90,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
|
||||
@@ -108,8 +107,8 @@ defmodule Kernel.ParallelCompiler 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 +116,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
|
||||
@@ -133,10 +132,10 @@ defmodule Kernel.ParallelCompiler do
|
||||
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 +161,155 @@ 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)
|
||||
:erlang.put(:elixir_compiler_dest, path)
|
||||
:elixir_compiler.file_to_path(Path.expand(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)
|
||||
:erlang.put(:elixir_compiler_dest, dest)
|
||||
Code.compile_file(file)
|
||||
|
||||
: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, __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
|
||||
|
||||
# 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)
|
||||
defp spawn_workers([], [_] = waiting, [_] = queued, result, warnings, state) do
|
||||
[{_, _, ref, _, _}] = waiting
|
||||
spawn_workers([{ref, :not_found}], waiting, queued, result, warnings, state)
|
||||
end
|
||||
|
||||
# Multiple entries, try to release modules.
|
||||
defp spawn_workers([], 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)
|
||||
defp spawn_workers([], waiting, queued, result, warnings, state)
|
||||
when length(waiting) == length(queued) do
|
||||
# The goal of this function is to find leaves in the dependency graph,
|
||||
# i.e. to find code that depends on code that we know is not being defined.
|
||||
# Note we only release modules because those can be rescued. A missing
|
||||
# struct is a guaranteed compile error, so we never release it and treat
|
||||
# it exclusively a missing entry/deadlock.
|
||||
pending =
|
||||
for {pid, _, _, _} <- queued,
|
||||
entry = waiting_on_without_definition(waiting, pid),
|
||||
{kind, _, ref, on, _} = entry,
|
||||
kind == :module,
|
||||
do: {on, {ref, :not_found}}
|
||||
|
||||
without_definition = without_definition(waiting, files) ->
|
||||
spawn_workers(without_definition, spawned, waiting, files, result, warnings, state)
|
||||
# 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)
|
||||
|
||||
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 +318,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 +396,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 +412,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)
|
||||
{kind, ^pid, _, on, _} = List.keyfind(waiting, pid, 1)
|
||||
description = "deadlocked waiting on #{kind} #{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("""
|
||||
@@ -491,10 +449,10 @@ defmodule Kernel.ParallelCompiler do
|
||||
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 +462,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.
|
||||
|
||||
@@ -192,7 +192,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 +239,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">>
|
||||
@@ -346,7 +328,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
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
|
||||
@@ -391,7 +373,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:
|
||||
@@ -408,7 +390,7 @@ defmodule Kernel.SpecialForms do
|
||||
iex> Kernel."+"(1, 2)
|
||||
3
|
||||
|
||||
Wrapping the function name in single- or double-quotes is always a
|
||||
Note that 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.
|
||||
@@ -486,11 +468,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 +485,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 +524,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 +548,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 +558,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
|
||||
|
||||
@@ -706,13 +687,10 @@ defmodule Kernel.SpecialForms do
|
||||
defmacro __CALLER__, do: error!([])
|
||||
|
||||
@doc """
|
||||
Returns the stacktrace for the currently handled exception.
|
||||
Returns the stacktrace for the curently 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!([])
|
||||
|
||||
@@ -784,7 +762,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 +782,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 +802,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 +833,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 +843,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:
|
||||
|
||||
@@ -947,8 +912,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 +933,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 +953,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 +973,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 +994,8 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
ContextHygiene.write()
|
||||
ContextHygiene.read()
|
||||
ContextHygiene.write
|
||||
ContextHygiene.read
|
||||
#=> 1
|
||||
|
||||
## Hygiene in aliases
|
||||
@@ -1046,14 +1008,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 +1028,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 +1063,10 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
|
||||
require Hygiene
|
||||
Hygiene.no_interference()
|
||||
Hygiene.no_interference
|
||||
#=> ** (UndefinedFunctionError) ...
|
||||
|
||||
Hygiene.interference()
|
||||
Hygiene.interference
|
||||
#=> "world"
|
||||
end
|
||||
|
||||
@@ -1128,8 +1088,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 +1123,7 @@ defmodule Kernel.SpecialForms do
|
||||
end
|
||||
end
|
||||
|
||||
Lazy.return_length()
|
||||
#=> 5
|
||||
Lazy.return_length #=> 5
|
||||
|
||||
## Stacktrace information
|
||||
|
||||
@@ -1197,30 +1155,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 +1201,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 +1222,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 +1234,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 +1303,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 +1323,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 +1343,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])
|
||||
|
||||
@@ -1493,9 +1388,8 @@ defmodule Kernel.SpecialForms do
|
||||
...> end
|
||||
{: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}
|
||||
@@ -1508,9 +1402,8 @@ defmodule Kernel.SpecialForms do
|
||||
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
|
||||
@@ -1542,8 +1435,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 +1501,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 +1576,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 +1625,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 +1635,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 +1644,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 +1661,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
case 10 do
|
||||
^x -> "Won't match"
|
||||
_ -> "Will match"
|
||||
_ -> "Will match"
|
||||
end
|
||||
#=> "Will match"
|
||||
|
||||
@@ -1819,15 +1707,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 +1724,10 @@ 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. To
|
||||
retrieve the stacktrace, access `__STACKTRACE__/0` inside the `rescue`
|
||||
or `catch` clause.
|
||||
|
||||
## `rescue` clauses
|
||||
|
||||
@@ -1922,7 +1811,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 +1823,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 +1969,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
|
||||
@@ -2114,7 +2002,7 @@ defmodule Kernel.SpecialForms do
|
||||
name when is_atom(name) ->
|
||||
name
|
||||
_ ->
|
||||
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
|
||||
@@ -2126,10 +2014,10 @@ defmodule Kernel.SpecialForms do
|
||||
name when is_atom(name) ->
|
||||
name
|
||||
_ ->
|
||||
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.
|
||||
@@ -2146,7 +2034,7 @@ defmodule Kernel.SpecialForms do
|
||||
in hexadecimal 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`.
|
||||
|
||||
+254
-487
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
|
||||
|
||||
@@ -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
|
||||
|
||||
"""
|
||||
@@ -230,36 +225,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 +256,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
|
||||
|
||||
+37
-49
@@ -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,18 +20,15 @@ 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:
|
||||
The two syntaxes are completely equivalent. If the keyword has foreign
|
||||
characters, 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.
|
||||
always atoms and quotes should only be used to handle foreign characters.
|
||||
In fact, if you attempt use quotes when not necessary, 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
|
||||
@@ -67,10 +64,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 +81,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 +113,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 +134,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)`,
|
||||
@@ -154,7 +147,7 @@ defmodule Keyword do
|
||||
|
||||
"""
|
||||
@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 +319,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 +379,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 +417,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 +435,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]
|
||||
|
||||
"""
|
||||
@@ -708,8 +701,8 @@ 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, []), do: keywords1
|
||||
def merge([], keywords2), do: keywords2
|
||||
|
||||
def merge(keywords1, keywords2) when is_list(keywords1) and is_list(keywords2) do
|
||||
if keyword?(keywords2) do
|
||||
@@ -719,13 +712,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 +741,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 +763,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 +785,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 +823,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 +892,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 +920,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 +938,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 +961,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 +995,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 +1023,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 +1035,7 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.to_list(a: 1)
|
||||
iex> Keyword.to_list([a: 1])
|
||||
[a: 1]
|
||||
|
||||
"""
|
||||
@@ -1058,6 +1045,7 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
@deprecated "Use Kernel.length/1 instead"
|
||||
def size(keyword) do
|
||||
length(keyword)
|
||||
|
||||
+142
-319
@@ -63,29 +63,21 @@ defmodule List do
|
||||
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.
|
||||
Additonally, getting a list's length and accessing it by index are
|
||||
linear time operations. Negative indexes are also supported but
|
||||
they imply the list will be iterated twice, once to calculate the
|
||||
proper index and another time to perform the operation.
|
||||
|
||||
## 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 +87,24 @@ 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`.
|
||||
|
||||
Improper lists are never deemed as charlists.
|
||||
A list can be checked if it is made of printable ascii
|
||||
codepoints with `ascii_printable?/2`.
|
||||
"""
|
||||
|
||||
@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 +112,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 +142,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 +158,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
|
||||
@@ -258,8 +217,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 +236,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 +265,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 +286,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 +301,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 +319,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 +340,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,7 +382,7 @@ 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
|
||||
@@ -453,7 +405,9 @@ defmodule List do
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec wrap(term) :: maybe_improper_list()
|
||||
@spec wrap(nil) :: []
|
||||
@spec wrap(list) :: list when list: maybe_improper_list()
|
||||
@spec wrap(term) :: nonempty_list(term) when term: any()
|
||||
def wrap(term)
|
||||
|
||||
def wrap(list) when is_list(list) do
|
||||
@@ -489,28 +443,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 +462,63 @@ 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 +541,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 +571,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 +601,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
|
||||
@@ -750,7 +681,7 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@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 +693,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 +713,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 +724,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 +798,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 +816,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 +826,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 +837,7 @@ defmodule List do
|
||||
#{inspect(list)}
|
||||
"""
|
||||
else
|
||||
result when is_list(result) ->
|
||||
result when is_binary(result) ->
|
||||
result
|
||||
|
||||
{:error, encoded, rest} ->
|
||||
@@ -1000,8 +861,6 @@ 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])
|
||||
@@ -1011,49 +870,19 @@ defmodule List do
|
||||
@doc since: "1.4.0"
|
||||
@spec myers_difference(list, list) :: [{:eq | :ins | :del, list}]
|
||||
def myers_difference(list1, list2) when is_list(list1) and is_list(list2) do
|
||||
myers_difference_with_diff_script(list1, list2, nil)
|
||||
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)
|
||||
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)
|
||||
find_script(0, length(list1) + length(list2), [path])
|
||||
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,68 +895,50 @@ 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
|
||||
defp proceed_path(_diag, _limit, [path1, path2 | rest]) do
|
||||
if elem(path1, 0) > elem(path2, 0) do
|
||||
{move_right(path1, diff_script), [path2 | rest]}
|
||||
{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
|
||||
end
|
||||
|
||||
defp move_right({y, list1, [elem | rest], edits}, _diff_script) do
|
||||
defp move_right({y, list1, [elem | rest], edits}) do
|
||||
{y, list1, rest, [{:ins, elem} | edits]}
|
||||
end
|
||||
|
||||
defp move_right({y, list1, [], edits}, _diff_script) do
|
||||
defp move_right({y, list1, [], edits}) do
|
||||
{y, list1, [], 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
|
||||
end
|
||||
|
||||
defp move_down({y, [elem | rest], list2, edits}, _diff_script) do
|
||||
defp move_down({y, [elem | rest], list2, edits}) do
|
||||
{y + 1, rest, list2, [{:del, elem} | edits]}
|
||||
end
|
||||
|
||||
defp move_down({y, [], list2, edits}, _diff_script) do
|
||||
defp move_down({y, [], list2, edits}) do
|
||||
{y + 1, [], list2, edits}
|
||||
end
|
||||
|
||||
@@ -1151,6 +962,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 +980,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 +994,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 +1012,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,6 +17,7 @@ defprotocol List.Chars do
|
||||
def to_charlist(term)
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use List.Chars.to_charlist/1 instead"
|
||||
Kernel.def to_char_list(term) do
|
||||
__MODULE__.to_charlist(term)
|
||||
|
||||
+41
-78
@@ -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
|
||||
@@ -131,17 +131,13 @@ defmodule Macro 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 +162,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 +198,15 @@ 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 +342,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})
|
||||
@@ -388,7 +380,7 @@ defmodule Macro do
|
||||
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
|
||||
@@ -397,7 +389,7 @@ defmodule Macro do
|
||||
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`
|
||||
## Comparison to `Kernel.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
|
||||
@@ -423,29 +415,11 @@ defmodule Macro do
|
||||
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, kind, unquote), 0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -506,7 +480,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 +509,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 +525,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()
|
||||
@@ -613,11 +587,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 +736,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 +782,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 +817,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,7 +830,7 @@ 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) ->
|
||||
@@ -1142,11 +1111,9 @@ defmodule Macro do
|
||||
* 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
|
||||
|
||||
@@ -1198,7 +1165,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
|
||||
@@ -1285,7 +1252,7 @@ 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 [:+, :-] ->
|
||||
@@ -1316,7 +1283,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 ->
|
||||
@@ -1365,22 +1332,19 @@ defmodule Macro do
|
||||
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 """
|
||||
@@ -1418,7 +1382,6 @@ defmodule Macro do
|
||||
"Hello10"
|
||||
|
||||
"""
|
||||
@spec underscore(atom | String.t()) :: String.t()
|
||||
def underscore(atom) when is_atom(atom) do
|
||||
"Elixir." <> rest = Atom.to_string(atom)
|
||||
underscore(rest)
|
||||
|
||||
@@ -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
|
||||
@@ -57,20 +57,20 @@ defmodule Macro.Env do
|
||||
@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 lexical_tracker :: pid | nil
|
||||
@type variable :: {atom, atom | term}
|
||||
@type var :: {atom, atom | non_neg_integer}
|
||||
|
||||
@typep vars :: [variable]
|
||||
@typep vars :: [var]
|
||||
@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 unused_vars :: %{{var, var_version} => non_neg_integer | false}
|
||||
@typep current_vars :: %{var => {var_version, var_type}}
|
||||
@typep prematch_vars :: current_vars | :warn | :raise | :pin | :apply
|
||||
@typep contextual_vars :: [atom]
|
||||
|
||||
@@ -85,7 +85,7 @@ 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,
|
||||
@@ -132,7 +132,7 @@ defmodule Macro.Env do
|
||||
atom or an integer.
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec vars(t) :: [variable]
|
||||
@spec vars(t) :: [var]
|
||||
def vars(env)
|
||||
|
||||
def vars(%{__struct__: Macro.Env, current_vars: current_vars}) do
|
||||
@@ -143,7 +143,7 @@ defmodule Macro.Env do
|
||||
Checks if a variable belongs to the environment.
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec has_var?(t, variable) :: boolean()
|
||||
@spec has_var?(t, var) :: boolean()
|
||||
def has_var?(env, var)
|
||||
|
||||
def has_var?(%{__struct__: Macro.Env, current_vars: current_vars}, var) do
|
||||
|
||||
+23
-50
@@ -91,12 +91,6 @@ 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.
|
||||
"""
|
||||
|
||||
@type key :: any
|
||||
@@ -171,7 +165,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,8 +206,8 @@ 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 """
|
||||
@@ -268,7 +262,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
|
||||
@@ -384,20 +378,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 +409,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
|
||||
|
||||
@@ -684,30 +670,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 +703,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
|
||||
@@ -852,7 +824,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,6 +892,7 @@ defmodule Map do
|
||||
def equal?(term, other), do: :erlang.error({:badmap, term}, [term, other])
|
||||
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
@deprecated "Use Kernel.map_size/1 instead"
|
||||
def size(map) do
|
||||
map_size(map)
|
||||
|
||||
@@ -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 """
|
||||
@@ -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))}
|
||||
@@ -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
|
||||
|
||||
+143
-238
@@ -8,8 +8,7 @@ defmodule Module do
|
||||
After a module is compiled, using many of the functions in
|
||||
this module will raise errors, since it is out of their scope
|
||||
to inspect runtime data. Most of the runtime data can be inspected
|
||||
via the [`__info__/1`](`c:Module.__info__/1`) function attached to
|
||||
each compiled module.
|
||||
via the `__info__/1` function attached to each compiled module.
|
||||
|
||||
## Module attributes
|
||||
|
||||
@@ -42,7 +41,7 @@ defmodule Module do
|
||||
@callback default_port() :: integer
|
||||
|
||||
@doc "Parses the given URL"
|
||||
@callback parse(uri_info :: URI.t()) :: URI.t()
|
||||
@callback parse(uri_info :: URI.t) :: URI.t
|
||||
end
|
||||
|
||||
And then a module may use it as:
|
||||
@@ -61,25 +60,14 @@ defmodule Module do
|
||||
To aid in the correct implementation of behaviours, you may optionally declare
|
||||
`@impl` for implemented callbacks of a behaviour. This makes callbacks
|
||||
explicit and can help you to catch errors in your code. The compiler will warn
|
||||
in these cases:
|
||||
you if you mark a function as `@impl` when in fact it is not a callback, and
|
||||
vice-versa. It also helps with maintainability by making it clear to other
|
||||
developers that the function's purpose is to implement a callback.
|
||||
|
||||
* if you mark a function with `@impl` when that function is not a callback.
|
||||
|
||||
* if you don't mark a function with `@impl` when other functions are marked
|
||||
with `@impl`. If you mark one function with `@impl`, you must mark all
|
||||
other callbacks for that behaviour as `@impl`.
|
||||
|
||||
`@impl` works on a per-context basis. If you generate a function through a macro
|
||||
and mark it with `@impl`, that won't affect the module where that function is
|
||||
generated in.
|
||||
|
||||
`@impl` also helps with maintainability by making it clear to other developers
|
||||
that the function is implementing a callback.
|
||||
|
||||
Using `@impl`, the example above can be rewritten as:
|
||||
Using `@impl` the example above can be rewritten as:
|
||||
|
||||
defmodule URI.HTTP do
|
||||
@behaviour URI.Parser
|
||||
@behaviour URI.parser
|
||||
|
||||
@impl true
|
||||
def default_port(), do: 80
|
||||
@@ -94,16 +82,14 @@ defmodule Module do
|
||||
@behaviour Bar
|
||||
@behaviour Baz
|
||||
|
||||
# Will warn if neither Bar nor Baz specify a callback named bar/0.
|
||||
@impl true
|
||||
@impl true # will warn if neither Bar nor Baz specify a callback named bar/0
|
||||
def bar(), do: :ok
|
||||
|
||||
# Will warn if Baz does not specify a callback named baz/0.
|
||||
@impl Baz
|
||||
@impl Baz # will warn if Baz does not specify a callback named baz/0
|
||||
def baz(), do: :ok
|
||||
end
|
||||
|
||||
The code is now more readable, as it is now clear which functions are
|
||||
Readability of the code is increased, as it is now clear which functions are
|
||||
part of your API and which ones are callback implementations. To reinforce this
|
||||
idea, `@impl true` automatically marks the function as `@doc false`, disabling
|
||||
documentation unless `@doc` is explicitly set.
|
||||
@@ -142,7 +128,7 @@ defmodule Module do
|
||||
Using the `@deprecated` attribute will also be reflected in the
|
||||
documentation of the given function and macro. You can choose between
|
||||
the `@deprecated` attribute and the documentation metadata to provide
|
||||
hard-deprecations (with warnings) and soft-deprecations (without warnings):
|
||||
hard-deprecations (with warnings) and soft-deprecations (with warnings):
|
||||
|
||||
This is a soft-deprecation as it simply annotates the documentation
|
||||
as deprecated:
|
||||
@@ -175,7 +161,7 @@ defmodule Module do
|
||||
|
||||
Accepts a string (often a heredoc) or `false` where `@doc false` will
|
||||
make the entity invisible to documentation extraction tools like
|
||||
[`ExDoc`](https://hexdocs.pm/ex_doc/). For example:
|
||||
ExDoc. For example:
|
||||
|
||||
defmodule MyModule do
|
||||
@typedoc "This type"
|
||||
@@ -198,10 +184,9 @@ defmodule Module do
|
||||
|
||||
As can be seen in the example above, `@doc` and `@typedoc` also accept
|
||||
a keyword list that serves as a way to provide arbitrary metadata
|
||||
about the entity. Tools like [`ExDoc`](https://hexdocs.pm/ex_doc/) and
|
||||
`IEx` may use this information to display annotations. A common use
|
||||
case is `since` that may be used to annotate in which version the
|
||||
function was introduced.
|
||||
about the entity. Tools like ExDoc and IEx may use this information to
|
||||
display annotations. A common use case is `since` that may be used
|
||||
to annotate in which version the function was introduced.
|
||||
|
||||
As illustrated in the example, it is possible to use these attributes
|
||||
more than once before an entity. However, the compiler will warn if
|
||||
@@ -213,9 +198,6 @@ defmodule Module do
|
||||
there are a few reserved keys that will be ignored and warned if used.
|
||||
Currently these are: `:opaque` and `:defaults`.
|
||||
|
||||
Once this module is compiled, this information becomes available via
|
||||
the `Code.fetch_docs/1` function.
|
||||
|
||||
### `@dialyzer`
|
||||
|
||||
Defines warnings to request or suppress when using a version of
|
||||
@@ -274,15 +256,12 @@ defmodule Module do
|
||||
|
||||
Accepts a string (often a heredoc) or `false` where `@moduledoc false`
|
||||
will make the module invisible to documentation extraction tools like
|
||||
[`ExDoc`](https://hexdocs.pm/ex_doc/).
|
||||
ExDoc.
|
||||
|
||||
Similarly to `@doc` also accepts a keyword list to provide metadata
|
||||
about the module. For more details, see the documentation of `@doc`
|
||||
above.
|
||||
|
||||
Once this module is compiled, this information becomes available via
|
||||
the `Code.fetch_docs/1` function.
|
||||
|
||||
### `@on_definition`
|
||||
|
||||
A hook that will be invoked when each function or macro in the current
|
||||
@@ -297,9 +276,9 @@ defmodule Module do
|
||||
|
||||
Accepts the function name (as an atom) of a function in the current module or
|
||||
`{function_name, 0}` tuple where `function_name` is the name of a function in
|
||||
the current module. The function must be public and have an arity of 0 (no
|
||||
arguments). If the function does not return `:ok`, the loading of the module
|
||||
will be aborted. For example:
|
||||
the current module. The function must have arity 0 (no arguments) and has to
|
||||
return `:ok`, otherwise the loading of the module will be aborted. For
|
||||
example:
|
||||
|
||||
defmodule MyModule do
|
||||
@on_load :load_check
|
||||
@@ -345,9 +324,8 @@ defmodule Module do
|
||||
### Custom attributes
|
||||
|
||||
In addition to the built-in attributes outlined above, custom attributes may
|
||||
also be added. Custom attributes are expressed using the `@/1` operator followed
|
||||
by a valid variable name. The value given to the custom attribute must be a valid
|
||||
Elixir value:
|
||||
also be added. A custom attribute is any valid identifier prefixed with an
|
||||
`@` and followed by a valid Elixir value:
|
||||
|
||||
defmodule MyModule do
|
||||
@custom_attr [some: "stuff"]
|
||||
@@ -378,7 +356,7 @@ defmodule Module do
|
||||
@after_compile __MODULE__
|
||||
|
||||
def __after_compile__(env, _bytecode) do
|
||||
IO.inspect(env)
|
||||
IO.inspect env
|
||||
end
|
||||
end
|
||||
|
||||
@@ -454,12 +432,12 @@ defmodule Module do
|
||||
|
||||
defmodule Hooks do
|
||||
def on_def(_env, kind, name, args, guards, body) do
|
||||
IO.puts("Defining #{kind} named #{name} with args:")
|
||||
IO.inspect(args)
|
||||
IO.puts("and guards")
|
||||
IO.inspect(guards)
|
||||
IO.puts("and body")
|
||||
IO.puts(Macro.to_string(body))
|
||||
IO.puts "Defining #{kind} named #{name} with args:"
|
||||
IO.inspect args
|
||||
IO.puts "and guards"
|
||||
IO.inspect guards
|
||||
IO.puts "and body"
|
||||
IO.puts Macro.to_string(body)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -502,37 +480,27 @@ defmodule Module do
|
||||
@typep definition :: {atom, arity}
|
||||
@typep def_kind :: :def | :defp | :defmacro | :defmacrop
|
||||
|
||||
@extra_error_msg_defines? "Use Kernel.function_exported?/3 and Kernel.macro_exported?/3 " <>
|
||||
"to check for public functions and macros instead"
|
||||
|
||||
@extra_error_msg_definitions_in "Use the Module.__info__/1 callback to get public functions and macros instead"
|
||||
|
||||
@doc """
|
||||
Provides runtime information about functions, macros, and other information
|
||||
defined by the module.
|
||||
Provides runtime information about functions and macros defined by the
|
||||
module, etc.
|
||||
|
||||
Each module gets an `__info__/1` function when it's compiled. The function
|
||||
takes one of the following items:
|
||||
takes one of the following atoms:
|
||||
|
||||
* `:attributes` - a keyword list with all persisted attributes
|
||||
* `:functions` - keyword list of public functions along with their arities
|
||||
|
||||
* `:compile` - a list with compiler metadata
|
||||
|
||||
* `:functions` - a keyword list of public functions and their arities
|
||||
|
||||
* `:macros` - a keyword list of public macros and their arities
|
||||
|
||||
* `:md5` - the MD5 of the module
|
||||
* `:macros` - keyword list of public macros along with their arities
|
||||
|
||||
* `:module` - the module atom name
|
||||
|
||||
* `:md5` - the MD5 of the module
|
||||
|
||||
* `:compile` - a list with compiler metadata
|
||||
|
||||
* `:attributes` - a list with all persisted attributes
|
||||
|
||||
"""
|
||||
@callback __info__(:attributes) :: keyword()
|
||||
@callback __info__(:compile) :: [term()]
|
||||
@callback __info__(:functions) :: keyword()
|
||||
@callback __info__(:macros) :: keyword()
|
||||
@callback __info__(:md5) :: binary()
|
||||
@callback __info__(:module) :: module()
|
||||
def __info__(kind)
|
||||
|
||||
@doc """
|
||||
Checks if a module is open.
|
||||
@@ -556,16 +524,11 @@ defmodule Module do
|
||||
## Examples
|
||||
|
||||
defmodule Foo do
|
||||
contents =
|
||||
quote do
|
||||
def sum(a, b), do: a + b
|
||||
end
|
||||
|
||||
Module.eval_quoted(__MODULE__, contents)
|
||||
contents = quote do: (def sum(a, b), do: a + b)
|
||||
Module.eval_quoted __MODULE__, contents
|
||||
end
|
||||
|
||||
Foo.sum(1, 2)
|
||||
#=> 3
|
||||
Foo.sum(1, 2) #=> 3
|
||||
|
||||
For convenience, you can pass any `Macro.Env` struct, such
|
||||
as `__ENV__/0`, as the first argument or as options. Both
|
||||
@@ -573,16 +536,11 @@ defmodule Module do
|
||||
from the environment:
|
||||
|
||||
defmodule Foo do
|
||||
contents =
|
||||
quote do
|
||||
def sum(a, b), do: a + b
|
||||
end
|
||||
|
||||
Module.eval_quoted(__ENV__, contents)
|
||||
contents = quote do: (def sum(a, b), do: a + b)
|
||||
Module.eval_quoted __ENV__, contents
|
||||
end
|
||||
|
||||
Foo.sum(1, 2)
|
||||
#=> 3
|
||||
Foo.sum(1, 2) #=> 3
|
||||
|
||||
Note that if you pass a `Macro.Env` struct as first argument
|
||||
while also passing `opts`, they will be merged with `opts`
|
||||
@@ -603,7 +561,7 @@ defmodule Module do
|
||||
|
||||
def eval_quoted(module, quoted, binding, opts)
|
||||
when is_atom(module) and is_list(binding) and is_list(opts) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
assert_not_compiled!(:eval_quoted, module)
|
||||
:elixir_def.reset_last(module)
|
||||
|
||||
{value, binding, _env, _scope} =
|
||||
@@ -637,8 +595,7 @@ defmodule Module do
|
||||
|
||||
Module.create(Hello, contents, Macro.Env.location(__ENV__))
|
||||
|
||||
Hello.world()
|
||||
#=> true
|
||||
Hello.world #=> true
|
||||
|
||||
## Differences from `defmodule`
|
||||
|
||||
@@ -665,7 +622,7 @@ defmodule Module do
|
||||
raise ArgumentError, "expected :file to be given as option"
|
||||
end
|
||||
|
||||
next = :elixir_module.next_counter(nil)
|
||||
next = :erlang.unique_integer()
|
||||
line = Keyword.get(opts, :line, 0)
|
||||
quoted = :elixir_quote.linify_with_context_counter(line, {module, next}, quoted)
|
||||
:elixir_module.compile(module, quoted, [], :elixir.env_for_eval(opts))
|
||||
@@ -919,27 +876,21 @@ defmodule Module do
|
||||
Use `defines?/3` to assert for a specific type.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
Use `Kernel.function_exported?/3` and `Kernel.macro_exported?/3` to check for
|
||||
public functions and macros respectively in compiled modules.
|
||||
|
||||
Note that `defines?` returns false for functions and macros that have
|
||||
been defined but then marked as overridable and no other implementation
|
||||
has been provided. You can check the overridable status by calling
|
||||
`overridable?/2`.
|
||||
Use `Kernel.function_exported?/3` to check compiled modules.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Example do
|
||||
Module.defines?(__MODULE__, {:version, 0}) #=> false
|
||||
Module.defines? __MODULE__, {:version, 0} #=> false
|
||||
def version, do: 1
|
||||
Module.defines?(__MODULE__, {:version, 0}) #=> true
|
||||
Module.defines? __MODULE__, {:version, 0} #=> true
|
||||
end
|
||||
|
||||
"""
|
||||
@spec defines?(module, definition) :: boolean
|
||||
def defines?(module, {name, arity} = tuple)
|
||||
when is_atom(module) and is_atom(name) and is_integer(arity) and arity >= 0 and arity <= 255 do
|
||||
assert_not_compiled!(__ENV__.function, module, @extra_error_msg_defines?)
|
||||
assert_not_compiled!(:defines?, module)
|
||||
{set, _bag} = data_tables_for(module)
|
||||
:ets.member(set, {:def, tuple})
|
||||
end
|
||||
@@ -951,15 +902,14 @@ defmodule Module do
|
||||
`kind` can be any of `:def`, `:defp`, `:defmacro`, or `:defmacrop`.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
Use `Kernel.function_exported?/3` and `Kernel.macro_exported?/3` to check for
|
||||
public functions and macros respectively in compiled modules.
|
||||
Use `Kernel.function_exported?/3` to check compiled modules.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Example do
|
||||
Module.defines?(__MODULE__, {:version, 0}, :def) #=> false
|
||||
Module.defines? __MODULE__, {:version, 0}, :defp #=> false
|
||||
def version, do: 1
|
||||
Module.defines?(__MODULE__, {:version, 0}, :def) #=> true
|
||||
Module.defines? __MODULE__, {:version, 0}, :defp #=> false
|
||||
end
|
||||
|
||||
"""
|
||||
@@ -967,8 +917,7 @@ defmodule Module do
|
||||
def defines?(module, {name, arity} = tuple, def_kind)
|
||||
when is_atom(module) and is_atom(name) and is_integer(arity) and arity >= 0 and arity <= 255 and
|
||||
def_kind in [:def, :defp, :defmacro, :defmacrop] do
|
||||
assert_not_compiled!(__ENV__.function, module, @extra_error_msg_defines?)
|
||||
|
||||
assert_not_compiled!(:defines?, module)
|
||||
{set, _bag} = data_tables_for(module)
|
||||
|
||||
case :ets.lookup(set, {:def, tuple}) do
|
||||
@@ -989,11 +938,9 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Copies the given spec as a callback.
|
||||
Converts the given spec to a callback.
|
||||
|
||||
Returns `true` if there is such a spec and it was copied as a callback.
|
||||
If the function associated to the spec has documentation defined prior to
|
||||
invoking this function, the docs are copied too.
|
||||
Returns `true` if there is such a spec and it was converted to a callback.
|
||||
"""
|
||||
@doc since: "1.7.0"
|
||||
@spec spec_to_callback(module, definition) :: boolean
|
||||
@@ -1002,27 +949,19 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all functions and macros defined in `module`.
|
||||
|
||||
It returns a list with all defined functions and macros, public and private,
|
||||
in the shape of `[{name, arity}, ...]`.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
Use the `c:Module.__info__/1` callback to get the public functions and macros in
|
||||
compiled modules.
|
||||
Returns all functions defined in `module`.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Example do
|
||||
def version, do: 1
|
||||
defmacrop test(arg), do: arg
|
||||
Module.definitions_in(__MODULE__) #=> [{:version, 0}, {:test, 1}]
|
||||
Module.definitions_in __MODULE__ #=> [{:version, 0}]
|
||||
end
|
||||
|
||||
"""
|
||||
@spec definitions_in(module) :: [definition]
|
||||
def definitions_in(module) when is_atom(module) do
|
||||
assert_not_compiled!(__ENV__.function, module, @extra_error_msg_definitions_in)
|
||||
assert_not_compiled!(:definitions_in, module)
|
||||
{_, bag} = data_tables_for(module)
|
||||
bag_lookup_element(bag, :defs, 2)
|
||||
end
|
||||
@@ -1031,23 +970,19 @@ defmodule Module do
|
||||
Returns all functions defined in `module`, according
|
||||
to its kind.
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
Use the `c:Module.__info__/1` callback to get the public functions and macros in
|
||||
compiled modules.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Example do
|
||||
def version, do: 1
|
||||
Module.definitions_in(__MODULE__, :def) #=> [{:version, 0}]
|
||||
Module.definitions_in(__MODULE__, :defp) #=> []
|
||||
Module.definitions_in __MODULE__, :def #=> [{:version, 0}]
|
||||
Module.definitions_in __MODULE__, :defp #=> []
|
||||
end
|
||||
|
||||
"""
|
||||
@spec definitions_in(module, def_kind) :: [definition]
|
||||
def definitions_in(module, def_kind)
|
||||
when is_atom(module) and def_kind in [:def, :defp, :defmacro, :defmacrop] do
|
||||
assert_not_compiled!(__ENV__.function, module, @extra_error_msg_definitions_in)
|
||||
assert_not_compiled!(:definitions_in, module)
|
||||
{set, _} = data_tables_for(module)
|
||||
:lists.concat(:ets.match(set, {{:def, :"$1"}, def_kind, :_, :_, :_, :_}))
|
||||
end
|
||||
@@ -1058,15 +993,10 @@ defmodule Module do
|
||||
An overridable function is lazily defined, allowing a
|
||||
developer to customize it. See `Kernel.defoverridable/1` for
|
||||
more information and documentation.
|
||||
|
||||
Once a function or a macro is marked as overridable, it will
|
||||
no longer be listed under `definitions_in/1` or return true
|
||||
when given to `defines?/2` until another implementation is
|
||||
given.
|
||||
"""
|
||||
@spec make_overridable(module, [definition]) :: :ok
|
||||
def make_overridable(module, tuples) when is_atom(module) and is_list(tuples) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
assert_not_compiled!(:make_overridable, module)
|
||||
|
||||
func = fn
|
||||
{function_name, arity} = tuple
|
||||
@@ -1079,7 +1009,18 @@ defmodule Module do
|
||||
|
||||
clause ->
|
||||
neighbours = :elixir_locals.yank(tuple, module)
|
||||
:elixir_overridable.record_overridable(module, tuple, clause, neighbours)
|
||||
overridable_definitions = :elixir_overridable.overridable(module)
|
||||
|
||||
count =
|
||||
case :maps.find(tuple, overridable_definitions) do
|
||||
{:ok, {count, _, _, _}} -> count + 1
|
||||
:error -> 1
|
||||
end
|
||||
|
||||
overridable_definitions =
|
||||
:maps.put(tuple, {count, clause, neighbours, false}, overridable_definitions)
|
||||
|
||||
:elixir_overridable.overridable(module, overridable_definitions)
|
||||
end
|
||||
|
||||
other ->
|
||||
@@ -1164,7 +1105,7 @@ defmodule Module do
|
||||
@spec overridable?(module, definition) :: boolean
|
||||
def overridable?(module, {function_name, arity} = tuple)
|
||||
when is_atom(function_name) and is_integer(arity) and arity >= 0 and arity <= 255 do
|
||||
:elixir_overridable.overridable_for(module, tuple) != :not_overridable
|
||||
:maps.is_key(tuple, :elixir_overridable.overridable(module))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1173,13 +1114,13 @@ defmodule Module do
|
||||
## Examples
|
||||
|
||||
defmodule MyModule do
|
||||
Module.put_attribute(__MODULE__, :custom_threshold_for_lib, 10)
|
||||
Module.put_attribute __MODULE__, :custom_threshold_for_lib, 10
|
||||
end
|
||||
|
||||
"""
|
||||
@spec put_attribute(module, atom, term) :: :ok
|
||||
def put_attribute(module, key, value) when is_atom(module) and is_atom(key) do
|
||||
__put_attribute__(module, key, value, nil)
|
||||
put_attribute(module, key, value, nil)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1199,32 +1140,21 @@ defmodule Module do
|
||||
|
||||
Module.get_attribute(__MODULE__, :foo)
|
||||
|
||||
This function can only be used on modules that have not yet been compiled.
|
||||
Use the `c:Module.__info__/1` callback to get all persisted attributes, or
|
||||
`Code.fetch_docs/1` to retrieve all documentation related attributes in
|
||||
compiled modules.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Foo do
|
||||
Module.put_attribute(__MODULE__, :value, 1)
|
||||
Module.get_attribute(__MODULE__, :value) #=> 1
|
||||
Module.put_attribute __MODULE__, :value, 1
|
||||
Module.get_attribute __MODULE__, :value #=> 1
|
||||
|
||||
Module.get_attribute(__MODULE__, :value, :default) #=> 1
|
||||
Module.get_attribute(__MODULE__, :not_found, :default) #=> :default
|
||||
|
||||
Module.register_attribute(__MODULE__, :value, accumulate: true)
|
||||
Module.put_attribute(__MODULE__, :value, 1)
|
||||
Module.get_attribute(__MODULE__, :value) #=> [1]
|
||||
Module.register_attribute __MODULE__, :value, accumulate: true
|
||||
Module.put_attribute __MODULE__, :value, 1
|
||||
Module.get_attribute __MODULE__, :value #=> [1]
|
||||
end
|
||||
|
||||
"""
|
||||
@spec get_attribute(module, atom, term) :: term
|
||||
def get_attribute(module, key, default \\ nil) when is_atom(module) and is_atom(key) do
|
||||
case __get_attribute__(module, key, nil) do
|
||||
nil -> default
|
||||
value -> value
|
||||
end
|
||||
@spec get_attribute(module, atom) :: term
|
||||
def get_attribute(module, key) when is_atom(module) and is_atom(key) do
|
||||
get_attribute(module, key, nil)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -1235,14 +1165,14 @@ defmodule Module do
|
||||
## Examples
|
||||
|
||||
defmodule MyModule do
|
||||
Module.put_attribute(__MODULE__, :custom_threshold_for_lib, 10)
|
||||
Module.delete_attribute(__MODULE__, :custom_threshold_for_lib)
|
||||
Module.put_attribute __MODULE__, :custom_threshold_for_lib, 10
|
||||
Module.delete_attribute __MODULE__, :custom_threshold_for_lib
|
||||
end
|
||||
|
||||
"""
|
||||
@spec delete_attribute(module, atom) :: term
|
||||
def delete_attribute(module, key) when is_atom(module) and is_atom(key) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
assert_not_compiled!(:delete_attribute, module)
|
||||
{set, bag} = data_tables_for(module)
|
||||
|
||||
case :ets.lookup(set, key) do
|
||||
@@ -1272,7 +1202,7 @@ defmodule Module do
|
||||
When registering an attribute, two options can be given:
|
||||
|
||||
* `:accumulate` - several calls to the same attribute will
|
||||
accumulate instead of overriding the previous one. New attributes
|
||||
accumulate instead of override the previous one. New attributes
|
||||
are always added to the top of the accumulated list.
|
||||
|
||||
* `:persist` - the attribute will be persisted in the Erlang
|
||||
@@ -1283,7 +1213,9 @@ defmodule Module do
|
||||
## Examples
|
||||
|
||||
defmodule MyModule do
|
||||
Module.register_attribute(__MODULE__, :custom_threshold_for_lib, accumulate: true)
|
||||
Module.register_attribute __MODULE__,
|
||||
:custom_threshold_for_lib,
|
||||
accumulate: true, persist: false
|
||||
|
||||
@custom_threshold_for_lib 10
|
||||
@custom_threshold_for_lib 20
|
||||
@@ -1294,7 +1226,7 @@ defmodule Module do
|
||||
@spec register_attribute(module, atom, [{:accumulate, boolean}, {:persist, boolean}]) :: :ok
|
||||
def register_attribute(module, attribute, options)
|
||||
when is_atom(module) and is_atom(attribute) and is_list(options) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
assert_not_compiled!(:register_attribute, module)
|
||||
{set, bag} = data_tables_for(module)
|
||||
|
||||
if Keyword.get(options, :persist) do
|
||||
@@ -1346,9 +1278,10 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use @doc instead"
|
||||
def add_doc(module, line, kind, {name, arity}, signature \\ [], doc) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
assert_not_compiled!(:add_doc, module)
|
||||
|
||||
if kind in [:defp, :defmacrop, :typep] do
|
||||
if doc, do: {:error, :private_doc}, else: :ok
|
||||
@@ -1383,7 +1316,7 @@ defmodule Module do
|
||||
"#{kind} #{name}/#{arity} is private, " <>
|
||||
"@doc attribute is always discarded for private functions/macros/types"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(%{env | line: line}))
|
||||
:elixir_errors.warn(line, env.file, message)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1483,7 +1416,7 @@ defmodule Module do
|
||||
|
||||
pending_callbacks =
|
||||
if impls != [] do
|
||||
{non_implemented_callbacks, contexts} = check_impls(env, behaviours, callbacks, impls)
|
||||
{non_implemented_callbacks, contexts} = check_impls(behaviours, callbacks, impls)
|
||||
warn_missing_impls(env, non_implemented_callbacks, contexts, all_definitions)
|
||||
non_implemented_callbacks
|
||||
else
|
||||
@@ -1501,21 +1434,27 @@ defmodule Module do
|
||||
message =
|
||||
"@behaviour #{inspect(behaviour)} must be an atom (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
acc
|
||||
|
||||
not Code.ensure_compiled?(behaviour) ->
|
||||
message =
|
||||
"@behaviour #{inspect(behaviour)} does not exist (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
unless standard_behaviour?(behaviour) do
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
end
|
||||
|
||||
acc
|
||||
|
||||
not function_exported?(behaviour, :behaviour_info, 1) ->
|
||||
message =
|
||||
"module #{inspect(behaviour)} is not a behaviour (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
unless standard_behaviour?(behaviour) do
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
end
|
||||
|
||||
acc
|
||||
|
||||
true ->
|
||||
@@ -1533,15 +1472,10 @@ defmodule Module do
|
||||
case acc do
|
||||
%{^callback => {_kind, conflict, _optional?}} ->
|
||||
message =
|
||||
if conflict == behaviour do
|
||||
"the behavior #{inspect(conflict)} has been declared twice " <>
|
||||
"(conflict in #{format_definition(kind, callback)} in module #{inspect(env.module)})"
|
||||
else
|
||||
"conflicting behaviours found. #{format_definition(kind, callback)} is required by " <>
|
||||
"#{inspect(conflict)} and #{inspect(behaviour)} (in module #{inspect(env.module)})"
|
||||
end
|
||||
"conflicting behaviours found. #{format_definition(kind, callback)} is required by " <>
|
||||
"#{inspect(conflict)} and #{inspect(behaviour)} (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
|
||||
%{} ->
|
||||
:ok
|
||||
@@ -1550,6 +1484,16 @@ defmodule Module do
|
||||
Map.put(acc, callback, {kind, behaviour, original in optional_callbacks})
|
||||
end
|
||||
|
||||
defp standard_behaviour?(behaviour) do
|
||||
behaviour in [
|
||||
Collectable,
|
||||
Enumerable,
|
||||
Inspect,
|
||||
List.Chars,
|
||||
String.Chars
|
||||
]
|
||||
end
|
||||
|
||||
defp check_callbacks(env, callbacks, all_definitions) do
|
||||
for {callback, {kind, behaviour, optional?}} <- callbacks do
|
||||
case :lists.keyfind(callback, 1, all_definitions) do
|
||||
@@ -1558,7 +1502,7 @@ defmodule Module do
|
||||
format_callback(callback, kind, behaviour) <>
|
||||
" is not implemented (in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
|
||||
{_, wrong_kind, _, _} when kind != wrong_kind ->
|
||||
message =
|
||||
@@ -1566,7 +1510,7 @@ defmodule Module do
|
||||
" was implemented as \"#{wrong_kind}\" but should have been \"#{kind}\" " <>
|
||||
"(in module #{inspect(env.module)})"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(env))
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
|
||||
_ ->
|
||||
:ok
|
||||
@@ -1588,7 +1532,7 @@ defmodule Module do
|
||||
module.__protocol__(:module) == module
|
||||
end
|
||||
|
||||
defp check_impls(env, behaviours, callbacks, impls) do
|
||||
defp check_impls(behaviours, callbacks, impls) do
|
||||
acc = {callbacks, %{}}
|
||||
|
||||
Enum.reduce(impls, acc, fn {fa, context, defaults, kind, line, file, value}, acc ->
|
||||
@@ -1601,8 +1545,7 @@ defmodule Module do
|
||||
end)
|
||||
|
||||
{:error, message} ->
|
||||
formatted = format_impl_warning(fa, kind, message)
|
||||
IO.warn(formatted, Macro.Env.stacktrace(%{env | line: line, file: file}))
|
||||
:elixir_errors.warn(line, file, format_impl_warning(fa, kind, message))
|
||||
acc
|
||||
end
|
||||
end)
|
||||
@@ -1718,7 +1661,7 @@ defmodule Module do
|
||||
"This either means you forgot to add the \"@impl true\" annotation before the " <>
|
||||
"definition or that you are accidentally overriding this callback"
|
||||
|
||||
IO.warn(message, Macro.Env.stacktrace(%{env | line: :elixir_utils.get_line(meta)}))
|
||||
:elixir_errors.warn(:elixir_utils.get_line(meta), env.file, message)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1758,13 +1701,8 @@ defmodule Module do
|
||||
@doc false
|
||||
# Used internally by Kernel's @.
|
||||
# This function is private and must be used only internally.
|
||||
def __get_attribute__(module, key, line) when is_atom(key) do
|
||||
assert_not_compiled!(
|
||||
{:get_attribute, 2},
|
||||
module,
|
||||
"Use the Module.__info__/1 callback or Code.fetch_docs/1 instead"
|
||||
)
|
||||
|
||||
def get_attribute(module, key, line) when is_atom(key) do
|
||||
assert_not_compiled!(:get_attribute, module)
|
||||
{set, bag} = data_tables_for(module)
|
||||
|
||||
case :ets.lookup(set, key) do
|
||||
@@ -1795,8 +1733,8 @@ defmodule Module do
|
||||
@doc false
|
||||
# Used internally by Kernel's @.
|
||||
# This function is private and must be used only internally.
|
||||
def __put_attribute__(module, key, value, line) when is_atom(key) do
|
||||
assert_not_compiled!(__ENV__.function, module)
|
||||
def put_attribute(module, key, value, line) when is_atom(key) do
|
||||
assert_not_compiled!(:put_attribute, module)
|
||||
{set, bag} = data_tables_for(module)
|
||||
value = preprocess_attribute(key, value)
|
||||
put_attribute(module, key, value, line, set, bag)
|
||||
@@ -1820,8 +1758,9 @@ defmodule Module do
|
||||
end
|
||||
end
|
||||
|
||||
# Optimize some attributes by avoiding writing to the attributes key
|
||||
# in the bag table since we handle them internally.
|
||||
# This is the same list of attributes as in :elixir_module.
|
||||
# We do not insert into the :attributes key in the bag table
|
||||
# because those attributes are deleted on every definition.
|
||||
defp put_attribute(module, key, value, line, set, _bag)
|
||||
when key in [:doc, :typedoc, :moduledoc, :impl, :deprecated] do
|
||||
try do
|
||||
@@ -1840,18 +1779,6 @@ defmodule Module do
|
||||
:ets.insert(set, {key, value, line})
|
||||
end
|
||||
|
||||
defp put_attribute(_module, :on_load, value, line, set, bag) do
|
||||
try do
|
||||
:ets.lookup_element(set, :on_load, 3)
|
||||
catch
|
||||
:error, :badarg ->
|
||||
:ets.insert(set, {:on_load, value, line})
|
||||
:ets.insert(bag, {:attributes, :on_load})
|
||||
else
|
||||
_ -> raise ArgumentError, "the @on_load attribute can only be set once per module"
|
||||
end
|
||||
end
|
||||
|
||||
defp put_attribute(_module, key, value, line, set, bag) do
|
||||
try do
|
||||
:ets.lookup_element(set, key, 3)
|
||||
@@ -1874,7 +1801,7 @@ defmodule Module do
|
||||
|
||||
defp preprocess_attribute(key, value) when key in [:moduledoc, :typedoc, :doc] do
|
||||
case value do
|
||||
{line, doc} when is_integer(line) and (is_binary(doc) or doc == false or is_nil(doc)) ->
|
||||
{line, doc} when is_integer(line) and (is_binary(doc) or is_boolean(doc) or is_nil(doc)) ->
|
||||
value
|
||||
|
||||
{line, [{key, _} | _]} when is_integer(line) and is_atom(key) ->
|
||||
@@ -1882,13 +1809,13 @@ defmodule Module do
|
||||
|
||||
{line, doc} when is_integer(line) ->
|
||||
raise ArgumentError,
|
||||
"@#{key} is a built-in module attribute for documentation. It should be either " <>
|
||||
"false, nil, a string, or a keyword list, got: #{inspect(doc)}"
|
||||
"@#{key} is a built-in module attribute for documentation. It should be " <>
|
||||
"a string, boolean, keyword list, or nil, got: #{inspect(doc)}"
|
||||
|
||||
_other ->
|
||||
raise ArgumentError,
|
||||
"@#{key} is a built-in module attribute for documentation. When set dynamically, " <>
|
||||
"it should be {line, doc} (where \"doc\" is either false, nil, a string, or a keyword list), " <>
|
||||
"it should be {line, doc} (where \"doc\" is a string, boolean, keyword list, or nil), " <>
|
||||
"got: #{inspect(value)}"
|
||||
end
|
||||
end
|
||||
@@ -1937,9 +1864,9 @@ defmodule Module do
|
||||
do: {atom, :__on_definition__}
|
||||
|
||||
defp preprocess_attribute(key, _value)
|
||||
when key in [:type, :typep, :opaque, :spec, :callback, :macrocallback] do
|
||||
when key in [:type, :typep, :export_type, :opaque, :spec, :callback, :macrocallback] do
|
||||
raise ArgumentError,
|
||||
"attributes type, typep, opaque, spec, callback, and macrocallback " <>
|
||||
"attributes type, typep, export_type, opaque, spec, callback, and macrocallback " <>
|
||||
"must be set directly via the @ notation"
|
||||
end
|
||||
|
||||
@@ -2001,18 +1928,6 @@ defmodule Module do
|
||||
"representing the replacement for the deprecated entity, got: #{inspect(value)}"
|
||||
end
|
||||
|
||||
defp validate_doc_meta(:delegate_to, value) do
|
||||
case value do
|
||||
{m, f, a} when is_atom(m) and is_atom(f) and is_integer(a) and a >= 0 ->
|
||||
:ok
|
||||
|
||||
_ ->
|
||||
raise ArgumentError,
|
||||
":delegate_to is a built-in documentation metadata key. It should be a three-element " <>
|
||||
"tuple in the form of {module, function, arity}, got: #{inspect(value)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp validate_doc_meta(_, _), do: :ok
|
||||
|
||||
defp get_doc_info(table, env) do
|
||||
@@ -2035,19 +1950,9 @@ defmodule Module do
|
||||
:error, :badarg -> []
|
||||
end
|
||||
|
||||
defp assert_not_compiled!(function_name_arity, module, extra_msg \\ "") do
|
||||
defp assert_not_compiled!(fun, module) do
|
||||
open?(module) ||
|
||||
raise ArgumentError,
|
||||
assert_not_compiled_message(function_name_arity, module, extra_msg)
|
||||
end
|
||||
|
||||
defp assert_not_compiled_message({function_name, arity}, module, extra_msg) do
|
||||
mfa = "Module.#{function_name}/#{arity}"
|
||||
|
||||
"could not call #{mfa} because the module #{inspect(module)} is already compiled" <>
|
||||
case extra_msg do
|
||||
"" -> ""
|
||||
_ -> ". " <> extra_msg
|
||||
end
|
||||
"could not call #{fun} with argument #{inspect(module)} because the module is already compiled"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,25 +4,23 @@
|
||||
#
|
||||
# ## Implementation
|
||||
#
|
||||
# The implementation uses ETS to track all dependencies
|
||||
# The implementation uses ets to track all dependencies
|
||||
# resembling a graph. The keys and what they point to are:
|
||||
#
|
||||
# * `:reattach` points to `{name, arity}`
|
||||
# * `{:local, {name, arity}}` points to `{{name, arity}, line, macro_dispatch?}`
|
||||
# * `{:local, {name, arity}}` points to `{name, arity}`
|
||||
# * `{:import, {name, arity}}` points to `Module`
|
||||
#
|
||||
# This is built on top of the internal module tables.
|
||||
defmodule Module.LocalsTracker do
|
||||
@moduledoc false
|
||||
|
||||
@defmacros [:defmacro, :defmacrop]
|
||||
|
||||
@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({_set, bag}, _kind, {name, arity} = pair, 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(bag, {:local, {name, i}}, pair)
|
||||
end
|
||||
|
||||
:ok
|
||||
@@ -31,9 +29,11 @@ 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?})
|
||||
def add_local({_set, bag}, from, to) when is_tuple(from) and is_tuple(to) do
|
||||
if from != to do
|
||||
put_edge(bag, {:local, from}, to)
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -56,14 +56,14 @@ defmodule Module.LocalsTracker do
|
||||
@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({_set, bag}, tuple, _kind, function, out_neigh) do
|
||||
for to <- out_neigh do
|
||||
put_edge(bag, {:local, 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(bag, {:local, function}, tuple)
|
||||
end
|
||||
|
||||
# Finally marked the new one as reattached
|
||||
@@ -98,38 +98,6 @@ defmodule Module.LocalsTracker do
|
||||
{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),
|
||||
@@ -202,7 +170,7 @@ defmodule Module.LocalsTracker do
|
||||
defp reachable_from(bag, local, vertices) do
|
||||
vertices = Map.put(vertices, local, true)
|
||||
|
||||
Enum.reduce(out_neighbours(bag, {:local, local}), vertices, fn {local, _line, _}, acc ->
|
||||
Enum.reduce(out_neighbours(bag, {:local, local}), vertices, fn {_, _} = local, acc ->
|
||||
case acc do
|
||||
%{^local => true} -> acc
|
||||
_ -> reachable_from(bag, local, acc)
|
||||
@@ -210,20 +178,6 @@ defmodule Module.LocalsTracker do
|
||||
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
|
||||
|
||||
@@ -250,7 +250,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 +259,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
|
||||
|
||||
@@ -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()]
|
||||
@@ -63,10 +40,8 @@ defmodule OptionParser do
|
||||
iex> OptionParser.parse(["--source", "lib"], strict: [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"],
|
||||
...> strict: [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.
|
||||
@@ -83,16 +58,15 @@ defmodule OptionParser do
|
||||
|
||||
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
|
||||
* `:strict` - defines strict switches. Any switch in `argv` that is not
|
||||
specified in the list is returned in the invalid options list.
|
||||
* `:switches` - defines some switches and their types. This function
|
||||
still attempts to parse switches that are not in this list.
|
||||
|
||||
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.
|
||||
If you supply both, an `ArgumentError` exception will be raised.
|
||||
@@ -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`.
|
||||
|
||||
@@ -165,17 +139,16 @@ defmodule OptionParser do
|
||||
# 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:
|
||||
used at some point later (or earlier) **in the same module**:
|
||||
|
||||
{opts, _, _} = OptionParser.parse(["--option-parser-example"], switches: [debug: :boolean])
|
||||
# ... then somewhere in the same module you access it ...
|
||||
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
|
||||
In other words, Elixir will do the correct thing and only parse options that are
|
||||
used by the runtime, ignoring all others. If you would like to parse all switches,
|
||||
regardless if they exist or not, you can force creation of atoms by passing
|
||||
`allow_nonexistent_atoms: true` as option. Use this option with care. It is only
|
||||
useful when you are building command-line applications that receive
|
||||
dynamically-named arguments and must be avoided in long-running systems.
|
||||
|
||||
## Aliases
|
||||
@@ -192,10 +165,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 +184,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 +219,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 +242,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 +267,23 @@ 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}
|
||||
@@ -452,6 +405,7 @@ defmodule OptionParser do
|
||||
option_key = config.aliases[key]
|
||||
|
||||
if key && option_key do
|
||||
# TODO: Remove this in Elixir v2.0
|
||||
IO.warn("multi-letter aliases are deprecated, got: #{inspect(key)}")
|
||||
next_tagged({:default, option_key}, value, original, rest, config)
|
||||
else
|
||||
@@ -493,7 +447,7 @@ 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
|
||||
|
||||
@@ -503,7 +457,7 @@ defmodule OptionParser do
|
||||
["--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 +466,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,6 +558,7 @@ defmodule OptionParser do
|
||||
{strict, true}
|
||||
|
||||
true ->
|
||||
# TODO: Remove this in Elixir v2.0
|
||||
IO.warn("not passing the :switches or :strict option to OptionParser is deprecated")
|
||||
{[], false}
|
||||
end
|
||||
|
||||
+18
-27
@@ -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
|
||||
@@ -626,10 +616,6 @@ 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.
|
||||
@@ -692,9 +678,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
|
||||
|
||||
|
||||
+13
-18
@@ -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"]])
|
||||
|
||||
"""
|
||||
|
||||
@@ -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,16 @@ 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,7 +278,7 @@ 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.
|
||||
"""
|
||||
|
||||
+44
-149
@@ -14,19 +14,11 @@ 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.
|
||||
|
||||
@@ -38,6 +30,15 @@ defmodule Process do
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
|
||||
@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}
|
||||
|
||||
@spec alive?(pid) :: boolean
|
||||
defdelegate alive?(pid), to: :erlang, as: :is_process_alive
|
||||
|
||||
@@ -52,17 +53,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 +66,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 +82,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 +106,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 +170,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 +181,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 +199,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 +209,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
|
||||
|
||||
"""
|
||||
@@ -293,9 +268,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`.
|
||||
|
||||
@@ -401,14 +373,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
|
||||
@@ -448,26 +412,12 @@ defmodule Process do
|
||||
If the process is already dead when calling `Process.monitor/1`, a
|
||||
`:DOWN` message is delivered immediately.
|
||||
|
||||
See [the need for monitoring](https://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
|
||||
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 +429,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 +444,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 +469,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 +485,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 +512,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 +539,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 +547,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 +558,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 +579,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 +594,7 @@ 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.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -726,7 +621,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,7 +634,7 @@ 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
|
||||
def info(pid) do
|
||||
@@ -750,7 +645,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 +670,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.
|
||||
"""
|
||||
|
||||
+179
-330
@@ -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,7 +42,7 @@ 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}) ||
|
||||
@callback unquote(name)(unquote_splicing(type_args)) :: term
|
||||
@@ -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, 'Docs', '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,11 +603,9 @@ 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
|
||||
|
||||
@@ -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) ->
|
||||
@@ -902,7 +751,7 @@ 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
|
||||
@@ -911,7 +760,7 @@ defmodule Protocol do
|
||||
## Helpers
|
||||
|
||||
@doc false
|
||||
def __built_in__ do
|
||||
def __builtin__ do
|
||||
[
|
||||
is_tuple: Tuple,
|
||||
is_atom: Atom,
|
||||
|
||||
+5
-35
@@ -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,33 +57,7 @@ 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"
|
||||
def range?(term)
|
||||
|
||||
+65
-108
@@ -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
|
||||
|
||||
+38
-90
@@ -7,7 +7,7 @@ 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.sigil_r/2`) or [`~R`](`Kernel.sigil_R/2`):
|
||||
|
||||
# A simple regular expression that matches foo anywhere in the string
|
||||
~r/foo/
|
||||
@@ -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 Erlang/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
|
||||
|
||||
@@ -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: ""
|
||||
@@ -155,7 +118,7 @@ defmodule Regex do
|
||||
|
||||
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
|
||||
[`~r`](`Kernel.sigil_r/2`) sigil, or a list of options, as
|
||||
expected by the Erlang's `:re` module.
|
||||
|
||||
It returns `{:ok, regex}` in case of success,
|
||||
@@ -171,7 +134,7 @@ defmodule Regex do
|
||||
|
||||
"""
|
||||
@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 +152,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 +166,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}"
|
||||
@@ -248,8 +211,13 @@ defmodule Regex do
|
||||
"""
|
||||
@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(), :erlang.system_info(:endian)}
|
||||
else
|
||||
{"8.33 2013-05-29", :erlang.system_info(:endian)}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -265,8 +233,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 """
|
||||
@@ -313,11 +281,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
|
||||
@@ -398,17 +366,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
|
||||
@@ -448,29 +406,18 @@ defmodule Regex do
|
||||
@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.
|
||||
@@ -530,11 +477,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)
|
||||
@@ -656,11 +603,11 @@ defmodule Regex do
|
||||
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 +791,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])
|
||||
|
||||
+60
-196
@@ -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,7 +83,6 @@ defmodule Registry do
|
||||
errors:
|
||||
|
||||
require Logger
|
||||
|
||||
Registry.dispatch(Registry.DispatcherTest, "hello", fn entries ->
|
||||
for {pid, {module, function}} <- entries do
|
||||
try do
|
||||
@@ -107,7 +90,7 @@ defmodule Registry do
|
||||
catch
|
||||
kind, reason ->
|
||||
formatted = Exception.format(kind, reason, __STACKTRACE__)
|
||||
Logger.error("Registry.dispatch/3 failed with #{formatted}")
|
||||
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
|
||||
@@ -203,19 +180,11 @@ defmodule Registry do
|
||||
@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
|
||||
@doc since: "1.4.0"
|
||||
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,17 +203,16 @@ 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
|
||||
@doc since: "1.4.0"
|
||||
def register_name({registry, key}, pid) when pid == self() do
|
||||
case register(registry, key, nil) do
|
||||
{:ok, _} -> :yes
|
||||
{:error, _} -> :no
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
@doc since: "1.4.0"
|
||||
def send({registry, key}, msg) do
|
||||
case lookup(registry, key) do
|
||||
[{pid, _}] -> Kernel.send(pid, msg)
|
||||
@@ -253,6 +221,7 @@ defmodule Registry do
|
||||
end
|
||||
|
||||
@doc false
|
||||
@doc since: "1.4.0"
|
||||
def unregister_name({registry, key}) do
|
||||
unregister(registry, key)
|
||||
end
|
||||
@@ -270,23 +239,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
|
||||
|
||||
@@ -322,17 +289,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, [])
|
||||
|
||||
@@ -435,8 +396,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
|
||||
@@ -622,11 +583,9 @@ 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}}]
|
||||
|
||||
"""
|
||||
@@ -691,7 +650,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 -> []
|
||||
@@ -768,8 +727,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)
|
||||
|
||||
@@ -818,7 +777,6 @@ defmodule Registry do
|
||||
|
||||
"""
|
||||
@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 +789,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 +801,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 +817,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 +839,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
|
||||
@@ -919,13 +878,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 +893,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,7 +929,7 @@ 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.
|
||||
|
||||
@@ -1104,20 +1060,20 @@ 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.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, :_, :_})
|
||||
iex> Registry.start_link(keys: :duplicate, name: Registry.MatchTest)
|
||||
iex> {:ok, _} = Registry.register(Registry.MatchTest, "hello", {1, :atom, 1})
|
||||
iex> {:ok, _} = Registry.register(Registry.MatchTest, "hello", {2, :atom, 2})
|
||||
iex> Registry.count_match(Registry.MatchTest, "hello", {1, :_, :_})
|
||||
1
|
||||
iex> Registry.count_match(Registry.CountMatchTest, "hello", {2, :_, :_})
|
||||
iex> Registry.count_match(Registry.MatchTest, "hello", {2, :_, :_})
|
||||
1
|
||||
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:_, :atom, :_})
|
||||
iex> Registry.count_match(Registry.MatchTest, "hello", {:_, :atom, :_})
|
||||
2
|
||||
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:"$1", :_, :"$1"})
|
||||
iex> Registry.count_match(Registry.MatchTest, "hello", {:"$1", :_, :"$1"})
|
||||
2
|
||||
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 1}])
|
||||
iex> Registry.count_match(Registry.MatchTest, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 1}])
|
||||
1
|
||||
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:_, :"$1", :_}, [{:is_atom, :"$1"}])
|
||||
iex> Registry.count_match(Registry.MatchTest, "hello", {:_, :"$1", :_}, [{:is_atom, :"$1"}])
|
||||
2
|
||||
|
||||
"""
|
||||
@@ -1144,80 +1100,6 @@ defmodule Registry do
|
||||
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 +1164,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 +1191,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 +1204,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 +1293,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 +1305,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
|
||||
|
||||
@@ -11,6 +11,7 @@ defmodule Set do
|
||||
@type values :: [value]
|
||||
@type t :: map
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
message = "Use the MapSet module for working with sets"
|
||||
|
||||
defmacrop target(set) do
|
||||
|
||||
+88
-118
@@ -4,7 +4,7 @@ defmodule Stream do
|
||||
|
||||
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,
|
||||
items one by one during enumeration is called a stream. For example,
|
||||
Elixir's `Range` is a stream:
|
||||
|
||||
iex> range = 1..5
|
||||
@@ -22,9 +22,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 +46,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 +63,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 +72,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 +95,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,16 +122,19 @@ defmodule Stream do
|
||||
|
||||
## Transformers
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
@deprecated "Use Stream.chunk_every/2 instead"
|
||||
def chunk(enum, n), do: chunk(enum, n, n, nil)
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
@deprecated "Use Stream.chunk_every/3 instead"
|
||||
def chunk(enum, n, step) do
|
||||
chunk_every(enum, n, step, nil)
|
||||
end
|
||||
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
@deprecated "Use Stream.chunk_every/4 instead"
|
||||
def chunk(enum, n, step, leftover)
|
||||
@@ -157,7 +150,7 @@ defmodule Stream do
|
||||
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.
|
||||
@@ -207,7 +200,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 +217,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 item, acc ->
|
||||
...> if rem(item, 2) == 0 do
|
||||
...> {:cont, Enum.reverse([item | acc]), []}
|
||||
...> else
|
||||
...> {:cont, [element | acc]}
|
||||
...> {:cont, [item | acc]}
|
||||
...> end
|
||||
...> end
|
||||
iex> after_fun = fn
|
||||
@@ -248,8 +241,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 +254,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)}
|
||||
@@ -315,16 +307,16 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@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 +330,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 +362,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,12 +404,12 @@ 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.
|
||||
|
||||
@@ -434,7 +426,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)
|
||||
@@ -461,7 +453,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@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
|
||||
|
||||
@@ -477,11 +469,12 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@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
|
||||
# TODO: Remove on 2.0
|
||||
@deprecated "Use Stream.filter/2 + Stream.map/2 instead"
|
||||
def filter_map(enum, filter, mapper) do
|
||||
lazy(enum, fn f1 -> R.filter_map(filter, mapper, f1) end)
|
||||
@@ -493,7 +486,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 +499,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 +513,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
|
||||
|
||||
@@ -565,15 +558,15 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@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.
|
||||
|
||||
@@ -594,15 +587,13 @@ defmodule Stream do
|
||||
"""
|
||||
@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
|
||||
|
||||
@@ -618,7 +609,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@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
|
||||
|
||||
@@ -661,7 +652,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 +669,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 +699,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 +733,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 +753,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,7 +761,7 @@ 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
|
||||
|
||||
@@ -788,14 +770,14 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@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 +804,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 +821,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
|
||||
|
||||
@@ -995,7 +976,7 @@ 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
|
||||
|
||||
@@ -1009,6 +990,7 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
@deprecated "Use Stream.uniq_by/2 instead"
|
||||
def uniq(enum, fun) do
|
||||
uniq_by(enum, fun)
|
||||
@@ -1016,7 +998,7 @@ defmodule Stream do
|
||||
|
||||
@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,7 +1006,7 @@ 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
|
||||
|
||||
@@ -1036,12 +1018,12 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@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 +1040,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
|
||||
|
||||
@@ -1131,7 +1113,8 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@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
|
||||
@@ -1258,8 +1241,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 +1260,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,18 +1278,6 @@ 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.
|
||||
@@ -1316,7 +1289,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@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 +1312,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 +1338,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 +1346,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
|
||||
|
||||
@@ -1496,7 +1466,7 @@ defmodule Stream do
|
||||
|
||||
"""
|
||||
@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
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ defmodule Stream.Reducers do
|
||||
end
|
||||
|
||||
after_fun = fn {acc_buffer, acc_count} ->
|
||||
if leftover == :discard or acc_count == 0 or acc_count >= count do
|
||||
if leftover == :discard or acc_count == 0 or (step > count and acc_count >= count) do
|
||||
{:cont, []}
|
||||
else
|
||||
{:cont, :lists.reverse(acc_buffer, Enum.take(leftover, count - acc_count)), []}
|
||||
|
||||
+118
-265
@@ -4,15 +4,15 @@ defmodule String do
|
||||
@moduledoc ~S"""
|
||||
A String in Elixir is a UTF-8 encoded binary.
|
||||
|
||||
## Code points and grapheme cluster
|
||||
## Codepoints and grapheme cluster
|
||||
|
||||
The functions in this module act according to the Unicode
|
||||
Standard, version 11.0.0.
|
||||
|
||||
As per the standard, a code point is a single Unicode Character,
|
||||
As per the standard, a codepoint is a single Unicode Character,
|
||||
which may be represented by one or more bytes.
|
||||
|
||||
For example, the code point "é" is two bytes:
|
||||
For example, the codepoint "é" is two bytes:
|
||||
|
||||
iex> byte_size("é")
|
||||
2
|
||||
@@ -24,9 +24,9 @@ defmodule String do
|
||||
|
||||
Furthermore, this module also presents the concept of grapheme cluster
|
||||
(from now on referenced as graphemes). Graphemes can consist of multiple
|
||||
code points that may be perceived as a single character by readers. For
|
||||
example, "é" can be represented either as a single "e with acute" code point
|
||||
or as the letter "e" followed by a "combining acute accent" (two code points):
|
||||
codepoints that may be perceived as a single character by readers. For
|
||||
example, "é" can be represented either as a single "e with acute" codepoint
|
||||
or as the letter "e" followed by a "combining acute accent" (two codepoints):
|
||||
|
||||
iex> string = "\u0065\u0301"
|
||||
iex> byte_size(string)
|
||||
@@ -51,7 +51,7 @@ defmodule String do
|
||||
Standard, but do not contain any of the locale specific behaviour.
|
||||
|
||||
More information about graphemes can be found in the [Unicode
|
||||
Standard Annex #29](https://www.unicode.org/reports/tr29/).
|
||||
Standard Annex #29](http://www.unicode.org/reports/tr29/).
|
||||
The current Elixir version implements Extended Grapheme Cluster
|
||||
algorithm.
|
||||
|
||||
@@ -62,7 +62,7 @@ defmodule String do
|
||||
|
||||
To act according to the Unicode Standard, many functions
|
||||
in this module run in linear time, as they need to traverse
|
||||
the whole string considering the proper Unicode code points.
|
||||
the whole string considering the proper Unicode codepoints.
|
||||
|
||||
For example, `String.length/1` will take longer as
|
||||
the input grows. On the other hand, `Kernel.byte_size/1` always runs
|
||||
@@ -110,7 +110,7 @@ defmodule String do
|
||||
it could still be improved. In this case, since we want to
|
||||
extract a substring from a string, we can use `Kernel.byte_size/1`
|
||||
and `Kernel.binary_part/3` as there is no chance we will slice in
|
||||
the middle of a code point made of more than one byte:
|
||||
the middle of a codepoint made of more than one byte:
|
||||
|
||||
iex> take_prefix = fn full, prefix ->
|
||||
...> base = byte_size(prefix)
|
||||
@@ -132,18 +132,18 @@ defmodule String do
|
||||
On the other hand, if you want to dynamically slice a string
|
||||
based on an integer value, then using `String.slice/3` is the
|
||||
best option as it guarantees we won't incorrectly split a valid
|
||||
code point into multiple bytes.
|
||||
codepoint into multiple bytes.
|
||||
|
||||
## Integer code points
|
||||
## Integer codepoints
|
||||
|
||||
Although code points could be represented as integers, this
|
||||
module represents all code points as strings. For example:
|
||||
Although codepoints could be represented as integers, this
|
||||
module represents all codepoints as strings. For example:
|
||||
|
||||
iex> String.codepoints("olá")
|
||||
["o", "l", "á"]
|
||||
|
||||
There are a couple of ways to retrieve a character integer
|
||||
code point. One may use the `?` construct:
|
||||
codepoint. One may use the `?` construct:
|
||||
|
||||
iex> ?o
|
||||
111
|
||||
@@ -157,7 +157,7 @@ defmodule String do
|
||||
iex> aacute
|
||||
225
|
||||
|
||||
As we have seen above, code points can be inserted into
|
||||
As we have seen above, codepoints can be inserted into
|
||||
a string by their hexadecimal code:
|
||||
|
||||
"ol\u0061\u0301" #=>
|
||||
@@ -168,11 +168,11 @@ defmodule String do
|
||||
The UTF-8 encoding is self-synchronizing. This means that
|
||||
if malformed data (i.e., data that is not possible according
|
||||
to the definition of the encoding) is encountered, only one
|
||||
code point needs to be rejected.
|
||||
codepoint needs to be rejected.
|
||||
|
||||
This module relies on this behaviour to ignore such invalid
|
||||
characters. For example, `length/1` will return
|
||||
a correct result even if an invalid code point is fed into it.
|
||||
a correct result even if an invalid codepoint is fed into it.
|
||||
|
||||
In other words, this module expects invalid data to be detected
|
||||
elsewhere, usually when retrieving data from the external source.
|
||||
@@ -183,7 +183,7 @@ defmodule String do
|
||||
## Patterns
|
||||
|
||||
Many functions in this module work with patterns. For example,
|
||||
`String.split/2` can split a string into multiple strings given
|
||||
`String.split/2` can split a string into multiple patterns given
|
||||
a pattern. This pattern can be a string, a list of strings or
|
||||
a compiled pattern:
|
||||
|
||||
@@ -198,24 +198,24 @@ defmodule String do
|
||||
["foo", "bar", ""]
|
||||
|
||||
The compiled pattern is useful when the same match will
|
||||
be done over and over again. Note though that the compiled
|
||||
be done over and over again. Note though the compiled
|
||||
pattern cannot be stored in a module attribute as the pattern
|
||||
is generated at runtime and does not survive compile time.
|
||||
is generated at runtime and does not survive compile term.
|
||||
"""
|
||||
|
||||
@typedoc """
|
||||
A UTF-8 encoded binary.
|
||||
|
||||
The types `String.t()` and `binary()` are equivalent to analysis tools.
|
||||
Note `String.t()` and `binary()` are equivalent to analysis tools.
|
||||
Although, for those reading the documentation, `String.t()` implies
|
||||
it is a UTF-8 encoded binary.
|
||||
"""
|
||||
@type t :: binary
|
||||
|
||||
@typedoc "A UTF-8 code point. It may be one or more bytes."
|
||||
@typedoc "A UTF-8 codepoint. It may be one or more bytes."
|
||||
@type codepoint :: t
|
||||
|
||||
@typedoc "Multiple code points that may be perceived as a single character by readers"
|
||||
@typedoc "Multiple codepoints that may be perceived as a single character by readers"
|
||||
@type grapheme :: t
|
||||
|
||||
@typedoc "Pattern used in functions like `replace/3` and `split/2`"
|
||||
@@ -307,9 +307,9 @@ defmodule String do
|
||||
defdelegate split(binary), to: String.Break
|
||||
|
||||
@doc ~S"""
|
||||
Divides a string into parts based on a pattern.
|
||||
Divides a string into substrings based on a pattern.
|
||||
|
||||
Returns a list of these parts. The pattern can
|
||||
Returns a list of these substrings. The pattern can
|
||||
be a string, a list of strings, a regular expression,
|
||||
or a compiled pattern.
|
||||
|
||||
@@ -387,15 +387,15 @@ defmodule String do
|
||||
iex> String.split("abc", "", parts: 3)
|
||||
["", "a", "bc"]
|
||||
|
||||
Be aware that this function can split within or across grapheme boundaries.
|
||||
Note this function can split within or across grapheme boundaries.
|
||||
For example, take the grapheme "é" which is made of the characters
|
||||
"e" and the acute accent. The following will split the string into two parts:
|
||||
"e" and the acute accent. The following returns the acute accent separately:
|
||||
|
||||
iex> String.split(String.normalize("é", :nfd), "e")
|
||||
["", "́"]
|
||||
|
||||
However, if "é" is represented by the single character "e with acute"
|
||||
accent, then it will split the string into just one part:
|
||||
accent, then it will return "e with acute":
|
||||
|
||||
iex> String.split(String.normalize("é", :nfc), "e")
|
||||
["é"]
|
||||
@@ -460,10 +460,10 @@ defmodule String do
|
||||
This is in contrast to `split/3` which splits the
|
||||
entire string upfront.
|
||||
|
||||
This function does not support regular expressions
|
||||
by design. When using regular expressions, it is often
|
||||
more efficient to have the regular expressions traverse
|
||||
the string at once than in parts, like this function does.
|
||||
Note splitter does not support regular expressions
|
||||
(as it is often more efficient to have the regular
|
||||
expressions traverse the string at once than in
|
||||
multiple passes).
|
||||
|
||||
## Options
|
||||
|
||||
@@ -632,24 +632,8 @@ defmodule String do
|
||||
"leña"
|
||||
|
||||
"""
|
||||
# TODO: Fully deprecate it on v1.10
|
||||
@doc deprecated:
|
||||
"Use :unicode.characters_to_nfc_binary/1 or :unicode.characters_to_nfd_binary/1 instead"
|
||||
def normalize(string, form)
|
||||
|
||||
def normalize(string, :nfd) do
|
||||
case :unicode.characters_to_nfd_binary(string) do
|
||||
string when is_binary(string) -> string
|
||||
{:error, bad, rest} -> bad <> normalize(rest, :nfd)
|
||||
end
|
||||
end
|
||||
|
||||
def normalize(string, :nfc) do
|
||||
case :unicode.characters_to_nfc_binary(string) do
|
||||
string when is_binary(string) -> string
|
||||
{:error, bad, rest} -> bad <> normalize(rest, :nfc)
|
||||
end
|
||||
end
|
||||
@spec normalize(t, atom) :: t
|
||||
defdelegate normalize(string, form), to: String.Normalizer
|
||||
|
||||
@doc """
|
||||
Converts all characters in the given string to uppercase according to `mode`.
|
||||
@@ -798,10 +782,12 @@ defmodule String do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.trim_trailing/1 instead"
|
||||
defdelegate rstrip(binary), to: String.Break, as: :trim_trailing
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.trim_trailing/2 with a binary as second argument instead"
|
||||
def rstrip(string, char) when is_integer(char) do
|
||||
replace_trailing(string, <<char::utf8>>, "")
|
||||
@@ -830,7 +816,7 @@ defmodule String do
|
||||
"ola ola world"
|
||||
|
||||
"""
|
||||
@spec replace_leading(t, t, t) :: t
|
||||
@spec replace_leading(t, t, t) :: t | no_return
|
||||
def replace_leading(string, match, replacement)
|
||||
when is_binary(string) and is_binary(match) and is_binary(replacement) do
|
||||
if match == "" do
|
||||
@@ -887,7 +873,7 @@ defmodule String do
|
||||
"hello mundo mundo"
|
||||
|
||||
"""
|
||||
@spec replace_trailing(t, t, t) :: t
|
||||
@spec replace_trailing(t, t, t) :: t | no_return
|
||||
def replace_trailing(string, match, replacement)
|
||||
when is_binary(string) and is_binary(match) and is_binary(replacement) do
|
||||
if match == "" do
|
||||
@@ -1011,22 +997,26 @@ defmodule String do
|
||||
defp append_unless_empty(prefix, suffix), do: prefix <> suffix
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.trim_leading/1 instead"
|
||||
defdelegate lstrip(binary), to: String.Break, as: :trim_leading
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.trim_leading/2 with a binary as second argument instead"
|
||||
def lstrip(string, char) when is_integer(char) do
|
||||
replace_leading(string, <<char::utf8>>, "")
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.trim/1 instead"
|
||||
def strip(string) do
|
||||
trim(string)
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.trim/2 with a binary second argument instead"
|
||||
def strip(string, char) do
|
||||
trim(string, <<char::utf8>>)
|
||||
@@ -1046,7 +1036,7 @@ defmodule String do
|
||||
defdelegate trim_leading(string), to: String.Break
|
||||
|
||||
@doc """
|
||||
Returns a string where all leading `to_trim` characters have been removed.
|
||||
Returns a string where all leading `to_trim`s have been removed.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1076,7 +1066,7 @@ defmodule String do
|
||||
defdelegate trim_trailing(string), to: String.Break
|
||||
|
||||
@doc """
|
||||
Returns a string where all trailing `to_trim` characters have been removed.
|
||||
Returns a string where all trailing `to_trim`s have been removed.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1110,7 +1100,7 @@ defmodule String do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a string where all leading and trailing `to_trim` characters have been
|
||||
Returns a string where all leading and trailing `to_trim`s have been
|
||||
removed.
|
||||
|
||||
## Examples
|
||||
@@ -1139,7 +1129,7 @@ defmodule String do
|
||||
When `count` is less than or equal to the length of `string`,
|
||||
given `string` is returned.
|
||||
|
||||
Raises `ArgumentError` if the given `padding` contains a non-string element.
|
||||
Raises `ArgumentError` if the given `padding` contains non-string element.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1181,7 +1171,7 @@ defmodule String do
|
||||
When `count` is less than or equal to the length of `string`,
|
||||
given `string` is returned.
|
||||
|
||||
Raises `ArgumentError` if the given `padding` contains a non-string element.
|
||||
Raises `ArgumentError` if the given `padding` contains non-string element.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1251,24 +1241,28 @@ defmodule String do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.pad_leading/2 instead"
|
||||
def rjust(subject, len) do
|
||||
rjust(subject, len, ?\s)
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.pad_leading/3 with a binary padding instead"
|
||||
def rjust(subject, len, pad) when is_integer(pad) and is_integer(len) and len >= 0 do
|
||||
pad(:leading, subject, len, [<<pad::utf8>>])
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.pad_trailing/2 instead"
|
||||
def ljust(subject, len) do
|
||||
ljust(subject, len, ?\s)
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.pad_trailing/3 with a binary padding instead"
|
||||
def ljust(subject, len, pad) when is_integer(pad) and is_integer(len) and len >= 0 do
|
||||
pad(:trailing, subject, len, [<<pad::utf8>>])
|
||||
@@ -1278,13 +1272,8 @@ defmodule String do
|
||||
Returns a new string created by replacing occurrences of `pattern` in
|
||||
`subject` with `replacement`.
|
||||
|
||||
The `subject` is always a string.
|
||||
|
||||
The `pattern` may be a string, a regular expression, or a compiled pattern.
|
||||
|
||||
The `replacement` may be a string or a function that receives the matched
|
||||
pattern and must return the replacement as a string or iodata.
|
||||
|
||||
By default it replaces all occurrences but this behaviour can be controlled
|
||||
through the `:global` option; see the "Options" section below.
|
||||
|
||||
@@ -1294,6 +1283,12 @@ defmodule String do
|
||||
with `replacement`, otherwise only the first occurrence is
|
||||
replaced. Defaults to `true`
|
||||
|
||||
* `:insert_replaced` - (integer or list of integers) specifies the position
|
||||
where to insert the replaced part inside the `replacement`. If any
|
||||
position given in the `:insert_replaced` option is larger than the
|
||||
replacement string, or is negative, an `ArgumentError` is raised. See the
|
||||
examples below
|
||||
|
||||
## Examples
|
||||
|
||||
iex> String.replace("a,b,c", ",", "-")
|
||||
@@ -1302,12 +1297,6 @@ defmodule String do
|
||||
iex> String.replace("a,b,c", ",", "-", global: false)
|
||||
"a-b,c"
|
||||
|
||||
The pattern may also be a list of strings and the replacement may also
|
||||
be a function that receives the matched patterns:
|
||||
|
||||
iex> String.replace("a,b,c", ["a", "c"], fn <<char>> -> <<char + 1>> end)
|
||||
"b,b,d"
|
||||
|
||||
When the pattern is a regular expression, one can give `\N` or
|
||||
`\g{N}` in the `replacement` string to access a specific capture in the
|
||||
regular expression:
|
||||
@@ -1320,11 +1309,25 @@ defmodule String do
|
||||
giving `\0`, one can inject the whole matched pattern in the replacement
|
||||
string.
|
||||
|
||||
When the pattern is a string, a developer can use the replaced part inside
|
||||
the `replacement` by using the `:insert_replaced` option and specifying the
|
||||
position(s) inside the `replacement` where the string pattern will be
|
||||
inserted:
|
||||
|
||||
iex> String.replace("a,b,c", "b", "[]", insert_replaced: 1)
|
||||
"a,[b],c"
|
||||
|
||||
iex> String.replace("a,b,c", ",", "[]", insert_replaced: 2)
|
||||
"a[],b[],c"
|
||||
|
||||
iex> String.replace("a,b,c", ",", "[]", insert_replaced: [1, 1])
|
||||
"a[,,]b[,,]c"
|
||||
|
||||
A compiled pattern can also be given:
|
||||
|
||||
iex> pattern = :binary.compile_pattern(",")
|
||||
iex> String.replace("a,b,c", pattern, "[]")
|
||||
"a[]b[]c"
|
||||
iex> String.replace("a,b,c", pattern, "[]", insert_replaced: 2)
|
||||
"a[],b[],c"
|
||||
|
||||
When an empty string is provided as a `pattern`, the function will treat it as
|
||||
an implicit empty string between each grapheme and the string will be
|
||||
@@ -1338,89 +1341,43 @@ defmodule String do
|
||||
"ELIXIR"
|
||||
|
||||
"""
|
||||
@spec replace(t, pattern | Regex.t(), t | (t -> t | iodata), keyword) :: t
|
||||
@spec replace(t, pattern | Regex.t(), t, keyword) :: t
|
||||
def replace(subject, pattern, replacement, options \\ [])
|
||||
def replace(subject, "", "", _), do: subject
|
||||
|
||||
def replace(subject, %{__struct__: Regex} = regex, replacement, options)
|
||||
when is_binary(replacement) or is_function(replacement, 1) do
|
||||
Regex.replace(regex, subject, replacement, options)
|
||||
end
|
||||
|
||||
def replace(subject, "", "", _) when is_binary(subject) do
|
||||
subject
|
||||
end
|
||||
|
||||
def replace(subject, "", replacement, options)
|
||||
when is_binary(subject) and is_binary(replacement) do
|
||||
def replace(subject, "", replacement, options) do
|
||||
if Keyword.get(options, :global, true) do
|
||||
IO.iodata_to_binary([replacement | intersperse_bin(subject, replacement)])
|
||||
IO.iodata_to_binary([replacement | intersperse(subject, replacement)])
|
||||
else
|
||||
replacement <> subject
|
||||
end
|
||||
end
|
||||
|
||||
def replace(subject, "", replacement, options)
|
||||
when is_binary(subject) and is_function(replacement, 1) do
|
||||
if Keyword.get(options, :global, true) do
|
||||
IO.iodata_to_binary([replacement.("") | intersperse_fun(subject, replacement)])
|
||||
def replace(subject, pattern, replacement, options) when is_binary(replacement) do
|
||||
if Regex.regex?(pattern) do
|
||||
Regex.replace(pattern, subject, replacement, global: options[:global])
|
||||
else
|
||||
IO.iodata_to_binary([replacement.("") | subject])
|
||||
opts = translate_replace_options(options)
|
||||
:binary.replace(subject, pattern, replacement, opts)
|
||||
end
|
||||
end
|
||||
|
||||
def replace(subject, pattern, replacement, options) when is_binary(subject) do
|
||||
if insert = Keyword.get(options, :insert_replaced) do
|
||||
IO.warn(
|
||||
"String.replace/4 with :insert_replaced option is deprecated. " <>
|
||||
"Please use :binary.replace/4 instead or pass an anonymous function as replacement"
|
||||
)
|
||||
|
||||
binary_options = if Keyword.get(options, :global) != false, do: [:global], else: []
|
||||
:binary.replace(subject, pattern, replacement, [insert_replaced: insert] ++ binary_options)
|
||||
else
|
||||
matches =
|
||||
if Keyword.get(options, :global, true) do
|
||||
:binary.matches(subject, pattern)
|
||||
else
|
||||
case :binary.match(subject, pattern) do
|
||||
:nomatch -> []
|
||||
match -> [match]
|
||||
end
|
||||
end
|
||||
|
||||
IO.iodata_to_binary(do_replace(subject, matches, replacement, 0))
|
||||
end
|
||||
end
|
||||
|
||||
defp intersperse_bin(subject, replacement) do
|
||||
defp intersperse(subject, replacement) do
|
||||
case next_grapheme(subject) do
|
||||
{current, rest} -> [current, replacement | intersperse_bin(rest, replacement)]
|
||||
{current, rest} -> [current, replacement | intersperse(rest, replacement)]
|
||||
nil -> []
|
||||
end
|
||||
end
|
||||
|
||||
defp intersperse_fun(subject, replacement) do
|
||||
case next_grapheme(subject) do
|
||||
{current, rest} -> [current, replacement.("") | intersperse_fun(rest, replacement)]
|
||||
nil -> []
|
||||
end
|
||||
end
|
||||
defp translate_replace_options(options) do
|
||||
global = if Keyword.get(options, :global) != false, do: [:global], else: []
|
||||
|
||||
defp do_replace(subject, [], _, n) do
|
||||
[binary_part(subject, n, byte_size(subject) - n)]
|
||||
end
|
||||
insert =
|
||||
if insert = Keyword.get(options, :insert_replaced),
|
||||
do: [{:insert_replaced, insert}],
|
||||
else: []
|
||||
|
||||
defp do_replace(subject, [{start, length} | matches], replacement, n) do
|
||||
prefix = binary_part(subject, n, start - n)
|
||||
|
||||
middle =
|
||||
if is_binary(replacement) do
|
||||
replacement
|
||||
else
|
||||
replacement.(binary_part(subject, start, length))
|
||||
end
|
||||
|
||||
[prefix, middle | do_replace(subject, matches, replacement, start + length)]
|
||||
global ++ insert
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -1489,9 +1446,9 @@ defmodule String do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all code points in the string.
|
||||
Returns all codepoints in the string.
|
||||
|
||||
For details about code points and graphemes, see the `String` module documentation.
|
||||
For details about codepoints and graphemes, see the `String` module documentation.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1514,46 +1471,23 @@ defmodule String do
|
||||
@spec codepoints(t) :: [codepoint]
|
||||
defdelegate codepoints(string), to: String.Unicode
|
||||
|
||||
@doc ~S"""
|
||||
Returns the next code point in a string.
|
||||
@doc """
|
||||
Returns the next codepoint in a string.
|
||||
|
||||
The result is a tuple with the code point and the
|
||||
The result is a tuple with the codepoint and the
|
||||
remainder of the string or `nil` in case
|
||||
the string reached its end.
|
||||
|
||||
As with other functions in the `String` module, `next_codepoint/1`
|
||||
works with binaries that are invalid UTF-8. If the string starts
|
||||
with a sequence of bytes that is not valid in UTF-8 encoding, the
|
||||
first element of the returned tuple is a binary with the first byte.
|
||||
As with other functions in the String module, this
|
||||
function does not check for the validity of the codepoint.
|
||||
That said, if an invalid codepoint is found, it will
|
||||
be returned by this function.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> String.next_codepoint("olá")
|
||||
{"o", "lá"}
|
||||
|
||||
iex> invalid = "\x80\x80OK" # first two bytes are invalid in UTF-8
|
||||
iex> {_, rest} = String.next_codepoint(invalid)
|
||||
{<<128>>, <<128, 79, 75>>}
|
||||
iex> String.next_codepoint(rest)
|
||||
{<<128>>, "OK"}
|
||||
|
||||
## Comparison with binary pattern matching
|
||||
|
||||
Binary pattern matching provides a similar way to decompose
|
||||
a string:
|
||||
|
||||
iex> <<codepoint::utf8, rest::binary>> = "Elixir"
|
||||
"Elixir"
|
||||
iex> codepoint
|
||||
69
|
||||
iex> rest
|
||||
"lixir"
|
||||
|
||||
though not entirely equivalent because `codepoint` comes as
|
||||
an integer, and the pattern won't match invalid UTF-8.
|
||||
|
||||
Binary pattern matching, however, is simpler and more efficient,
|
||||
so pick the option that better suits your use case.
|
||||
"""
|
||||
@compile {:inline, next_codepoint: 1}
|
||||
@spec next_codepoint(t) :: {codepoint, t} | nil
|
||||
@@ -1588,6 +1522,7 @@ defmodule String do
|
||||
def valid?(_), do: false
|
||||
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
@deprecated "Use String.valid?/1 instead"
|
||||
def valid_character?(string) do
|
||||
case string do
|
||||
@@ -1655,14 +1590,14 @@ defmodule String do
|
||||
defp make_chunk_pred(:valid), do: &valid?/1
|
||||
defp make_chunk_pred(:printable), do: &printable?/1
|
||||
|
||||
@doc ~S"""
|
||||
@doc """
|
||||
Returns Unicode graphemes in the string as per Extended Grapheme
|
||||
Cluster algorithm.
|
||||
|
||||
The algorithm is outlined in the [Unicode Standard Annex #29,
|
||||
Unicode Text Segmentation](https://www.unicode.org/reports/tr29/).
|
||||
Unicode Text Segmentation](http://www.unicode.org/reports/tr29/).
|
||||
|
||||
For details about code points and graphemes, see the `String` module documentation.
|
||||
For details about codepoints and graphemes, see the `String` module documentation.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -2127,17 +2062,17 @@ defmodule String do
|
||||
iex> String.contains?("elixir of life", ["", "other"])
|
||||
true
|
||||
|
||||
Be aware that this function can match within or across grapheme boundaries.
|
||||
Note this function can match within or across grapheme boundaries.
|
||||
For example, take the grapheme "é" which is made of the characters
|
||||
"e" and the acute accent. The following returns `true`:
|
||||
|
||||
iex> String.contains?(:unicode.characters_to_nfd_binary("é"), "e")
|
||||
iex> String.contains?(String.normalize("é", :nfd), "e")
|
||||
true
|
||||
|
||||
However, if "é" is represented by the single character "e with acute"
|
||||
accent, then it will return `false`:
|
||||
|
||||
iex> String.contains?(:unicode.characters_to_nfc_binary("é"), "e")
|
||||
iex> String.contains?(String.normalize("é", :nfc), "e")
|
||||
false
|
||||
|
||||
"""
|
||||
@@ -2157,8 +2092,8 @@ defmodule String do
|
||||
@doc """
|
||||
Converts a string into a charlist.
|
||||
|
||||
Specifically, this function takes a UTF-8 encoded binary and returns a list of its integer
|
||||
code points. It is similar to `codepoints/1` except that the latter returns a list of code points as
|
||||
Specifically, this functions takes a UTF-8 encoded binary and returns a list of its integer
|
||||
codepoints. It is similar to `codepoints/1` except that the latter returns a list of codepoints as
|
||||
strings.
|
||||
|
||||
In case you need to work with bytes, take a look at the
|
||||
@@ -2195,7 +2130,8 @@ defmodule String do
|
||||
By default, the maximum number of atoms is `1_048_576`. This limit
|
||||
can be raised or lowered using the VM option `+t`.
|
||||
|
||||
The maximum atom size is of 255 Unicode code points.
|
||||
The maximum atom size is of 255 characters. Prior to Erlang/OTP 20,
|
||||
only latin1 characters are allowed.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -2213,7 +2149,8 @@ defmodule String do
|
||||
@doc """
|
||||
Converts a string to an existing atom.
|
||||
|
||||
The maximum atom size is of 255 Unicode code points.
|
||||
The maximum atom size is of 255 characters. Prior to Erlang/OTP 20,
|
||||
only latin1 characters are allowed.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -2235,11 +2172,6 @@ defmodule String do
|
||||
@doc """
|
||||
Returns an integer whose text representation is `string`.
|
||||
|
||||
`string` must be the string representation of an integer.
|
||||
Otherwise, an `ArgumentError` will be raised. If you want
|
||||
to parse a string that may contain an ill-formatted integer,
|
||||
use `Integer.parse/1`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
@@ -2247,11 +2179,6 @@ defmodule String do
|
||||
iex> String.to_integer("123")
|
||||
123
|
||||
|
||||
Passing a string that does not represent an integer leads to an error:
|
||||
|
||||
String.to_integer("invalid data")
|
||||
#=> ** (ArgumentError) argument error
|
||||
|
||||
"""
|
||||
@spec to_integer(String.t()) :: integer
|
||||
def to_integer(string) do
|
||||
@@ -2301,90 +2228,15 @@ defmodule String do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Computes the bag distance between two strings.
|
||||
|
||||
Returns a float value between 0 and 1 representing the bag
|
||||
Returns a float value between 0 (equates to no similarity) and 1 (is an exact match)
|
||||
representing [Jaro](https://en.wikipedia.org/wiki/Jaro–Winkler_distance)
|
||||
distance between `string1` and `string2`.
|
||||
|
||||
The bag distance is meant to be an efficient approximation
|
||||
of the distance between two strings to quickly rule out strings
|
||||
that are largely different.
|
||||
|
||||
The algorithm is outlined in the "String Matching with Metric
|
||||
Trees Using an Approximate Distance" paper by Ilaria Bartolini,
|
||||
Paolo Ciaccia, and Marco Patella.
|
||||
The Jaro distance metric is designed and best suited for short strings such as person names.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> String.bag_distance("abc", "")
|
||||
0.0
|
||||
iex> String.bag_distance("abcd", "a")
|
||||
0.25
|
||||
iex> String.bag_distance("abcd", "ab")
|
||||
0.5
|
||||
iex> String.bag_distance("abcd", "abc")
|
||||
0.75
|
||||
iex> String.bag_distance("abcd", "abcd")
|
||||
1.0
|
||||
|
||||
"""
|
||||
@spec bag_distance(t, t) :: float
|
||||
@doc since: "1.8.0"
|
||||
def bag_distance(_string, ""), do: 0.0
|
||||
def bag_distance("", _string), do: 0.0
|
||||
|
||||
def bag_distance(string1, string2) do
|
||||
{bag1, length1} = string_to_bag(string1, %{}, 0)
|
||||
{bag2, length2} = string_to_bag(string2, %{}, 0)
|
||||
|
||||
diff1 = bag_difference(bag1, bag2)
|
||||
diff2 = bag_difference(bag2, bag1)
|
||||
|
||||
1 - max(diff1, diff2) / max(length1, length2)
|
||||
end
|
||||
|
||||
defp string_to_bag(string, bag, length) do
|
||||
case next_grapheme(string) do
|
||||
{char, rest} ->
|
||||
bag =
|
||||
case bag do
|
||||
%{^char => current} -> %{bag | char => current + 1}
|
||||
%{} -> Map.put(bag, char, 1)
|
||||
end
|
||||
|
||||
string_to_bag(rest, bag, length + 1)
|
||||
|
||||
nil ->
|
||||
{bag, length}
|
||||
end
|
||||
end
|
||||
|
||||
defp bag_difference(bag1, bag2) do
|
||||
Enum.reduce(bag1, 0, fn {char, count1}, sum ->
|
||||
case bag2 do
|
||||
%{^char => count2} -> sum + max(count1 - count2, 0)
|
||||
%{} -> sum + count1
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Computes the Jaro distance (similarity) between two strings.
|
||||
|
||||
Returns a float value between `0.0` (equates to no similarity) and `1.0`
|
||||
(is an exact match) representing [Jaro](https://en.wikipedia.org/wiki/Jaro-Winkler_distance)
|
||||
distance between `string1` and `string2`.
|
||||
|
||||
The Jaro distance metric is designed and best suited for short
|
||||
strings such as person names. Elixir itself uses this function
|
||||
to provide the "did you mean?" functionality. For instance, when you
|
||||
are calling a function in a module and you have a typo in the
|
||||
function name, we attempt to suggest the most similar function
|
||||
name available, if any, based on the `jaro_distance/2` score.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> String.jaro_distance("Dwayne", "Duane")
|
||||
iex> String.jaro_distance("dwayne", "duane")
|
||||
0.8222222222222223
|
||||
iex> String.jaro_distance("even", "odd")
|
||||
0.0
|
||||
@@ -2492,6 +2344,7 @@ defmodule String do
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove by 2.0
|
||||
@deprecated "Use String.to_charlist/1 instead"
|
||||
@spec to_char_list(t) :: charlist
|
||||
def to_char_list(string), do: String.to_charlist(string)
|
||||
|
||||
@@ -31,14 +31,14 @@ defmodule StringIO do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> StringIO.open("foo", [], fn pid ->
|
||||
iex> StringIO.open("foo", [], fn(pid) ->
|
||||
...> input = IO.gets(pid, ">")
|
||||
...> IO.write(pid, "The input was #{input}")
|
||||
...> StringIO.contents(pid)
|
||||
...> end)
|
||||
{:ok, {"", "The input was foo"}}
|
||||
|
||||
iex> StringIO.open("foo", [capture_prompt: true], fn pid ->
|
||||
iex> StringIO.open("foo", [capture_prompt: true], fn(pid) ->
|
||||
...> input = IO.gets(pid, ">")
|
||||
...> IO.write(pid, "The input was #{input}")
|
||||
...> StringIO.contents(pid)
|
||||
@@ -91,7 +91,7 @@ defmodule StringIO do
|
||||
iex> StringIO.contents(pid)
|
||||
{"", ">"}
|
||||
|
||||
iex> StringIO.open("foo", fn pid ->
|
||||
iex> StringIO.open("foo", fn(pid) ->
|
||||
...> input = IO.gets(pid, ">")
|
||||
...> IO.write(pid, "The input was #{input}")
|
||||
...> StringIO.contents(pid)
|
||||
@@ -101,7 +101,7 @@ defmodule StringIO do
|
||||
"""
|
||||
@spec open(binary, keyword) :: {:ok, pid}
|
||||
@spec open(binary, (pid -> res)) :: {:ok, res} when res: var
|
||||
def open(string, options_or_function \\ [])
|
||||
def open(path, options_or_function \\ [])
|
||||
|
||||
def open(string, options_or_function) when is_binary(string) and is_list(options_or_function) do
|
||||
GenServer.start_link(__MODULE__, {string, options_or_function}, [])
|
||||
@@ -279,8 +279,6 @@ defmodule StringIO do
|
||||
{_, _, _} ->
|
||||
{{:error, req}, state}
|
||||
end
|
||||
rescue
|
||||
ArgumentError -> {{:error, req}, state}
|
||||
end
|
||||
|
||||
## get_chars
|
||||
|
||||
+154
-144
@@ -97,11 +97,50 @@ defmodule Supervisor do
|
||||
Supervisors support different strategies; in the example above, we
|
||||
have chosen `:one_for_one`. Furthermore, each supervisor can have many
|
||||
workers and/or supervisors as children, with each one having its own
|
||||
configuration (as outlined in the "Child specification" section).
|
||||
configuration (as outlined in the “Child specification” section).
|
||||
|
||||
The rest of this document will cover how child processes are specified,
|
||||
how they can be started and stopped, different supervision strategies
|
||||
and more.
|
||||
The rest of this document will cover how child processes are started,
|
||||
how they can be specified, different supervision strategies and more.
|
||||
|
||||
## Start and shutdown
|
||||
|
||||
When the supervisor starts, it traverses all child specifications and
|
||||
then starts each child in the order they are defined. This is done by
|
||||
calling the function defined under the `:start` key in the child
|
||||
specification and typically defaults to `start_link/1`.
|
||||
|
||||
The `start_link/1` (or a custom) is then called for each child process.
|
||||
The `start_link/1` function must return `{:ok, pid}` where `pid` is the
|
||||
process identifier of a new process that is linked to the supervisor.
|
||||
The child process usually starts its work by executing the `init/1`
|
||||
callback. Generally speaking, the `init` callback is where we initialize
|
||||
and configure the child process.
|
||||
|
||||
The shutdown process happens in reverse order.
|
||||
|
||||
When a supervisor shuts down, it terminates all children in the opposite
|
||||
order they are listed. The termination happens by sending a shutdown exit
|
||||
signal, via `Process.exit(child_pid, :shutdown)`, to the child process and
|
||||
then awaiting for a time interval for the child process to terminate. This
|
||||
interval defaults to 5000 milliseconds. If the child process does not
|
||||
terminate in this interval, the supervisor abruptly terminates the child
|
||||
with reason `:kill`. The shutdown time can be configured in the child
|
||||
specification which is fully detailed in the next section.
|
||||
|
||||
If the child process is not trapping exits, it will shutdown immediately
|
||||
when it receives the first exit signal. If the child process is trapping
|
||||
exits, then the `terminate` callback is invoked, and the child process
|
||||
must terminate in a reasonable time interval before being abruptly
|
||||
terminated by the supervisor.
|
||||
|
||||
In other words, if it is important that a process cleans after itself
|
||||
when your application or the supervision tree is shutting down, then
|
||||
this process must trap exits and its child specification should specify
|
||||
the proper `:shutdown` value, ensuring it terminates within a reasonable
|
||||
interval.
|
||||
|
||||
Now that we understand the start and shutdown process, let's take a
|
||||
complete look at all of the options provided in the child specification.
|
||||
|
||||
## Child specification
|
||||
|
||||
@@ -144,11 +183,11 @@ defmodule Supervisor do
|
||||
terminated using `Process.exit(child, :kill)`.
|
||||
|
||||
* any integer >= 0 - the amount of time in milliseconds that the
|
||||
supervisor will wait for its children to terminate after emitting a
|
||||
supervisor will wait for children to terminate after emitting a
|
||||
`Process.exit(child, :shutdown)` signal. If the child process is
|
||||
not trapping exits, the initial `:shutdown` signal will terminate
|
||||
the child process immediately. If the child process is trapping
|
||||
exits, it has the given amount of time to terminate.
|
||||
exits, it has the given amount of time in milliseconds to terminate.
|
||||
If it doesn't terminate within the specified time, the child process
|
||||
is unconditionally terminated by the supervisor via
|
||||
`Process.exit(child, :kill)`.
|
||||
@@ -156,7 +195,7 @@ defmodule Supervisor do
|
||||
* `:infinity` - works as an integer except the supervisor will wait
|
||||
indefinitely for the child to terminate. If the child process is a
|
||||
supervisor, the recommended value is `:infinity` to give the supervisor
|
||||
and its children enough time to shut down. This option can be used with
|
||||
and its children enough time to shutdown. This option can be used with
|
||||
regular workers but doing so is discouraged and requires extreme care.
|
||||
If not used carefully, the child process will never terminate,
|
||||
preventing your application from terminating as well.
|
||||
@@ -208,31 +247,30 @@ defmodule Supervisor do
|
||||
|
||||
The supervisor will then invoke `Stack.child_spec([:hello])` to retrieve a
|
||||
child specification. Now the `Stack` module is responsible for building its
|
||||
own specification, for example, we could write:
|
||||
own specification. By default, `use GenServer` defines a `Stack.child_spec/1`
|
||||
function which returns the same child specification we had before:
|
||||
|
||||
def child_spec(arg) do
|
||||
%{
|
||||
id: Stack,
|
||||
start: {Stack, :start_link, [arg]}
|
||||
}
|
||||
end
|
||||
%{
|
||||
id: Stack,
|
||||
start: {Stack, :start_link, [[:hello]]}
|
||||
}
|
||||
|
||||
Luckily for us, `use GenServer` already defines a `Stack.child_spec/1`
|
||||
exactly like above. If you need to customize the `GenServer`, you can
|
||||
pass the options directly to `use GenServer`:
|
||||
|
||||
use GenServer, restart: :transient
|
||||
|
||||
Finally, note it is also possible to simply pass the `Stack` module as
|
||||
a child:
|
||||
It is also possible to simply pass the `Stack` module as a child:
|
||||
|
||||
children = [
|
||||
Stack
|
||||
]
|
||||
|
||||
When only the module name is given, it is equivalent to `{Stack, []}`.
|
||||
When only the module name is given, it is equivalent to `{Stack, []}`. In this
|
||||
case, we will end-up with a child specification that looks like this:
|
||||
|
||||
%{
|
||||
id: Stack,
|
||||
start: {Stack, :start_link, [[]]}
|
||||
}
|
||||
|
||||
By replacing the map specification by `{Stack, [:hello]}` or `Stack`, we keep
|
||||
the child specification encapsulated in the `Stack` module, using the default
|
||||
the child specification encapsulated in the Stack module, using the default
|
||||
implementation defined by `use GenServer`. We can now share our `Stack` worker
|
||||
with other developers and they can add it directly to their supervision tree
|
||||
without worrying about the low-level details of the worker.
|
||||
@@ -247,7 +285,7 @@ defmodule Supervisor do
|
||||
* a module - such as `Stack`. In this case, `Stack.child_spec([])`
|
||||
is called to retrieve the child specification
|
||||
|
||||
If you need to convert a tuple or a module child specification to a map or
|
||||
If you need to convert how a tuple or module child specification to a map or
|
||||
modify a child specification, you can use the `Supervisor.child_spec/2` function.
|
||||
For example, to run the stack with a different `:id` and a `:shutdown` value of
|
||||
10 seconds (10_000 milliseconds):
|
||||
@@ -256,6 +294,55 @@ defmodule Supervisor do
|
||||
Supervisor.child_spec({Stack, [:hello]}, id: MyStack, shutdown: 10_000)
|
||||
]
|
||||
|
||||
The call to `Supervisor.child_spec/2` above will return the following specification:
|
||||
|
||||
%{
|
||||
id: MyStack,
|
||||
start: {Stack, :start_link, [[:hello]]},
|
||||
shutdown: 10_000
|
||||
}
|
||||
|
||||
You may also configure the child specification in the Stack module itself to
|
||||
use a different `:id` or `:shutdown` value by passing options to `use GenServer`:
|
||||
|
||||
defmodule Stack do
|
||||
use GenServer, id: MyStack, shutdown: 10_000
|
||||
|
||||
The options above will customize the `Stack.child_spec/1` function defined
|
||||
by `use GenServer`. It accepts the same options as the `Supervisor.child_spec/2`
|
||||
function.
|
||||
|
||||
You may also completely override the `child_spec/1` function in the Stack module
|
||||
and return your own child specification. Note there is no guarantee the `child_spec/1`
|
||||
function will be called by the Supervisor process, as other processes may invoke
|
||||
it to retrieve the child specification before reaching the supervisor.
|
||||
|
||||
## Exit reasons and restarts
|
||||
|
||||
A supervisor restarts a child process depending on its `:restart`
|
||||
configuration. For example, when `:restart` is set to `:transient`, the
|
||||
supervisor does not restart the child in case it exits with reason `:normal`,
|
||||
`:shutdown` or `{:shutdown, term}`.
|
||||
|
||||
So one may ask: which exit reason should I choose when exiting? There are
|
||||
three options:
|
||||
|
||||
* `:normal` - in such cases, the exit won't be logged, there is no restart
|
||||
in transient mode, and linked processes do not exit
|
||||
|
||||
* `:shutdown` or `{:shutdown, term}` - in such cases, the exit won't be
|
||||
logged, there is no restart in transient mode, and linked processes exit
|
||||
with the same reason unless they're trapping exits
|
||||
|
||||
* any other term - in such cases, the exit will be logged, there are
|
||||
restarts in transient mode, and linked processes exit with the same
|
||||
reason unless they're trapping exits
|
||||
|
||||
Notice that the supervisor that reaches maximum restart intensity will exit with
|
||||
`:shutdown` reason. In this case the supervisor will only be restarted if its
|
||||
child specification was defined with the `:restart` option set to `:permanent`
|
||||
(the default).
|
||||
|
||||
## Module-based supervisors
|
||||
|
||||
In the example above, a supervisor was started by passing the supervision
|
||||
@@ -266,12 +353,12 @@ defmodule Supervisor do
|
||||
# Automatically defines child_spec/1
|
||||
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
|
||||
|
||||
@impl true
|
||||
def init(_init_arg) do
|
||||
def init(_arg) do
|
||||
children = [
|
||||
{Stack, [:hello]}
|
||||
]
|
||||
@@ -288,8 +375,7 @@ defmodule Supervisor do
|
||||
`c:init/1` callback.
|
||||
|
||||
`use Supervisor` also defines a `child_spec/1` function which allows
|
||||
us to run `MyApp.Supervisor` as a child of another supervisor or
|
||||
at the top of your supervision tree as:
|
||||
us to run `MyApp.Supervisor` as a child of another supervisor:
|
||||
|
||||
children = [
|
||||
MyApp.Supervisor
|
||||
@@ -301,34 +387,27 @@ defmodule Supervisor do
|
||||
module only at the top of your supervision tree, generally in the
|
||||
`c:Application.start/2` callback. We recommend using module-based
|
||||
supervisors for any other supervisor in your application, so they
|
||||
can run as a child of another supervisor in the tree. The `child_spec/1`
|
||||
generated automatically by `Supervisor` can be customized with the
|
||||
following options:
|
||||
can run as a child of another supervision in the tree. The generated
|
||||
`child_spec/1` can be customized with the following options:
|
||||
|
||||
* `:id` - the child specification identifier, defaults to the current module
|
||||
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
|
||||
* `:restart` - when the supervisor should be restarted, defaults to `:permanent`
|
||||
|
||||
The `@doc` annotation immediately preceding `use Supervisor` will be
|
||||
attached to the generated `child_spec/1` function.
|
||||
|
||||
## `start_link/2`, `init/2`, and strategies
|
||||
|
||||
So far we have started the supervisor passing a single child as a tuple
|
||||
as well as a strategy called `:one_for_one`:
|
||||
|
||||
children = [
|
||||
Supervisor.start_link([
|
||||
{Stack, [:hello]}
|
||||
]
|
||||
|
||||
Supervisor.start_link(children, strategy: :one_for_one)
|
||||
], strategy: :one_for_one)
|
||||
|
||||
or from inside the `c:init/1` callback:
|
||||
|
||||
children = [
|
||||
Supervisor.init([
|
||||
{Stack, [:hello]}
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :one_for_one)
|
||||
], strategy: :one_for_one)
|
||||
|
||||
The first argument given to `start_link/2` and `init/2` is a list of child
|
||||
specifications as defined in the "child_spec/1" section above.
|
||||
@@ -375,73 +454,10 @@ defmodule Supervisor do
|
||||
differently when this strategy was used. See the `DynamicSupervisor` module
|
||||
for more information and migration strategies.
|
||||
|
||||
### Name registration
|
||||
## Name registration
|
||||
|
||||
A supervisor is bound to the same name registration rules as a `GenServer`.
|
||||
Read more about these rules in the documentation for `GenServer`.
|
||||
|
||||
## Start and shutdown
|
||||
|
||||
When the supervisor starts, it traverses all child specifications and
|
||||
then starts each child in the order they are defined. This is done by
|
||||
calling the function defined under the `:start` key in the child
|
||||
specification and typically defaults to `start_link/1`.
|
||||
|
||||
The `start_link/1` (or a custom) is then called for each child process.
|
||||
The `start_link/1` function must return `{:ok, pid}` where `pid` is the
|
||||
process identifier of a new process that is linked to the supervisor.
|
||||
The child process usually starts its work by executing the `c:init/1`
|
||||
callback. Generally speaking, the `init` callback is where we initialize
|
||||
and configure the child process.
|
||||
|
||||
The shutdown process happens in reverse order.
|
||||
|
||||
When a supervisor shuts down, it terminates all children in the opposite
|
||||
order they are listed. The termination happens by sending a shutdown exit
|
||||
signal, via `Process.exit(child_pid, :shutdown)`, to the child process and
|
||||
then awaiting for a time interval for the child process to terminate. This
|
||||
interval defaults to 5000 milliseconds. If the child process does not
|
||||
terminate in this interval, the supervisor abruptly terminates the child
|
||||
with reason `:kill`. The shutdown time can be configured in the child
|
||||
specification which is fully detailed in the next section.
|
||||
|
||||
If the child process is not trapping exits, it will shutdown immediately
|
||||
when it receives the first exit signal. If the child process is trapping
|
||||
exits, then the `terminate` callback is invoked, and the child process
|
||||
must terminate in a reasonable time interval before being abruptly
|
||||
terminated by the supervisor.
|
||||
|
||||
In other words, if it is important that a process cleans after itself
|
||||
when your application or the supervision tree is shutting down, then
|
||||
this process must trap exits and its child specification should specify
|
||||
the proper `:shutdown` value, ensuring it terminates within a reasonable
|
||||
interval.
|
||||
|
||||
## Exit reasons and restarts
|
||||
|
||||
A supervisor restarts a child process depending on its `:restart` configuration.
|
||||
For example, when `:restart` is set to `:transient`, the supervisor does not
|
||||
restart the child in case it exits with reason `:normal`, `:shutdown` or
|
||||
`{:shutdown, term}`.
|
||||
|
||||
So one may ask: which exit reason should I choose when exiting? There are
|
||||
three options:
|
||||
|
||||
* `:normal` - in such cases, the exit won't be logged, there is no restart
|
||||
in transient mode, and linked processes do not exit
|
||||
|
||||
* `:shutdown` or `{:shutdown, term}` - in such cases, the exit won't be
|
||||
logged, there is no restart in transient mode, and linked processes exit
|
||||
with the same reason unless they're trapping exits
|
||||
|
||||
* any other term - in such cases, the exit will be logged, there are
|
||||
restarts in transient mode, and linked processes exit with the same
|
||||
reason unless they're trapping exits
|
||||
|
||||
Notice that the supervisor that reaches maximum restart intensity will exit with
|
||||
`:shutdown` reason. In this case the supervisor will only be restarted if its
|
||||
child specification was defined with the `:restart` option set to `:permanent`
|
||||
(the default).
|
||||
"""
|
||||
|
||||
@doc false
|
||||
@@ -450,18 +466,15 @@ defmodule Supervisor do
|
||||
import Supervisor.Spec
|
||||
@behaviour Supervisor
|
||||
|
||||
if Module.get_attribute(__MODULE__, :doc) == nil do
|
||||
@doc """
|
||||
Returns a specification to start this module under a supervisor.
|
||||
@doc """
|
||||
Returns a specification to start this module under a supervisor.
|
||||
|
||||
See `Supervisor`.
|
||||
"""
|
||||
end
|
||||
|
||||
def child_spec(init_arg) do
|
||||
See `Supervisor`.
|
||||
"""
|
||||
def child_spec(arg) do
|
||||
default = %{
|
||||
id: __MODULE__,
|
||||
start: {__MODULE__, :start_link, [init_arg]},
|
||||
start: {__MODULE__, :start_link, [arg]},
|
||||
type: :supervisor
|
||||
}
|
||||
|
||||
@@ -478,7 +491,7 @@ defmodule Supervisor do
|
||||
Developers typically invoke `Supervisor.init/2` at the end of their
|
||||
init callback to return the proper supervision flags.
|
||||
"""
|
||||
@callback init(init_arg :: term) ::
|
||||
@callback init(args :: term) ::
|
||||
{:ok, {:supervisor.sup_flags(), [:supervisor.child_spec()]}}
|
||||
| :ignore
|
||||
|
||||
@@ -531,10 +544,10 @@ defmodule Supervisor do
|
||||
@doc """
|
||||
Starts a supervisor with the given children.
|
||||
|
||||
The children is a list of modules, two-element tuples with module and
|
||||
The children is a list of modules, 2-element tuples with module and
|
||||
arguments or a map with the child specification. A strategy is required
|
||||
to be provided through the `:strategy` option. See
|
||||
"start_link/2, init/2, and strategies" for examples and other options.
|
||||
"start_link/2, init/2 and strategies" for examples and other options.
|
||||
|
||||
The options can also be used to register a supervisor name.
|
||||
The supported values are described under the "Name registration"
|
||||
@@ -557,19 +570,18 @@ defmodule Supervisor do
|
||||
process and exits not only on crashes but also if the parent process exits
|
||||
with `:normal` reason.
|
||||
"""
|
||||
@spec start_link([:supervisor.child_spec() | {module, term} | module], options) ::
|
||||
{:ok, pid} | {:error, {:already_started, pid} | {:shutdown, term} | term}
|
||||
@spec start_link([:supervisor.child_spec() | {module, term} | module], options) :: on_start
|
||||
def start_link(children, options) when is_list(children) do
|
||||
{sup_opts, start_opts} = Keyword.split(options, [:strategy, :max_seconds, :max_restarts])
|
||||
start_link(Supervisor.Default, init(children, sup_opts), start_opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a list of `children` to initialize and a set of `options`.
|
||||
Receives a list of children to initialize and a set of options.
|
||||
|
||||
This is typically invoked at the end of the `c:init/1` callback of
|
||||
module-based supervisors. See the sections "Module-based supervisors"
|
||||
and "start_link/2, init/2, and strategies" in the module
|
||||
and "start_link/2, init/2 and strategies" in the module
|
||||
documentation for more information.
|
||||
|
||||
This function returns a tuple containing the supervisor
|
||||
@@ -577,12 +589,10 @@ defmodule Supervisor do
|
||||
|
||||
## Examples
|
||||
|
||||
def init(_init_arg) do
|
||||
children = [
|
||||
def init(_arg) do
|
||||
Supervisor.init([
|
||||
{Stack, [:hello]}
|
||||
]
|
||||
|
||||
Supervisor.init(children, strategy: :one_for_one)
|
||||
], strategy: :one_for_one)
|
||||
end
|
||||
|
||||
## Options
|
||||
@@ -594,15 +604,15 @@ defmodule Supervisor do
|
||||
* `:max_restarts` - the maximum number of restarts allowed in
|
||||
a time frame. Defaults to `3`.
|
||||
|
||||
* `:max_seconds` - the time frame in seconds in which `:max_restarts`
|
||||
applies. Defaults to `5`.
|
||||
* `:max_seconds` - the time frame in which `:max_restarts` applies.
|
||||
Defaults to `5`.
|
||||
|
||||
The `:strategy` option is required and by default a maximum of 3 restarts
|
||||
is allowed within 5 seconds. Check the `Supervisor` module for a detailed
|
||||
description of the available strategies.
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
# TODO: Warn if simple_one_for_one strategy is used on Elixir v1.10
|
||||
# TODO: Warn if simple_one_for_one strategy is used on Elixir v1.8.
|
||||
@spec init([:supervisor.child_spec() | {module, term} | module], [init_option]) :: {:ok, tuple}
|
||||
def init(children, options) when is_list(children) and is_list(options) do
|
||||
unless strategy = options[:strategy] do
|
||||
@@ -740,10 +750,10 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a module-based supervisor process with the given `module` and `init_arg`.
|
||||
Starts a module-based supervisor process with the given `module` and `arg`.
|
||||
|
||||
To start the supervisor, the `c:init/1` callback will be invoked in the given
|
||||
`module`, with `init_arg` as its argument. The `c:init/1` callback must return a
|
||||
`module`, with `arg` as its argument. The `c:init/1` callback must return a
|
||||
supervisor specification which can be created with the help of the `init/2`
|
||||
function.
|
||||
|
||||
@@ -762,19 +772,19 @@ defmodule Supervisor do
|
||||
# all to start_link(children, options).
|
||||
@spec start_link(module, term) :: on_start
|
||||
@spec start_link(module, term, GenServer.options()) :: on_start
|
||||
def start_link(module, init_arg, options \\ []) when is_list(options) do
|
||||
def start_link(module, arg, options \\ []) when is_list(options) do
|
||||
case Keyword.get(options, :name) do
|
||||
nil ->
|
||||
:supervisor.start_link(module, init_arg)
|
||||
:supervisor.start_link(module, arg)
|
||||
|
||||
atom when is_atom(atom) ->
|
||||
:supervisor.start_link({:local, atom}, module, init_arg)
|
||||
:supervisor.start_link({:local, atom}, module, arg)
|
||||
|
||||
{:global, _term} = tuple ->
|
||||
:supervisor.start_link(tuple, module, init_arg)
|
||||
:supervisor.start_link(tuple, module, arg)
|
||||
|
||||
{:via, via_module, _term} = tuple when is_atom(via_module) ->
|
||||
:supervisor.start_link(tuple, module, init_arg)
|
||||
:supervisor.start_link(tuple, module, arg)
|
||||
|
||||
other ->
|
||||
raise ArgumentError, """
|
||||
@@ -796,7 +806,7 @@ defmodule Supervisor do
|
||||
`child_spec` should be a valid child specification. The child process will
|
||||
be started as defined in the child specification.
|
||||
|
||||
If a child specification with the specified ID already exists, `child_spec` is
|
||||
If a child specification with the specified id already exists, `child_spec` is
|
||||
discarded and this function returns an error with `:already_started` or
|
||||
`:already_present` if the corresponding child process is running or not,
|
||||
respectively.
|
||||
@@ -820,7 +830,7 @@ defmodule Supervisor do
|
||||
call(supervisor, {:start_child, child_spec})
|
||||
end
|
||||
|
||||
# TODO: Deprecate this clause on Elixir v1.10
|
||||
# TODO: Deprecate this on Elixir v1.8. Remove and update typespec on v2.0.
|
||||
def start_child(supervisor, args) when is_list(args) do
|
||||
call(supervisor, {:start_child, args})
|
||||
end
|
||||
@@ -830,7 +840,7 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Terminates the given child identified by `child_id`.
|
||||
Terminates the given child identified by child id.
|
||||
|
||||
The process is terminated, if there's one. The child specification is
|
||||
kept unless the child is temporary.
|
||||
@@ -840,14 +850,14 @@ defmodule Supervisor do
|
||||
Use `delete_child/2` to remove the child specification.
|
||||
|
||||
If successful, this function returns `:ok`. If there is no child
|
||||
specification for the given child ID, this function returns
|
||||
specification for the given child id, this function returns
|
||||
`{:error, :not_found}`.
|
||||
"""
|
||||
@spec terminate_child(supervisor, term()) :: :ok | {:error, error}
|
||||
when error: :not_found | :simple_one_for_one
|
||||
def terminate_child(supervisor, child_id)
|
||||
|
||||
# TODO: Deprecate this clause on Elixir v1.10
|
||||
# TODO: Deprecate this clause on Elixir v1.8
|
||||
def terminate_child(supervisor, pid) when is_pid(pid) do
|
||||
call(supervisor, {:terminate_child, pid})
|
||||
end
|
||||
|
||||
@@ -86,7 +86,7 @@ defmodule Supervisor.Spec do
|
||||
In this case the supervisor will only be restarted if its child specification was defined with
|
||||
the `:restart` option is set to `:permanent` (the default).
|
||||
|
||||
### Shutdown values (`:shutdown`)
|
||||
### Shutdown values (:shutdown)
|
||||
|
||||
The following shutdown values are supported in the `:shutdown` option:
|
||||
|
||||
@@ -94,14 +94,14 @@ defmodule Supervisor.Spec do
|
||||
using `Process.exit(child, :kill)`
|
||||
|
||||
* `:infinity` - if the child process is a supervisor, this is a mechanism
|
||||
to give the subtree enough time to shut down; it can also be used with
|
||||
to give the subtree enough time to shutdown; it can also be used with
|
||||
workers with care
|
||||
|
||||
* a non-negative integer - the amount of time in milliseconds
|
||||
* any integer - the value of `:shutdown` can also be any integer meaning
|
||||
that the supervisor tells the child process to terminate by calling
|
||||
`Process.exit(child, :shutdown)` and then waits for an exit signal back.
|
||||
If no exit signal is received within the specified time,
|
||||
the child process is unconditionally terminated
|
||||
If no exit signal is received within the specified time (the value of this
|
||||
option, in milliseconds), the child process is unconditionally terminated
|
||||
using `Process.exit(child, :kill)`
|
||||
|
||||
"""
|
||||
@@ -109,7 +109,7 @@ defmodule Supervisor.Spec do
|
||||
@moduledoc deprecated:
|
||||
"Use the new child specifications outlined in the Supervisor module instead"
|
||||
|
||||
# TODO: Deprecate all functions in this module on Elixir v1.9.
|
||||
# TODO: Deprecate all functions in this module on Elixir v1.8.
|
||||
# Also deprecate entry in Supervisor.Default.
|
||||
|
||||
@typedoc "Supported strategies"
|
||||
@@ -127,7 +127,7 @@ defmodule Supervisor.Spec do
|
||||
@typedoc "Supported module values"
|
||||
@type modules :: :dynamic | [module]
|
||||
|
||||
@typedoc "Supported ID values"
|
||||
@typedoc "Supported id values"
|
||||
@type child_id :: term
|
||||
|
||||
@typedoc "The supervisor specification"
|
||||
@@ -135,8 +135,8 @@ defmodule Supervisor.Spec do
|
||||
{child_id, start_fun :: {module, atom, [term]}, restart, shutdown, worker, modules}
|
||||
|
||||
@doc """
|
||||
Receives a list of `children` (workers or supervisors) to
|
||||
supervise and a set of `options`.
|
||||
Receives a list of children (workers or supervisors) to
|
||||
supervise and a set of options.
|
||||
|
||||
Returns a tuple containing the supervisor specification. This tuple can be
|
||||
used as the return value of the `c:init/1` callback when implementing a
|
||||
@@ -196,7 +196,7 @@ defmodule Supervisor.Spec do
|
||||
defp assert_unique_ids([id | rest]) do
|
||||
if id in rest do
|
||||
raise ArgumentError,
|
||||
"duplicated ID #{inspect(id)} found in the supervisor specification, " <>
|
||||
"duplicated id #{inspect(id)} found in the supervisor specification, " <>
|
||||
"please explicitly pass the :id option when defining this worker/supervisor"
|
||||
else
|
||||
assert_unique_ids(rest)
|
||||
@@ -216,16 +216,14 @@ defmodule Supervisor.Spec do
|
||||
By default, the function `start_link` is invoked on the given
|
||||
module. Overall, the default values for the options are:
|
||||
|
||||
[
|
||||
id: module,
|
||||
function: :start_link,
|
||||
restart: :permanent,
|
||||
shutdown: 5000,
|
||||
modules: [module]
|
||||
]
|
||||
[id: module,
|
||||
function: :start_link,
|
||||
restart: :permanent,
|
||||
shutdown: 5000,
|
||||
modules: [module]]
|
||||
|
||||
See the "Supervisor and worker options" section in the `Supervisor.Spec` module for more
|
||||
information on the available options.
|
||||
Check the documentation for the `Supervisor.Spec` module for more
|
||||
information on the options.
|
||||
"""
|
||||
@spec worker(
|
||||
module,
|
||||
@@ -244,21 +242,19 @@ defmodule Supervisor.Spec do
|
||||
Defines the given `module` as a supervisor which will be started
|
||||
with the given arguments.
|
||||
|
||||
supervisor(module, [], restart: :permanent)
|
||||
supervisor(ExUnit.Runner, [], restart: :permanent)
|
||||
|
||||
By default, the function `start_link` is invoked on the given
|
||||
module. Overall, the default values for the options are:
|
||||
|
||||
[
|
||||
id: module,
|
||||
function: :start_link,
|
||||
restart: :permanent,
|
||||
shutdown: :infinity,
|
||||
modules: [module]
|
||||
]
|
||||
[id: module,
|
||||
function: :start_link,
|
||||
restart: :permanent,
|
||||
shutdown: :infinity,
|
||||
modules: [module]]
|
||||
|
||||
See the "Supervisor and worker options" section in the `Supervisor.Spec` module for more
|
||||
information on the available options.
|
||||
Check the documentation for the `Supervisor.Spec` module for more
|
||||
information on the options.
|
||||
"""
|
||||
@spec supervisor(
|
||||
module,
|
||||
|
||||
+72
-248
@@ -36,11 +36,11 @@ defmodule System do
|
||||
|
||||
Generally speaking, the VM provides three time measurements:
|
||||
|
||||
* `os_time/0` - the time reported by the operating system (OS). This time may be
|
||||
* `os_time/0` - the time reported by the OS. This time may be
|
||||
adjusted forwards or backwards in time with no limitation;
|
||||
|
||||
* `system_time/0` - the VM view of the `os_time/0`. The system time and operating
|
||||
system time may not match in case of time warps although the VM works towards
|
||||
* `system_time/0` - the VM view of the `os_time/0`. The system time and OS
|
||||
time may not match in case of time warps although the VM works towards
|
||||
aligning them. This time is not monotonic (i.e., it may decrease)
|
||||
as its behaviour is configured [by the VM time warp
|
||||
mode](http://www.erlang.org/doc/apps/erts/time_correction.html#Time_Warp_Modes);
|
||||
@@ -49,7 +49,7 @@ defmodule System do
|
||||
by the Erlang VM.
|
||||
|
||||
The time functions in this module work in the `:native` unit
|
||||
(unless specified otherwise), which is operating system dependent. Most of
|
||||
(unless specified otherwise), which is OS dependent. Most of
|
||||
the time, all calculations are done in the `:native` unit, to
|
||||
avoid loss of precision, with `convert_time_unit/3` being
|
||||
invoked at the end to convert to a specific time unit like
|
||||
@@ -73,13 +73,22 @@ defmodule System do
|
||||
parts_per_second` seconds. For example, using the `:millisecond` time unit
|
||||
is equivalent to using `1000` as the time unit (as the time will be returned
|
||||
in 1/1000 seconds - milliseconds).
|
||||
|
||||
Keep in mind the Erlang API prior to version 19.1 will use `:milli_seconds`,
|
||||
`:micro_seconds` and `:nano_seconds` as time units although Elixir normalizes
|
||||
their spelling to match the SI convention.
|
||||
"""
|
||||
# TODO: Warn all old mappings once Elixir requires Erlang/OTP 19.1+ (on v1.8)
|
||||
@type time_unit ::
|
||||
:second
|
||||
| :millisecond
|
||||
| :microsecond
|
||||
| :nanosecond
|
||||
| pos_integer
|
||||
| :seconds
|
||||
| :milliseconds
|
||||
| :microseconds
|
||||
| :nanoseconds
|
||||
|
||||
@base_dir :filename.join(__DIR__, "../../..")
|
||||
@version_file :filename.join(@base_dir, "VERSION")
|
||||
@@ -101,7 +110,7 @@ defmodule System do
|
||||
# Read and strip the version from the VERSION file.
|
||||
defmacrop get_version do
|
||||
case read_stripped(@version_file) do
|
||||
"" -> raise "could not read the version number from VERSION"
|
||||
"" -> raise RuntimeError, message: "could not read the version number from VERSION"
|
||||
data -> data
|
||||
end
|
||||
end
|
||||
@@ -111,7 +120,7 @@ defmodule System do
|
||||
:erlang.list_to_binary(:erlang.system_info(:otp_release))
|
||||
end
|
||||
|
||||
# Tries to run "git rev-parse --short=7 HEAD". In the case of success returns
|
||||
# Tries to run "git rev-parse --short HEAD". In the case of success returns
|
||||
# the short revision hash. If that fails, returns an empty string.
|
||||
defmacrop get_revision do
|
||||
null =
|
||||
@@ -120,7 +129,7 @@ defmodule System do
|
||||
_ -> '/dev/null'
|
||||
end
|
||||
|
||||
'git rev-parse --short=7 HEAD 2> '
|
||||
'git rev-parse --short HEAD 2> '
|
||||
|> Kernel.++(null)
|
||||
|> :os.cmd()
|
||||
|> strip
|
||||
@@ -129,21 +138,8 @@ defmodule System do
|
||||
defp revision, do: get_revision()
|
||||
|
||||
# Get the date at compilation time.
|
||||
# Follows https://reproducible-builds.org/specs/source-date-epoch/
|
||||
defmacrop get_date do
|
||||
unix_epoch =
|
||||
if source_date_epoch = :os.getenv('SOURCE_DATE_EPOCH') do
|
||||
try do
|
||||
List.to_integer(source_date_epoch)
|
||||
rescue
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
unix_epoch = unix_epoch || :os.system_time(:second)
|
||||
|
||||
{{year, month, day}, {hour, minute, second}} =
|
||||
:calendar.gregorian_seconds_to_datetime(unix_epoch + 62_167_219_200)
|
||||
{{year, month, day}, {hour, minute, second}} = :calendar.universal_time()
|
||||
|
||||
"~4..0b-~2..0b-~2..0bT~2..0b:~2..0b:~2..0bZ"
|
||||
|> :io_lib.format([year, month, day, hour, minute, second])
|
||||
@@ -153,7 +149,6 @@ defmodule System do
|
||||
@doc """
|
||||
Returns the endianness.
|
||||
"""
|
||||
@spec endianness() :: :little | :big
|
||||
def endianness do
|
||||
:erlang.system_info(:endian)
|
||||
end
|
||||
@@ -162,7 +157,6 @@ defmodule System do
|
||||
Returns the endianness the system was compiled with.
|
||||
"""
|
||||
@endianness :erlang.system_info(:endian)
|
||||
@spec compiled_endianness() :: :little | :big
|
||||
def compiled_endianness do
|
||||
@endianness
|
||||
end
|
||||
@@ -178,42 +172,9 @@ defmodule System do
|
||||
@doc """
|
||||
Elixir build information.
|
||||
|
||||
Returns a map with the Elixir version, the Erlang/OTP release it was compiled
|
||||
with, a short Git revision hash and the date and time it was built.
|
||||
|
||||
Every value in the map is a string, and these are:
|
||||
|
||||
* `:build` - the Elixir version, short Git revision hash and
|
||||
Erlang/OTP release it was compiled with
|
||||
* `:date` - a string representation of the ISO8601 date and time it was built
|
||||
* `:opt_release` - OTP release it was compiled with
|
||||
* `:revision` - short Git revision hash. If Git was not available at building
|
||||
time, it is set to `""`
|
||||
* `:version` - the Elixir version
|
||||
|
||||
One should not rely on the specific formats returned by each of those fields.
|
||||
Instead one should use specialized functions, such as `version/0` to retrieve
|
||||
the Elixir version and `otp_release/0` to retrieve the Erlang/OTP release.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> System.build_info()
|
||||
%{
|
||||
build: "1.9.0-dev (772a00a0c) (compiled with Erlang/OTP 21)",
|
||||
date: "2018-12-24T01:09:21Z",
|
||||
otp_release: "21",
|
||||
revision: "772a00a0c",
|
||||
version: "1.9.0-dev"
|
||||
}
|
||||
|
||||
Returns a keyword list with Elixir version, Git short revision hash and compilation date.
|
||||
"""
|
||||
@spec build_info() :: %{
|
||||
build: String.t(),
|
||||
date: String.t(),
|
||||
revision: String.t(),
|
||||
version: String.t(),
|
||||
otp_release: String.t()
|
||||
}
|
||||
@spec build_info() :: map
|
||||
def build_info do
|
||||
%{
|
||||
build: build(),
|
||||
@@ -255,54 +216,38 @@ defmodule System do
|
||||
:elixir_config.put(:argv, args)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Marks if the system should halt or not at the end of ARGV processing.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec no_halt(boolean) :: :ok
|
||||
def no_halt(boolean) when is_boolean(boolean) do
|
||||
:elixir_config.put(:no_halt, boolean)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the system will halt or not at the end of ARGV processing.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec no_halt() :: boolean
|
||||
def no_halt() do
|
||||
:elixir_config.get(:no_halt)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Current working directory.
|
||||
|
||||
Returns the current working directory or `nil` if one
|
||||
is not available.
|
||||
"""
|
||||
@deprecated "Use File.cwd/0 instead"
|
||||
@spec cwd() :: String.t() | nil
|
||||
def cwd do
|
||||
case File.cwd() do
|
||||
{:ok, cwd} -> cwd
|
||||
case :file.get_cwd() do
|
||||
{:ok, base} -> IO.chardata_to_string(fix_drive_letter(base))
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp fix_drive_letter([l, ?:, ?/ | rest] = original) when l in ?A..?Z do
|
||||
case :os.type() do
|
||||
{:win32, _} -> [l + ?a - ?A, ?:, ?/ | rest]
|
||||
_ -> original
|
||||
end
|
||||
end
|
||||
|
||||
defp fix_drive_letter(original), do: original
|
||||
|
||||
@doc """
|
||||
Current working directory, exception on error.
|
||||
|
||||
Returns the current working directory or raises `RuntimeError`.
|
||||
"""
|
||||
@deprecated "Use File.cwd!/0 instead"
|
||||
@spec cwd!() :: String.t()
|
||||
def cwd! do
|
||||
case File.cwd() do
|
||||
{:ok, cwd} ->
|
||||
cwd
|
||||
|
||||
_ ->
|
||||
raise "could not get a current working directory, the current location is not accessible"
|
||||
end
|
||||
cwd() ||
|
||||
raise RuntimeError,
|
||||
message:
|
||||
"could not get a current working directory, the current location is not accessible"
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -310,7 +255,6 @@ defmodule System do
|
||||
|
||||
Returns the user home directory (platform independent).
|
||||
"""
|
||||
@spec user_home() :: String.t() | nil
|
||||
def user_home do
|
||||
:elixir_config.get(:home)
|
||||
end
|
||||
@@ -321,9 +265,10 @@ defmodule System do
|
||||
Same as `user_home/0` but raises `RuntimeError`
|
||||
instead of returning `nil` if no user home is set.
|
||||
"""
|
||||
@spec user_home!() :: String.t()
|
||||
def user_home! do
|
||||
user_home() || raise "could not find the user home, please set the HOME environment variable"
|
||||
user_home() ||
|
||||
raise RuntimeError,
|
||||
message: "could not find the user home, please set the HOME environment variable"
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -340,17 +285,9 @@ defmodule System do
|
||||
|
||||
Returns `nil` if none of the above are writable.
|
||||
"""
|
||||
@spec tmp_dir() :: String.t() | nil
|
||||
def tmp_dir do
|
||||
write_env_tmp_dir('TMPDIR') || write_env_tmp_dir('TEMP') || write_env_tmp_dir('TMP') ||
|
||||
write_tmp_dir('/tmp') || write_cwd_tmp_dir()
|
||||
end
|
||||
|
||||
defp write_cwd_tmp_dir do
|
||||
case File.cwd() do
|
||||
{:ok, cwd} -> write_tmp_dir(cwd)
|
||||
_ -> nil
|
||||
end
|
||||
write_tmp_dir('/tmp') || ((cwd = cwd()) && write_tmp_dir(cwd))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -359,10 +296,12 @@ defmodule System do
|
||||
Same as `tmp_dir/0` but raises `RuntimeError`
|
||||
instead of returning `nil` if no temp dir is set.
|
||||
"""
|
||||
@spec tmp_dir!() :: String.t()
|
||||
def tmp_dir! do
|
||||
tmp_dir() ||
|
||||
raise "could not get a writable temporary directory, please set the TMPDIR environment variable"
|
||||
raise RuntimeError,
|
||||
message:
|
||||
"could not get a writable temporary directory, " <>
|
||||
"please set the TMPDIR environment variable"
|
||||
end
|
||||
|
||||
defp write_env_tmp_dir(env) do
|
||||
@@ -401,10 +340,8 @@ defmodule System do
|
||||
|
||||
The function must receive the exit status code as an argument.
|
||||
"""
|
||||
@spec at_exit((non_neg_integer -> any)) :: :ok
|
||||
def at_exit(fun) when is_function(fun, 1) do
|
||||
:elixir_config.update(:at_exit, &[fun | &1])
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -413,7 +350,7 @@ defmodule System do
|
||||
This function looks up an executable program given
|
||||
its name using the environment variable PATH on Unix
|
||||
and Windows. It also considers the proper executable
|
||||
extension for each operating system, so for Windows it will try to
|
||||
extension for each OS, so for Windows it will try to
|
||||
lookup files with `.com`, `.cmd` or similar extensions.
|
||||
"""
|
||||
@spec find_executable(binary) :: binary | nil
|
||||
@@ -445,80 +382,17 @@ defmodule System do
|
||||
Returns the value of the given environment variable.
|
||||
|
||||
The returned value of the environment variable
|
||||
`varname` is a string. If the environment variable
|
||||
is not set, returns the string specified in `default` or
|
||||
`nil` if none is specified.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> System.get_env("PORT")
|
||||
"4000"
|
||||
|
||||
iex> System.get_env("NOT_SET")
|
||||
nil
|
||||
|
||||
iex> System.get_env("NOT_SET", "4001")
|
||||
"4001"
|
||||
|
||||
`varname` is a string, or `nil` if the environment
|
||||
variable is undefined.
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec get_env(String.t(), String.t() | nil) :: String.t() | nil
|
||||
def get_env(varname, default \\ nil)
|
||||
when is_binary(varname) and
|
||||
(is_binary(default) or is_nil(default)) do
|
||||
@spec get_env(String.t()) :: String.t() | nil
|
||||
def get_env(varname) when is_binary(varname) do
|
||||
case :os.getenv(String.to_charlist(varname)) do
|
||||
false -> default
|
||||
false -> nil
|
||||
other -> List.to_string(other)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value of the given environment variable or `:error` if not found.
|
||||
|
||||
If the environment variable `varname` is set, then `{:ok, value}` is returned
|
||||
where `value` is a string. If `varname` is not set, `:error` is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> System.fetch_env("PORT")
|
||||
{:ok, "4000"}
|
||||
|
||||
iex> System.fetch_env("NOT_SET")
|
||||
:error
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec fetch_env(String.t()) :: {:ok, String.t()} | :error
|
||||
def fetch_env(varname) when is_binary(varname) do
|
||||
case :os.getenv(String.to_charlist(varname)) do
|
||||
false -> :error
|
||||
other -> {:ok, List.to_string(other)}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value of the given environment variable or raises if not found.
|
||||
|
||||
Same as `get_env/1` but raises instead of returning `nil` when the variable is
|
||||
not set.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> System.fetch_env!("PORT")
|
||||
"4000"
|
||||
|
||||
iex> System.fetch_env!("NOT_SET")
|
||||
** (ArgumentError) could not fetch environment variable "NOT_SET" because it is not set
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec fetch_env!(String.t()) :: String.t()
|
||||
def fetch_env!(varname) when is_binary(varname) do
|
||||
get_env(varname) ||
|
||||
raise ArgumentError,
|
||||
"could not fetch environment variable #{inspect(varname)} because it is not set"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Erlang VM process identifier.
|
||||
|
||||
@@ -580,13 +454,12 @@ defmodule System do
|
||||
|
||||
Note that the Erlang VM (and therefore this function) does not
|
||||
return the current stacktrace but rather the stacktrace of the
|
||||
latest exception. To retrieve the stacktrace of the current process,
|
||||
use `Process.info(self(), :current_stacktrace)` instead.
|
||||
latest exception.
|
||||
"""
|
||||
# TODO: Fully deprecate it on Elixir v1.11 via @deprecated
|
||||
# TODO: Fully deprecate it on Elixir v1.9.
|
||||
# It is currently partially deprecated in elixir_dispatch.erl
|
||||
def stacktrace do
|
||||
apply(:erlang, :get_stacktrace, [])
|
||||
:erlang.get_stacktrace()
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -630,40 +503,6 @@ defmodule System do
|
||||
:erlang.halt(String.to_charlist(status))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the operating system PID for the current Erlang runtime system instance.
|
||||
|
||||
Returns a string containing the (usually) numerical identifier for a process.
|
||||
On UNIX, this is typically the return value of the `getpid()` system call.
|
||||
On Windows, the process ID as returned by the `GetCurrentProcessId()` system
|
||||
call is used.
|
||||
|
||||
## Examples
|
||||
|
||||
System.pid()
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec pid :: String.t()
|
||||
def pid do
|
||||
List.to_string(:os.getpid())
|
||||
end
|
||||
|
||||
@doc """
|
||||
Restarts all applications in the Erlang runtime system.
|
||||
|
||||
All applications are taken down smoothly, all code is unloaded, and all ports
|
||||
are closed before the system starts all applications once again.
|
||||
|
||||
## Examples
|
||||
|
||||
System.restart()
|
||||
|
||||
"""
|
||||
@doc since: "1.9.0"
|
||||
@spec restart :: :ok
|
||||
defdelegate restart(), to: :init
|
||||
|
||||
@doc """
|
||||
Carefully stops the Erlang runtime system.
|
||||
|
||||
@@ -676,6 +515,8 @@ defmodule System do
|
||||
Note that on many platforms, only the status codes 0-255 are supported
|
||||
by the operating system.
|
||||
|
||||
For more information, see `:init.stop/1`.
|
||||
|
||||
## Examples
|
||||
|
||||
System.stop(0)
|
||||
@@ -910,14 +751,10 @@ defmodule System do
|
||||
`convert_time_unit/3` accepts an additional time unit (other than the
|
||||
ones in the `t:time_unit/0` type) called `:native`. `:native` is the time
|
||||
unit used by the Erlang runtime system. It's determined when the runtime
|
||||
starts and stays the same until the runtime is stopped, but could differ
|
||||
the next time the runtime is started on the same machine. For this reason,
|
||||
you should use this function to convert `:native` time units to a predictable
|
||||
unit before you display them to humans.
|
||||
|
||||
To determine how many seconds the `:native` unit represents in your current
|
||||
runtime, you can can call this function to convert 1 second to the `:native`
|
||||
time unit: `System.convert_time_unit(1, :second, :native)`.
|
||||
starts and stays the same until the runtime is stopped. To determine what
|
||||
the `:native` unit amounts to in a system, you can call this function to
|
||||
convert 1 second to the `:native` time unit (i.e.,
|
||||
`System.convert_time_unit(1, :second, :native)`).
|
||||
"""
|
||||
@spec convert_time_unit(integer, time_unit | :native, time_unit | :native) :: integer
|
||||
def convert_time_unit(time, from_unit, to_unit) do
|
||||
@@ -954,7 +791,7 @@ defmodule System do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current operating system (OS) time.
|
||||
Returns the current OS time.
|
||||
|
||||
The result is returned in the `:native` time unit.
|
||||
|
||||
@@ -969,7 +806,7 @@ defmodule System do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current operating system (OS) time in the given time `unit`.
|
||||
Returns the current OS time in the given time `unit`.
|
||||
|
||||
This time may be adjusted forwards or backwards in time
|
||||
with no limitation and is not monotonic.
|
||||
@@ -1044,39 +881,26 @@ defmodule System do
|
||||
|
||||
defp normalize_time_unit(:native), do: :native
|
||||
|
||||
defp normalize_time_unit(:second), do: :second
|
||||
defp normalize_time_unit(:millisecond), do: :millisecond
|
||||
defp normalize_time_unit(:microsecond), do: :microsecond
|
||||
defp normalize_time_unit(:nanosecond), do: :nanosecond
|
||||
defp normalize_time_unit(:second), do: :seconds
|
||||
defp normalize_time_unit(:millisecond), do: :milli_seconds
|
||||
defp normalize_time_unit(:microsecond), do: :micro_seconds
|
||||
defp normalize_time_unit(:nanosecond), do: :nano_seconds
|
||||
|
||||
defp normalize_time_unit(:seconds), do: warn(:seconds, :second)
|
||||
defp normalize_time_unit(:milliseconds), do: warn(:milliseconds, :millisecond)
|
||||
defp normalize_time_unit(:microseconds), do: warn(:microseconds, :microsecond)
|
||||
defp normalize_time_unit(:nanoseconds), do: warn(:nanoseconds, :nanosecond)
|
||||
|
||||
defp normalize_time_unit(:milli_seconds), do: warn(:milli_seconds, :millisecond)
|
||||
defp normalize_time_unit(:micro_seconds), do: warn(:micro_seconds, :microsecond)
|
||||
defp normalize_time_unit(:nano_seconds), do: warn(:nano_seconds, :nanosecond)
|
||||
defp normalize_time_unit(:seconds), do: :seconds
|
||||
defp normalize_time_unit(:milliseconds), do: :milli_seconds
|
||||
defp normalize_time_unit(:microseconds), do: :micro_seconds
|
||||
defp normalize_time_unit(:nanoseconds), do: :nano_seconds
|
||||
|
||||
defp normalize_time_unit(unit) when is_integer(unit) and unit > 0, do: unit
|
||||
|
||||
defp normalize_time_unit(erlang_unit)
|
||||
when erlang_unit in [:milli_seconds, :micro_seconds, :nano_seconds] do
|
||||
erlang_unit
|
||||
end
|
||||
|
||||
defp normalize_time_unit(other) do
|
||||
raise ArgumentError,
|
||||
"unsupported time unit. Expected :second, :millisecond, " <>
|
||||
":microsecond, :nanosecond, or a positive integer, " <> "got #{inspect(other)}"
|
||||
end
|
||||
|
||||
defp warn(unit, replacement_unit) do
|
||||
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
|
||||
stacktrace = Enum.drop(stacktrace, 3)
|
||||
|
||||
:elixir_config.warn({System, unit}, stacktrace) &&
|
||||
IO.warn(
|
||||
"deprecated time unit: #{inspect(unit)}. A time unit should be " <>
|
||||
":second, :millisecond, :microsecond, :nanosecond, or a positive integer",
|
||||
stacktrace
|
||||
)
|
||||
|
||||
replacement_unit
|
||||
end
|
||||
end
|
||||
|
||||
+99
-195
@@ -9,7 +9,7 @@ defmodule Task do
|
||||
by computing a value asynchronously:
|
||||
|
||||
task = Task.async(fn -> do_some_work() end)
|
||||
res = do_some_other_work()
|
||||
res = do_some_other_work()
|
||||
res + Task.await(task)
|
||||
|
||||
Tasks spawned with `async` can be awaited on by their caller
|
||||
@@ -62,8 +62,8 @@ defmodule Task do
|
||||
a function to run:
|
||||
|
||||
Supervisor.start_link([
|
||||
{Task, fn -> :some_work end}
|
||||
], strategy: :one_for_one)
|
||||
{Task, fn -> ... some function ... end}
|
||||
])
|
||||
|
||||
However, if you want to invoke a specific module, function and
|
||||
arguments, or give the task process a name, you need to define
|
||||
@@ -85,20 +85,21 @@ defmodule Task do
|
||||
|
||||
Supervisor.start_link([
|
||||
{MyTask, arg}
|
||||
], strategy: :one_for_one)
|
||||
])
|
||||
|
||||
Since these tasks are supervised and not directly linked to
|
||||
the caller, they cannot be awaited on. `start_link/1`, unlike
|
||||
`async/1`, returns `{:ok, pid}` (which is the result expected
|
||||
by supervisors).
|
||||
the caller, they cannot be awaited on. Note `start_link/1`,
|
||||
unlike `async/1`, returns `{:ok, pid}` (which is the result
|
||||
expected by supervisors).
|
||||
|
||||
`use Task` defines a `child_spec/1` function, allowing the
|
||||
Note `use Task` defines a `child_spec/1` function, allowing the
|
||||
defined module to be put under a supervision tree. The generated
|
||||
`child_spec/1` can be customized with the following options:
|
||||
|
||||
* `:id` - the child specification identifier, defaults to the current module
|
||||
* `:start` - how to start the child process (defaults to calling `__MODULE__.start_link/1`)
|
||||
* `:restart` - when the child should be restarted, defaults to `:temporary`
|
||||
* `:shutdown` - how to shut down the child, either immediately or by giving it time to shut down
|
||||
* `:shutdown` - how to shut down the child
|
||||
|
||||
Opposite to `GenServer`, `Agent` and `Supervisor`, a Task has
|
||||
a default `:restart` of `:temporary`. This means the task will
|
||||
@@ -111,10 +112,7 @@ defmodule Task do
|
||||
|
||||
use Task, restart: :permanent
|
||||
|
||||
See the "Child specification" section in the `Supervisor` module
|
||||
for more detailed information. The `@doc` annotation immediately
|
||||
preceding `use Task` will be attached to the generated `child_spec/1`
|
||||
function.
|
||||
See the `Supervisor` docs for more information.
|
||||
|
||||
## Dynamically supervised tasks
|
||||
|
||||
@@ -124,12 +122,9 @@ defmodule Task do
|
||||
A short example is:
|
||||
|
||||
{:ok, pid} = Task.Supervisor.start_link()
|
||||
|
||||
task =
|
||||
Task.Supervisor.async(pid, fn ->
|
||||
# Do something
|
||||
end)
|
||||
|
||||
task = Task.Supervisor.async(pid, fn ->
|
||||
# Do something
|
||||
end)
|
||||
Task.await(task)
|
||||
|
||||
However, in the majority of cases, you want to add the task supervisor
|
||||
@@ -137,7 +132,7 @@ defmodule Task do
|
||||
|
||||
Supervisor.start_link([
|
||||
{Task.Supervisor, name: MyApp.TaskSupervisor}
|
||||
], strategy: :one_for_one)
|
||||
])
|
||||
|
||||
Now you can dynamically start supervised tasks:
|
||||
|
||||
@@ -149,22 +144,21 @@ defmodule Task do
|
||||
|
||||
Task.Supervisor.async(MyApp.TaskSupervisor, fn ->
|
||||
# Do something
|
||||
end)
|
||||
|> Task.await()
|
||||
end) |> Task.await()
|
||||
|
||||
Finally, check `Task.Supervisor` for other supported operations.
|
||||
|
||||
## Distributed tasks
|
||||
|
||||
Since Elixir provides a `Task.Supervisor`, it is easy to use one
|
||||
to dynamically start tasks across nodes:
|
||||
Since Elixir provides a Task supervisor, it is easy to use one
|
||||
to dynamically spawn tasks across nodes:
|
||||
|
||||
# On the remote node
|
||||
Task.Supervisor.start_link(name: MyApp.DistSupervisor)
|
||||
|
||||
# On the client
|
||||
supervisor = {MyApp.DistSupervisor, :remote@local}
|
||||
Task.Supervisor.async(supervisor, MyMod, :my_fun, [arg1, arg2, arg3])
|
||||
Task.Supervisor.async({MyApp.DistSupervisor, :remote@local},
|
||||
MyMod, :my_fun, [arg1, arg2, arg3])
|
||||
|
||||
Note that, when working with distributed tasks, one should use the `Task.Supervisor.async/4` function
|
||||
that expects explicit module, function and arguments, instead of `Task.Supervisor.async/2` that
|
||||
@@ -172,37 +166,6 @@ defmodule Task do
|
||||
the same module version to exist on all involved nodes. Check the `Agent` module
|
||||
documentation for more information on distributed processes as the limitations
|
||||
described there apply to the whole ecosystem.
|
||||
|
||||
## Ancestor and Caller Tracking
|
||||
|
||||
Whenever you start a new process, Elixir annotates the parent of that process
|
||||
through the `$ancestors` key in the process dictionary. This is often used to
|
||||
track the hierarchy inside a supervision tree.
|
||||
|
||||
For example, we recommend developers to always start tasks under a supervisor.
|
||||
This provides more visibility and allows you to control how those tasks are
|
||||
terminated when a node shuts down. That might look something like
|
||||
`Task.Supervisor.start_child(MySupervisor, task_specification)`. This means
|
||||
that, although your code is the one who invokes the task, the actual ancestor of
|
||||
the task is the supervisor, as the supervisor is the one effectively starting it.
|
||||
|
||||
To track the relationship between your code and the task, we use the `$callers`
|
||||
key in the process dictionary. Therefore, assuming the `Task.Supervisor` call
|
||||
above, we have:
|
||||
|
||||
[your code] -- calls --> [supervisor] ---- spawns --> [task]
|
||||
|
||||
Which means we store the following relationships:
|
||||
|
||||
[your code] [supervisor] <-- ancestor -- [task]
|
||||
^ |
|
||||
|--------------------- caller ---------------------|
|
||||
|
||||
The list of callers of the current process can be retrieved from the Process
|
||||
dictionary with `Process.get(:"$callers")`. This will return either `nil` or
|
||||
a list `[pid_n, ..., pid2, pid1]` with at least one entry Where `pid_n` is
|
||||
the PID that called the current process, `pid2` called `pid_n`, and `pid2` was
|
||||
called by `pid1`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
@@ -218,34 +181,16 @@ defmodule Task do
|
||||
* `:owner` - the PID of the process that started the task
|
||||
|
||||
"""
|
||||
@enforce_keys [:pid, :ref, :owner]
|
||||
defstruct pid: nil, ref: nil, owner: nil
|
||||
|
||||
@typedoc """
|
||||
The Task type.
|
||||
|
||||
See `%Task{}` for information about each field of the structure.
|
||||
"""
|
||||
@type t :: %__MODULE__{
|
||||
pid: pid() | nil,
|
||||
ref: reference() | nil,
|
||||
owner: pid() | nil
|
||||
}
|
||||
|
||||
defguardp is_timeout(timeout)
|
||||
when timeout == :infinity or (is_integer(timeout) and timeout >= 0)
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
@doc """
|
||||
Returns a specification to start a task under a supervisor.
|
||||
|
||||
`arg` is passed as the argument to `Task.start_link/1` in the `:start` field
|
||||
of the spec.
|
||||
|
||||
For more information, see the `Supervisor` module,
|
||||
the `Supervisor.child_spec/2` function and the `t:Supervisor.child_spec/0` type.
|
||||
See `Supervisor`.
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
@spec child_spec(term) :: Supervisor.child_spec()
|
||||
def child_spec(arg) do
|
||||
%{
|
||||
id: Task,
|
||||
@@ -257,18 +202,12 @@ defmodule Task do
|
||||
@doc false
|
||||
defmacro __using__(opts) do
|
||||
quote location: :keep, bind_quoted: [opts: opts] do
|
||||
if Module.get_attribute(__MODULE__, :doc) == nil do
|
||||
@doc """
|
||||
Returns a specification to start this module under a supervisor.
|
||||
|
||||
`arg` is passed as the argument to `Task.start_link/1` in the `:start` field
|
||||
of the spec.
|
||||
|
||||
For more information, see the `Supervisor` module,
|
||||
the `Supervisor.child_spec/2` function and the `t:Supervisor.child_spec/0` type.
|
||||
"""
|
||||
end
|
||||
@doc """
|
||||
Returns a specification to start this module under a supervisor.
|
||||
|
||||
See `Supervisor`.
|
||||
"""
|
||||
@doc since: "1.5.0"
|
||||
def child_spec(arg) do
|
||||
default = %{
|
||||
id: __MODULE__,
|
||||
@@ -286,12 +225,10 @@ defmodule Task do
|
||||
@doc """
|
||||
Starts a process linked to the current process.
|
||||
|
||||
`fun` must be a zero-arity anonymous function.
|
||||
|
||||
This is often used to start the process as part of a supervision tree.
|
||||
"""
|
||||
@spec start_link((() -> any)) :: {:ok, pid}
|
||||
def start_link(fun) when is_function(fun, 0) do
|
||||
def start_link(fun) do
|
||||
start_link(:erlang, :apply, [fun, []])
|
||||
end
|
||||
|
||||
@@ -299,23 +236,19 @@ defmodule Task do
|
||||
Starts a task as part of a supervision tree.
|
||||
"""
|
||||
@spec start_link(module, atom, [term]) :: {:ok, pid}
|
||||
def start_link(module, function_name, args)
|
||||
when is_atom(module) and is_atom(function_name) and is_list(args) do
|
||||
mfa = {module, function_name, args}
|
||||
Task.Supervised.start_link(get_owner(self()), get_callers(self()), mfa)
|
||||
def start_link(mod, fun, args) do
|
||||
Task.Supervised.start_link(get_info(self()), {mod, fun, args})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task.
|
||||
|
||||
`fun` must be a zero-arity anonymous function.
|
||||
|
||||
This is only used when the task is used for side-effects
|
||||
(i.e. no interest in the returned result) and it should not
|
||||
be linked to the current process.
|
||||
"""
|
||||
@spec start((() -> any)) :: {:ok, pid}
|
||||
def start(fun) when is_function(fun, 0) do
|
||||
def start(fun) do
|
||||
start(:erlang, :apply, [fun, []])
|
||||
end
|
||||
|
||||
@@ -327,27 +260,24 @@ defmodule Task do
|
||||
be linked to the current process.
|
||||
"""
|
||||
@spec start(module, atom, [term]) :: {:ok, pid}
|
||||
def start(module, function_name, args)
|
||||
when is_atom(module) and is_atom(function_name) and is_list(args) do
|
||||
mfa = {module, function_name, args}
|
||||
Task.Supervised.start(get_owner(self()), get_callers(self()), mfa)
|
||||
def start(mod, fun, args) do
|
||||
Task.Supervised.start(get_info(self()), {mod, fun, args})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task that must be awaited on.
|
||||
|
||||
`fun` must be a zero-arity anonymous function.
|
||||
This function spawns a process that is linked to and monitored
|
||||
by the caller process. A `Task` struct is returned containing
|
||||
the relevant information.
|
||||
|
||||
Read the `Task` module documentation for more information about the
|
||||
general usage of `async/1` and `async/3`.
|
||||
Read the `Task` module documentation for more info on general
|
||||
usage of `async/1` and `async/3`.
|
||||
|
||||
See also `async/3`.
|
||||
"""
|
||||
@spec async((() -> any)) :: t
|
||||
def async(fun) when is_function(fun, 0) do
|
||||
def async(fun) do
|
||||
async(:erlang, :apply, [fun, []])
|
||||
end
|
||||
|
||||
@@ -358,8 +288,8 @@ defmodule Task do
|
||||
Developers must eventually call `Task.await/2` or `Task.yield/2`
|
||||
followed by `Task.shutdown/2` on the returned task.
|
||||
|
||||
Read the `Task` module documentation for more information about
|
||||
the general usage of `async/1` and `async/3`.
|
||||
Read the `Task` module documentation for more info on general
|
||||
usage of `async/1` and `async/3`.
|
||||
|
||||
## Linking
|
||||
|
||||
@@ -415,52 +345,49 @@ defmodule Task do
|
||||
and `result` is the return value of the task function.
|
||||
"""
|
||||
@spec async(module, atom, [term]) :: t
|
||||
def async(module, function_name, args)
|
||||
when is_atom(module) and is_atom(function_name) and is_list(args) do
|
||||
mfa = {module, function_name, args}
|
||||
def async(mod, fun, args) do
|
||||
mfa = {mod, fun, args}
|
||||
owner = self()
|
||||
{:ok, pid} = Task.Supervised.start_link(get_owner(owner), get_callers(owner), :nomonitor, mfa)
|
||||
pid = Task.Supervised.spawn_link(owner, get_info(owner), mfa)
|
||||
ref = Process.monitor(pid)
|
||||
send(pid, {owner, ref})
|
||||
%Task{pid: pid, ref: ref, owner: owner}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a stream where the given function (`module` and `function_name`)
|
||||
is mapped concurrently on each element in `enumerable`.
|
||||
Returns a stream that runs the given `module`, `function`, and `args`
|
||||
concurrently on each item in `enumerable`.
|
||||
|
||||
Each element of `enumerable` will be prepended to the given `args` and
|
||||
processed by its own task. The tasks will be linked to an intermediate
|
||||
process that is then linked to the current process. This means a failure
|
||||
in a task terminates the current process and a failure in the current process
|
||||
Each item will be prepended to the given `args` and processed by its
|
||||
own task. The tasks will be linked to an intermediate process that is
|
||||
then linked to the current process. This means a failure in a task
|
||||
terminates the current process and a failure in the current process
|
||||
terminates all tasks.
|
||||
|
||||
When streamed, each task will emit `{:ok, value}` upon successful
|
||||
completion or `{:exit, reason}` if the caller is trapping exits.
|
||||
The order of results depends on the value of the `:ordered` option.
|
||||
Results are emitted in the same order as the original `enumerable`.
|
||||
|
||||
The level of concurrency and the time tasks are allowed to run can
|
||||
be controlled via options (see the "Options" section below).
|
||||
The level of concurrency can be controlled via the `:max_concurrency`
|
||||
option and defaults to `System.schedulers_online/0`. A timeout
|
||||
can also be given as an option representing the maximum amount of
|
||||
time to wait without a task reply.
|
||||
|
||||
Consider using `Task.Supervisor.async_stream/6` to start tasks
|
||||
Finally, consider using `Task.Supervisor.async_stream/6` to start tasks
|
||||
under a supervisor. If you find yourself trapping exits to handle exits
|
||||
inside the async stream, consider using `Task.Supervisor.async_stream_nolink/6`
|
||||
to start tasks that are not linked to the calling process.
|
||||
to start tasks that are not linked to the current process.
|
||||
|
||||
## Options
|
||||
|
||||
* `:max_concurrency` - sets the maximum number of tasks to run
|
||||
at the same time. Defaults to `System.schedulers_online/0`.
|
||||
|
||||
* `:ordered` - whether the results should be returned in the same order
|
||||
as the input stream. This option is useful when you have large
|
||||
streams and don't want to buffer results before they are delivered.
|
||||
This is also useful when you're using the tasks for side effects.
|
||||
Defaults to `true`.
|
||||
|
||||
* `:timeout` - the maximum amount of time (in milliseconds) each
|
||||
task is allowed to execute for. Defaults to `5000`.
|
||||
|
||||
* `:on_timeout` - what to do when a task times out. The possible
|
||||
values are:
|
||||
* `:exit` (default) - the process that spawned the tasks exits.
|
||||
@@ -477,39 +404,29 @@ defmodule Task do
|
||||
The concurrency can be increased or decreased using the `:max_concurrency`
|
||||
option. For example, if the tasks are IO heavy, the value can be increased:
|
||||
|
||||
max_concurrency = System.schedulers_online() * 2
|
||||
max_concurrency = System.schedulers_online * 2
|
||||
stream = Task.async_stream(collection, Mod, :expensive_fun, [], max_concurrency: max_concurrency)
|
||||
Enum.to_list(stream)
|
||||
|
||||
If you do not care about the results of the computation, you can run
|
||||
the stream with `Stream.run/1`. Also set `ordered: false`, as you don't
|
||||
care about the order of the results either:
|
||||
|
||||
stream = Task.async_stream(collection, Mod, :expensive_fun, [], ordered: false)
|
||||
Stream.run(stream)
|
||||
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec async_stream(Enumerable.t(), module, atom, [term], keyword) :: Enumerable.t()
|
||||
def async_stream(enumerable, module, function_name, args, options \\ [])
|
||||
when is_atom(module) and is_atom(function_name) and is_list(args) do
|
||||
build_stream(enumerable, {module, function_name, args}, options)
|
||||
def async_stream(enumerable, module, function, args, options \\ [])
|
||||
when is_atom(module) and is_atom(function) and is_list(args) do
|
||||
build_stream(enumerable, {module, function, args}, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a stream that runs the given function `fun` concurrently
|
||||
on each element in `enumerable`.
|
||||
on each item in `enumerable`.
|
||||
|
||||
Works the same as `async_stream/5` but with an anonymous function instead of a
|
||||
module-function-arguments tuple. `fun` must be a one-arity anonymous function.
|
||||
|
||||
Each `enumerable` element is passed as argument to the given function `fun` and
|
||||
Each `enumerable` item is passed as argument to the given function `fun` and
|
||||
processed by its own task. The tasks will be linked to the current process,
|
||||
similarly to `async/1`.
|
||||
|
||||
## Example
|
||||
|
||||
Count the code points in each string asynchronously, then add the counts together using reduce.
|
||||
Count the codepoints in each string asynchronously, then add the counts together using reduce.
|
||||
|
||||
iex> strings = ["long string", "longer string", "there are many of these"]
|
||||
iex> stream = Task.async_stream(strings, fn text -> text |> String.codepoints() |> Enum.count() end)
|
||||
@@ -520,36 +437,27 @@ defmodule Task do
|
||||
"""
|
||||
@doc since: "1.4.0"
|
||||
@spec async_stream(Enumerable.t(), (term -> term), keyword) :: Enumerable.t()
|
||||
def async_stream(enumerable, fun, options \\ [])
|
||||
when is_function(fun, 1) and is_list(options) do
|
||||
def async_stream(enumerable, fun, options \\ []) when is_function(fun, 1) do
|
||||
build_stream(enumerable, fun, options)
|
||||
end
|
||||
|
||||
defp build_stream(enumerable, fun, options) do
|
||||
&Task.Supervised.stream(enumerable, &1, &2, fun, options, fn [owner | _] = callers, mfa ->
|
||||
{:ok, pid} = Task.Supervised.start_link(get_owner(owner), callers, :nomonitor, mfa)
|
||||
{:ok, :link, pid}
|
||||
&Task.Supervised.stream(enumerable, &1, &2, fun, options, fn owner, mfa ->
|
||||
{:link, Task.Supervised.spawn_link(owner, get_info(owner), mfa)}
|
||||
end)
|
||||
end
|
||||
|
||||
# Returns a tuple with the node where this is executed and either the
|
||||
# registered name of the given PID or the PID of where this is executed. Used
|
||||
# registered name of the given pid or the pid of where this is executed. Used
|
||||
# when exiting from tasks to print out from where the task was started.
|
||||
defp get_owner(pid) do
|
||||
defp get_info(pid) do
|
||||
self_or_name =
|
||||
case Process.info(pid, :registered_name) do
|
||||
{:registered_name, name} when is_atom(name) -> name
|
||||
_ -> pid
|
||||
end
|
||||
|
||||
{node(), self_or_name, pid}
|
||||
end
|
||||
|
||||
defp get_callers(owner) do
|
||||
case :erlang.get(:"$callers") do
|
||||
[_ | _] = list -> [owner | list]
|
||||
_ -> [owner]
|
||||
end
|
||||
{node(), self_or_name}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -558,7 +466,7 @@ defmodule Task do
|
||||
In case the task process dies, the current process will exit with the same
|
||||
reason as the task.
|
||||
|
||||
A timeout in milliseconds or `:infinity`, can be given with a default value of `5000`. If the
|
||||
A timeout, in milliseconds, can be given with default value of `5000`. If the
|
||||
timeout is exceeded, then the current process will exit. If the task process
|
||||
is linked to the current process which is the case when a task is started with
|
||||
`async`, then the task process will also exit. If the task process is trapping
|
||||
@@ -577,9 +485,7 @@ defmodule Task do
|
||||
|
||||
It is not recommended to `await` a long-running task inside an OTP
|
||||
behaviour such as `GenServer`. Instead, you should match on the message
|
||||
coming from a task inside your `c:GenServer.handle_info/2` callback. For
|
||||
more information on the format of the message, see the documentation for
|
||||
`async/1`.
|
||||
coming from a task inside your `GenServer.handle_info/2` callback.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -588,12 +494,14 @@ defmodule Task do
|
||||
2
|
||||
|
||||
"""
|
||||
@spec await(t, timeout) :: term
|
||||
def await(%Task{ref: ref, owner: owner} = task, timeout \\ 5000) when is_timeout(timeout) do
|
||||
if owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
@spec await(t, timeout) :: term | no_return
|
||||
def await(task, timeout \\ 5000)
|
||||
|
||||
def await(%Task{owner: owner} = task, _) when owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
def await(%Task{ref: ref} = task, timeout) do
|
||||
receive do
|
||||
{^ref, reply} ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
@@ -609,7 +517,8 @@ defmodule Task do
|
||||
end
|
||||
|
||||
@doc false
|
||||
@deprecated "Pattern match directly on the message instead"
|
||||
# TODO: Remove on 2.0
|
||||
@deprecated "Pattern match on the message directly instead"
|
||||
def find(tasks, {ref, reply}) when is_reference(ref) do
|
||||
Enum.find_value(tasks, fn
|
||||
%Task{ref: ^ref} = task ->
|
||||
@@ -646,7 +555,7 @@ defmodule Task do
|
||||
* it isn't linked to the caller
|
||||
* the caller is trapping exits
|
||||
|
||||
A timeout, in milliseconds or `:infinity`, can be given with a default value
|
||||
A timeout, in milliseconds, can be given with default value
|
||||
of `5000`. If the time runs out before a message from
|
||||
the task is received, this function will return `nil`
|
||||
and the monitor will remain active. Therefore `yield/2` can be
|
||||
@@ -663,9 +572,8 @@ defmodule Task do
|
||||
case Task.yield(task, timeout) || Task.shutdown(task) do
|
||||
{:ok, result} ->
|
||||
result
|
||||
|
||||
nil ->
|
||||
Logger.warn("Failed to get a result in #{timeout}ms")
|
||||
Logger.warn "Failed to get a result in #{timeout}ms"
|
||||
nil
|
||||
end
|
||||
|
||||
@@ -674,11 +582,13 @@ defmodule Task do
|
||||
handle this case and return the result.
|
||||
"""
|
||||
@spec yield(t, timeout) :: {:ok, term} | {:exit, term} | nil
|
||||
def yield(%Task{ref: ref, owner: owner} = task, timeout \\ 5000) when is_timeout(timeout) do
|
||||
if owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
def yield(task, timeout \\ 5000)
|
||||
|
||||
def yield(%Task{owner: owner} = task, _) when owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
def yield(%Task{ref: ref} = task, timeout) do
|
||||
receive do
|
||||
{^ref, reply} ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
@@ -700,10 +610,8 @@ defmodule Task do
|
||||
|
||||
This function receives a list of tasks and waits for their
|
||||
replies in the given time interval. It returns a list
|
||||
of two-element tuples, with the task as the first element
|
||||
and the yielded result as the second. The tasks in the returned
|
||||
list will be in the same order as the tasks supplied in the `tasks`
|
||||
input argument.
|
||||
of tuples of two elements, with the task as the first element
|
||||
and the yielded result as the second.
|
||||
|
||||
Similarly to `yield/2`, each task's result will be
|
||||
|
||||
@@ -712,9 +620,6 @@ defmodule Task do
|
||||
* `{:exit, reason}` if the task has died
|
||||
* `nil` if the task keeps running past the timeout
|
||||
|
||||
A timeout, in milliseconds or `:infinity`, can be given with a default value
|
||||
of `5000`.
|
||||
|
||||
Check `yield/2` for more information.
|
||||
|
||||
## Example
|
||||
@@ -736,27 +641,26 @@ defmodule Task do
|
||||
|
||||
tasks_with_results = Task.yield_many(tasks, 5000)
|
||||
|
||||
results =
|
||||
Enum.map(tasks_with_results, fn {task, res} ->
|
||||
# Shut down the tasks that did not reply nor exit
|
||||
res || Task.shutdown(task, :brutal_kill)
|
||||
end)
|
||||
results = Enum.map(tasks_with_results, fn {task, res} ->
|
||||
# Shutdown the tasks that did not reply nor exit
|
||||
res || Task.shutdown(task, :brutal_kill)
|
||||
end)
|
||||
|
||||
# Here we are matching only on {:ok, value} and
|
||||
# ignoring {:exit, _} (crashed tasks) and `nil` (no replies)
|
||||
for {:ok, value} <- results do
|
||||
IO.inspect(value)
|
||||
IO.inspect value
|
||||
end
|
||||
|
||||
In the example above, we create tasks that sleep from 1
|
||||
up to 10 seconds and return the number of seconds they slept for.
|
||||
up to 10 seconds and return the number of seconds they slept.
|
||||
If you execute the code all at once, you should see 1 up to 5
|
||||
printed, as those were the tasks that have replied in the
|
||||
given time. All other tasks will have been shut down using
|
||||
the `Task.shutdown/2` call.
|
||||
"""
|
||||
@spec yield_many([t], timeout) :: [{t, {:ok, term} | {:exit, term} | nil}]
|
||||
def yield_many(tasks, timeout \\ 5000) when is_timeout(timeout) do
|
||||
def yield_many(tasks, timeout \\ 5000) do
|
||||
timeout_ref = make_ref()
|
||||
|
||||
timer_ref =
|
||||
@@ -810,7 +714,7 @@ defmodule Task do
|
||||
`{:exit, reason}` if the task died, otherwise `nil`.
|
||||
|
||||
The second argument is either a timeout or `:brutal_kill`. In case
|
||||
of a timeout, a `:shutdown` exit signal is sent to the task process
|
||||
of a `timeout`, a `:shutdown` exit signal is sent to the task process
|
||||
and if it does not exit within the timeout, it is killed. With `:brutal_kill`
|
||||
the task is killed straight away. In case the task terminates abnormally
|
||||
(possibly killed by another process), this function will exit with the same reason.
|
||||
@@ -819,10 +723,10 @@ defmodule Task do
|
||||
exiting with reason `:normal` or if the task is trapping exits. If the caller is
|
||||
exiting with a reason other than `:normal` and the task is not trapping exits, the
|
||||
caller's exit signal will stop the task. The caller can exit with reason
|
||||
`:shutdown` to shut down all of its linked processes, including tasks, that
|
||||
`:shutdown` to shutdown all of its linked processes, including tasks, that
|
||||
are not trapping exits without generating any log messages.
|
||||
|
||||
If a task's monitor has already been demonitored or received and there is not
|
||||
If a task's monitor has already been demonitored or received and there is not
|
||||
a response waiting in the message queue this function will return
|
||||
`{:exit, :noproc}` as the result or exit reason can not be determined.
|
||||
"""
|
||||
@@ -853,7 +757,7 @@ defmodule Task do
|
||||
end
|
||||
end
|
||||
|
||||
def shutdown(%Task{pid: pid} = task, timeout) when is_timeout(timeout) do
|
||||
def shutdown(%Task{pid: pid} = task, timeout) do
|
||||
mon = Process.monitor(pid)
|
||||
exit(pid, :shutdown)
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user