Compare commits

..
60 Commits
Author SHA1 Message Date
José Valim 2b338092b6 Release v1.7.2 2018-08-05 09:01:26 +02:00
Scott Riley 557b779bd4 Move parentheses in System.cmd() docs (#8046) 2018-08-04 18:44:11 +02:00
José Valim ece40480f6 Update CHANGELOG 2018-08-04 16:12:28 +02:00
José Valim a77445c46b Revert "Fix errors with ref/4 IEx helper (#8016)"
This reverts commit b9d6f4f9c1.
2018-08-04 15:37:15 +02:00
José Valim 4d55fa6814 Accordingly mark top level optional dependencies from child deps (#8035)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-08-04 14:08:33 +02:00
Fernando Tapia Rico b9d6f4f9c1 Fix errors with ref/4 IEx helper (#8016)
* Fix errors with ref/4 IEx helper

In addition, include documentation for that function.

* Update helpers.ex

* Add IEx.Helpers.ref/* tests

* Use examples that do not return ArgumentError

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-08-04 14:07:37 +02:00
Fernando Tapia Rico b0cf76c74c Fix year in negative datetimes with offset (#8038) 2018-08-04 13:18:41 +02:00
José Valim 7841fbb8e0 Do not fail suite if there are no tests to run
This matters in umbrella apps where some apps may not
have Elixir tests.

Closes #8040

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-08-04 00:46:04 +02:00
José Valim f5773fed42 Take negative years into account in DateTime (#8033)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-08-03 11:39:30 +02:00
José Valim e2b8ab9a0d Unify alias doc handling
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-31 23:20:35 +02:00
Dave Lucia 1dc6d10f30 mix help when theres a task and alias (#8020)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-31 23:20:30 +02:00
José Valim e7138fb2c6 Avoid Mix.ProjectStack deadlock, closes #8024
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-31 21:29:45 +02:00
José Valim 04ecc05adc Remove warnings for docs in the same clause
Unfortunately this leads to false positives when trying
to override the docs of an overridable definition.

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-28 21:33:46 +02:00
José Valim 8aab53b941 Release v1.7.1 2018-07-26 21:33:18 +02:00
Michał Muskała 07c276ab5c Fix issues with dialyzer infinite loop (#7993)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-26 21:30:35 +02:00
David Cuddeback 89e3a81adc Skip intermediate parts when creating NaiveDateTime 2018-07-26 11:37:43 +02:00
David Cuddeback 9752f1ce05 Optimize ISO-8601 parsing for UTC 2018-07-26 11:37:43 +02:00
José Valim 06e5ec2d4c Release v1.7.0 2018-07-25 22:36:59 +02:00
José Valim b6d3f29b69 Allow to prune metadata while escaping (#7949)
We use this option in ExUnit as we are escaping
the code to eventually convert it to a string
representation and therefore the metadata is not
relevant.

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-25 00:56:53 +02:00
Boyd Multerer bfdaf3f8d1 Update Process.info spec to reflect the possible nil return value (#7977)
The @spec for it did not reflect that possible return value - which causes dialyzer to throw errors if you check for that condition. Now the spec allows for a return value of nil.

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-25 00:56:28 +02:00
José Valim bc26f10040 Mark contextual_vars as private
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-25 00:56:07 +02:00
Tobiasz Małecki 52868e556d Added missing info about new StringIO.open/3 function (#7970) 2018-07-22 19:56:17 +02:00
José Valim 04c45b031a Use quote fragments to remove duplication and optimize calendar parsing (#7951)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-21 10:48:38 +02:00
José Valim d410466ce4 Format lib/elixir/lib/code.ex 2018-07-20 16:36:11 +02:00
José Valim fe3c263599 Remove other hard deprecations 2018-07-20 16:26:14 +02:00
José Valim 7558c9a023 Remove warning on since module attribute 2018-07-20 09:50:31 +02:00
José Valim 0d30dbfd24 Properly annotate key errors for structs, closes #7935 2018-07-19 22:41:45 +02:00
José Valim 793bbe6e3c Move some runtime deprecations to compile time 2018-07-19 22:36:21 +02:00
Jean-Philippe Cugnet 3318d33d2e Fix a typo in the unnecessary quotes warning (#7934)
[ci skip]

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-19 20:12:30 +02:00
José Valim b6d7769674 Release v1.7.0-rc.1 2018-07-19 12:09:05 +02:00
José Valim dc3a56c44f Remove leftover spaces
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:40:45 +02:00
Callum Dempsey Leach ea9cb84eb1 Clarify Application.get_env by illustrating a use case (#7929)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:40:41 +02:00
José Valim b7b7282a22 Optimize metadata merging and ensure no nil leakage (#7927)
Closes #7926

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:40:32 +02:00
José Valim d3d1833d47 Properly populate top_level field of deps (#7931)
Closes #7930

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:40:26 +02:00
Gary Rennie 7d427719fe Fix nested use of Mix.Config.eval!/2 (#7925)
Previously, if a config file uses Mix.Config.eval!/2 then it would fail with the
following error:

 > ** (RuntimeError) could not set configuration via Mix.Config. This usually
 >  means you are trying to execute a configuration file directly instead of
 > using the proper command, such as mix loadconfig
 > code: assert Mix.Config.eval!(fixture_path("configs/nested_import.exs")) ==
 > stacktrace:
 >   (mix) lib/mix/config.ex:71: Mix.Config.raise_improper_use!/0
 >   (mix) lib/mix/config.ex:222: Mix.Config.eval!/2
 >   lib/mix/test/mix/config_test.exs:110: (test)

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:40:17 +02:00
Tobiasz Małecki 9812b74a2c Added missing @doc :since metadata (#7922)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:40:01 +02:00
Tobiasz Małecki aebc7e2fe1 Fixed indentation in StringIO.open/3 examples (#7920)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:39:57 +02:00
Fernando Tapia Rico f0aeb7136f Use "time zone" instead of "timezone" everywhere (#7917)
There is a mix of "timezone" and "time zone" in the codebase. For what I read, "time zone" is usually more correct.

In addition, there are some variables and types named `time_zone`, so I think the best option is to go with "time zone".

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:39:50 +02:00
Tobiasz Małecki d92a450637 fixed typo in float moduledoc (#7915)
[ci skip]

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:39:44 +02:00
Andrea Leopardi afb4667cd0 Add an additional example to the docs for String.jaro_distance/1
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:39:39 +02:00
Fernando Tapia Rico eb50a09679 Clarify documentation of @compile :inline flag (#7909)
[ci skip]

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:39:30 +02:00
Henrik Larsson 2f8ecec32c Clarify how to assert on exits from linked processes (#7904)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:39:17 +02:00
Fernando Tapia Rico 4354938168 Remove duplicated sentence from Map's doc (#7907)
[ci skip]

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 19:39:13 +02:00
José Valim b06d82c93e Allow all args on Mix.Dep.loaded
Even though the API was private, developers were abusing it.
So we allow it to work for now until we remove it in future
versions.

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-18 10:36:59 +02:00
Myron Marston 3dbe8a6661 Changelog: explain mix test --failed more accurately. (#7911)
`mix test --failed` runs all tests that failed the last time they ran,
even if they were not included in the last run of the test suite. This
is important, because it means that you can run individual failed tests
to focus on fixing them without worrying about mix losing track of the
other failed tests.
2018-07-16 09:21:54 +02:00
José Valim 6932e25159 Doc fixes 2018-07-15 22:24:56 +02:00
José Valim 22abd6a48e Fix CHANGELOG typo 2018-07-14 14:21:39 +02:00
José Valim 05f5073cd1 Clarify that any metadata is accepted, only some are shown 2018-07-14 11:51:39 +02:00
Tobiasz Małecki 18d96c2fd5 Add Function docs to "Basic Types" group (#7900)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-13 23:45:26 +02:00
Wojtek Mach ea04ec8f81 Clarify how to configure :test_coverage (#7896)
[ci skip]

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-13 23:45:18 +02:00
Wojtek Mach 0a5d633a5e Update --cover docs (#7897)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-13 23:45:05 +02:00
José Valim 7392d736c4 Print checksums headings 2018-07-13 15:17:14 +02:00
José Valim 1164784b8e Release v1.7.0-rc.0 2018-07-13 14:43:27 +02:00
José Valim 6694ddbd2f Properly add parens around when with keywords as argument 2018-07-13 00:07:26 +02:00
José Valim 85a4d55efb Update CHANGELOG 2018-07-12 20:28:19 +02:00
Arkadiusz Gil 9b238e0316 Ignore unknown child info in Logger.Translator (#7892)
This prevents Logger application from crashing when supervisors report
children progress with fields unknown to Logger.Translator.

Closes #7889

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-12 20:15:08 +02:00
László Bácsi ca82388792 Show documentation metadata in IEx (only since and deprecated for now) (#7886) 2018-07-12 20:07:45 +02:00
Lukasz Samson b33dd12d86 Fix error when :trim_bom is used with :encoding
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-12 14:37:14 +02:00
Dimitrios Zorbas c36b2c9070 Fix syntax highlighting of Calendar.ISO docs (#7880)
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
2018-07-12 12:44:41 +02:00
José Valim ac75cdbe05 Prepare for v1.7 release 2018-07-12 11:57:29 +02:00
525 changed files with 22027 additions and 49258 deletions
+18
View File
@@ -0,0 +1,18 @@
version: 1-{branch}+{build}
build_script:
- cmd: C:\MinGW\msys\1.0\bin\make
- cmd: rmdir /s /q .git
before_test:
- cmd: set PATH=%PATH%;C:\Program Files\erl8.3\erts-8.3\bin
test_script:
- cmd: C:\MinGW\msys\1.0\bin\make --keep-going test_windows
environment:
ELIXIR_ASSERT_TIMEOUT: 2000
matrix:
allow_failures:
- platform: x86
- platform: x64
- platform: Any CPU
-165
View File
@@ -1,165 +0,0 @@
env:
CIRRUS_CLONE_DEPTH: 50
ELIXIR_ASSERT_TIMEOUT: 2000
ELIXIRC_OPTS: "--warnings-as-errors"
ERLC_OPTS: "+warnings_as_errors"
LANG: C.UTF-8
test_template: &DEFAULT_TEST_SETTINGS
# don't cancel the task execution if it's master or a release branch
auto_cancellation: $CIRRUS_BRANCH != 'master' && $CIRRUS_BRANCH !=~ 'v\d+\.\d+.*'
test_linux_task:
<<: *DEFAULT_TEST_SETTINGS
container:
image: buildpack-deps:trusty
cpu: 8
memory: 1536Mi
env:
PATH: "${CIRRUS_WORKING_DIR}/otp/bin:${PATH}"
matrix:
- name: Linux, ${OTP_RELEASE}, Ubuntu 14.04
alias: Linux Stable
matrix:
- env:
CHECK_POSIX_COMPLIANT: true
CHECK_REPRODUCIBLE: true
OTP_RELEASE: OTP-23.0
- env:
OTP_RELEASE: OTP-22.3
- env:
OTP_RELEASE: OTP-22.0
- env:
OTP_RELEASE: OTP-21.3.8
- env:
OTP_RELEASE: OTP-21.0
- name: Linux, OTP-${OTP_RELEASE}, development, Ubuntu 14.04
alias: Linux Development
allow_failures: true
skip_notifications: true
depends_on:
- Linux Stable
- FreeBSD Stable
matrix:
- env:
OTP_RELEASE: master
- env:
OTP_RELEASE: maint
install_script:
- wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-14.04/${OTP_RELEASE}.tar.gz
- mkdir -p otp
- tar zxf otp.tar.gz -C otp --strip-components=1
- otp/Install -minimal ${CIRRUS_WORKING_DIR}/otp
- rm -rf .git
- make compile
build_info_script: bin/elixir --version
test_formatted_script:
- make test_formatted &&
echo "All Elixir source code files are properly formatted."
dialyzer_script: dialyzer -pa lib/elixir/ebin --build_plt --output_plt elixir.plt --apps lib/elixir/ebin/elixir.beam lib/elixir/ebin/Elixir.Kernel.beam
test_erlang_script: make test_erlang
test_elixir_script: make test_elixir
check_posix_compliant_script: |
if [ -n "$CHECK_POSIX_COMPLIANT" ]; then
apt update
apt install -y shellcheck
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"
else
echo "The format of the shell scripts is only checked in the last stable Erlang/OTP version."
fi
check_reproducible_script: |
if [ -n "$CHECK_REPRODUCIBLE" ]; then
make check_reproducible
else
echo "The reproducibility of the build is only checked in the last stable Erlang/OTP version."
fi
test_windows_task:
<<: *DEFAULT_TEST_SETTINGS
name: Windows, OTP-${OTP_RELEASE}, Windows Server 2019
alias: Windows Stable
matrix:
- env:
OS_VERSION: 2019
OTP_RELEASE: 22.0
- env:
OS_VERSION: 2019
OTP_RELEASE: 21.0.1
windows_container:
image: fertapric/elixir-ci:otp-win64-${OTP_RELEASE}
os_version: ${OS_VERSION}
cpu: 4
memory: 6GB
install_script:
- rmdir /s /q .git
- make compile
build_info_script: bin/elixir --version
test_formatted_script:
- make test_formatted &&
echo "All Elixir source code files are properly formatted."
test_erlang_script: make --keep-going test_erlang
test_elixir_script: make --keep-going test_elixir
test_freebsd_task:
<<: *DEFAULT_TEST_SETTINGS
name: FreeBSD 12.1
alias: FreeBSD Stable
freebsd_instance:
image_family: freebsd-12-1
cpu: 8
memory: 7424Mi
env:
CHECK_REPRODUCIBLE: true
LC_ALL: en_US.UTF-8
install_script:
- pkg install -y erlang git gmake
- rm -rf .git
- gmake compile
build_info_script: bin/elixir --version
test_formatted_script:
- gmake test_formatted &&
echo "All Elixir source code files are properly formatted."
dialyzer_script: dialyzer -pa lib/elixir/ebin --build_plt --output_plt elixir.plt --apps lib/elixir/ebin/elixir.beam lib/elixir/ebin/Elixir.Kernel.beam
test_erlang_script: gmake test_erlang
test_elixir_script: gmake test_elixir
check_reproducible_script: |
if [ -n "$CHECK_REPRODUCIBLE" ]; then
gmake check_reproducible
else
echo "The reproducibility of the build is only checked in the last stable Erlang/OTP version."
fi
-17
View File
@@ -1,17 +0,0 @@
on: check_suite
name: CI email
jobs:
sendEmail:
name: Send email
runs-on: ubuntu-latest
steps:
- name: Send email
# Source: https://github.com/elixir-lang/elixir-ci
uses: docker://fertapric/elixir-ci-email:latest
env:
APP_NAME: Cirrus CI
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MAIL_FROM: ci@elixir-lang.org
MAIL_HOST: smtp.sendgrid.net
MAIL_PASSWORD: ${{ secrets.CI_EMAIL_PASSWORD }}
MAIL_USERNAME: ${{ secrets.CI_EMAIL_USERNAME }}
+44
View File
@@ -0,0 +1,44 @@
language: bash
sudo: false
env:
global:
- ELIXIR_ASSERT_TIMEOUT=2000
matrix:
- 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
matrix:
fast_finish: true
allow_failures:
- env: OTP_RELEASE=maint
- env: OTP_RELEASE=master
install:
- wget -O otp.tar.gz https://repo.hex.pm/builds/otp/ubuntu-14.04/${OTP_RELEASE}.tar.gz
- mkdir -p otp
- tar zxf otp.tar.gz -C otp --strip-components=1
- otp/Install -minimal $(pwd)/otp
- PATH=$(pwd)/otp/bin:$PATH
script:
- make compile
- rm -rf .git
- 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
notifications:
recipients:
- jose.valim@gmail.com
- eric.meadows.jonsson@gmail.com
- lexmag@me.com
- an.leopardi@gmail.com
+183 -302
View File
@@ -1,391 +1,272 @@
# Changelog for Elixir v1.10
# Changelog for Elixir v1.7
## Support for Erlang/OTP 21+
Elixir v1.7 is the last release to support Erlang/OTP 19. We recommend everyone to migrate to Erlang/OTP 20+.
Elixir v1.10 requires Erlang/OTP 21+, allowing Elixir to integrate with Erlang/OTP's new logger. Currently, this means that the logger level, logger metadata, as well as all log messages are now shared between Erlang and Elixir APIs.
## Documentation metadata
We will continue improving the relationship between the logging systems in future releases. In particular, we plan to expose all log levels and runtime filtering functionalities available in Erlang directly into Elixir in the next Elixir version.
This release also adds two new guards, `is_struct/1` and `is_map_key/2`, thanks to the strict requirement on Erlang/OTP 21+.
## Releases improvements
Elixir v1.9 introduced releases as a mechanism to package self-contained applications. Elixir v1.10 further improves releases with bug fixes and new enhancements based on feedback we got from the community. The highlights are:
* Allow the dual boot system of releases to be disabled on environments that are boot-time sensitive, such as embedded devices
* Track and raise if compile-time configuration is set or changes at runtime (more in the next section)
* Support for easily adding extra files to releases via overlays
* Allow `RELEASE_DISTRIBUTION` to be set to `none` in order to fully disable it
* Add a built-in `:tar` step that automatically packages releases
See the full CHANGELOG for more improvements.
## Improvements to sort-based APIs in Enum
`Enum.sort/1` in Elixir by default sorts from lowest to highest:
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:
```elixir
iex> Enum.sort(["banana", "apple", "pineapple"])
["apple", "banana", "pineapple"]
@moduledoc "A brand new module"
@moduledoc authors: ["Jane", "Mary"], since: "1.4.0"
```
If you want to sort from highest to lowest, you need to call `Enum.sort/2` with a custom sorting function, such as `Enum.sort(collection, &>=/2)`, which is not immediately obvious to someone reading the code:
Passing metadata is supported on `@doc`, `@moduledoc` and `@typedoc`.
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.
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.
## The `__STACKTRACE__` construct
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:
```elixir
iex> Enum.sort(["banana", "apple", "pineapple"], &>=/2)
["pineapple", "banana", "apple"]
```
Furthermore, comparison operators, such as `<=` and `>=`, perform structural sorting, instead of a semantic one. For example, using `>=` to sort dates descendingly won't yield the correct result:
```elixir
iex> Enum.sort([~D[2019-12-31], ~D[2020-01-01]])
[~D[2020-01-01], ~D[2019-12-31]]
```
To perform proper semantic comparison for dates, one would also need to pass a custom sorting function:
```elixir
iex> Enum.sort([~D[2019-12-31], ~D[2020-01-01]], &(Date.compare(&1, &2) != :lt))
[~D[2019-12-31], ~D[2020-01-01]]
```
Elixir v1.10 streamlines the sorting functions by introducing both `:asc` and `:desc` shortcuts:
```elixir
iex> Enum.sort(["banana", "apple", "pineapple"], :asc)
["apple", "banana", "pineapple"]
iex> Enum.sort(["banana", "apple", "pineapple"], :desc)
["pineapple", "banana", "apple"]
```
As well as adding the possibility to pass a module to perform semantic comparisons. For example, to sort dates, one now only needs to pass the `Date` module or even `{:desc, Date}` for descending semantical sort:
```elixir
iex> Enum.sort([~D[2019-12-31], ~D[2020-01-01]], Date)
[~D[2019-12-31], ~D[2020-01-01]]
iex> Enum.sort([~D[2019-12-31], ~D[2020-01-01]], {:desc, Date})
[~D[2020-01-01], ~D[2019-12-31]]
```
These API improvements make the code more concise and readable and they have also been added to `Enum.sort_by`, `Enum.min_by`, `Enum.max_by`, and friends.
## Tracking of compile-time configuration
In Elixir, we organize our code in applications. Libraries, your dependencies, and your own project are all separate applications. All applications in Elixir also come with an application environment.
The application environment is a key-value store that allows us to configure said application. While reading the application environment at runtime is the preferred approach, in some rare occasions you may want to use the application environment to configure the compilation of a certain project. This is often done by calling `Application.get_env/3` outside of a function:
```elixir
defmodule MyApp.DBClient do
@db_host Application.get_env(:my_app, :db_host, "db.local")
def start_link() do
SomeLib.DBClient.start_link(host: @db_host)
end
try do
... something that may fail ...
rescue
e ->
log(e, System.stacktrace())
reraise(e, System.stacktrace())
end
```
This approach has one big limitation: if you change the value of the application environment after the code is compiled, the value used at runtime is not going to change! For example, if you are using `mix release` and your `config/releases.exs` has:
config :my_app, :db_host, "db.production"
Because `config/releases.exs` is read after the code is compiled, the new value will have no effect as the code was compiled to connect to "db.local".
Of course, the obvious solution to this mismatch is to not read the application environment at compilation time in the first place, and instead move the code to inside a function:
In Elixir v1.7, this can be written as:
```elixir
defmodule MyApp.DBClient do
def start_link() do
SomeLib.DBClient.start_link(host: db_host())
end
defp db_host() do
Application.get_env(:my_app, :db_host, "db.local")
end
try do
... something that may fail ...
rescue
e ->
log(e, __STACKTRACE__)
reraise(e, __STACKTRACE__)
end
```
While this is the preferred approach, there are still two scenarios we need to address:
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.
1. Not everyone may be aware of this pitfall, so they will mistakenly read the application environemnt at compile-time, until they are bitten by this behaviour
Other parts of the exception system have been improved. For example, more information is provided in certain occurrences of `ArgumentError`, `ArithmeticError` and `KeyError` messages.
2. In rare occasions, you trully need to read the application environment at compile-time, and you want to be warned when you try to configure at runtime something that is valid only at compilation time
## Erlang/OTP logger integration
Elixir v1.10 aims to solve these two scenarios by introducing a `Application.compile_env/3` function. For example, to read the value at compile time, you can now do:
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:
* `:crash_reason` - a two-element tuple with the throw/error/exit reason as first argument and the stacktrace as second
* `:initial_call` - the initial call that started the process
* `:registered_name` - the process registered name as an atom
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.
## Other Logger improvements
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.
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:
```elixir
@db_host Application.compile_env(:my_app, :db_host, "db.local")
config :logger,
compile_time_purge_matching: [
[application: :foo, level_lower_than: :info],
[module: Bar, function: "foo/3"]
]
```
By using `compile_env/3`, Elixir will store the values used during compilation and compare them with the runtime values whenever your system starts, raising an error in case they differ. This helps developers ensure they are running their production systems with the configuration they intend to.
## ExUnit improvements
In future versions, we will deprecate the use `Application.get_env` at compile-time with a clear message pointing users to configuration best practices, effectively addressing the scenario where users read from the application environment at compile time unaware of its pitfalls.
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:
## Compiler tracing
```
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:
This release brings enhancements to the Elixir compiler and adds new capabilities for developers to listen to compilation events.
# 1
3
In previous Elixir versions, Elixir would compile a database of cross references between modules (such as function calls, references, structs, etc) for each project in order to perform all kinds of checks, such as deprecations and undefined functions.
# 2
7
Although this database was not public, developers would still use it to run their own checks against their projects. With time, developers would request more data to be included in the database, which was problematic as Elixir itself did not have a use for the additional data, and the database was not meant to be used externally in the first place.
In Elixir v1.10, we have addressed these problems by introducing compiler tracing. The compiler tracing allows developers to listen to events as they are emitted by the compiler, so they can store all of the information they need - and only the information they need.
Elixir itself is using the new compiler tracing to provide new functionality. One advantage of this approach is that developers can now disable undefined function warnings directly on the callsite. For example, imagine you have an optional dependency which may not be available in some cases. You can tell the compiler to skip warning on calls to optional modules with:
@compile {:no_warn_undefined, OptionalDependency}
defdelegate my_function_call(arg), to: OptionalDependency
Previously, this information had to be added to the overall project configuration, which was far away from where the optional call effectively happened.
## Other enhancements
Elixir's calendar data types got many improvements, such as sigil support for third-party calendars, as well as the additions of `DateTime.now!/2`, `DateTime.shift_zone!/3`, and `NaiveDateTime.local_now/0`.
There are many improvements related to Elixir's AST in this release too. First of all, `Code.string_to_quoted/2` has two new options, `:token_metadata` and `:literal_encoder`, that give more control over Elixir's parser. This information was already available to the Elixir code formatter and has now been made public. We have also extensively documented all of Elixir's AST metadata. These changes alongside compiler tracing means static analyzers and IDE integrations have a better foundation to analyze the source code.
ExUnit, our test framework, ships two small but important improvements: `ExUnit.CaptureIO` can now be used by tests that run concurrently and we have added "pattern-matching diffing". To understand the last feature, take this code:
```elixir
assert %{"status" => 200, "body" => %{"key" => "foo"}} = json_payload
stacktrace:
lib/ex_unit/examples/one_of_each.exs:158: (test)
```
Now imagine that `json_payload` is a large JSON blob and the `"key"` inside the `"body"` did not have value of `"foo"`. In previous Elixir versions, if the assertion failed, Elixir would print the right side and let you up to your own devices to figure out what went wrong. In Elixir v1.10, we diff the data structure against the pattern so you can see exactly which parts of the data matched the pattern and which ones did not. Note ExUnit already performed diffing when comparing data types, this new version adds diffing when matching data against a pattern.
Furthermore, failures in doctests are now colored and diffed.
## v1.10.4 (2020-07-04)
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.2 (2018-08-05)
### 1. Bug fixes
#### Elixir
* [Kernel] Fix a bug where custom types were printed as built-in types
* [Kernel] Don't add compile-time dependency on `defdelegate`
* [Kernel] Add line numbers to warnings on deprecated imports
* [Kernel] Report the correct line number when raising inside a macro
* [Task] Include callers in translated Logger metadata for Task
* [Task] Fix Task PID and caller in Task Supervisor reports
#### ExUnit
* [ExUnit.Formatter] Avoid crashes when diffing guards when the pattern does not match
* [ExUnit.Formatter] Also blame exceptions that come from linked and trapped exits
#### IEx
* [IEx.Helpers] Do not crash when printing a type that cannot be code formatted
#### Mix
* [mix app.start] Fix reading `.app` file located in archives (`.ez` files)
* [mix local.hex] Provide more guidance when Hex can't be installed
* [mix release] Properly encode config in releases
## v1.10.3 (2020-04-25)
### 1. Bug fixes
#### Elixir
* [Code] Return `[{mod, bin}]` from `Code.compile_file/2`, `Code.require_file/2`, `Code.load_file/2`
* [Code] Make sure the formatter respects newlines before and after module attributes
* [Kernel.ParallelCompiler] Fix a bug where the parallel compiler would raise in long compilation cycles
* [Kernel.ParallelCompiler] Fix a bug where the parallel compiler would raise if some of the modules being compiled referred to a module that has been loaded directly to memory
* [Module] Fix accidental breaking change where bodiless clauses had their body value on `@on_definition` callbacks set to an empty list instead of `nil`
* [String] Undeprecate `String.normalize/2` normalize and fix infinite loop caused by certain invalid strings
#### ExUnit
* [ExUnit.Assertions] Fix pattern matching diff when matching on pinned variables
* [ExUnit.Assertions] Fix pattern matching diff when matching variable struct names
* [ExUnit.Assertions] Fix pattern matching diff when matching on the binary concat operator (`<>`) and the left side is not a literal string
* [ExUnit.Assertions] Fix pattern matching diff when matching on pseudo-vars (`__MODULE__`, `__DIR__`, etc)
* [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] Respect the `:path` option when creating a `:tar` file for releases
* [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.10.2 (2020-02-26)
## v1.7.1 (2018-07-26)
### 1. Bug fixes
#### Elixir
* [Macro] Fix a bug where `Macro.to_string/1` would emit invalid code for sigils
* [Task] Do not crash `async_stream` monitor if it receives spurious DOWN messages
* [Calendar] Work-around a Dialyzer bug that causes it to loop for a long time, potentially indefinitely
#### Logger
* [Logger] Fix a bug where the Logger formatter would fail when handling unknown metadata values
#### Mix
* [mix compile] Do not write files to disk if `--warnings-as-errors` was given and warnings were emitted
## v1.10.1 (2020-02-10)
### 1. Bug fixes
#### Elixir
* [Code] Do not emit invalid code when formatting `nil`, `false`, and `true` keys in maps
* [Kernel] Ensure `with` clauses properly unpack "implicit guards" (such as matching on the struct name)
* [Kernel] Do not warn if commas are used by themselves in `~w`/`~W` sigils
* [Kernel] Do not validate the `:line` option in quote (the validation has been moved to v1.11 to give users more time to update their code)
* [Module] Ensure the code verifier handles the `:erlang.size/1` guard properly
#### Logger
* [Logger] Properly handle the `report_cb/2` option from Erlang
* [Logger] Fix truncation for multi-byte characters
* [Logger] Do not rebroadcast messages from remote nodes as this is now taken care by Erlang's logger
#### ExUnit
* [ExUnit] Ensure `assert_receive` produces valid exception messages in case of errors
#### Mix
* [mix release] Make sure the install command (Window specific) works on paths with spaces in the name
* [mix release] Allow using `remote` and `rpc` commands with `Application.compile_env/3`
## v1.10.0 (2020-01-27)
## v1.7.0 (2018-07-25)
### 1. Enhancements
#### Elixir
* [Application] Add `Application.compile_env/3` and `Application.compile_env!/2` for reading values at compilation time and tracking if they accidentally change during runtime
* [Calendar] Allow custom calendar representations in calendar sigils
* [Calendar] Add `c:Calendar.parse_time/1`, `c:Calendar.parse_date/1`, `c:Calendar.parse_naive_datetime/1` and `c:Calendar.parse_utc_datetime/1` callbacks to calendar behaviour
* [CLI] Add support for `NO_COLOR` environment variable
* [Code] Add `:token_metadata` and `:literal_encoder` support to `Code.string_to_quoted/2`
* [Code] Add compiler tracing to lift events done by the compiler
* [Code] Return `{:error, :unavailable}` in `Code.ensure_compiled/1` if module is in a deadlock
* [DateTime] Add `DateTime.now!/2` and `DateTime.shift_zone!/3`
* [Enum] Speed up getting one random element from enumerables
* [Enum] Add `Enum.frequencies/1`, `Enum.frequencies_by/2`, and `Enum.map_intersperse/2`
* [Enum] Allow a sorting function on `Enum.min/max/min_by/max_by`
* [Enum] Add `asc/desc` and `compare/1` support to `Enum.sort/2`
* [Exception] Add version alongside app names in stacktraces
* [Function] Add `Function.identity/1`
* [Kernel] Add `Kernel.is_struct/1` and `Kernel.is_map_key/2`
* [Kernel] Warn when function head comes immediately after the implementation instead of before the implementation
* [Kernel] Warn if duplicate key is found in struct declaration
* [Kernel] Print all undefined functions as warnings and then raise. This allows users to see all undefined calls at once, when it would otherwise require them to compile the code multiple times
* [Kernel] Allow file, line and context to be dynamically set on `quote`
* [Keyword] Add `Keyword.pop!/2` and `Keyword.pop_values/2`
* [Map] Add `Map.pop!/2`
* [MapSet] Optimize multiple operations
* [Module] Add `Module.has_attribute?/2`
* [Module] Add `@compile {:no_warn_undefined, mfa_or_module}` to turn off undefined function warnings
* [NaiveDateTime] Add `NaiveDateTime.local_now/0`
* [Record] Warn if duplicate key is found in record declaration
* [String] Update to Unicode 12.1
* [StringIO] Add `:encoding` option to StringIO and optimize `get_chars` operation
* [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.Assertions] Support diffs in pattern matching and in `assert_receive`
* [ExUnit.CaptureIO] Supports capturing named devices in asynchronous tests
* [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] Warn on circular file imports when loading default `.iex.exs`
* [IEx] Allow customization of the continuation prompt on IEx
* [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] Allow `start_options` to be configured on Logger's GenEvent
* [Logger] Integrate Elixir's Logger with Erlang/OTP 21+'s logger. This means setting up the logger level in Elixir will automatically change the logger level for Erlang and vice-versa
* [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 compile] Add `--profile time` flag to profile compilation steps
* [mix deps.compile] Add `--skip-umbrella-children` flag. The new flag does not compile umbrella apps. This is useful for building caches in CD/CI pipelines
* [mix deps.unlock] Add `--check-unused` flag. The new flag raises if there are any unused dependencies in the lock file
* [mix release] Allow `RELEASE_DISTRIBUTION` to be set to `none`
* [mix release] Support overlays in `rel/overlays`
* [mix release] Allow configuration reboot to be disabled in releases
* [mix test] Add support for simple round-robin test partitioning across multiple machines
* [Mix.Project] Add `MIX_DEPS_PATH` environment variable for setting `:deps_path`
* [Mix.Project] Add `Mix.Project.deps_scms/1` that returns deps with their SCMs
* [Mix.Task] Add `Mix.Task.Compiler.after_compiler/2` callback, to simplify compilers that may need to run something at multiple steps
* [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] Ensure multiline do/end with no spaces compile under trim mode
#### Elixir
* [Enum] Allow positive range slices on infinite streams given to `Enum.slice/2`
* [Kernel] Raise error on functions/guards without implementation
* [Kernel] Do not expand expressions inside interpolation twice
* [Keyword] Ensure keyword replace and update preserve order
* [Module] Raise instead of silently failing when performing a write module operation during after-compile
* [Module] Fix `@macrocallback` definitions with a `when` clause
* [Path] Fix `Path.absname/1` to correctly handle UNC paths on Windows
* [Stream] Close with correct accumulator in `Stream.resource/3` when called for a single-element list
* [Stream] Allow `Stream.cycle/1` to be double nested inside `Stream.cycle/1`
* [URI] Preserve slashes in URIs without authority
* [URI] Require a nil or an absolute path on URIs with host or authority
* [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.Assertions] Fix `assert_receive` and `assert match?` to behave consistently compared `receive` and `match?` when given an invalid macro
#### IEx
* [IEx] Exit IEx session if the group leader exits
* [IEx] Allow `pry` to be used in non-tty terminals
* [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] Do not filter out warning for external files from diagnostics
* [Mix.Project] Ensure user given `:manager` to dependencies has higher precedence than the SCM one
* [Mix.Project] Recompile umbrella children when config files change and `mix compile` is called from the umbrella root
* [Mix.Task] Always recompile before running tasks from dependencies
* [Mix.Task] Ensure project's Logger config is used when running Mix tasks
* [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)
#### Elixir
* [Code] `compiler_options/0` is deprecated in favor of `compiler_option/1`
* [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`
#### Mix
#### Logger
* [Mix.Config] `Mix.Config.persist/1` has been deprecated. Instead of `Mix.Config.persist(config)` use `Application.put_all_env(config, persistent: true)` (`Application.put_all_env/2` was added in v1.9)
* [mix xref] `calls/0` is deprecated in favor of compiler tracer
* [mix xref] The `xref.exclude` option has been moved to `elixirc_options.no_warn_undefined` as the `xref` pass has been moved into the compiler
* [Logger] `compile_time_purge_level` is deprecated in favor of `compile_time_purge_matching`
### 4. Hard-deprecations
#### Elixir
* [Code] `Code.load_file/2` has been deprecated in favor of `Code.require_file/2` or `Code.compile_file/2`
* [Code] `Code.loaded_files/0` and `Code.unload_file/1` have been deprecated in favor of `Code.required_files/0` and `Code.unrequire_file/1` respectively
* [Code] `Code.ensure_compiled?/1` is deprecated in favor of `Code.ensure_compiled/1`
* [String] `String.normalize/2` has been deprecated in favor of `:unicode.characters_to_nfc_binary/1` or `:unicode.characters_to_nfd_binary/1` which ship as part of Erlang/OTP 20+
* [Supervisor] `Supervisor.Spec.supervise/2` has been deprecated in favor of the new Supervisor child specification
* [Supervisor] The `:simple_one_for_one` strategy in `Supervisor` has been deprecated in favor of `DynamicSupervisor`
* [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
#### Logger
## v1.6
* [Logger] `:compile_time_purge_level` application environment configuration has been deprecated in favor of the more general `:compile_time_purge_matching` config
* [Logger] Deprecate logging non-chardata values
#### Mix
* [mix compile.xref] This check has been moved into the compiler and has no effect now
* [mix xref] `xref` now only tracks dependencies between modules and files, no longer between functions. See "Compilation tracers" to learn more about how to track this information directly
* [mix xref deprecations] This check has been moved into the compiler and has no effect now
* [mix xref unreachable] This check has been moved into the compiler and has no effect now
## v1.9
The CHANGELOG for v1.9 releases can be found [in the v1.9 branch](https://github.com/elixir-lang/elixir/blob/v1.9/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
View File
@@ -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
@@ -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.
+25
View File
@@ -174,3 +174,28 @@
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2012 Plataformatec
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+29 -78
View File
@@ -1,10 +1,8 @@
PREFIX ?= /usr/local
TEST_FILES ?= "*_test.exs"
SHARE_PREFIX ?= $(PREFIX)/share
MAN_PREFIX ?= $(SHARE_PREFIX)/man
CANONICAL := v1.10/ # 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))
@@ -17,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 format 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 >= 21)])' -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 21.0 is required to build Elixir"; \
echo "At least Erlang/OTP 19.0 is required to build Elixir"; \
exit 1; \
fi
endef
@@ -37,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)"
@@ -45,20 +45,8 @@ 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)"
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/$(TEST_FILES)";
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)))
@ echo "==> $(1) (exunit)"
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/*_test.exs";
endef
#==> Compilation tasks
@@ -90,9 +78,10 @@ $(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
echo "==> bootstrap (compile)"; \
$(ERL) -s elixir_compiler bootstrap -s erlang halt; \
fi
$(Q) $(MAKE) unicode
@ echo "==> elixir (compile)";
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/kernel.ex" -o ebin;
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/**/*.ex" -o ebin;
$(Q) $(MAKE) unicode
$(Q) $(MAKE) app
app: $(APP)
@@ -127,32 +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/ex_unit/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/ex_unit/ebin/* lib/ex_unit/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/ex_unit/ebin/ lib/ex_unit/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
@@ -176,16 +139,16 @@ clean_residual_files:
#==> Documentation tasks
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}")
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" --main "$(3)" --source-url "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) --output doc/$(2) --canonical "https://hexdocs.pm/$(2)/$(CANONICAL)" --homepage-url "https://elixir-lang.org/docs.html" --formatter "$(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
docs_elixir: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (elixir)"
$(Q) rm -rf doc/elixir
$(call COMPILE_DOCS,Elixir,elixir,Kernel,--config "lib/elixir/docs.exs")
$(call COMPILE_DOCS,Elixir,elixir,Kernel,-c lib/elixir/docs.exs)
docs_eex: compile ../ex_doc/bin/ex_doc
@ echo "==> ex_doc (eex)"
@@ -230,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:"
@@ -240,7 +203,6 @@ zips: Precompiled.zip Docs.zip
#==> Test tasks
# If you modify this task, please update .cirrus.yml accordingly
test: test_formatted test_erlang test_elixir
test_windows: test test_taskkill
@@ -253,19 +215,8 @@ TEST_ERL = lib/elixir/test/erlang
TEST_EBIN = lib/elixir/test/ebin
TEST_ERLS = $(addprefix $(TEST_EBIN)/, $(addsuffix .beam, $(basename $(notdir $(wildcard $(TEST_ERL)/*.erl)))))
define FORMAT
$(Q) if [ "$(OS)" = "Windows_NT" ]; then \
cmd //C call ./bin/mix.bat format $(1); \
else \
bin/elixir bin/mix format $(1); \
fi
endef
format: compile
$(call FORMAT)
test_formatted: compile
$(call FORMAT,--check-formatted)
bin/elixir bin/mix format --check-formatted
test_erlang: compile $(TEST_ERLS)
@ echo "==> elixir (eunit)"
@@ -279,12 +230,12 @@ $(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_FILES)"; \
cd lib/elixir && cmd //C call ../../bin/elixir.bat -r "test/elixir/test_helper.exs" -pr "test/elixir/**/*_test.exs"; \
else \
cd lib/elixir && ../../bin/elixir -r "test/elixir/test_helper.exs" -pr "test/elixir/**/$(TEST_FILES)"; \
cd lib/elixir && ../../bin/elixir -r "test/elixir/test_helper.exs" -pr "test/elixir/**/*_test.exs"; \
fi
#==> Dialyzer tasks
@@ -303,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
@@ -328,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
+4 -18
View File
@@ -1,7 +1,9 @@
LEGAL NOTICE INFORMATION
------------------------
All the files in this distribution are copyright to the terms below.
All the files in this distribution are copyright (c) 2012 Plataformatec
covered under Elixir's license (see the file LICENSE) except the cases
below.
== lib/elixir/src/elixir_parser.erl (generated by build scripts)
@@ -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,
+53 -73
View File
@@ -1,35 +1,16 @@
![Elixir](https://github.com/elixir-lang/elixir-lang.github.com/raw/master/images/logo/logo.png)
=========
[![Build status](https://api.cirrus-ci.com/github/elixir-lang/elixir.svg?branch=master)](https://cirrus-ci.com/github/elixir-lang/elixir)
[![Travis build](https://secure.travis-ci.org/elixir-lang/elixir.svg?branch=master
"Build Status")](https://travis-ci.org/elixir-lang/elixir)
Elixir is a dynamic, functional language designed for building scalable
and maintainable applications.
Elixir is a dynamic, functional language designed for building scalable and maintainable applications.
For more about Elixir, installation and documentation,
[check Elixir's website](https://elixir-lang.org/).
## 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
@@ -45,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 21.0 or later). You can check your Erlang/OTP version
by calling `erl` in the command line. You will see some information similar to:
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 21 [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
@@ -63,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
@@ -78,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`.
@@ -110,9 +101,9 @@ To recompile (including Erlang modules):
make compile
```
After your changes are done, please remember to run `make 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:
@@ -123,17 +114,17 @@ 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 Cirrus CI](https://cirrus-ci.com/github/elixir-lang/elixir).
More tasks can be found by reading the [Makefile](Makefile).
[the official build status on Travis-CI](https://travis-ci.org/elixir-lang/elixir).
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
@@ -156,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
@@ -168,45 +158,35 @@ 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
cd ../elixir && make docs
```
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
```
This will produce documentation sets for `elixir`, `eex`, `ex_unit`, `iex`, `logger`,
and `mix` under the `doc` directory. If you are planning to contribute documentation,
This will produce documentation sets for `elixir`, `mix`, etc. under
the `doc` directory. If you are planning to contribute documentation,
[please check our best practices for writing documentation](https://hexdocs.pm/elixir/writing-documentation.html).
## 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
View File
@@ -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), update `erlang.csv` to the precompiled OTP version, 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 /SECURITY.md in "Compatibility and Deprecations"
4. Commit "Start vMAJOR.MINOR+1"
3. Commit "Start vMAJOR.MINOR+1"
-23
View File
@@ -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.10 | Bug fixes and security patches
| 1.9 | Security patches only
| 1.8 | Security patches only
| 1.7 | Security patches only
| 1.6 | Security patches only
## 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
+1 -1
View File
@@ -1 +1 @@
1.10.4
1.7.2
+62 -170
View File
@@ -1,58 +1,31 @@
#!/bin/sh
set -e
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
cat <<USAGE >&2
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.
USAGE
** 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
@@ -62,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")
@@ -206,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
if [ "$OS" != "Windows_NT" ] && [ -z "$NO_COLOR" ]; then
# 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 "$@"
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
View File
@@ -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 -11
View File
@@ -1,24 +1,20 @@
#!/bin/sh
set -e
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
cat <<USAGE >&2
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.
USAGE
** 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
@@ -28,7 +24,7 @@ readlink_f () {
if [ -h "$filename" ]; then
readlink_f "$(readlink "$filename")"
else
echo "$(pwd -P)/$filename"
echo "`pwd -P`/$filename"
fi
}
+28 -11
View File
@@ -1,18 +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
cat <<USAGE >&2
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".
USAGE
** 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
@@ -22,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
View File
@@ -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
+2 -2
View File
@@ -1,3 +1,3 @@
#!/usr/bin/env elixir
Mix.start()
Mix.CLI.main()
Mix.start
Mix.CLI.main
+7 -16
View File
@@ -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
@@ -76,12 +75,6 @@ defmodule EEx do
This will never appear
<% end %>
To escape an EEx expression in EEx use `<%% content %>`. For example:
<%%= x + 3 %>
will be rendered as `<%= x + 3 %>`.
Notice that different engines may have different rules
for each tag. Other tags may be added in future versions.
@@ -112,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"
@@ -148,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
@@ -175,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
@@ -184,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)
@@ -215,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
View File
@@ -9,7 +9,7 @@ defmodule EEx.Compiler do
and the engine together by handling the tokens and invoking
the engine every time a full expression or text is received.
"""
@spec compile(String.t(), keyword) :: Macro.t()
@spec compile(String.t(), keyword) :: Macro.t() | no_return
def compile(source, opts) when is_binary(source) and is_list(opts) do
file = opts[:file] || "nofile"
line = opts[:line] || 1
@@ -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 immediately 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
View File
@@ -2,70 +2,58 @@ defmodule EEx.Engine do
@moduledoc ~S"""
Basic EEx engine that ships with Elixir.
An engine needs to implement all callbacks below.
An engine needs to implement six functions:
An engine may also `use EEx.Engine` to get the default behaviour
but this is not advised. In such cases, if any of the callbacks
are overridden, they must call `super()` to delegate to the
underlying `EEx.Engine`.
* `init(opts)` - called at the beginning of every text
and it must return the initial state.
* `handle_body(state)` - receives the state of the document
and it must return a quoted expression.
* `handle_text(state, text)` - it receives the state,
the text and must return a new quoted expression.
* `handle_expr(state, marker, expr)` - it receives the state,
the marker, the expr and must return a new state.
* `handle_begin(state)` - called every time there a new state
is needed with an empty buffer. Typically called for do/end
blocks, case expressions, anonymous functions, etc
* `handle_end(state)` - opposite of `handle_begin(state)` and
it must return quoted expression
The marker is what follows exactly after `<%`. For example,
`<% foo %>` has an empty marker, but `<%= foo %>` has `"="`
as marker. The allowed markers so far are:
* `""`
* `"="`
* `"/"`
* `"|"`
Markers `"/"` and `"|"` are only for use in custom EEx engines
and are not implemented by default. Using them without the
implementation raises `EEx.SyntaxError`.
If your engine does not implement all markers, please ensure that
`handle_expr/3` falls back to `EEx.Engine.handle_expr/3`
to raise the proper error message.
Read `handle_expr/3` below for more information about the markers
implemented by default by this engine.
`EEx.Engine` can be used directly if one desires to use the
default implementations for the functions above.
"""
@type state :: term
@doc """
Called at the beginning of every template.
It must return the initial state.
"""
@callback init(opts :: keyword) :: state
@doc """
Called at the end of every template.
It must return Elixir's quoted expressions for the template.
"""
@callback handle_body(state) :: Macro.t()
@doc """
Called for the text/static parts of a template.
It must return the updated state.
"""
@callback handle_text(state, text :: String.t()) :: state
@doc """
Called for the dynamic/code parts of a template.
The marker is what follows exactly after `<%`. For example,
`<% foo %>` has an empty marker, but `<%= foo %>` has `"="`
as marker. The allowed markers so far are:
* `""`
* `"="`
* `"/"`
* `"|"`
Markers `"/"` and `"|"` are only for use in custom EEx engines
and are not implemented by default. Using them without an
appropriate implementation raises `EEx.SyntaxError`.
It must return the updated state.
"""
@callback handle_expr(state, marker :: String.t(), expr :: Macro.t()) :: state
@doc """
Invoked at the beginning of every nesting.
It must return a new state that is used only inside the nesting.
Once the nesting terminates, the current `state` is resumed.
"""
@callback handle_begin(state) :: state
@doc """
Invokes at the end of a nesting.
It must return Elixir's quoted expressions for the nesting.
"""
@callback handle_end(state) :: Macro.t()
@doc false
@@ -77,24 +65,24 @@ defmodule EEx.Engine do
EEx.Engine.init(opts)
end
def handle_body(state) do
EEx.Engine.handle_body(state)
def handle_body(quoted) do
EEx.Engine.handle_body(quoted)
end
def handle_begin(state) do
EEx.Engine.handle_begin(state)
def handle_begin(quoted) do
EEx.Engine.handle_begin(quoted)
end
def handle_end(state) do
EEx.Engine.handle_end(state)
def handle_end(quoted) do
EEx.Engine.handle_end(quoted)
end
def handle_text(state, text) do
EEx.Engine.handle_text(state, text)
def handle_text(buffer, text) do
EEx.Engine.handle_text(buffer, text)
end
def handle_expr(state, marker, expr) do
EEx.Engine.handle_expr(state, marker, expr)
def handle_expr(buffer, marker, expr) do
EEx.Engine.handle_expr(buffer, marker, expr)
end
defoverridable EEx.Engine
@@ -110,9 +98,9 @@ defmodule EEx.Engine do
This can be added to any custom engine by invoking
`handle_assign/1` with `Macro.prewalk/2`:
def handle_expr(state, token, expr) do
def handle_expr(buffer, token, expr) do
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
super(state, token, expr)
EEx.Engine.handle_expr(buffer, token, expr)
end
"""
@@ -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
+2 -3
View File
@@ -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"
"""
+35 -50
View File
@@ -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,10 +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)
expr = pad_if_needed(token, expr, trimmed?)
{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
@@ -115,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)
@@ -156,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
@@ -184,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
@@ -206,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
@@ -233,14 +221,11 @@ 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
defp trim_whitespace(list) do
list
end
defp pad_if_needed(:start_expr, [h | _] = expr, true) when h not in @spaces, do: [?\s | expr]
defp pad_if_needed(_, expr, _), do: expr
end
+8 -10
View File
@@ -1,7 +1,8 @@
Code.require_file("../test_helper.exs", __DIR__)
defmodule EEx.SmartEngineTest do
use ExUnit.Case, async: true
# TODO: Make this async: true once capture_io is removed
use ExUnit.Case
test "evaluates simple string" do
assert_eval("foo bar", "foo bar")
@@ -28,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
+23 -52
View File
@@ -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 -152
View File
@@ -64,20 +64,6 @@ defmodule EExTest do
assert_eval(" • • •\n Jößé Vâlìm Jößé Vâlìm\n", template)
end
test "no spaces" do
string = """
<%=cond do%>
<%false ->%>
this
<%true ->%>
that
<%end%>
"""
expected = "\n that\n\n"
assert_eval(expected, string, [])
end
test "trim mode" do
string = "<%= 123 %> \n456\n <%= 789 %>"
expected = "123456\n789"
@@ -98,32 +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 "trim mode with no spaces" do
string = """
<%=cond do%>
<%false ->%>
this
<%true ->%>
that
<%end%>
"""
expected = " that\n"
assert_eval(expected, string, [], trim: true)
end
test "embedded code" do
assert_eval("foo bar", "foo <%= :bar %>")
end
@@ -140,12 +100,6 @@ defmodule EExTest do
assert_eval("foo ", "foo <%= if false do %>bar<% end %>")
end
test "embedded code with do preceded 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
@@ -178,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) ) %>"
@@ -283,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 = """
@@ -422,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
@@ -627,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
@@ -645,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
@@ -653,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
+3 -1
View File
@@ -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
View File
@@ -1,10 +1,6 @@
# Returns config for Elixir docs
[
extras: Path.wildcard("lib/elixir/pages/*.md"),
groups_for_functions: [
Guards: & &1[:guard] == true
],
skip_undefined_reference_warnings_on: ["compatibility-and-deprecations"],
groups_for_modules: [
# [Kernel, Kernel.SpecialForms],
@@ -12,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,
+1 -1
View File
@@ -10,4 +10,4 @@ main([Source, Target, Version]) ->
Props = lists:keyreplace(vsn, 1, Props1, {vsn, Version}),
AppDef = io_lib:format("~tp.~n", [{application, Name, Props}]),
ok = file:write_file(Target, AppDef),
io:format("Generated ~ts app~n", [Name]).
io:format("Generated ~ts.app~n", [Name]).
+108 -95
View File
@@ -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
@@ -236,25 +287,6 @@ defmodule Access do
:error
end
@doc """
Same as `fetch/2` but returns the value directly,
or raises a `KeyError` exception if `key` is not found.
## Examples
iex> Access.fetch!(%{name: "meg", age: 26}, :name)
"meg"
"""
@doc since: "1.10.0"
@spec fetch!(container, term) :: term
def fetch!(container, key) do
case fetch(container, key) do
{:ok, value} -> value
:error -> raise(KeyError, key: key, term: container)
end
end
@doc """
Gets the value for the given key in a container (a map, keyword
list, or struct that implements the `Access` behaviour).
@@ -329,14 +361,6 @@ defmodule Access do
The returned value is a two-element tuple with the "get" value returned by
`fun` and a new container with the updated value under `key`.
## Examples
iex> Access.get_and_update([a: 1], :a, fn current_value ->
...> {current_value, current_value + 1}
...> end)
{1, [a: 2]}
"""
@spec get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
when get_value: var, data: container
@@ -587,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]}
@@ -637,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:
@@ -667,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
@@ -697,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 -119
View File
@@ -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
unless Module.has_attribute?(__MODULE__, :doc) 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
@@ -423,15 +374,6 @@ defmodule Agent do
Same as `update/3` but a module, function, and arguments are expected
instead of an anonymous function. The state is added as first
argument to the given list of arguments.
## Examples
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
iex> Agent.update(pid, Kernel, :+, [12])
:ok
iex> Agent.get(pid, fn state -> state end)
54
"""
@spec update(agent, module, atom, [term], timeout) :: :ok
def update(agent, module, fun, args, timeout \\ 5000) do
@@ -447,15 +389,6 @@ defmodule Agent do
Note that `cast` returns `:ok` immediately, regardless of whether `agent` (or
the node it should live on) exists.
## Examples
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
iex> Agent.cast(pid, fn state -> state + 1 end)
:ok
iex> Agent.get(pid, fn state -> state end)
43
"""
@spec cast(agent, (state -> state)) :: :ok
def cast(agent, fun) when is_function(fun, 1) do
@@ -468,15 +401,6 @@ defmodule Agent do
Same as `cast/2` but a module, function, and arguments are expected
instead of an anonymous function. The state is added as first
argument to the given list of arguments.
## Examples
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
iex> Agent.cast(pid, Kernel, :+, [12])
:ok
iex> Agent.get(pid, fn state -> state end)
54
"""
@spec cast(agent, module, atom, [term]) :: :ok
def cast(agent, module, fun, args) do
+124 -355
View File
@@ -8,120 +8,78 @@ defmodule Application do
An application is a component implementing some specific functionality, with a
standardized directory structure, configuration, and lifecycle. Applications
are *loaded*, *started*, and *stopped*. Each application also has its own
environment, which provides a unified API for configuring each application.
are *loaded*, *started*, and *stopped*.
Developers typically interact with the application environment and its
callback module. Therefore those will be the topics we will cover first
before jumping into details about the application resource file and life-cycle.
## The application resource file
Applications are specified in their [*resource
file*](http://erlang.org/doc/man/app.html), which is a file called `APP.app`,
where `APP` is the application name. For example, the application resource
file of the OTP application `ex_unit` is called `ex_unit.app`.
You'll find the resource file of an application in its `ebin` directory, it is
generated automatically by Mix. Some of its keys are taken from the keyword
lists returned by the `project/0` and `application/0` functions defined in
`mix.exs`, and others are generated by Mix itself.
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`.
## The application environment
Each application has its own environment. The environment is a keyword list
that maps atoms to terms. Note that this environment is unrelated to the
operating system environment.
The key `env` of an application resource file has a list of tuples that map
atoms to terms, and its contents are known as the application *environment*.
Note that this environment is unrelated to the operating system environment.
By default, the environment of an application is an empty list. In a Mix
project's `mix.exs` file, you can set the `:env` key in `application/0`:
project you can set that key in `application/0`:
def application do
[env: [db_host: "localhost"]]
[env: [redis_host: "localhost"]]
end
Now, in your application, you can read this environment by using functions
such as `fetch_env!/2` and friends:
and the generated application resource file is going to have it included.
defmodule MyApp.DBClient do
def start_link() do
SomeLib.DBClient.start_link(host: db_host())
end
The environment is available after loading the application, which is a process
explained later:
defp db_host do
Application.fetch_env!(:my_app, :db_host)
end
end
Application.load(:APP_NAME)
#=> :ok
Application.get_env(:APP_NAME, :redis_host)
#=> "localhost"
In Mix projects, the environment of the application and its dependencies can
be overridden via the `config/config.exs` file. For example, someone using
your application can override its `:db_host` environment variable as follows:
be overridden via the `config/config.exs` file. If you start the application
with Mix, that configuration is available at compile time, and at runtime too,
but take into account it is not included in the generated application resource
file, and it is not available if you start the application without Mix.
import Config
config :my_app, :db_host, "db.local"
For example, someone using your application can override its `:redis_host`
environment variable as follows:
You can also change the application environment dynamically by using functions
such as `put_env/3` and `delete_env/2`. However, as a rule of thumb, each application
is responsible for its own environment. Please do not use the functions in this
module for directly accessing or modifying the environment of other applications.
config :APP_NAME, redis_host: "redis.local"
### Compile-time environment
The function `put_env/3` allows dynamic configuration of the application
environment, but as a rule of thumb each application is responsible for its
own environment. Please do not use the functions in this module for directly
accessing or modifying the environment of other applications.
In the previous example, we read the application environment at runtime:
defmodule MyApp.DBClient do
def start_link() do
SomeLib.DBClient.start_link(host: db_host())
end
defp db_host do
Application.fetch_env!(:my_app, :db_host)
end
end
In other words, the environment key `:db_host` for application `:my_app`
will only be read when `MyApp.DBClient` effectively starts. While reading
the application environment at runtime is the preferred approach, in some
rare occasions you may want to use the application environment to configure
the compilation of a certain project. This is often done by calling `get_env/3`
outside of a function:
defmodule MyApp.DBClient do
@db_host Application.get_env(:my_app, :db_host, "db.local")
def start_link() do
SomeLib.DBClient.start_link(host: @db_host)
end
end
This approach has one big limitation: if you change the value of the
application environment after the code is compiled, the value used at
runtime is not going to change! For example, if you are using `mix release`
and your `config/releases.exs` has:
config :my_app, :db_host, "db.production"
This value will have no effect as the code was compiled to connect to "db.local",
which is mostly likely unavailable in the production environment.
For those reasons, reading the application environment at runtime should be the
first choice. However, if you really have to read the application environment
during compilation, we recommend you to use `compile_env/3` instead:
@db_host Application.compile_env(:my_app, :db_host, "db.local")
By using `compile_env/3`, tools like Mix will store the values used during
compilation and compare the compilation values with the runtime values whenever
your system starts, raising an error in case they differ.
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
Applications can be loaded, started, and stopped. Generally, build tools
like Mix take care of starting an application and all of its dependencies
for you, but you can also do it manually by calling:
{:ok, _} = Application.ensure_all_started(:some_app)
When an application starts, developers may configure a callback module
that executes custom code. Developers use this callback to start the
application supervision tree.
The first step to do so is to add a `:mod` key to the `application/0`
definition in your `mix.exs` file. It expects a tuple, with the application
callback module and start argument (commonly an empty list):
The `mod` key of an application resource file configures an application
callback module and start argument:
def application do
[mod: {MyApp, []}]
end
This key is optional, only needed for applications that start a supervision tree.
The `MyApp` module given to `:mod` needs to implement the `Application` behaviour.
This can be done by putting `use Application` in that module and implementing the
`c:start/2` callback, for example:
@@ -158,19 +116,6 @@ defmodule Application do
tree is terminated. Its argument is the state returned by `c:start/2`, if it did,
or `[]` otherwise, and its return value is passed to `c:stop/1`.
## The application resource file
In the sections above, we have configured an application in the
`application/0` section of the `mix.exs` file. Ultimately, Mix will use
this configuration to create an [*application resource
file*](http://erlang.org/doc/man/app.html), which is a file called
`APP_NAME.app`. For example, the application resource file of the OTP
application `ex_unit` is called `ex_unit.app`.
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`.
## The application lifecycle
### Loading applications
@@ -181,8 +126,16 @@ defmodule Application do
Application.load(:ex_unit)
#=> :ok
If an application has included applications, they are also loaded. And the
procedure recurses if they in turn have included applications. Included
applications are unrelated to applications in Mix umbrella projects, they are
an Erlang/OTP concept that has to do with coordinated starts.
When an application is loaded, the environment specified in its resource file
is merged with any overrides from config files.
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` flags passed to `erl`.
Loading an application *does not* load its modules.
@@ -202,12 +155,14 @@ defmodule Application do
system. Instead, you start one or more applications, each with their own
initialization and termination logic.
When an application is started, the `Application.load/1` is automatically
invoked if it hasn't been done yet. Then, it checks if the dependencies listed
in the `applications` key of the resource file are already started. Having at
least one dependency not started is an error condition. Functions like
`ensure_all_started/1` takes care of starting an application and all of its
dependencies for you.
When an application is started, the runtime loads it if it hasn't been loaded
yet (in the technical sense described above). Then, it checks if the
dependencies listed in the `applications` key of the resource file are already
started. Having at least one dependency not started is an error condition, but
when you start an application with `mix run`, Mix takes care of starting all
the dependencies for you, so in practice you don't need to worry about it
unless you are starting applications manually with the API provided by this
module.
If the application does not have a callback module configured, starting is
done at this point. Otherwise, its `c:start/2` callback if invoked. The PID of
@@ -226,9 +181,9 @@ defmodule Application do
Stopping an application with a callback module has three steps:
1. If present, invoke the optional callback `c:prep_stop/1`.
2. Terminate the top-level supervisor.
3. Invoke the required callback `c:stop/1`.
1. If present, invoke the optional callback `c:prep_stop/1`.
2. Terminate the top-level supervisor.
3. Invoke the required callback `c:stop/1`.
The arguments passed to the callbacks are related to the state optionally
returned by `c:start/2`, and are documented in the section about the callback
@@ -242,22 +197,23 @@ 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
The Mix build tool automates most of the application management tasks. For example,
The Mix build tool can also be used to start your applications. For example,
`mix test` automatically starts your application dependencies and your application
itself before your test runs. `mix run --no-halt` boots your current project and
can be used to start a long running system. See `mix help run`.
Developers can also use `mix release` to build **releases**. Releases are able to
package all of your source code as well as the Erlang VM into a single directory.
Releases also give you explicit control over how each application is started and in
which order. They also provide a more streamlined mechanism for starting and
stopping systems, debugging, logging, as well as system monitoring.
Developers can also use tools like [Distillery](https://github.com/bitwalker/distillery)
that build **releases**. Releases are able to package all of your source code
as well as the Erlang VM into a single directory. Releases also give you explicit
control over how each application is started and in which order. They also provide
a more streamlined mechanism for starting and stopping systems, debugging, logging,
as well as system monitoring.
Finally, Elixir provides tools such as escripts and archives, which are
different mechanisms for packaging your application. Those are typically used
@@ -297,7 +253,7 @@ defmodule Application do
application specification key `:start_phases` is not `:undefined`.
`start_args` are the arguments passed to the application in the `:mod`
specification key (for example, `mod: {MyApp, [:my_args]}`).
specification key (e.g., `mod: {MyApp, [:my_args]}`).
This function should either return `{:ok, pid}` or `{:ok, pid, state}` if
startup is successful. `pid` should be the PID of the top supervisor. `state`
@@ -340,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
@@ -350,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
@@ -377,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,
@@ -391,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`.
@@ -411,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
@@ -426,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
@@ -453,139 +395,16 @@ 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
@doc """
Reads the application environment at compilation time.
Similar to `get_env/3`, except it must be used to read values
at compile time. This allows Elixir to track when configuration
values change between compile time and runtime.
The first argument is the application name. The second argument
`key_or_path` is either an atom key or a path to traverse in
search of the configuration, starting with an atom key.
For example, imagine the following configuration:
config :my_app, :key, [foo: [bar: :baz]]
We can access it during compile time as:
Application.compile_env(:my_app, :key)
#=> [foo: [bar: :baz]]
Application.compile_env(:my_app, [:key, :foo])
#=> [bar: :baz]
Application.compile_env(:my_app, [:key, :foo, :bar])
#=> :baz
A default value can also be given as third argument. If
any of the keys in the path along the way is missing, the
default value is used:
Application.compile_env(:my_app, [:unknown, :foo, :bar], :default)
#=> :default
Application.compile_env(:my_app, [:key, :unknown, :bar], :default)
#=> :default
Application.compile_env(:my_app, [:key, :foo, :unknown], :default)
#=> :default
Giving a path is useful to let Elixir know that only certain paths
in a large configuration are compile time dependent.
"""
# TODO: Warn if get_env/fetch_env/fetch_env! is used at compile time instead of compile_env
@doc since: "1.10.0"
@spec compile_env(app, key | list, value) :: value
defmacro compile_env(app, key_or_path, default \\ nil) when is_atom(app) do
if __CALLER__.function do
raise "Application.compile_env/3 cannot be called inside functions, only in the module body"
end
quote do
Application.__compile_env__(unquote(app), unquote(key_or_path), unquote(default), __ENV__)
end
end
@doc false
def __compile_env__(app, key_or_path, default, env) do
case fetch_compile_env(app, key_or_path, env) do
{:ok, value} -> value
:error -> default
end
end
@doc """
Reads the application environment at compilation time or raises.
This is the same as `compile_env/3` but it raises an
ArgumentError if the configuration is not available.
"""
@doc since: "1.10.0"
@spec compile_env!(app, key | list) :: value
defmacro compile_env!(app, key_or_path) when is_atom(app) do
if __CALLER__.function do
raise "Application.compile_env!/2 cannot be called inside functions, only in the module body"
end
quote do
Application.__compile_env__!(unquote(app), unquote(key_or_path), __ENV__)
end
end
@doc false
def __compile_env__!(app, key_or_path, env) do
case fetch_compile_env(app, key_or_path, env) do
{:ok, value} ->
value
:error ->
raise ArgumentError,
"could not fetch application environment #{inspect(key_or_path)} for application " <>
"#{inspect(app)} #{fetch_env_failed_reason(app, key_or_path)}"
end
end
defp fetch_compile_env(app, key, env) when is_atom(key),
do: fetch_compile_env(app, key, [], env)
defp fetch_compile_env(app, [key | paths], env) when is_atom(key),
do: fetch_compile_env(app, key, paths, env)
defp fetch_compile_env(app, key, path, env) do
return = traverse_env(fetch_env(app, key), path)
for tracer <- env.tracers do
tracer.trace({:compile_env, app, [key | path], return}, env)
end
return
end
defp traverse_env(return, []), do: return
defp traverse_env(:error, _paths), do: :error
defp traverse_env({:ok, value}, [key | keys]), do: traverse_env(Access.fetch(value, key), keys)
@doc """
Returns the value for `key` in `app`'s environment.
If the configuration parameter does not exist, the function returns the
`default` value.
**Important:** if you are reading the application environment at compilation
time, for example, inside the module definition instead of inside of a
function, see `compile_env/3` instead.
**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).
## Examples
`get_env/3` is commonly used to read the configuration of your OTP applications.
@@ -599,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]
@@ -615,9 +434,14 @@ defmodule Application do
by module names). Our database engine can then traverse each repository in the
list and then call `get_env(:my_app, Databases.RepoOne)` and so forth to retrieve
the configuration of each one.
**Important:** if you are writing a library to be used by other developers,
it is generally recommended to avoid the application environment, as the
application environment is effectively a global storage. For more information,
read our [library guidelines](/library-guidelines.html).
"""
@spec get_env(app, key, value) :: value
def get_env(app, key, default \\ nil) when is_atom(app) do
def get_env(app, key, default \\ nil) do
:application.get_env(app, key, default)
end
@@ -627,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
@@ -638,35 +462,31 @@ defmodule Application do
Returns the value for `key` in `app`'s environment.
If the configuration parameter does not exist, raises `ArgumentError`.
**Important:** if you are reading the application environment at compilation
time, for example, inside the module definition instead of inside of a
function, see `compile_env!/2` instead.
"""
@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
:error ->
raise ArgumentError,
"could not fetch application environment #{inspect(key)} for application " <>
"#{inspect(app)} #{fetch_env_failed_reason(app, key)}"
end
end
vsn = :application.get_key(app, :vsn)
app = inspect(app)
key = inspect(key)
defp fetch_env_failed_reason(app, key) do
vsn = :application.get_key(app, :vsn)
case vsn do
{:ok, _} ->
raise ArgumentError,
"could not fetch application environment #{key} for application #{app} " <>
"because configuration #{key} was not set"
case vsn do
{:ok, _} ->
"because configuration at #{inspect(key)} was not set"
:undefined ->
"because the application was not loaded/started. If your application " <>
"depends on #{inspect(app)} at runtime, make sure to load/start it or " <>
"list it under :extra_applications in your mix.exs file"
:undefined ->
raise ArgumentError,
"could not fetch application environment #{key} for application #{app} " <>
"because the application was not loaded/started. If your application " <>
"depends on #{app} at runtime, make sure to load/start it or list it " <>
"under :extra_applications in your mix.exs file"
end
end
end
@@ -682,58 +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
# TODO: Remove this once we support Erlang/OTP 22+ exclusively.
@compile {:no_warn_undefined, {:application, :set_env, 2}}
@doc """
Puts the environment for multiple apps at the same time.
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`.
"""
@doc since: "1.9.0"
@spec put_all_env([{app, [{key, value}]}], timeout: timeout, persistent: boolean) :: :ok
def put_all_env(config, opts \\ []) when is_list(config) and is_list(opts) do
# 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
@@ -752,22 +537,6 @@ defmodule Application do
:application.ensure_started(app, type)
end
@doc """
Ensures the given `app` is loaded.
Same as `load/2` but returns `:ok` if the application was already
loaded.
"""
@doc since: "1.10.0"
@spec ensure_loaded(app) :: :ok | {:error, term}
def ensure_loaded(app) when is_atom(app) do
case :application.load(app) do
:ok -> :ok
{:error, {:already_loaded, ^app}} -> :ok
{:error, _} = error -> error
end
end
@doc """
Ensures the given `app` and its applications are started.
@@ -825,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
@@ -908,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
+3 -40
View File
@@ -1,46 +1,8 @@
defmodule Atom do
@moduledoc """
Atoms are constants whose values are their own name.
They are often useful to enumerate over distinct values, such as:
iex> :apple
:apple
iex> :orange
:orange
iex> :watermelon
:watermelon
Atoms are equal if their names are equal.
iex> :apple == :apple
true
iex> :apple == :orange
false
Often they are used to express the state of an operation, by using
values such as `:ok` and `:error`.
The booleans `true` and `false` are also atoms:
iex> true == :true
true
iex> is_atom(false)
true
iex> is_boolean(:false)
true
Elixir allows you to skip the leading `:` for the atoms `false`, `true`,
and `nil`.
Atoms must be composed of Unicode characters such as letters, numbers,
underscore, and `@`. If the keyword has a character that does not
belong to the category above, such as spaces, you can wrap it in
quotes:
iex> :"this is an atom with spaces"
:"this is an atom with spaces"
Convenience functions for working with atoms.
See also `Kernel.is_atom/1`.
"""
@doc """
@@ -75,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
+66 -66
View File
@@ -10,85 +10,85 @@ defmodule Base do
## Base 16 alphabet
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|:---------|------:|:---------|------:|:---------|------:|:---------|
| 0 | 0 | 4 | 4 | 8 | 8 | 12 | C |
| 1 | 1 | 5 | 5 | 9 | 9 | 13 | D |
| 2 | 2 | 6 | 6 | 10 | A | 14 | E |
| 3 | 3 | 7 | 7 | 11 | B | 15 | F |
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
| 0| 0| 4| 4| 8| 8| 12| C|
| 1| 1| 5| 5| 9| 9| 13| D|
| 2| 2| 6| 6| 10| A| 14| E|
| 3| 3| 7| 7| 11| B| 15| F|
## Base 32 alphabet
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|:---------|------:|:---------|------:|:---------|------:|:---------|
| 0 | A | 9 | J | 18 | S | 27 | 3 |
| 1 | B | 10 | K | 19 | T | 28 | 4 |
| 2 | C | 11 | L | 20 | U | 29 | 5 |
| 3 | D | 12 | M | 21 | V | 30 | 6 |
| 4 | E | 13 | N | 22 | W | 31 | 7 |
| 5 | F | 14 | O | 23 | X | | |
| 6 | G | 15 | P | 24 | Y | (pad) | = |
| 7 | H | 16 | Q | 25 | Z | | |
| 8 | I | 17 | R | 26 | 2 | | |
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
| 0| A| 9| J| 18| S| 27| 3|
| 1| B| 10| K| 19| T| 28| 4|
| 2| C| 11| L| 20| U| 29| 5|
| 3| D| 12| M| 21| V| 30| 6|
| 4| E| 13| N| 22| W| 31| 7|
| 5| F| 14| O| 23| X| | |
| 6| G| 15| P| 24| Y| (pad)| =|
| 7| H| 16| Q| 25| Z| | |
| 8| I| 17| R| 26| 2| | |
## Base 32 (extended hex) alphabet
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|:---------|------:|:---------|------:|:---------|------:|:---------|
| 0 | 0 | 9 | 9 | 18 | I | 27 | R |
| 1 | 1 | 10 | A | 19 | J | 28 | S |
| 2 | 2 | 11 | B | 20 | K | 29 | T |
| 3 | 3 | 12 | C | 21 | L | 30 | U |
| 4 | 4 | 13 | D | 22 | M | 31 | V |
| 5 | 5 | 14 | E | 23 | N | | |
| 6 | 6 | 15 | F | 24 | O | (pad) | = |
| 7 | 7 | 16 | G | 25 | P | | |
| 8 | 8 | 17 | H | 26 | Q | | |
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
| 0| 0| 9| 9| 18| I| 27| R|
| 1| 1| 10| A| 19| J| 28| S|
| 2| 2| 11| B| 20| K| 29| T|
| 3| 3| 12| C| 21| L| 30| U|
| 4| 4| 13| D| 22| M| 31| V|
| 5| 5| 14| E| 23| N| | |
| 6| 6| 15| F| 24| O| (pad)| =|
| 7| 7| 16| G| 25| P| | |
| 8| 8| 17| H| 26| Q| | |
## Base 64 alphabet
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|:----------|------:|:---------|------:|:---------|------:|:---------|
| 0 | A | 17 | R | 34 | i | 51 | z |
| 1 | B | 18 | S | 35 | j | 52 | 0 |
| 2 | C | 19 | T | 36 | k | 53 | 1 |
| 3 | D | 20 | U | 37 | l | 54 | 2 |
| 4 | E | 21 | V | 38 | m | 55 | 3 |
| 5 | F | 22 | W | 39 | n | 56 | 4 |
| 6 | G | 23 | X | 40 | o | 57 | 5 |
| 7 | H | 24 | Y | 41 | p | 58 | 6 |
| 8 | I | 25 | Z | 42 | q | 59 | 7 |
| 9 | J | 26 | a | 43 | r | 60 | 8 |
| 10 | K | 27 | b | 44 | s | 61 | 9 |
| 11 | L | 28 | c | 45 | t | 62 | + |
| 12 | M | 29 | d | 46 | u | 63 | / |
| 13 | N | 30 | e | 47 | v | | |
| 14 | O | 31 | f | 48 | w | (pad) | = |
| 15 | P | 32 | g | 49 | x | | |
| 16 | Q | 33 | h | 50 | y | | |
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
| 0| A| 17| R| 34| i| 51| z|
| 1| B| 18| S| 35| j| 52| 0|
| 2| C| 19| T| 36| k| 53| 1|
| 3| D| 20| U| 37| l| 54| 2|
| 4| E| 21| V| 38| m| 55| 3|
| 5| F| 22| W| 39| n| 56| 4|
| 6| G| 23| X| 40| o| 57| 5|
| 7| H| 24| Y| 41| p| 58| 6|
| 8| I| 25| Z| 42| q| 59| 7|
| 9| J| 26| a| 43| r| 60| 8|
| 10| K| 27| b| 44| s| 61| 9|
| 11| L| 28| c| 45| t| 62| +|
| 12| M| 29| d| 46| u| 63| /|
| 13| N| 30| e| 47| v| | |
| 14| O| 31| f| 48| w| (pad)| =|
| 15| P| 32| g| 49| x| | |
| 16| Q| 33| h| 50| y| | |
## Base 64 (URL and filename safe) alphabet
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|:---------|------:|:---------|------:|:---------|------:|:---------|
| 0 | A | 17 | R | 34 | i | 51 | z |
| 1 | B | 18 | S | 35 | j | 52 | 0 |
| 2 | C | 19 | T | 36 | k | 53 | 1 |
| 3 | D | 20 | U | 37 | l | 54 | 2 |
| 4 | E | 21 | V | 38 | m | 55 | 3 |
| 5 | F | 22 | W | 39 | n | 56 | 4 |
| 6 | G | 23 | X | 40 | o | 57 | 5 |
| 7 | H | 24 | Y | 41 | p | 58 | 6 |
| 8 | I | 25 | Z | 42 | q | 59 | 7 |
| 9 | J | 26 | a | 43 | r | 60 | 8 |
| 10 | K | 27 | b | 44 | s | 61 | 9 |
| 11 | L | 28 | c | 45 | t | 62 | - |
| 12 | M | 29 | d | 46 | u | 63 | _ |
| 13 | N | 30 | e | 47 | v | | |
| 14 | O | 31 | f | 48 | w | (pad) | = |
| 15 | P | 32 | g | 49 | x | | |
| 16 | Q | 33 | h | 50 | y | | |
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
| 0| A| 17| R| 34| i| 51| z|
| 1| B| 18| S| 35| j| 52| 0|
| 2| C| 19| T| 36| k| 53| 1|
| 3| D| 20| U| 37| l| 54| 2|
| 4| E| 21| V| 38| m| 55| 3|
| 5| F| 22| W| 39| n| 56| 4|
| 6| G| 23| X| 40| o| 57| 5|
| 7| H| 24| Y| 41| p| 58| 6|
| 8| I| 25| Z| 42| q| 59| 7|
| 9| J| 26| a| 43| r| 60| 8|
| 10| K| 27| b| 44| s| 61| 9|
| 11| L| 28| c| 45| t| 62| -|
| 12| M| 29| d| 46| u| 63| _|
| 13| N| 30| e| 47| v| | |
| 14| O| 31| f| 48| w| (pad)| =|
| 15| P| 32| g| 49| x| | |
| 16| Q| 33| h| 50| y| | |
"""
+3 -3
View File
@@ -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
+29 -118
View File
@@ -1,11 +1,8 @@
defmodule Bitwise do
@moduledoc """
A set of functions that perform calculations on bits.
A set of macros that perform calculations on bits.
All bitwise functions work only on integers; otherwise an
`ArithmeticError` is raised.
The functions in this module come in two flavors: named or
The macros in this module come in two flavors: named or
operators. For example:
iex> use Bitwise
@@ -29,16 +26,16 @@ defmodule Bitwise do
When invoked with no options, `use Bitwise` is equivalent
to `import Bitwise`.
All bitwise functions can be used in guards:
All bitwise macros can be used in guards:
iex> use Bitwise
iex> odd? = fn
...> int when Bitwise.band(int, 1) == 1 -> true
...> int when band(int, 1) == 1 -> true
...> _ -> false
...> end
iex> odd?.(1)
true
All functions in this module are inlined by the compiler.
"""
@doc false
@@ -63,246 +60,160 @@ defmodule Bitwise do
@doc """
Calculates the bitwise NOT of its argument.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> bnot(2)
-3
iex> bnot(2) &&& 3
1
"""
@doc guard: true
@spec bnot(integer) :: integer
def bnot(expr) do
:erlang.bnot(expr)
defmacro bnot(expr) do
quote(do: :erlang.bnot(unquote(expr)))
end
@doc """
Prefix (unary) operator; calculates the bitwise NOT of its argument.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> ~~~2
-3
iex> ~~~2 &&& 3
1
"""
@doc guard: true
@spec ~~~integer :: integer
def ~~~expr do
:erlang.bnot(expr)
defmacro ~~~expr do
quote(do: :erlang.bnot(unquote(expr)))
end
@doc """
Calculates the bitwise AND of its arguments.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> band(9, 3)
1
"""
@doc guard: true
@spec band(integer, integer) :: integer
def band(left, right) do
:erlang.band(left, right)
defmacro band(left, right) do
quote(do: :erlang.band(unquote(left), unquote(right)))
end
@doc """
Infix operator; calculates the bitwise AND of its arguments.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> 9 &&& 3
1
"""
@doc guard: true
@spec integer &&& integer :: integer
def left &&& right do
:erlang.band(left, right)
defmacro left &&& right do
quote(do: :erlang.band(unquote(left), unquote(right)))
end
@doc """
Calculates the bitwise OR of its arguments.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> bor(9, 3)
11
"""
@doc guard: true
@spec bor(integer, integer) :: integer
def bor(left, right) do
:erlang.bor(left, right)
defmacro bor(left, right) do
quote(do: :erlang.bor(unquote(left), unquote(right)))
end
@doc """
Infix operator; calculates the bitwise OR of its arguments.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> 9 ||| 3
11
"""
@doc guard: true
@spec integer ||| integer :: integer
def left ||| right do
:erlang.bor(left, right)
defmacro left ||| right do
quote(do: :erlang.bor(unquote(left), unquote(right)))
end
@doc """
Calculates the bitwise XOR of its arguments.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> bxor(9, 3)
10
"""
@doc guard: true
@spec bxor(integer, integer) :: integer
def bxor(left, right) do
:erlang.bxor(left, right)
defmacro bxor(left, right) do
quote(do: :erlang.bxor(unquote(left), unquote(right)))
end
@doc """
Infix operator; calculates the bitwise XOR of its arguments.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> 9 ^^^ 3
10
"""
@doc guard: true
@spec integer ^^^ integer :: integer
def left ^^^ right do
:erlang.bxor(left, right)
defmacro left ^^^ right do
quote(do: :erlang.bxor(unquote(left), unquote(right)))
end
@doc """
Calculates the result of an arithmetic left bitshift.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> bsl(1, 2)
4
iex> bsl(1, -2)
0
iex> bsl(-1, 2)
-4
iex> bsl(-1, -2)
-1
"""
@doc guard: true
@spec bsl(integer, integer) :: integer
def bsl(left, right) do
:erlang.bsl(left, right)
defmacro bsl(left, right) do
quote(do: :erlang.bsl(unquote(left), unquote(right)))
end
@doc """
Infix operator; calculates the result of an arithmetic left bitshift.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> 1 <<< 2
4
iex> 1 <<< -2
0
iex> -1 <<< 2
-4
iex> -1 <<< -2
-1
"""
@doc guard: true
@spec integer <<< integer :: integer
def left <<< right do
:erlang.bsl(left, right)
defmacro left <<< right do
quote(do: :erlang.bsl(unquote(left), unquote(right)))
end
@doc """
Calculates the result of an arithmetic right bitshift.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> bsr(1, 2)
0
iex> bsr(1, -2)
4
iex> bsr(-1, 2)
-1
iex> bsr(-1, -2)
-4
"""
@doc guard: true
@spec bsr(integer, integer) :: integer
def bsr(left, right) do
:erlang.bsr(left, right)
defmacro bsr(left, right) do
quote(do: :erlang.bsr(unquote(left), unquote(right)))
end
@doc """
Infix operator; calculates the result of an arithmetic right bitshift.
Allowed in guard tests. Inlined by the compiler.
## Examples
iex> 1 >>> 2
0
iex> 1 >>> -2
4
iex> -1 >>> 2
-1
iex> -1 >>> -2
-4
"""
@doc guard: true
@spec integer >>> integer :: integer
def left >>> right do
:erlang.bsr(left, right)
defmacro left >>> right do
quote(do: :erlang.bsr(unquote(left), unquote(right)))
end
end
+17 -122
View File
@@ -11,26 +11,17 @@ defmodule Calendar do
For the actual date, time and datetime structures, see `Date`,
`Time`, `NaiveDateTime` and `DateTime`.
Note designations for year, month, day, and the like, are overspecified
Note the year, month, day, etc. designations are overspecified
(i.e. an integer instead of `1..12` for months) because different
calendars may have a different number of days per month, months per year and so on.
"""
@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
@typedoc """
A tuple representing the `day` and the `era`.
"""
@type day_of_era :: {day :: non_neg_integer(), era}
@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.
@@ -45,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}
@@ -57,15 +53,15 @@ defmodule Calendar do
representing the microseconds to external format. If the precision is 0,
it means microseconds must be skipped.
"""
@type microsecond :: {non_neg_integer, non_neg_integer}
@type microsecond :: {0..999_999, 0..6}
@typedoc "A calendar implementation"
@type calendar :: module
@typedoc "The time zone ID according to the IANA tz database (for example, Europe/Zurich)"
@typedoc "The time zone ID according to the IANA tz database (e.g. Europe/Zurich)"
@type time_zone :: String.t()
@typedoc "The time zone abbreviation (for example, CET or CEST or BST, and such)"
@typedoc "The time zone abbreviation (e.g. CET or CEST or BST etc.)"
@type zone_abbr :: String.t()
@typedoc "The time zone UTC offset in seconds"
@@ -116,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 configured 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.
"""
@@ -160,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) :: day_of_era()
@callback day_of_week(year, month, day) :: non_neg_integer()
@doc """
Converts the date into a string according to the calendar.
@@ -216,7 +170,7 @@ defmodule Calendar do
@callback time_to_string(hour, minute, second, microsecond) :: String.t()
@doc """
Converts the given datetime (without time zone) into the `t:iso_days/0` format.
Converts the given datetime (with time zone) into the `t:iso_days/0` format.
"""
@callback naive_datetime_to_iso_days(year, month, day, hour, minute, second, microsecond) ::
iso_days
@@ -270,47 +224,6 @@ defmodule Calendar do
"""
@callback valid_time?(hour, minute, second, microsecond) :: boolean
@doc """
Parses the string representation for a time returned by `c:time_to_string/4`
into a time-tuple.
"""
@doc since: "1.10.0"
@callback parse_time(String.t()) ::
{:ok, {hour, minute, second, microsecond}}
| {:error, atom}
@doc """
Parses the string representation for a date returned by `c:date_to_string/3`
into a date-tuple.
"""
@doc since: "1.10.0"
@callback parse_date(String.t()) ::
{:ok, {year, month, day}}
| {:error, atom}
@doc """
Parses the string representation for a naive datetime returned by
`c:naive_datetime_to_string/7` into a naive-datetime-tuple.
The given string may contain a timezone offset but it is ignored.
"""
@doc since: "1.10.0"
@callback parse_naive_datetime(String.t()) ::
{:ok, {year, month, day, hour, minute, second, microsecond}}
| {:error, atom}
@doc """
Parses the string representation for a datetime returned by
`c:datetime_to_string/11` into a datetime-tuple.
The returned datetime must be in UTC. The original `utc_offset`
it was written in must be returned in the result.
"""
@doc since: "1.10.0"
@callback parse_utc_datetime(String.t()) ::
{:ok, {year, month, day, hour, minute, second, microsecond}, utc_offset}
| {:error, atom}
# General Helpers
@doc """
@@ -345,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
+37 -129
View File
@@ -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")
@@ -263,9 +261,30 @@ defmodule Date do
"""
@spec from_iso8601(String.t(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
def from_iso8601(string, calendar \\ Calendar.ISO) do
with {:ok, {year, month, day}} <- Calendar.ISO.parse_date(string) do
convert(%Date{year: year, month: month, day: day}, calendar)
def from_iso8601(string, calendar \\ Calendar.ISO)
def from_iso8601(<<?-, rest::binary>>, calendar) do
with {:ok, %{year: year} = date} <- raw_from_iso8601(rest, calendar) do
{:ok, %{date | year: -year}}
end
end
def from_iso8601(<<rest::binary>>, calendar) do
raw_from_iso8601(rest, calendar)
end
[match_date, guard_date, read_date] = Calendar.ISO.__match_date__()
defp raw_from_iso8601(string, calendar) do
with unquote(match_date) <- string,
true <- unquote(guard_date) do
{year, month, day} = unquote(read_date)
with {:ok, date} <- new(year, month, day, Calendar.ISO) do
convert(date, calendar)
end
else
_ -> {:error, :invalid_format}
end
end
@@ -322,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
@@ -446,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
@@ -537,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 """
@@ -631,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
@@ -748,11 +655,12 @@ defmodule Date do
end
defimpl Inspect do
def inspect(%{calendar: calendar, year: year, month: month, day: day}, _) do
"~D[" <> calendar.date_to_string(year, month, day) <> suffix(calendar) <> "]"
def inspect(%{calendar: Calendar.ISO, year: year, month: month, day: day}, _) do
"~D[" <> Calendar.ISO.date_to_string(year, month, day) <> "]"
end
defp suffix(Calendar.ISO), do: ""
defp suffix(calendar), do: " " <> inspect(calendar)
def inspect(date, opts) do
Inspect.Any.inspect(date, opts)
end
end
end
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+93 -158
View File
@@ -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(),
@@ -117,53 +122,6 @@ defmodule NaiveDateTime do
|> DateTime.to_naive()
end
@doc """
Returns the "local time" for the machine the Elixir program is running on.
WARNING: This function can cause insidious bugs. It depends on the time zone
configuration at run time. This can changed and be set to a time zone that has
daylight saving jumps (spring forward or fall back).
This function can be used to display what the time is right now for the time
zone configuration that the machine happens to have. An example would be a
desktop program displaying a clock to the user. For any other uses it is
probably a bad idea to use this function.
For most cases, use `DateTime.now/2` or `DateTime.utc_now/1` instead.
Does not include fractional seconds.
## Examples
iex> naive_datetime = NaiveDateTime.local_now()
iex> naive_datetime.year >= 2019
true
"""
@doc since: "1.10.0"
@spec local_now(Calendar.calendar()) :: t
def local_now(calendar \\ Calendar.ISO)
def local_now(Calendar.ISO) do
{{year, month, day}, {hour, minute, second}} = :erlang.localtime()
{:ok, ndt} = NaiveDateTime.new(year, month, day, hour, minute, second)
ndt
end
def local_now(calendar) do
naive_datetime = local_now()
case convert(naive_datetime, calendar) do
{:ok, value} ->
value
{:error, :incompatible_calendars} ->
raise ArgumentError,
~s(cannot get "local now" in target calendar #{inspect(calendar)}, ) <>
"reason: cannot convert from Calendar.ISO to #{inspect(calendar)}."
end
end
@doc """
Builds a new ISO naive datetime.
@@ -188,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}
@@ -208,7 +168,7 @@ defmodule NaiveDateTime do
Calendar.hour(),
Calendar.minute(),
Calendar.second(),
Calendar.microsecond() | non_neg_integer,
Calendar.microsecond(),
Calendar.calendar()
) :: {:ok, t} | {:error, atom}
def new(year, month, day, hour, minute, second, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
@@ -275,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
@@ -305,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
@@ -358,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 " <>
@@ -380,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)
@@ -401,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`.
@@ -438,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
@@ -464,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,
@@ -532,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
@@ -581,21 +493,35 @@ defmodule NaiveDateTime do
"""
@spec from_iso8601(String.t(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
def from_iso8601(string, calendar \\ Calendar.ISO) do
with {:ok, {year, month, day, hour, minute, second, microsecond}} <-
Calendar.ISO.parse_naive_datetime(string) do
convert(
%NaiveDateTime{
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
microsecond: microsecond
},
calendar
)
def from_iso8601(string, calendar \\ Calendar.ISO)
def from_iso8601(<<?-, rest::binary>>, calendar) do
with {:ok, %{year: year} = naive_datetime} <- raw_from_iso8601(rest, calendar) do
{:ok, %{naive_datetime | year: -year}}
end
end
def from_iso8601(<<rest::binary>>, calendar) do
raw_from_iso8601(rest, calendar)
end
@sep [?\s, ?T]
[match_date, guard_date, read_date] = Calendar.ISO.__match_date__()
[match_time, guard_time, read_time] = Calendar.ISO.__match_time__()
defp raw_from_iso8601(string, calendar) do
with <<unquote(match_date), sep, unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_date) and sep in @sep and unquote(guard_time),
{microsec, rest} <- Calendar.ISO.parse_microsecond(rest),
{_offset, ""} <- Calendar.ISO.parse_offset(rest) do
{year, month, day} = unquote(read_date)
{hour, min, sec} = unquote(read_time)
with {:ok, utc_date} <- new(year, month, day, hour, min, sec, microsec, Calendar.ISO) do
convert(utc_date, calendar)
end
else
_ -> {:error, :invalid_format}
end
end
@@ -615,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} ->
@@ -673,8 +599,16 @@ defmodule NaiveDateTime do
microsecond: microsecond
} = naive_datetime
Calendar.ISO.date_to_string(year, month, day, format) <>
"T" <> Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
Calendar.ISO.naive_datetime_to_iso8601(
year,
month,
day,
hour,
minute,
second,
microsecond,
format
)
end
def to_iso8601(%{calendar: _} = naive_datetime, format) when format in [:basic, :extended] do
@@ -697,8 +631,8 @@ defmodule NaiveDateTime do
iex> NaiveDateTime.to_erl(~N[2000-01-01 13:30:15])
{{2000, 1, 1}, {13, 30, 15}}
This function can also be used to convert a DateTime to an Erlang
datetime tuple without the time zone information:
This function can also be used to convert a DateTime to a erl format
without 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},
@@ -737,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 """
@@ -757,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} ->
@@ -961,7 +896,7 @@ defmodule NaiveDateTime do
end
defimpl Inspect do
def inspect(naive_datetime, _) do
def inspect(%{calendar: Calendar.ISO} = naive_datetime, _) do
%{
year: year,
month: month,
@@ -969,17 +904,17 @@ defmodule NaiveDateTime do
hour: hour,
minute: minute,
second: second,
microsecond: microsecond,
calendar: calendar
microsecond: microsecond
} = naive_datetime
formatted =
calendar.naive_datetime_to_string(year, month, day, hour, minute, second, microsecond)
Calendar.ISO.naive_datetime_to_string(year, month, day, hour, minute, second, microsecond)
"~N[" <> formatted <> suffix(calendar) <> "]"
"~N[" <> formatted <> "]"
end
defp suffix(Calendar.ISO), do: ""
defp suffix(calendar), do: " " <> inspect(calendar)
def inspect(naive, opts) do
Inspect.Any.inspect(naive, opts)
end
end
end
+52 -73
View File
@@ -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}
@@ -110,7 +113,7 @@ defmodule Time do
Calendar.hour(),
Calendar.minute(),
Calendar.second(),
Calendar.microsecond() | non_neg_integer,
Calendar.microsecond() | integer,
Calendar.calendar()
) :: {:ok, t} | {:error, atom}
def new(hour, minute, second, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)
@@ -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
@@ -213,12 +215,30 @@ defmodule Time do
"""
@spec from_iso8601(String.t(), Calendar.calendar()) :: {:ok, t} | {:error, atom}
def from_iso8601(string, calendar \\ Calendar.ISO) do
with {:ok, {hour, minute, second, microsecond}} <- Calendar.ISO.parse_time(string) do
convert(
%Time{hour: hour, minute: minute, second: second, microsecond: microsecond},
calendar
)
def from_iso8601(string, calendar \\ Calendar.ISO)
def from_iso8601(<<?T, rest::binary>>, calendar) do
raw_from_iso8601(rest, calendar)
end
def from_iso8601(<<rest::binary>>, calendar) do
raw_from_iso8601(rest, calendar)
end
[match_time, guard_time, read_time] = Calendar.ISO.__match_time__()
defp raw_from_iso8601(string, calendar) do
with <<unquote(match_time), rest::binary>> <- string,
true <- unquote(guard_time),
{microsec, rest} <- Calendar.ISO.parse_microsecond(rest),
{_offset, ""} <- Calendar.ISO.parse_offset(rest) do
{hour, min, sec} = unquote(read_time)
with {:ok, utc_time} <- new(hour, min, sec, microsec, Calendar.ISO) do
convert(utc_time, calendar)
end
else
_ -> {:error, :invalid_format}
end
end
@@ -283,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
@@ -385,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,
@@ -398,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.
@@ -589,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)
@@ -627,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)
@@ -675,20 +654,20 @@ defmodule Time do
end
defimpl Inspect do
def inspect(time, _) do
def inspect(%{calendar: Calendar.ISO} = time, _) do
%{
hour: hour,
minute: minute,
second: second,
microsecond: microsecond,
calendar: calendar
calendar: Calendar.ISO
} = time
"~T[" <>
calendar.time_to_string(hour, minute, second, microsecond) <> suffix(calendar) <> "]"
"~T[" <> Calendar.ISO.time_to_string(hour, minute, second, microsecond) <> "]"
end
defp suffix(Calendar.ISO), do: ""
defp suffix(calendar), do: " " <> inspect(calendar)
def inspect(time, opts) do
Inspect.Any.inspect(time, opts)
end
end
end
@@ -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
+123 -379
View File
@@ -1,5 +1,5 @@
defmodule Code do
@moduledoc ~S"""
@moduledoc """
Utilities for managing code compilation, code evaluation, and code loading.
This module complements Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html)
@@ -19,121 +19,17 @@ defmodule Code do
* `eval_file/2` - evaluates the file contents without tracking its name. It
returns the result of the last expression in the file, instead of the modules
defined in it. Evaluated files do not trigger the compilation tracers described
in the next section.
defined in it.
In a nutshell, the first must be used when you want to keep track of the files
handled by the system, to avoid the same file from being compiled multiple
times. This is common in scripts.
`compile_file/2` must be used when you are interested in the modules defined in a
file, without tracking. `eval_file/2` should be used when you are interested in
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.
## Compilation tracers
Elixir supports compilation tracers, which allows modules to observe constructs
handled by the Elixir compiler when compiling files. A tracer is a module
that implements the `trace/2` function. The function receives the event name
as first argument and `Macro.Env` as second and it must return `:ok`. It is
very important for a tracer to do as little work as possible synchronously
and dispatch the bulk of the work to a separate process. **Slow tracers will
slow down compilation**.
You can configure your list of tracers via `put_compiler_option/2`. The
following events are available to tracers:
* `:start` - (since v1.11.0) invoked whenever the compiler starts to trace
a new lexical context, such as a new file. Keep in mind the compiler runs
in parallel, so multiple files may invoke `:start` and run at the same
time. The value of the `lexical_tracker` of the macro environment, albeit
opaque, can be used to uniquely identify the environment.
* `:stop` - (since v1.11.0) invoked whenever the compiler stops tracing a
new lexical context, such as a new file.
* `{:import, meta, module, opts}` - traced whenever `module` is imported.
`meta` is the import AST metadata and `opts` are the import options.
* `{:imported_function, meta, module, name, arity}` and
`{:imported_macro, meta, module, name, arity}` - traced whenever an
imported function or macro is invoked. `meta` is the call AST metadata,
`module` is the module the import is from, followed by the `name` and `arity`
of the imported function/macro.
* `{:alias, meta, alias, as, opts}` - traced whenever `alias` is aliased
to `as`. `meta` is the alias AST metadata and `opts` are the alias options.
* `{:alias_expansion, meta, as, alias}` traced whenever there is an alias
expansion for a previously defined `alias`, i.e. when the user writes `as`
which is expanded to `alias`. `meta` is the alias expansion AST metadata.
* `{:alias_reference, meta, module}` - traced whenever there is an alias
in the code, i.e. whenever the user writes `MyModule.Foo.Bar` in the code,
regardless if it was expanded or not.
* `{:require, meta, module, opts}` - traced whenever `module` is required.
`meta` is the require AST metadata and `opts` are the require options.
* `{:struct_expansion, meta, module, keys}` - traced whenever `module`'s struct
is expanded. `meta` is the struct AST metadata and `keys` are the keys being
used by expansion
* `{:remote_function, meta, module, name, arity}` and
`{:remote_macro, meta, module, name, arity}` - traced whenever a remote
function or macro is referenced. `meta` is the call AST metadata, `module`
is the invoked module, followed by the `name` and `arity`.
* `{:local_function, meta, name, arity}` and
`{:local_macro, meta, name, arity}` - traced whenever a local
function or macro is referenced. `meta` is the call AST metadata, `module`
is the invoked module, followed by the `name` and `arity`.
* `{:compile_env, app, path, return}` - traced whenever `Application.compile_env/3`
or `Application.compile_env!/2` are called. `app` is an atom, `path` is a list
of keys to traverse in the application environemnt and `return` is either
`{:ok, value}` or `:error`.
The `:tracers` compiler option can be combined with the `:parser_options`
compiler option to enrich the metadata of the traced events above.
New events may be added at any time in the future, therefore it is advised
for the `trace/2` function to have a "catch-all" clause.
Below is an example tracer that prints all remote function invocations:
defmodule MyTracer do
def trace({:remote_function, _meta, module, name, arity}, env) do
IO.puts "#{env.file}:#{env.line} #{inspect(module)}.#{name}/#{arity}"
:ok
end
def trace(_event, _env) do
:ok
end
end
"""
@typedoc """
A list with all variable bindings.
The binding keys are usually atoms, but they may be a tuple for variables
defined in a different context.
"""
@type binding :: [{atom() | tuple(), any}]
@boolean_compiler_options [
:docs,
:debug_info,
:ignore_module_conflict,
:relative_paths,
:warnings_as_errors
]
@list_compiler_options [:no_warn_undefined, :tracers, :parser_options]
@available_compiler_options @boolean_compiler_options ++ @list_compiler_options
@doc """
Lists all required files.
@@ -150,7 +46,7 @@ defmodule Code do
:elixir_code_server.call(:required)
end
@deprecated "Use Code.required_files/0 instead"
# TODO: Deprecate me on 1.9
@doc false
def loaded_files do
required_files()
@@ -182,7 +78,7 @@ defmodule Code do
:elixir_code_server.cast({:unrequire_files, files})
end
@deprecated "Use Code.unrequire_files/1 instead"
# TODO: Deprecate me on 1.9
@doc false
def unload_files(files) do
unrequire_files(files)
@@ -259,7 +155,7 @@ defmodule Code do
@doc """
Evaluates the contents given by `string`.
The `binding` argument is a list of variable bindings.
The `binding` argument is a keyword list of variable bindings.
The `opts` argument is a keyword list of environment options.
**Warning**: `string` can be any Elixir code and will be executed with
@@ -300,8 +196,8 @@ defmodule Code do
where `value` is the value returned from evaluating `string`.
If an error occurs while evaluating `string` an exception will be raised.
`binding` is a list with all variable bindings
after evaluating `string`. The binding keys are usually atoms, but they
`binding` is a keyword list with the value of all variable bindings
after evaluating `string`. The binding key is usually an atom, but it
may be a tuple for variables defined in a different context.
## Examples
@@ -323,22 +219,17 @@ defmodule Code do
{3, [a: 1, b: 2]}
"""
@spec eval_string(List.Chars.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
@spec eval_string(List.Chars.t(), list, Macro.Env.t() | keyword) :: {term, binding :: list}
def eval_string(string, binding \\ [], opts \\ [])
def eval_string(string, binding, %Macro.Env{} = env) do
eval_string_with_error_handling(string, binding, Map.to_list(env))
{value, binding, _env, _scope} = :elixir.eval(to_charlist(string), binding, Map.to_list(env))
{value, binding}
end
def eval_string(string, binding, opts) when is_list(opts) do
validate_eval_opts(opts)
eval_string_with_error_handling(string, binding, opts)
end
defp eval_string_with_error_handling(string, binding, opts) do
%{line: line, file: file} = env = :elixir.env_for_eval(opts)
forms = :elixir.string_to_quoted!(to_charlist(string), line, file, [])
{value, binding, _env} = :elixir.eval_forms(forms, binding, env)
{value, binding, _env, _scope} = :elixir.eval(to_charlist(string), binding, opts)
{value, binding}
end
@@ -373,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.
@@ -395,7 +279,7 @@ defmodule Code do
a whole.
The formatter does not hard code names. The formatter will not behave
specially because a function is named `defmodule`, `def`, or the like. This
specially because a function is named `defmodule`, `def`, etc. This
principle mirrors Elixir's goal of being an extensible language where
developers can extend the language with new constructs as if they were
part of the language. When it is absolutely necessary to change behaviour
@@ -525,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, and the like
### 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
@@ -677,7 +561,7 @@ defmodule Code do
Macro arguments are typically transformed by unquoting them into the
returned quoted expressions (instead of evaluated).
See `eval_string/3` for a description of `binding` and options.
See `eval_string/3` for a description of bindings and options.
## Examples
@@ -693,17 +577,17 @@ defmodule Code do
{3, [a: 1, b: 2]}
"""
@spec eval_quoted(Macro.t(), binding, Macro.Env.t() | keyword) :: {term, binding}
@spec eval_quoted(Macro.t(), list, Macro.Env.t() | keyword) :: {term, binding :: list}
def eval_quoted(quoted, binding \\ [], opts \\ [])
def eval_quoted(quoted, binding, %Macro.Env{} = env) do
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, Map.to_list(env))
{value, binding, _env, _scope} = :elixir.eval_quoted(quoted, binding, Map.to_list(env))
{value, binding}
end
def eval_quoted(quoted, binding, opts) when is_list(opts) do
validate_eval_opts(opts)
{value, binding, _env} = :elixir.eval_quoted(quoted, binding, opts)
{value, binding, _env, _scope} = :elixir.eval_quoted(quoted, binding, opts)
{value, binding}
end
@@ -745,7 +629,7 @@ defmodule Code do
end
end
@doc ~S"""
@doc """
Converts the given string to its quoted form.
Returns `{:ok, quoted_form}` if it succeeds,
@@ -766,24 +650,6 @@ defmodule Code do
when non-existing atoms are found by the tokenizer.
Defaults to `false`.
* `:token_metadata` (since v1.10.0) - when `true`, includes token-related
metadata in the expression AST, such as metadata for `do` and `end`
tokens, for closing tokens, end of expressions, as well as delimiters
for sigils. See `t:Macro.metadata/0`. Defaults to `false`.
* `:literal_encoder` (since v1.10.0) - how to encode literals in the AST.
It must be a function that receives two arguments, the literal and its
metadata, and it must return `{:ok, ast :: Macro.t}` or
`{:error, reason :: binary}`. If you return anything than the literal
itself as the `term`, then the AST is no longer valid. This option
may still useful for textual analysis of the source code.
* `:static_atoms_encoder` - the static atom encoder function, see
"The `:static_atoms_encoder` function" section below. Note 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`.
@@ -793,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_atoms_encoder` function
When `static_atoms_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. To produce a valid AST, it is required to return `{:ok, term}`,
where `term` is an atom. It is possible to return something other 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}}
@@ -862,24 +697,24 @@ 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 binding (exactly the same return value as `eval_string/3`).
evaluation result and its bindings (exactly the same return value as `eval_string/3`).
"""
@spec eval_file(binary, nil | binary) :: {term, binding}
@spec eval_file(binary, nil | binary) :: {term, binding :: list}
def eval_file(file, relative_to \\ nil) when is_binary(file) do
file = find_file(file, relative_to)
eval_string(File.read!(file), [], file: file, line: 1)
end
@deprecated "Use Code.require_file/2 or Code.compile_file/2 instead"
# 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})
verify_loaded(loaded)
loaded
end
@doc """
@@ -897,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
@@ -908,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
@@ -918,93 +753,81 @@ 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})
verify_loaded(loaded)
loaded
end
end
@doc """
Gets all compilation options from the code server.
Gets the compilation options from the code server.
To get invidual options, see `get_compiler_option/1`.
For a description of all options, see `put_compiler_option/2`.
Check `compiler_options/1` for more information.
## 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 :: map
@spec compiler_options() :: %{optional(atom) => boolean}
def compiler_options do
for key <- @available_compiler_options, into: %{} do
{key, :elixir_config.get(key)}
end
:elixir_config.get(:compiler_options)
end
@doc """
Stores all given compilation options.
Returns a list with the available compiler options.
To store invidual options, see `put_compiler_option/2`.
For a description of all options, see `put_compiler_option/2`.
See `compiler_options/1` for more info.
## Examples
Code.compiler_options()
#=> %{debug_info: true, docs: true, ...}
"""
@spec compiler_options(Enumerable.t()) :: %{optional(atom) => boolean}
def compiler_options(opts) do
for {key, value} <- opts, into: %{} do
put_compiler_option(key, value)
{key, value}
end
end
@doc """
Returns the value of a given compiler option.
For a description of all options, see `put_compiler_option/2`.
## Examples
Code.get_compiler_option(:debug_info)
#=> true
"""
@doc since: "1.10.0"
@spec get_compiler_option(atom) :: term
def get_compiler_option(key) when key in @available_compiler_options do
:elixir_config.get(key)
end
@doc """
Returns a list with all available compiler options.
For a description of all options, see `put_compiler_option/2`.
## 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 """
Stores a compilation option.
Purge compiler modules.
These options are global since they are stored by Elixir's code server.
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 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 resued. If there are any processes running
any code from such modules, they will be terminated too.
It returns `{:ok, number_of_modules_purged}`.
"""
@doc since: "1.7.0"
@spec purge_compiler_modules() :: {:ok, non_neg_integer()}
def purge_compiler_modules() do
:elixir_code_server.call(:purge_compiler_modules)
end
@doc """
Sets compilation options.
These options are global since they are stored by Elixir's Code Server.
Available options are:
@@ -1013,7 +836,7 @@ defmodule Code do
* `:debug_info` - when `true`, retain debug information in the compiled
module. This allows a developer to reconstruct the original source
code. Defaults to `true`.
code. Defaults to `false`.
* `:ignore_module_conflict` - when `true`, override modules that were
already defined without raising errors. Defaults to `false`.
@@ -1025,91 +848,33 @@ defmodule Code do
* `:warnings_as_errors` - causes compilation to fail when warnings are
generated. Defaults to `false`.
* `:no_warn_undefined` (since v1.10.0) - list of modules and `{Mod, fun, arity}`
tuples that will not emit warnings that the module or function does not exist
at compilation time. Pass atom `:all` to skip warning for all undefined
functions. This can be useful when doing dynamic compilation. Defaults to `[]`.
* `:tracers` (since v1.10.0) - a list of tracers (modules) to be used during
compilation. See the module docs for more information. Defaults to `[]`.
* `:parser_options` (since v1.10.0) - a keyword list of options to be given
to the parser when compiling files. It accepts the same options as
`string_to_quoted/2` (except by the options that change the AST itself).
This can be used in combination with the tracer to retrieve localized
information about events happening during compilation. Defaults to `[]`.
It always returns `:ok`. Raises an error for invalid options.
It returns the new map of compiler options.
## Examples
Code.put_compiler_option(:debug_info, true)
#=> :ok
Code.compiler_options(debug_info: true)
#=> %{debug_info: true, docs: true,
#=> warnings_as_errors: false, ignore_module_conflict: false}
"""
@doc since: "1.10.0"
@spec put_compiler_option(atom, term) :: :ok
def put_compiler_option(key, value) when key in @boolean_compiler_options do
if not is_boolean(value) do
raise "compiler option #{inspect(key)} should be a boolean, got: #{inspect(value)}"
end
@spec compiler_options(Enumerable.t()) :: %{optional(atom) => boolean}
def compiler_options(opts) do
available = available_compiler_options()
:elixir_config.put(key, value)
:ok
end
Enum.each(opts, fn {key, value} ->
cond do
key not in available ->
raise "unknown compiler option: #{inspect(key)}"
def put_compiler_option(:no_warn_undefined, value) do
if value != :all and not is_list(value) do
raise "compiler option :no_warn_undefined should be a list or the atom :all, " <>
"got: #{inspect(value)}"
end
not is_boolean(value) ->
raise "compiler option #{inspect(key)} should be a boolean, got: #{inspect(value)}"
:elixir_config.put(:no_warn_undefined, value)
:ok
end
true ->
:ok
end
end)
def put_compiler_option(key, value) when key in @list_compiler_options do
if not is_list(value) do
raise "compiler option #{inspect(key)} should be a list, got: #{inspect(value)}"
end
if key == :parser_options and not Keyword.keyword?(value) do
raise "compiler option #{inspect(key)} should be a keyword list, " <>
"got: #{inspect(value)}"
end
if key == :tracers and not Enum.all?(value, &is_atom/1) do
raise "compiler option #{inspect(key)} should be a list of modules, " <>
"got: #{inspect(value)}"
end
:elixir_config.put(key, value)
:ok
end
def put_compiler_option(key, _value) do
raise "unknown compiler option: #{inspect(key)}"
end
@doc """
Purge compiler modules.
The compiler utilizes temporary modules to compile code. For example,
`elixir_compiler_1`, `elixir_compiler_2`, and so on. In case the compiled code
stores references to anonymous functions or similar, the Elixir compiler
may be unable to reclaim those modules, keeping an unnecessary amount of
code in memory and eventually leading to modules such as `elixir_compiler_12345`.
This function purges all modules currently kept by the compiler, allowing
old compiler module names to be reused. If there are any processes running
any code from such modules, they will be terminated too.
It returns `{:ok, number_of_modules_purged}`.
"""
@doc since: "1.7.0"
@spec purge_compiler_modules() :: {:ok, non_neg_integer()}
def purge_compiler_modules() do
:elixir_code_server.call(:purge_compiler_modules)
:elixir_config.update(:compiler_options, &Enum.into(opts, &1))
end
@doc """
@@ -1128,8 +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
loaded = :elixir_compiler.string(to_charlist(string), file, fn _, _ -> :ok end)
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
:elixir_compiler.string(to_charlist(string), file)
end
@doc """
@@ -1142,8 +906,7 @@ defmodule Code do
"""
@spec compile_quoted(Macro.t(), binary) :: [{module, binary}]
def compile_quoted(quoted, file \\ "nofile") when is_binary(file) do
loaded = :elixir_compiler.quoted(quoted, file, fn _, _ -> :ok end)
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
:elixir_compiler.quoted(quoted, file)
end
@doc """
@@ -1160,11 +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
loaded = :elixir_compiler.file(find_file(file, relative_to), fn _, _ -> :ok end)
verify_loaded(loaded)
:elixir_compiler.file(find_file(file, relative_to))
end
@doc """
@@ -1247,40 +1008,23 @@ defmodule Code do
Ensures the given module is compiled and loaded.
If the module is already loaded, it works as no-op. If the module was
not compiled yet, `ensure_compiled/1` halts the compilation of the caller
until the module given to `ensure_compiled/1` becomes available or
all files for the current project have been compiled. If compilation
finishes and the module is not available, an error tuple is returned.
Given this function halts compilation, use it carefully. In particular,
avoid using it to guess which modules are in the system. Overuse of this
function can also lead to deadlocks, where two modules check at the same time
if the other is compiled. This returns a specific unavailable error code,
where we cannot successfully verify a module is available or not.
not loaded yet, it checks if it needs to be compiled first and then
tries to load it.
If it succeeds in loading the module, it returns `{:module, module}`.
If not, returns `{:error, reason}` with the error reason.
If the module being checked is currently in a compiler deadlock,
this function returns `{:error, :unavailable}`. Unavailable doesn't
necessarily mean the module doesn't exist, just that it is not currently
available, but it (or may not) become available in the future.
Check `ensure_loaded/1` for more information on module loading
and when to use `ensure_loaded/1` or `ensure_compiled/1`.
"""
@spec ensure_compiled(module) ::
{:module, module}
| {:error, :embedded | :badfile | :nofile | :on_load_failure | :unavailable}
{:module, module} | {:error, :embedded | :badfile | :nofile | :on_load_failure}
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}
:deadlock -> {:error, :unavailable}
:not_found -> {:error, :nofile}
end
if is_pid(:erlang.get(:elixir_compiler_pid)) and
Kernel.ErrorHandler.ensure_compiled(module, :module) do
{:module, module}
else
error
end
@@ -1290,14 +1034,20 @@ defmodule Code do
end
end
@doc false
@deprecated "Use Code.ensure_compiled/1 instead (see the proper disclaimers in its docs)"
@doc """
Ensures the given module is compiled and loaded.
Similar to `ensure_compiled/1`, but returns `true` if the module
is already loaded or was successfully loaded and compiled.
Returns `false` otherwise.
"""
@spec ensure_compiled?(module) :: boolean
def ensure_compiled?(module) when is_atom(module) do
match?({:module, ^module}, ensure_compiled(module))
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.
@@ -1313,7 +1063,7 @@ defmodule Code do
# Module documentation of an existing module
iex> {:docs_v1, _, :elixir, _, %{"en" => module_doc}, _, _} = Code.fetch_docs(Atom)
iex> module_doc |> String.split("\n") |> Enum.at(0)
"Atoms are constants whose values are their own name."
"Convenience functions for working with atoms."
# A module that doesn't exist
iex> Code.fetch_docs(ModuleNotGood)
@@ -1322,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)
@@ -1343,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'
@@ -1372,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
@@ -1397,10 +1147,4 @@ defmodule Code do
raise Code.LoadError, file: file
end
end
defp verify_loaded(loaded) do
maps_binaries = Enum.map(loaded, fn {_module, map, binary} -> {map, binary} end)
Module.ParallelChecker.verify(maps_binaries, [])
Enum.map(loaded, fn {module, _map, binary} -> {module, binary} end)
end
end
File diff suppressed because it is too large Load Diff
+5 -12
View File
@@ -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 ->
+35 -22
View File
@@ -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
@@ -217,8 +231,7 @@ defmodule Code.Typespec do
end
defp typespec_to_quoted({:user_type, line, name, args}) do
args = for arg <- args, do: typespec_to_quoted(arg)
{name, [line: line], args}
typespec_to_quoted({:type, line, name, args})
end
defp typespec_to_quoted({:type, line, :tuple, :any}) do
@@ -330,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
@@ -342,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(
+6 -16
View File
@@ -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
@@ -37,11 +37,11 @@ defprotocol Collectable do
iex> collector_fun.(updated_acc, :done)
#MapSet<[1, 2, 3]>
To show how the protocol can be implemented, we can again look at the
To show how the protocol can be implemented, we can take again a look at the
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)
@@ -143,7 +133,7 @@ end
defimpl Collectable, for: Map do
def into(original) do
fun = fn
map, {:cont, {k, v}} -> Map.put(map, k, v)
map, {:cont, {k, v}} -> :maps.put(k, v, map)
map, :done -> map
_, :halt -> :ok
end
-266
View File
@@ -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 is 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
executed 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()) :: {term, Code.binding()}
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
-377
View File
@@ -1,377 +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 in
the release configuration:
releases: [
demo: [
# ...,
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 `{:system, system_var, path}` tuple where the config is the
concatenation of the environment variable `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,
reboot_after_config: true,
validate_compile_env: 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 \\ &restart_and_sleep/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)
# The key we store if the system already booted
booted_key = :"#{key}_booted"
case :application.get_env(app, booted_key) do
{:ok, {:booted, path}} ->
path && File.rm(path)
with {:ok, %Config.Provider{} = provider} <- :application.get_env(app, key) do
maybe_validate_compile_env(provider)
end
:booted
_ ->
case :application.get_env(app, key) do
{:ok, %Config.Provider{} = provider} ->
path = resolve_config_path!(provider.config_path)
reboot_config = [{app, [{booted_key, booted_value(provider, path)}]}]
boot_providers(path, provider, reboot_config, restart_fun)
_ ->
:skip
end
end
end
defp boot_providers(path, provider, reboot_config, restart_fun) do
validate_no_cyclic_boot!(path)
loaded_applications = :application.loaded_applications()
original_config = read_config!(path)
config =
original_config
|> Config.__merge__(provider.extra_config)
|> run_providers(provider)
if provider.reboot_after_config do
config
|> Config.__merge__(reboot_config)
|> write_config!(path)
restart_fun.()
else
for {app, _, _} <- loaded_applications, config[app] != original_config[app] do
abort("""
Cannot configure #{inspect(app)} because :reboot_after_config has been set \
to false and #{inspect(app)} has already been loaded, meaning any further \
configuration won't have an effect.
The configuration for #{inspect(app)} before config providers was:
#{inspect(original_config[app])}
The configuration for #{inspect(app)} after config providers was:
#{inspect(config[app])}
""")
end
_ = Application.put_all_env(config, persistent: true)
maybe_validate_compile_env(provider)
:ok
end
end
defp maybe_validate_compile_env(provider) do
with [_ | _] = compile_env <- provider.validate_compile_env do
validate_compile_env(compile_env)
end
end
@doc false
def validate_compile_env(compile_env) do
for {app, [key | path], compile_return} <- compile_env,
Application.ensure_loaded(app) == :ok do
try do
traverse_env(Application.fetch_env(app, key), path)
rescue
e ->
abort("""
application #{inspect(app)} failed reading its compile environment #{path(key, path)}:
#{Exception.format(:error, e, __STACKTRACE__)}
Expected it to match the compile time value of #{return_to_text(compile_return)}.
#{compile_env_tips(app)}
""")
else
^compile_return ->
:ok
runtime_return ->
abort("""
the application #{inspect(app)} has a different value set #{path(key, path)} \
during runtime compared to compile time. Since this application environment entry was \
marked as compile time, this difference can lead to different behaviour than expected:
* Compile time value #{return_to_text(compile_return)}
* Runtime value #{return_to_text(runtime_return)}
#{compile_env_tips(app)}
""")
end
end
:ok
end
defp path(key, []), do: "for key #{inspect(key)}"
defp path(key, path), do: "for path #{inspect(path)} inside key #{inspect(key)}"
defp compile_env_tips(app),
do: """
To fix this error, you might:
* Make the runtime value match the compile time one
* Recompile your project. If the misconfigured application is a dependency, \
you may need to run "mix deps.compile #{app} --force"
* Alternatively, you can disable this check. If you are using releases, you can \
set :validate_compile_env to false in your release configuration. If you are \
using Mix to start your system, you can pass the --no-validate-compile-env flag
"""
defp return_to_text({:ok, value}), do: "was set to: #{inspect(value)}"
defp return_to_text(:error), do: "was not set"
defp traverse_env(return, []), do: return
defp traverse_env(:error, _paths), do: :error
defp traverse_env({:ok, value}, [key | keys]), do: traverse_env(Access.fetch(value, key), keys)
defp restart_and_sleep do
:init.restart()
Process.sleep(:infinity)
end
defp booted_value(%{prune_after_boot: true}, path), do: {:booted, path}
defp booted_value(%{prune_after_boot: false}, _path), do: {:booted, nil}
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, IO.chardata_to_string(contents)) 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)
:erlang.raise(:error, "aborting boot", [{Config.Provider, :boot, 2, []}])
end
end
-100
View File
@@ -1,100 +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: the configuration path
(as outlined in `t:Config.Provider.config_path/0`) for the file to
be read and loaded during the system boot.
For example, if you expect the target system to have a config file
in an absolute path, you can configure your `mix release` as:
config_providers: [{Config.Reader, "/etc/config.exs"}]
Or if you want to read a custom path inside the release:
config_provider: [{Config.Reader, {:system, "RELEASE_ROOT", "/config.exs"}}]
Note by default Mix releases supports runtime configuration via
a `config/releases.exs`. If a `config/releases.exs` exists in your
application, it is automatically copied inside the release and
automatically set as a config provider.
"""
@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
+3 -1
View File
@@ -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
+53 -91
View File
@@ -6,13 +6,14 @@ 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
A dynamic supervisor is started with no children, a supervision strategy
(the only strategy currently supported is `:one_for_one`), and a name:
A dynamic supervisor is started with no children, often under a
supervisor with the supervision strategy (the only strategy currently
supported is `:one_for_one`) and a name:
children = [
{DynamicSupervisor, strategy: :one_for_one, name: MyApp.DynamicSupervisor}
@@ -46,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
@@ -77,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)
@@ -102,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
@@ -114,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
@@ -136,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() :: %{
@@ -148,9 +147,12 @@ defmodule DynamicSupervisor do
}
@typedoc "Option values used by the `start*` functions"
@type option :: GenServer.option()
@type option :: {:name, Supervisor.name()} | init_option()
@typedoc "Options given to `start_link/1` and `init/1`"
@typedoc "Options used by the `start*` functions"
@type options :: [option, ...]
@typedoc "Options given to `start_link/2` and `init/1`"
@type init_option ::
{:strategy, strategy()}
| {:max_restarts, non_neg_integer()}
@@ -168,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,
@@ -207,14 +208,12 @@ defmodule DynamicSupervisor do
defmacro __using__(opts) do
quote location: :keep, bind_quoted: [opts: opts] do
@behaviour DynamicSupervisor
unless Module.has_attribute?(__MODULE__, :doc) 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__,
@@ -251,7 +250,7 @@ defmodule DynamicSupervisor do
with `:normal` reason.
"""
@doc since: "1.6.0"
@spec start_link([option | init_option]) :: Supervisor.on_start()
@spec start_link(options) :: Supervisor.on_start()
def start_link(options) when is_list(options) do
keys = [:extra_arguments, :max_children, :max_seconds, :max_restarts, :strategy]
{sup_opts, start_opts} = Keyword.split(options, keys)
@@ -277,16 +276,16 @@ defmodule DynamicSupervisor do
section in the `GenServer` module docs.
"""
@doc since: "1.6.0"
@spec start_link(module, term, [option]) :: Supervisor.on_start()
def start_link(mod, init_arg, opts \\ []) do
GenServer.start_link(__MODULE__, {mod, init_arg, opts[:name]}, opts)
@spec start_link(module, term, GenServer.options()) :: Supervisor.on_start()
def start_link(mod, args, opts \\ []) do
GenServer.start_link(__MODULE__, {mod, args, opts[:name]}, opts)
end
@doc """
Dynamically adds a child specification to `supervisor` and starts that child.
`child_spec` should be a valid child specification as detailed in the
"Child specification" section of the documentation for `Supervisor`. The child
"child_spec/1" section of the documentation for `Supervisor`. The child
process will be started as defined in the child specification.
If the child process start function returns `{:ok, child}` or `{:ok, child,
@@ -298,21 +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
| (old_erlang_child_spec :: :supervisor.child_spec())
) ::
@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)
@@ -407,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
@@ -469,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/1`.
The options received by this function are also supported by `start_link/2`.
This function returns a tuple containing the supervisor options.
@@ -489,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
@@ -534,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
@@ -547,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}
@@ -747,26 +740,13 @@ defmodule DynamicSupervisor do
end
def handle_info(msg, state) do
:logger.error(
%{
label: {DynamicSupervisor, :unexpected_msg},
report: %{
msg: msg
}
},
%{
domain: [:otp, :elixir],
error_logger: %{tag: :error_msg},
report_cb: &__MODULE__.format_report/1
}
)
:error_logger.error_msg('DynamicSupervisor received unexpected message: ~p~n', [msg])
{:noreply, state}
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}
@@ -1003,22 +983,12 @@ defmodule DynamicSupervisor do
end
defp report_error(error, reason, pid, child, %{name: name, extra_arguments: extra}) do
:logger.error(
%{
label: {:supervisor, error},
report: [
{:supervisor, name},
{:errorContext, error},
{:reason, reason},
{:offender, extract_child(pid, child, extra)}
]
},
%{
domain: [:otp, :sasl],
report_cb: &:logger.format_otp_report/1,
logger_formatter: %{title: "SUPERVISOR REPORT"},
error_logger: %{tag: :error_report, type: :supervisor_report}
}
:error_logger.error_report(
:supervisor_report,
supervisor: name,
errorContext: error,
reason: reason,
offender: extract_child(pid, child, extra)
)
end
@@ -1049,12 +1019,4 @@ defmodule DynamicSupervisor do
defp call(supervisor, req) do
GenServer.call(supervisor, req, :infinity)
end
@doc false
def format_report(%{
label: {__MODULE__, :unexpected_msg},
report: %{msg: msg}
}) do
{'DynamicSupervisor received unexpected message: ~p~n', [msg]}
end
end
+370 -776
View File
File diff suppressed because it is too large Load Diff
+41 -136
View File
@@ -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)
@@ -544,17 +546,8 @@ defmodule Exception do
defp format_application(module) do
# We cannot use Application due to bootstrap issues
case :application.get_application(module) do
{:ok, app} ->
case :application.get_key(app, :vsn) do
{:ok, vsn} when is_list(vsn) ->
"(" <> Atom.to_string(app) <> " " <> List.to_string(vsn) <> ") "
_ ->
"(" <> Atom.to_string(app) <> ") "
end
:undefined ->
""
{:ok, app} -> "(" <> Atom.to_string(app) <> ") "
:undefined -> ""
end
end
@@ -797,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
@@ -865,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
@@ -955,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
@@ -982,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}
@@ -1007,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)
@@ -1045,25 +1005,11 @@ defmodule UndefinedFunctionError do
UndefinedFunctionError ->
[]
end
defp deprecated_functions_for(module) do
if function_exported?(module, :__info__, 1) do
for {name_arity, _message} <- module.__info__(:deprecated), do: name_arity
else
[]
end
rescue
# In case the module was removed while we are computing this
UndefinedFunctionError ->
[]
end
end
defmodule FunctionClauseError do
defexception [:module, :function, :arity, :kind, :args, :clauses]
@clause_limit 10
@impl true
def message(exception) do
case exception do
@@ -1109,46 +1055,37 @@ defmodule FunctionClauseError do
mfa = Exception.format_mfa(module, function, arity)
format_clause_fun = fn {args, guards} ->
code = Enum.reduce(guards, {function, [], args}, &{:when, [], [&2, &1]})
" #{kind} " <> Macro.to_string(code, ast_fun) <> "\n"
end
formatted_args =
args
|> Enum.with_index(1)
|> Enum.map(fn {arg, i} ->
["\n # ", Integer.to_string(i), "\n ", pad(inspect_fun.(arg)), "\n"]
end)
"\n\nThe following arguments were given to #{mfa}:\n" <>
"#{format_args(args, inspect_fun)}" <>
"#{format_clauses(clauses, format_clause_fun, @clause_limit)}"
formatted_clauses =
if clauses do
format_clause_fun = fn {args, guards} ->
code = Enum.reduce(guards, {function, [], args}, &{:when, [], [&2, &1]})
" #{kind} " <> Macro.to_string(code, ast_fun) <> "\n"
end
top_10 =
clauses
|> Enum.take(10)
|> Enum.map(format_clause_fun)
[
"\nAttempted function clauses (showing #{length(top_10)} out of #{length(clauses)}):",
"\n\n",
top_10
]
else
""
end
"\n\nThe following arguments were given to #{mfa}:\n#{formatted_args}#{formatted_clauses}"
end
defp format_args(args, inspect_fun) do
args
|> Enum.with_index(1)
|> Enum.map(fn {arg, i} ->
[pad("\n# "), Integer.to_string(i), pad("\n"), pad(inspect_fun.(arg)), "\n"]
end)
end
defp format_clauses(clauses, format_clause_fun, limit)
defp format_clauses(nil, _, _), do: ""
defp format_clauses([], _, _), do: ""
defp format_clauses(clauses, format_clause_fun, limit) do
top_clauses =
clauses
|> Enum.take(limit)
|> Enum.map(format_clause_fun)
[
"\nAttempted function clauses (showing #{length(top_clauses)} out of #{length(clauses)}):",
"\n\n",
top_clauses,
non_visible_clauses(length(clauses) - limit)
]
end
defp non_visible_clauses(n) when n <= 0, do: []
defp non_visible_clauses(1), do: [" ...\n (1 clause not shown)\n"]
defp non_visible_clauses(n), do: [" ...\n (#{n} clauses not shown)\n"]
defp pad(string) do
String.replace(string, "\n", "\n ")
end
@@ -1168,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
@@ -1194,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 ->
""
@@ -1210,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
@@ -1339,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: ""]
+141 -243
View File
@@ -6,7 +6,7 @@ defmodule File do
to interact with files or IO devices, like `open/2`,
`copy/3` and others. This module also provides higher
level functions that work with filenames and have their naming
based on Unix variants. For example, one can copy a file
based on UNIX variants. For example, one can copy a file
via `cp/3` and remove files and directories recursively
via `rm_rf/1`.
@@ -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-like 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-like 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-like 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, []}
"""
@@ -1243,8 +1144,8 @@ defmodule File do
end
# On Windows, symlinks are treated as directory and must be removed
# with rmdir/1. But on Unix-like systems, 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
# 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 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} ->
@@ -1310,7 +1211,7 @@ defmodule File do
The allowed modes:
* `:binary` - opens the file in binary mode, disabling special handling of Unicode sequences
* `:binary` - opens the file in binary mode, disabling special handling of unicode sequences
(default mode).
* `:read` - the file, which must exist, is opened for reading.
@@ -1355,11 +1256,9 @@ defmodule File do
* `{:ok, io_device}` - the file has been opened in the requested mode.
`io_device` is actually the PID of the process which handles the file.
This process monitors the process that originally opened the file (the
owner process). If the owner process terminates, the file is closed and
the process itself terminates too. If any process to which the `io_device`
is linked terminates, the file will be closed and the process itself will
be terminated.
This process is linked to the process which originally opened the file.
If any process to which the `io_device` is linked terminates, the file
will be closed and the process itself will be terminated.
An `io_device` returned from this call can be used as an argument to the
`IO` module functions.
@@ -1402,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)
@@ -1425,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} ->
@@ -1443,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} ->
@@ -1464,7 +1363,7 @@ defmodule File do
@doc """
Gets the current working directory.
In rare circumstances, this function can fail on Unix-like systems. It may happen
In rare circumstances, this function can fail on Unix. It may happen
if read permissions do not exist for the parent directories of the
current directory. For this reason, returns `{:ok, cwd}` in case
of success, `{:error, reason}` otherwise.
@@ -1487,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} ->
@@ -1511,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 ->
@@ -1562,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} ->
@@ -1612,7 +1512,7 @@ defmodule File do
in raw mode for performance reasons. Therefore, Elixir **will** open
streams in `:raw` mode with the `:read_ahead` option unless an encoding
is specified. This means any data streamed into the file must be
converted to `t:iodata/0` type. If you pass, for example, `[encoding: :utf8]`
converted to `t:iodata/0` type. If you pass e.g. `[encoding: :utf8]`
or `[encoding: {:utf16, :little}]` in the modes parameter,
the underlying stream will use `IO.write/2` and the `String.Chars` protocol
to convert the data. See `IO.binwrite/2` and `IO.write/2` .
@@ -1636,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
@@ -1650,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
@@ -1675,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 ->
@@ -1693,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.
"""
@@ -1703,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 ->
@@ -1721,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.
"""
@@ -1731,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 ->
@@ -1771,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")
+7 -24
View File
@@ -12,7 +12,7 @@ defmodule File.Stat do
* `size` - size of file in bytes.
* `type` - `:device | :directory | :regular | :other | :symlink`; the type of the
* `type` - `:device | :directory | :regular | :other`; the type of the
file.
* `access` - `:read | :write | :read_write | :none`; the current system
@@ -23,7 +23,7 @@ defmodule File.Stat do
* `mtime` - the last time the file was written.
* `ctime` - the interpretation of this time field depends on the operating
system. On Unix-like operating systems, it is the last time the file or the inode was changed.
system. On Unix, it is the last time the file or the inode was changed.
In Windows, it is the time of creation.
* `mode` - the file permissions.
@@ -35,17 +35,17 @@ defmodule File.Stat do
In Windows, the number indicates a drive as follows: 0 means A:, 1 means
B:, and so on.
* `minor_device` - only valid for character devices on Unix-like systems. In all other
* `minor_device` - only valid for character devices on Unix. In all other
cases, this field is zero.
* `inode` - gives the inode number. On non-Unix-like file systems, this field
* `inode` - gives the inode number. On non-Unix file systems, this field
will be zero.
* `uid` - indicates the owner of the file. Will be zero for non-Unix-like file
* `uid` - indicates the owner of the file. Will be zero for non-Unix file
systems.
* `gid` - indicates the group that owns the file. Will be zero for
non-Unix-like file systems.
non-Unix file systems.
The time type returned in `atime`, `mtime`, and `ctime` is dependent on the
time type set in options. `{:time, type}` where type can be `:local`,
@@ -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
View File
@@ -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))
+5 -60
View File
@@ -2,43 +2,11 @@ defmodule Function do
@moduledoc """
A set of functions for working with functions.
Anonymous functions are typically created by using `fn`:
iex> add = fn a, b -> a + b end
iex> add.(1, 2)
3
It is also possible to capture module functions and pass them around
as if they were anonymous functions by using the capture operator `&/1`:
iex> add = &Kernel.+/2
iex> add.(1, 2)
3
iex> length = &String.length/1
iex> length.("hello")
5
It is also possible to capture a definition in the current module by
skipping the module prefix, such as `&my_fun/2`.
The capture operator can also be used to create anonymous functions
that expect at least one argument:
iex> add = &(&1 + &2)
iex> add.(1, 2)
3
In such cases, using the capture operator is no different than using `fn`.
We say that functions that point to definitions residing in modules, such
as `&String.length/1`, are **external** functions. All other functions are
**local** and they are always bound to the file or module that defined them.
Besides the functions in this module to work with functions, `Kernel` also
has an `apply/2` function that invokes a function with a dynamic number of
arguments, as well as `is_function/1` and `is_function/2`, to check
respectively if a given value is a function or a function of a given arity.
There are two types of captured functions: **external** and **local**.
External functions are functions residing in modules that are captured
with `&/1`, such as `&String.length/1`. Local functions are anonymous functions
defined with `fn/1` or with the capture operator `&/1` using `&1`, `&2`,
and so on as replacements.
"""
@type information ::
@@ -166,27 +134,4 @@ defmodule Function do
@doc since: "1.7.0"
@spec info(fun, item) :: {item, term} when item: information
def info(fun, item), do: :erlang.fun_info(fun, item)
@doc """
Returns its input `value`. This function can be passed as an anonymous function
to transformation functions.
## Examples
iex> Function.identity("Hello world!")
"Hello world!"
iex> 'abcdaabccc' |> Enum.sort() |> Enum.chunk_by(&Function.identity/1)
['aaa', 'bb', 'cccc', 'd']
iex> Enum.group_by('abracadabra', &Function.identity/1)
%{97 => 'aaaaa', 98 => 'bb', 99 => 'c', 100 => 'd', 114 => 'rr'}
iex> Enum.map([1, 2, 3, 4], &Function.identity/1)
[1, 2, 3, 4]
"""
@doc since: "1.10.0"
@spec identity(value) :: value when value: var
def identity(value), do: value
end
+13 -2
View File
@@ -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
+98 -206
View File
@@ -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
@@ -629,18 +557,18 @@ defmodule GenServer do
Therefore it is not guaranteed that `c:terminate/2` is called when a `GenServer`
exits. For such reasons, we usually recommend important clean-up rules to
happen in separated processes either by use of monitoring or by links
themselves. There is no cleanup needed when the `GenServer` controls a `port` (for example,
themselves. There is no cleanup needed when the `GenServer` controls a `port` (e.g.
`:gen_tcp.socket`) or `t:File.io_device/0`, because these will be closed on
receiving a `GenServer`'s exit signal and do not need to be closed manually
in `c:terminate/2`.
If `reason` is neither `:normal`, `:shutdown`, nor `{:shutdown, term}` an error is
If `reason` is not `:normal`, `:shutdown`, nor `{:shutdown, term}` an error is
logged.
This callback is optional.
"""
@callback terminate(reason, state :: term) :: term
when reason: :normal | :shutdown | {:shutdown, term} | term
when reason: :normal | :shutdown | {:shutdown, term}
@doc """
Invoked to change the state of the `GenServer` when a different version of a
@@ -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
unless Module.has_attribute?(__MODULE__, :doc) 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
@@ -785,22 +705,8 @@ defmodule GenServer do
{_, name} -> name
end
:logger.error(
%{
label: {GenServer, :no_handle_info},
report: %{
module: __MODULE__,
message: msg,
name: proc
}
},
%{
domain: [:otp, :elixir],
error_logger: %{tag: :error_msg},
report_cb: &GenServer.format_report/1
}
)
pattern = '~p ~p received unexpected message in handle_info/2: ~p~n'
:error_logger.error_msg(pattern, [__MODULE__, proc, msg])
{:noreply, state}
end
@@ -844,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
@@ -871,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.
@@ -894,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
@@ -911,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 """
@@ -921,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, """
@@ -1006,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]}})
@@ -1037,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)
@@ -1172,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 """
@@ -1219,12 +1119,4 @@ defmodule GenServer do
def whereis({name, node} = server) when is_atom(name) and is_atom(node) do
server
end
@doc false
def format_report(%{
label: {GenServer, :no_handle_info},
report: %{module: mod, message: msg, name: proc}
}) do
{'~p ~p received unexpected message in handle_info/2: ~p~n', [mod, proc, msg]}
end
end
+2
View File
@@ -7,6 +7,8 @@ defmodule HashDict do
@moduledoc deprecated: "Use Map instead"
# TODO: Remove by 2.0
use Dict
@node_bitmap 0b111
+18 -114
View File
@@ -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,15 +31,16 @@ defprotocol Inspect do
end
end
The [`concat/1`](`Inspect.Algebra.concat/1`) function comes from `Inspect.Algebra` and it
concatenates algebra documents together. In the example above it is
concatenating the string `"MapSet<"`, the document returned by
`Inspect.Algebra.to_doc/2`, and the final string `">"`. All strings are
valid algebra documents that keep their formatting when pretty printed.
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
printed), the document returned by `Inspect.Algebra.to_doc/2` and the
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
@@ -47,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
@@ -168,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,
@@ -252,7 +226,7 @@ defimpl Inspect, for: Map do
end
def inspect(map, name, opts) do
map = Map.to_list(map)
map = :maps.to_list(map)
open = color("%" <> name <> "{", :map, opts)
sep = color(",", :map, opts)
close = color("}", :map, opts)
@@ -315,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'
@@ -407,34 +372,6 @@ defimpl Inspect, for: Reference do
end
defimpl Inspect, for: Any do
defmacro __deriving__(module, struct, options) do
fields = Map.keys(struct) -- [:__exception__, :__struct__]
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
Inspect.Map
else
Inspect.Any
end
quote do
defimpl Inspect, for: unquote(module) do
def inspect(var!(struct), var!(opts)) do
var!(map) = Map.take(var!(struct), unquote(filtered_fields))
var!(name) = Identifier.inspect_as_atom(unquote(module))
unquote(inspect_module).inspect(var!(map), var!(name), var!(opts))
end
end
end
end
def inspect(%module{} = struct, opts) do
try do
module.__struct__
@@ -442,46 +379,13 @@ defimpl Inspect, for: Any do
_ -> Inspect.Map.inspect(struct, opts)
else
dunder ->
if Map.keys(dunder) == Map.keys(struct) do
pruned = Map.drop(struct, [:__struct__, :__exception__])
Inspect.Map.inspect(pruned, Identifier.inspect_as_atom(module), opts)
if :maps.keys(dunder) == :maps.keys(struct) do
pruned = :maps.remove(:__exception__, :maps.remove(:__struct__, struct))
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 = Map.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
require Protocol
Protocol.derive(
Inspect,
Macro.Env,
only: [
:module,
:file,
:line,
:function,
:context,
:aliases,
:requires,
:functions,
:macros,
:macro_aliases,
:context_modules,
:lexical_tracker
]
)
+116 -143
View File
@@ -1,107 +1,89 @@
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:
* `:base` - prints integers as `:binary`, `:octal`, `:decimal`, or `:hex`,
defaults to `:decimal`. When inspecting binaries any `:base` other than
`:decimal` implies `binaries: :as_binaries`.
* `:binaries` - when `:as_binaries` all binaries will be printed in bit
syntax.
When `:as_strings` all binaries will be printed as strings, non-printable
bytes will be escaped.
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.
* `:charlists` - when `:as_charlists` all lists will be printed as charlists,
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.
* `: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.
* `:inspect_fun` (since v1.9.0) - a function to build algebra documents,
defaults to `Inspect.inspect/2`.
* `:limit` - limits the number of items that are inspected 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`.
* `:pretty` - if set to `true` enables pretty printing, defaults to `false`.
* `: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`.
* `: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.
* `:structs` - when `false`, structs are not formatted by the inspect
protocol, they are instead printed as maps, defaults to `true`.
* `: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
each type (for example, `[number: :red, atom: :blue]`). Types can include
`:atom`, `:binary`, `:boolean`, `:list`, `:map`, `:number`, `:regex`,
`:string`, and `:tuple`. Custom data types may provide their own options.
Colors can be any `t:IO.ANSI.ansidata/0` as accepted by `IO.ANSI.format/1`.
* `:binaries` - when `:as_strings` all binaries will be printed as strings,
non-printable bytes will be escaped.
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.
* `: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.
* `:limit` - limits the number of items that are printed for tuples,
bitstrings, maps, lists and any other collection of items. It does not
apply to strings nor charlists and defaults to 50. If you don't want to limit
the number of items to a particular number, use `:infinity`.
* `: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`.
* `:width` - defaults to 80 characters, used when pretty is `true` or when
printing to IO devices. Set to 0 to force each item to be printed on its
own line. If you don't want to limit the number of items to a particular
number, use `:infinity`.
* `:base` - prints integers as `:binary`, `:octal`, `:decimal`, or `:hex`,
defaults to `:decimal`. When inspecting binaries any `:base` other than
`:decimal` implies `binaries: :as_binaries`.
* `: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
* `: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`.
"""
# TODO: Remove :char_lists key on v2.0
defstruct base: :decimal,
# TODO: Remove :char_lists key by 2.0
defstruct structs: true,
binaries: :infer,
char_lists: :infer,
charlists: :infer,
custom_options: [],
inspect_fun: &Inspect.inspect/2,
char_lists: :infer,
limit: 50,
pretty: false,
printable_limit: 4096,
width: 80,
base: :decimal,
pretty: false,
safe: true,
structs: true,
syntax_colors: [],
width: 80
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__{
base: :decimal | :binary | :hex | :octal,
binaries: :infer | :as_binaries | :as_strings,
char_lists: :infer | :as_lists | :as_char_lists,
charlists: :infer | :as_lists | :as_charlists,
custom_options: keyword,
inspect_fun: (any, t -> Inspect.Algebra.t()),
limit: pos_integer | :infinity,
pretty: boolean,
printable_limit: pos_integer | :infinity,
safe: boolean,
structs: boolean,
syntax_colors: [{color_key, IO.ANSI.ansidata()}],
width: pos_integer | :infinity
binaries: :infer | :as_binaries | :as_strings,
charlists: :infer | :as_lists | :as_charlists,
char_lists: :infer | :as_lists | :as_char_lists,
limit: pos_integer | :infinity,
printable_limit: pos_integer | :infinity,
width: pos_integer | :infinity,
base: :decimal | :binary | :hex | :octal,
pretty: boolean,
safe: boolean,
syntax_colors: [{color_key, IO.ANSI.ansidata()}]
}
end
@@ -166,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
@@ -193,17 +175,17 @@ defmodule Inspect.Algebra do
@type t ::
binary
| :doc_line
| :doc_nil
| doc_break
| doc_collapse
| doc_color
| doc_cons
| doc_fits
| doc_force
| doc_group
| doc_nest
| :doc_line
| doc_string
| doc_cons
| doc_nest
| doc_break
| doc_group
| doc_color
| doc_force
| doc_fits
| doc_collapse
@typep doc_string :: {:doc_string, t, non_neg_integer}
defmacrop doc_string(string, length) do
@@ -251,15 +233,15 @@ defmodule Inspect.Algebra do
end
@docs [
:doc_break,
:doc_collapse,
:doc_color,
:doc_string,
:doc_cons,
:doc_fits,
:doc_force,
:doc_group,
:doc_nest,
:doc_string
:doc_break,
:doc_group,
:doc_color,
:doc_force,
:doc_fits,
:doc_collapse
]
defguard is_doc(doc)
@@ -275,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
@@ -317,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"""
@@ -330,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
@@ -342,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! ...]"
@@ -365,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
@@ -377,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))
@@ -429,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,
@@ -560,7 +538,7 @@ defmodule Inspect.Algebra do
["hello", "\n ", "world"]
"""
@spec nest(t, non_neg_integer | :cursor | :reset, :always | :break) :: doc_nest
@spec nest(t, non_neg_integer) :: doc_nest
def nest(doc, level, mode \\ :always)
def nest(doc, :cursor, mode) when is_doc(doc) and mode in [:always, :break] do
@@ -662,7 +640,7 @@ defmodule Inspect.Algebra do
"""
@doc since: "1.6.0"
@spec next_break_fits(t, :enabled | :disabled) :: doc_fits
@spec next_break_fits(t) :: doc_fits
def next_break_fits(doc, mode \\ @next_break_fits)
when is_doc(doc) and mode in [:enabled, :disabled] do
doc_fits(doc, mode)
@@ -702,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"
@@ -757,31 +735,29 @@ defmodule Inspect.Algebra do
## Examples
iex> doc =
...> Inspect.Algebra.group(
...> Inspect.Algebra.concat(
...> Inspect.Algebra.group(
...> Inspect.Algebra.concat(
...> "Hello,",
...> Inspect.Algebra.concat(
...> Inspect.Algebra.break(),
...> "A"
...> )
...> )
...> ),
iex> doc = Inspect.Algebra.group(
...> Inspect.Algebra.concat(
...> Inspect.Algebra.group(
...> Inspect.Algebra.concat(
...> Inspect.Algebra.break(),
...> "B"
...> "Hello,",
...> Inspect.Algebra.concat(
...> Inspect.Algebra.break,
...> "A"
...> )
...> )
...> ),
...> Inspect.Algebra.concat(
...> Inspect.Algebra.break,
...> "B"
...> )
...> )
...> ))
iex> Inspect.Algebra.format(doc, 80)
["Hello,", " ", "A", " ", "B"]
iex> Inspect.Algebra.format(doc, 6)
["Hello,", "\n", "A", "\n", "B"]
"""
@spec group(t, :self | :inherit) :: doc_group
@spec group(t) :: doc_group
def group(doc, mode \\ :self) when is_doc(doc) do
doc_group(doc, mode)
end
@@ -811,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"]
@@ -825,7 +800,7 @@ defmodule Inspect.Algebra do
@doc ~S"""
Inserts a mandatory linebreak between two documents.
See `line/0`.
See `line/1`.
## Examples
@@ -847,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"]
@@ -898,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.
+11 -14
View File
@@ -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
@@ -233,7 +233,7 @@ defmodule Integer do
raise ArgumentError, "invalid base #{inspect(base)}"
end
def parse(binary, base) when is_binary(binary) do
def parse(binary, base) do
case count_digits(binary, base) do
0 ->
:error
@@ -244,14 +244,14 @@ defmodule Integer do
end
end
defp count_digits(<<sign, rest::bits>>, base) when sign in '+-' do
defp count_digits(<<sign, rest::binary>>, base) when sign in '+-' do
case count_digits_nosign(rest, base, 1) do
1 -> 0
count -> count
end
end
defp count_digits(<<rest::bits>>, base) do
defp count_digits(<<rest::binary>>, base) do
count_digits_nosign(rest, base, 0)
end
@@ -261,17 +261,14 @@ defmodule Integer do
char <- chars do
digit = char + diff
defp count_digits_nosign(<<unquote(char), rest::bits>>, base, count)
defp count_digits_nosign(<<unquote(char), rest::binary>>, base, count)
when base > unquote(digit) do
count_digits_nosign(rest, base, count + 1)
end
end
defp count_digits_nosign(<<_::bits>>, _, count), do: count
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)
+58 -174
View File
@@ -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` and `IO`, 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 code points. Bytes
(`t:byte/0`) are integers in the `0..255` range, while Unicode code points
(`t:char/0`) are integers in the range `0..0x10FFFF`. The `IO` module provides
the `chardata_to_string/1` function for chardata as the "counter-part" of the
`iodata_to_binary/1` function for IO data.
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 code point 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`.
@@ -148,7 +68,7 @@ defmodule IO do
def read(device \\ :stdio, line_or_chars)
def read(device, :all) do
do_read_all(map_dev(device), :empty)
do_read_all(map_dev(device), "")
end
def read(device, :line) do
@@ -161,27 +81,12 @@ defmodule IO do
defp do_read_all(mapped_dev, acc) do
case :io.get_line(mapped_dev, "") do
line when is_binary(line) or is_list(line) -> do_read_all(mapped_dev, concat(acc, line))
:eof -> read_eof(mapped_dev, acc)
line when is_binary(line) -> do_read_all(mapped_dev, acc <> line)
:eof -> acc
other -> other
end
end
defp concat(:empty, line), do: line
defp concat(acc, line) when is_binary(acc), do: acc <> line
defp concat(acc, line) when is_list(acc), do: acc ++ line
defp read_eof(device, :empty) do
with [_ | _] = opts <- :io.getopts(device),
false <- Keyword.get(opts, :binary, true) do
''
else
_ -> ""
end
end
defp read_eof(_device, acc), do: acc
@doc """
Reads from the IO `device`. The operation is Unicode unsafe.
@@ -236,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 """
@@ -285,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
"""
@@ -309,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(0, 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 || 0,
file && List.to_string(file),
message,
[message, ?\n, " ", formatted_trace, ?\n]
)
:elixir_errors.bare_warn(line, file && List.to_string(file), message)
end
@doc """
@@ -341,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
@@ -372,7 +263,7 @@ defmodule IO do
## Examples
IO.inspect(<<0, 1, 2>>, width: 40)
IO.inspect <<0, 1, 2>>, width: 40
Prints:
@@ -380,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:
@@ -392,7 +283,7 @@ defmodule IO do
|> IO.inspect(label: "before")
|> Enum.map(&(&1 * 2))
|> IO.inspect(label: "after")
|> Enum.sum()
|> Enum.sum
Prints:
@@ -424,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.
@@ -446,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:
@@ -483,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
@@ -512,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()
@@ -548,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.
@@ -568,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
@@ -578,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.
@@ -607,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.
@@ -626,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
+1 -1
View File
@@ -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
+26 -100
View File
@@ -14,7 +14,6 @@ defmodule IO.ANSI.Docs do
* `:doc_code` - code blocks (cyan)
* `:doc_headings` - h1, h2, h3, h4, h5, h6 headings (yellow)
* `:doc_metadata` - documentation metadata keys (yellow)
* `:doc_quote` - leading quote character `> ` (light black)
* `:doc_inline_code` - inline code (cyan)
* `:doc_table_heading` - the style for table headings
* `:doc_title` - top level heading (reverse, yellow)
@@ -32,7 +31,6 @@ defmodule IO.ANSI.Docs do
doc_code: [:cyan],
doc_headings: [:yellow],
doc_metadata: [:yellow],
doc_quote: [:light_black],
doc_inline_code: [:cyan],
doc_table_heading: [:reverse],
doc_title: [:reverse, :yellow],
@@ -58,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.
"""
@@ -68,21 +66,14 @@ 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
{key, value}, _printed when is_binary(value) and key in @metadata_filter ->
label = metadata_label(key, options)
indent = String.duplicate(" ", length_without_escape(label, 0) + 1)
write_with_wrap([label | String.split(value, @spaces)], options[:width], indent, true, "")
{key, value}, _printed when is_boolean(value) and key in @metadata_filter ->
IO.puts([metadata_label(key, options), ' ', to_string(value)])
{:delegate_to, {m, f, a}}, _printed ->
label = metadata_label(:delegate_to, options)
IO.puts([label, ' ', Exception.format_mfa(m, f, a)])
write_with_wrap([label | String.split(value, @spaces)], options[:width], indent, true)
_metadata, printed ->
printed
@@ -100,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
@@ -141,11 +132,6 @@ defmodule IO.ANSI.Docs do
write_heading(heading, rest, text, indent, options)
end
defp process([">" <> line | rest], text, indent, options) do
write_text(text, indent, options)
process_quote(rest, [line], indent, options)
end
defp process(["" | rest], text, indent, options) do
write_text(text, indent, options)
process(rest, [], indent, options)
@@ -190,47 +176,6 @@ defmodule IO.ANSI.Docs do
process(rest, [], "", options)
end
## Quotes
defp process_quote([], lines, indent, options) do
write_quote(lines, indent, options, false)
end
defp process_quote([">", ">" <> line | rest], lines, indent, options) do
write_quote(lines, indent, options, true)
write_empty_quote_line(options)
process_quote(rest, [line], indent, options)
end
defp process_quote([">" <> line | rest], lines, indent, options) do
process_quote(rest, [line | lines], indent, options)
end
defp process_quote(rest, lines, indent, options) do
write_quote(lines, indent, options, false)
process(rest, [], indent, options)
end
defp write_quote(lines, indent, options, no_wrap) do
lines
|> Enum.map(&String.trim/1)
|> Enum.reverse()
|> write_lines(
indent,
options,
no_wrap,
quote_prefix(options)
)
end
defp quote_prefix(options), do: "#{color(:doc_quote, options)}> #{IO.ANSI.reset()}"
defp write_empty_quote_line(options) do
options
|> quote_prefix()
|> IO.puts()
end
## Lists
defp process_rest(stripped, rest, count, text, indent, options) do
@@ -315,25 +260,16 @@ defmodule IO.ANSI.Docs do
end
defp write_text(lines, indent, options, no_wrap) do
write_lines(lines, indent, options, no_wrap, "")
end
defp write_lines(lines, indent, options, no_wrap, prefix) do
lines
|> Enum.join(" ")
|> format_text(options)
|> handle_links
|> handle_inline(options)
|> String.split(@spaces)
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap, prefix)
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap)
unless no_wrap, do: newline_after_block()
end
defp format_text(text, options) do
text
|> handle_links()
|> handle_inline(options)
end
## Code blocks
defp process_code([], code, indent, options) do
@@ -405,17 +341,17 @@ defmodule IO.ANSI.Docs do
defp split_into_columns(line, options) do
line
|> String.trim(" ")
|> String.trim("|")
|> String.split("|")
|> String.trim()
|> String.split(" | ")
|> Enum.map(&render_column(&1, options))
end
defp render_column(col, options) do
col =
col
|> String.trim()
|> String.replace("\\\|", "|")
|> String.trim()
|> handle_links
|> handle_inline(options)
@@ -507,7 +443,7 @@ defmodule IO.ANSI.Docs do
end
defp table_line?(line) do
line =~ ~r/[:\ -]\|[:\ -]/
line =~ " | "
end
## Helpers
@@ -527,27 +463,14 @@ defmodule IO.ANSI.Docs do
IO.puts([color(style, options), string, IO.ANSI.reset()])
end
defp write_with_wrap([], _available, _indent, _first, _prefix) do
defp write_with_wrap([], _available, _indent, _first) do
:ok
end
defp write_with_wrap(words, available, indent, first, prefix) do
words
|> wrap_text(available, indent, first, prefix, [])
|> Enum.join("\n")
|> IO.puts()
end
defp wrap_text([], _available, _indent, _first, _prefix, wrapped_lines) do
Enum.reverse(wrapped_lines)
end
defp wrap_text(words, available, indent, first, prefix, wrapped_lines) do
prefix_length = length_without_escape(prefix, 0)
{words, rest} = take_words(words, available - prefix_length, [])
line = [if(first, do: "", else: indent), prefix, Enum.join(words, " ")]
wrap_text(rest, available, indent, false, prefix, [line | wrapped_lines])
defp write_with_wrap(words, available, indent, first) do
{words, rest} = take_words(words, available, [])
IO.puts(if(first, do: "", else: indent) <> Enum.join(words, " "))
write_with_wrap(rest, available, indent, false)
end
defp take_words([word | words], available, acc) do
@@ -594,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 *
@@ -669,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
+590 -824
View File
File diff suppressed because it is too large Load Diff
+24 -70
View File
@@ -1,19 +1,16 @@
defmodule Kernel.CLI do
@moduledoc false
@compile {:no_warn_undefined, [Logger, IEx]}
@blank_config %{
commands: [],
output: ".",
compile: [],
no_halt: false,
halt: true,
compiler_options: [],
errors: [],
pa: [],
pz: [],
verbose_compile: false,
profile: nil
verbose_compile: false
}
@doc """
@@ -24,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)
@@ -35,7 +31,7 @@ defmodule Kernel.CLI do
end
end
run(fun)
run(fun, config.halt)
end
@doc """
@@ -46,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
@@ -62,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)
@@ -98,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 -> Code.eval_string(expr) end)
catch
kind, reason -> {kind, reason, __STACKTRACE__}
end
## Helpers
defp at_exit(res) do
@@ -244,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
@@ -268,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
@@ -312,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}
@@ -357,12 +335,6 @@ defmodule Kernel.CLI do
parse_compiler(t, %{config | verbose_compile: true})
end
# Private compiler options
defp parse_compiler(["--profile", "time" | t], config) do
parse_compiler(t, %{config | profile: :time})
end
defp parse_compiler([h | t] = list, config) do
case h do
"-" <> _ ->
@@ -423,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}} ->
@@ -496,22 +459,13 @@ defmodule Kernel.CLI do
wrapper(fn ->
Code.compiler_options(config.compiler_options)
verbose_opts =
opts =
if config.verbose_compile do
[each_long_compilation: &IO.puts("Compiling #{&1} (it's taking more than 15s)")]
else
[]
end
profile_opts =
if config.profile do
[profile: config.profile]
else
[]
end
opts = verbose_opts ++ profile_opts
case Kernel.ParallelCompiler.compile_to_path(files, config.output, opts) do
{:ok, _, _} -> :ok
{:error, _, _} -> exit({:shutdown, 1})
+9 -9
View File
@@ -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
+87 -41
View File
@@ -6,14 +6,28 @@
# any of the `GenServer.Behaviour` conveniences.
defmodule Kernel.LexicalTracker do
@moduledoc false
@timeout :infinity
@timeout 30000
@behaviour :gen_server
@doc """
Returns all references in this lexical scope.
Returns all remotes referenced in this lexical scope.
"""
def references(pid) do
:gen_server.call(pid, :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(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
@@ -26,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
@@ -40,18 +54,23 @@ defmodule Kernel.LexicalTracker do
end
@doc false
def remote_dispatch(pid, module, mode) when is_atom(module) do
:gen_server.cast(pid, {:remote_dispatch, module, mode})
def remote_reference(pid, module, mode) when is_atom(module) do
:gen_server.cast(pid, {:remote_reference, module, mode})
end
@doc false
def remote_struct(pid, module) when is_atom(module) do
:gen_server.cast(pid, {:remote_struct, module})
def remote_dispatch(pid, module, fa, line, mode) when is_atom(module) do
:gen_server.cast(pid, {:remote_dispatch, module, fa, line, mode})
end
@doc false
def import_dispatch(pid, module, fa) when is_atom(module) do
:gen_server.cast(pid, {:import_dispatch, module, fa})
def remote_struct(pid, module, line) when is_atom(module) do
:gen_server.cast(pid, {:remote_struct, module, line})
end
@doc false
def import_dispatch(pid, module, fa, line, mode) when is_atom(module) do
:gen_server.cast(pid, {:import_dispatch, module, fa, line, mode})
end
@doc false
@@ -59,11 +78,6 @@ defmodule Kernel.LexicalTracker do
:gen_server.cast(pid, {:alias_dispatch, module})
end
@doc false
def add_compile_env(pid, app, path, return) do
:gen_server.cast(pid, {:compile_env, app, path, return})
end
@doc false
def set_file(pid, file) do
:gen_server.cast(pid, {:set_file, file})
@@ -106,9 +120,10 @@ defmodule Kernel.LexicalTracker do
state = %{
directives: %{},
references: %{},
compile: %{},
runtime: %{},
structs: %{},
cache: %{},
compile_env: :ordsets.new(),
file: nil
}
@@ -125,35 +140,45 @@ defmodule Kernel.LexicalTracker do
{:reply, Enum.sort(directives), state}
end
def handle_call(:references, _from, state) do
{compile, runtime} = partition(Map.to_list(state.references), [], [])
{:reply, {compile, Map.keys(state.structs), runtime, state.compile_env}, state}
def handle_call(:remote_references, _from, state) do
{compile, runtime} = partition(:maps.to_list(state.references), [], [])
{:reply, {compile, :maps.keys(state.structs), runtime}, state}
end
def handle_call(:remote_dispatches, _from, state) do
{:reply, {state.compile, state.runtime}, state}
end
def handle_call({:read_cache, key}, _from, %{cache: cache} = state) do
{:reply, Map.get(cache, key), state}
end
def handle_call(:stop, _from, state) do
{:stop, :normal, :ok, state}
{:reply, :maps.get(key, cache), state}
end
def handle_cast({:write_cache, key, value}, %{cache: cache} = state) do
{:noreply, %{state | cache: Map.put(cache, key, value)}}
{:noreply, %{state | cache: :maps.put(key, value, cache)}}
end
def handle_cast({:remote_struct, module}, state) do
structs = Map.put(state.structs, module, true)
def handle_cast({:remote_reference, module, mode}, state) do
{:noreply, %{state | references: add_reference(state.references, module, mode)}}
end
def handle_cast({:remote_struct, module, line}, state) do
state = add_remote_dispatch(state, module, {:__struct__, 1}, line, :compile)
structs = :maps.put(module, true, state.structs)
{:noreply, %{state | structs: structs}}
end
def handle_cast({:remote_dispatch, module, mode}, state) do
def handle_cast({:remote_dispatch, module, fa, line, mode}, state) do
references = add_reference(state.references, module, mode)
state = add_remote_dispatch(state, module, fa, line, mode)
{:noreply, %{state | references: references}}
end
def handle_cast({:import_dispatch, module, {function, arity}}, state) do
state = add_import_dispatch(state, module, function, arity)
def handle_cast({:import_dispatch, module, {function, arity} = fa, line, mode}, state) do
state =
state
|> add_import_dispatch(module, function, arity)
|> add_remote_dispatch(module, fa, line, mode)
{:noreply, state}
end
@@ -169,15 +194,11 @@ defmodule Kernel.LexicalTracker do
{:noreply, %{state | file: nil}}
end
def handle_cast({:compile_env, app, path, return}, state) do
{:noreply, update_in(state.compile_env, &:ordsets.add_element({app, path, return}, &1))}
end
def handle_cast({:add_import, module, fas, line, warn}, state) do
directives =
state.directives
|> Enum.reject(&match?({{:import, {^module, _, _}}, _}, &1))
|> Map.new()
|> :maps.from_list()
|> add_directive(module, line, warn, :import)
directives =
@@ -192,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}
@@ -218,15 +243,28 @@ defmodule Kernel.LexicalTracker do
# Callbacks helpers
defp add_reference(references, module, :compile) when is_atom(module),
do: Map.put(references, module, :compile)
do: :maps.put(module, :compile, references)
defp add_reference(references, module, :runtime) when is_atom(module) do
case Map.fetch(references, module) do
case :maps.find(module, references) do
{:ok, _} -> references
:error -> Map.put(references, module, :runtime)
:error -> :maps.put(module, :runtime, references)
end
end
defp add_remote_dispatch(state, module, fa, line, mode) when is_atom(module) do
location = location(state.file, line)
map_update(mode, %{module => %{fa => [location]}}, state, fn mode_dispatches ->
map_update(module, %{fa => [location]}, mode_dispatches, fn module_dispatches ->
map_update(fa, [location], module_dispatches, &[location | List.delete(&1, location)])
end)
end)
end
defp location(nil, line), do: line
defp location(file, line), do: {file, line}
defp add_import_dispatch(state, module, function, arity) do
directives =
add_dispatch(state.directives, module, :import)
@@ -235,6 +273,7 @@ defmodule Kernel.LexicalTracker do
# Always compile time because we depend
# on the module at compile time
references = add_reference(state.references, module, :compile)
%{state | directives: directives, references: references}
end
@@ -243,10 +282,17 @@ defmodule Kernel.LexicalTracker do
# If the value is true, it was imported/aliased and used
defp add_directive(directives, module_or_mfa, line, warn, tag) do
marker = if warn, do: line, else: true
Map.put(directives, {tag, module_or_mfa}, marker)
:maps.put({tag, module_or_mfa}, marker, directives)
end
defp add_dispatch(directives, module_or_mfa, tag) do
Map.put(directives, {tag, module_or_mfa}, true)
:maps.put({tag, module_or_mfa}, true, directives)
end
defp map_update(key, initial, map, fun) do
case :maps.find(key, map) do
{:ok, val} -> :maps.put(key, fun.(val), map)
:error -> :maps.put(key, initial, map)
end
end
end
+158 -320
View File
@@ -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
@@ -62,24 +61,16 @@ defmodule Kernel.ParallelCompiler do
the file, module and the module bytecode
* `:each_cycle` - after the given files are compiled, invokes this function
that should return the following values:
* `{:compile, modules}` - to continue compilation with a list of further modules to compile
* `{:runtime, modules}` - to stop compilation and verify the list of modules because
dependent modules have changed
that return a list with potentially more files to compile
* `:long_compilation_threshold` - the timeout (in seconds) after the
`:each_long_compilation` callback is invoked; defaults to `15`
* `:profile` - if set to `:time` measure the compilation time of each compilation cycle
and group pass checker
* `: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.
* `:beam_timestamp` - the modification timestamp to give all BEAM files
"""
@doc since: "1.6.0"
def compile(files, options \\ []) when is_list(options) do
@@ -99,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
@@ -116,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
@@ -125,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
@@ -139,20 +130,16 @@ defmodule Kernel.ParallelCompiler do
compiler_pid = self()
:elixir_code_server.cast({:reset_warnings, compiler_pid})
schedulers = max(:erlang.system_info(:schedulers_online), 2)
beam_timestamp = Keyword.get(options, :beam_timestamp)
outcome =
spawn_workers(files, 0, [], [], %{}, [], %{
result =
spawn_workers(files, [], [], [], [], %{
dest: Keyword.get(options, :dest),
each_cycle: Keyword.get(options, :each_cycle, fn -> {:runtime, []} end),
each_file: Keyword.get(options, :each_file, fn _, _ -> :ok end) |> each_file(),
each_cycle: Keyword.get(options, :each_cycle, fn -> [] end),
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),
long_compilation_threshold: Keyword.get(options, :long_compilation_threshold, 15),
profile: Keyword.get(options, :profile),
cycle_start: System.monotonic_time(),
module_counter: 0,
output: output,
long_compilation_threshold: Keyword.get(options, :long_compilation_threshold, 15),
schedulers: schedulers
})
@@ -160,7 +147,7 @@ defmodule Kernel.ParallelCompiler do
# compilation status will be set to error.
compilation_status = :elixir_code_server.call({:compilation_status, compiler_pid})
case {outcome, compilation_status} do
case {result, compilation_status} do
{{:ok, _, warnings}, :error} ->
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
IO.puts(:stderr, message)
@@ -169,376 +156,235 @@ defmodule Kernel.ParallelCompiler do
{{:error, errors, warnings}, :error} ->
{:error, errors ++ warnings, []}
{{:ok, outcome, warnings}, _} ->
{:ok, write_module_binaries(outcome, output, beam_timestamp), warnings}
{{:error, errors, warnings}, _} ->
{:error, errors, warnings}
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
defp write_module_binaries(result, {:compile, path}, timestamp) do
Enum.flat_map(result, fn
{{:module, module}, {binary, _map}} ->
full_path = Path.join(path, Atom.to_string(module) <> ".beam")
File.write!(full_path, binary)
if timestamp, do: File.touch!(full_path, timestamp)
[module]
_ ->
[]
end)
end
defp write_module_binaries(result, _output, _timestamp) do
for {{:module, module}, _} <- result, do: module
end
## Verification
defp verify_modules(result, warnings, dependent_modules, state) do
checker_warnings = maybe_check_modules(result, dependent_modules, state)
warnings = Enum.reverse(warnings, checker_warnings)
{:ok, result, warnings}
end
defp maybe_check_modules(result, runtime_modules, state) do
%{schedulers: schedulers, profile: profile} = state
if :elixir_config.get(:bootstrap) do
[]
else
compiled_modules = checker_compiled_modules(result)
runtime_modules = checker_runtime_modules(runtime_modules)
profile_checker(profile, compiled_modules, runtime_modules, fn ->
Module.ParallelChecker.verify(compiled_modules, runtime_modules, schedulers)
end)
result
end
end
defp checker_compiled_modules(result) do
for {{:module, _module}, {binary, module_map}} <- result do
{module_map, binary}
end
end
defp checker_runtime_modules(modules) do
for module <- modules,
path = :code.which(module),
is_list(path) and path != [] do
{module, File.read!(path)}
end
end
defp profile_checker(_profile = :time, compiled_modules, runtime_modules, fun) do
{time, result} = :timer.tc(fun)
time = div(time, 1000)
num_modules = length(compiled_modules) + length(runtime_modules)
IO.puts(:stderr, "[profile] Finished group pass check of #{num_modules} modules in #{time}ms")
result
end
defp profile_checker(_profile = nil, _compiled_modules, _runtime_modules, fun) do
fun.()
end
## Compiler worker spawning
# 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} -> compile_file(file, path, parent)
:compile -> compile_file(file, dest, parent)
:require -> require_file(file, parent)
end
catch
kind, reason ->
send(parent, {:file_error, self(), file, {kind, reason, __STACKTRACE__}})
end
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)
Code.compile_file(file)
:require ->
Code.require_file(file)
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
state = cycle_timing(result, state)
# 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
warnings = Enum.reverse(warnings)
{:ok, modules, warnings}
case each_cycle_return(state.each_cycle.()) do
{:runtime, dependent_modules} ->
verify_modules(result, warnings, dependent_modules, state)
{:compile, []} ->
verify_modules(result, warnings, [], state)
{:compile, more} ->
spawn_workers(more, 0, [], [], result, warnings, state)
more ->
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
defp compile_file(file, path, parent) do
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
:erlang.put(:elixir_compiler_dest, path)
:elixir_compiler.file(file, &each_file(&1, &2, parent))
end
defp waiting_on_without_definition(waiting, pid) do
{_, ^pid, _, on, _} = entry = List.keyfind(waiting, pid, 1)
defp require_file(file, parent) do
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})
if Enum.any?(waiting, fn {_, _, _, _, defining} -> on in defining end) do
nil
else
entry
end
end
defp cycle_timing(result, %{profile: :time} = state) do
%{cycle_start: cycle_start, module_counter: module_counter} = state
num_modules = count_modules(result)
diff_modules = num_modules - module_counter
now = System.monotonic_time()
time = System.convert_time_unit(now - cycle_start, :native, :millisecond)
IO.puts(
:stderr,
"[profile] Finished compilation cycle of #{diff_modules} modules in #{time}ms"
)
%{state | cycle_start: now, module_counter: num_modules}
end
defp cycle_timing(_result, %{profile: nil} = state) do
state
end
defp count_modules(result) do
Enum.count(result, &match?({{:module, _}, _}, &1))
end
# TODO: Deprecate on v1.14
defp each_cycle_return(modules) when is_list(modules), do: {:compile, modules}
defp each_cycle_return(other), do: other
# 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 that not all files have been compile yet, so they may not be in waiting.
defp without_definition(waiting, files) do
nillify_empty(
for {pid, _, _, _} <- files,
{_, ^pid, ref, on, _, _} <- List.wrap(List.keyfind(waiting, pid, 1)),
not Enum.any?(waiting, fn {_, _, _, _, defining, _} -> on in defining end),
do: {ref, :not_found}
)
end
defp deadlocked(waiting, type) do
nillify_empty(for {_, _, ref, _, _, ^type} <- waiting, do: {ref, :deadlock})
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, module_map} ->
{:module_available, child, ref, file, module, binary} ->
state.each_module.(file, module, binary)
# Release the module loader which is waiting for an ack
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}, {binary, module_map})
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
@@ -550,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, [])]}
_ ->
@@ -570,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("""
@@ -606,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
@@ -619,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
@@ -652,12 +496,6 @@ defmodule Kernel.ParallelCompiler do
end
end
defp get_line(file, _reason, [{_, _, _, [file: 'expanding macro']}, {_, _, _, info} | _]) do
if Keyword.get(info, :file) == to_charlist(Path.relative_to_cwd(file)) do
Keyword.get(info, :line)
end
end
defp get_line(file, _reason, [{_, _, _, info} | _]) do
if Keyword.get(info, :file) == to_charlist(Path.relative_to_cwd(file)) do
Keyword.get(info, :line)
+1 -1
View File
@@ -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
+140 -258
View File
@@ -3,21 +3,14 @@ defmodule Kernel.SpecialForms do
Special forms are the basic building blocks of Elixir, and therefore
cannot be overridden by the developer.
The `Kernel.SpecialForms` module consists solely of macros that can be
invoked anywhere in Elixir code without the use of the
`Kernel.SpecialForms.` prefix. This is possible because they all have
been automatically imported, in the same fashion as the functions and
macros from the `Kernel` module.
These building blocks are defined in this module. Some of these special forms are lexical (such as
`alias/2` and `case/2`). The macros `{}/1` and `<<>>/1` are also special
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
forms used to define tuple and binary data structures respectively.
This module also documents macros that return information about Elixir's
compilation environment, such as (`__ENV__/0`, `__MODULE__/0`, `__DIR__/0`,
`__STACKTRACE__/0`, and `__CALLER__/0`).
compilation environment, such as (`__ENV__/0`, `__MODULE__/0`, `__DIR__/0` and `__CALLER__/0`).
Additionally, it documents two special forms, `__block__/1` and
Finally, it also documents two special forms, `__block__/1` and
`__aliases__/1`, which are not intended to be called directly by the
developer but they appear in quoted contents since they are essential
in Elixir's constructs.
@@ -44,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.
@@ -199,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>>
@@ -246,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">>
@@ -351,9 +326,9 @@ defmodule Kernel.SpecialForms do
13::size(8), 10::size(8), 26::size(8), 10::size(8)>>
@jpg_signature <<255::size(8), 216::size(8)>>
def type(<<@png_signature, _rest::binary>>), do: :png
def type(<<@jpg_signature, _rest::binary>>), do: :jpg
def type(_), do: :unknown
def type(<<@png_signature, rest::binary>>), do: :png
def type(<<@jpg_signature, rest::binary>>), do: :jpg
def type(_), do :unknown
end
### Performance & Optimizations
@@ -398,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:
@@ -415,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.
@@ -493,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
@@ -510,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
@@ -549,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])
@@ -574,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.
"""
@@ -584,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
@@ -713,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!([])
@@ -791,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,
@@ -805,15 +776,36 @@ defmodule Kernel.SpecialForms do
* The first element of the tuple is always an atom or
another tuple in the same representation.
* The second element of the tuple represents [metadata](t:Macro.metadata/0).
* The second element of the tuple represents metadata.
* The third element of the tuple are the arguments for the
function call. The third argument may be an atom, which is
usually a variable (or a local call).
## Options
* `: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
information.
* `:line` - sets the quoted expressions to have the given line.
* `:generated` - marks the given chunk as generated so it does not emit warnings.
Currently it only works on special forms (for example, you can annotate a `case`
but not an `if`).
* `:context` - sets the resolution context.
* `: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
are also part of its AST. Those literals return themselves when
quoted. They are:
when quoted return themselves. They are:
:sum #=> Atoms
1 #=> Integers
@@ -822,46 +814,11 @@ defmodule Kernel.SpecialForms do
"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
* `:bind_quoted` - passes a binding to the macro. Whenever a binding is
given, `unquote/1` is automatically disabled.
* `:context` - sets the resolution context.
* `:generated` - marks the given chunk as generated so it does not emit warnings.
Currently it only works on special forms (for example, you can annotate a `case`
but not an `if`).
* `:file` - sets the quoted expressions to have the given file.
* `:line` - sets the quoted expressions to have the given line.
* `:location` - when set to `:keep`, keeps the current line and file from
quote. Read the "Stacktrace information" section below for more information.
* `: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"]}
## 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:
@@ -876,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)`
@@ -886,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:
@@ -955,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
@@ -977,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
@@ -998,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
@@ -1019,9 +973,9 @@ defmodule Kernel.SpecialForms do
end
end
Hygiene.write()
Hygiene.read()
** (RuntimeError) undefined variable a or undefined function a/0
Hygiene.write
Hygiene.read
#=> ** (RuntimeError) undefined variable a or undefined function a/0
For such, you can explicitly pass the current module scope as
argument:
@@ -1040,8 +994,8 @@ defmodule Kernel.SpecialForms do
end
end
ContextHygiene.write()
ContextHygiene.read()
ContextHygiene.write
ContextHygiene.read
#=> 1
## Hygiene in aliases
@@ -1054,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
@@ -1075,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
@@ -1111,10 +1063,10 @@ defmodule Kernel.SpecialForms do
end
require Hygiene
Hygiene.no_interference()
** (UndefinedFunctionError) ...
Hygiene.no_interference
#=> ** (UndefinedFunctionError) ...
Hygiene.interference()
Hygiene.interference
#=> "world"
end
@@ -1136,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`
@@ -1172,8 +1123,7 @@ defmodule Kernel.SpecialForms do
end
end
Lazy.return_length()
#=> 5
Lazy.return_length #=> 5
## Stacktrace information
@@ -1199,36 +1149,36 @@ defmodule Kernel.SpecialForms do
require Sample
Sample.add(:one, :two)
** (ArithmeticError) bad argument in arithmetic expression
adder.ex:5: Sample.add/2
#=> ** (ArithmeticError) bad argument in arithmetic expression
#=> adder.ex:5: Sample.add/2
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:
@@ -1251,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
@@ -1272,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
@@ -1284,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
@@ -1377,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"]
@@ -1397,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
@@ -1417,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])
@@ -1501,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}
@@ -1516,12 +1402,11 @@ 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
#=> ** (MatchError) no match of right hand side value: :bar
As with any other function or macro call in Elixir, explicit parens can
also be used around the arguments before the `do`/`end` block:
@@ -1550,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])
@@ -1617,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 +1568,7 @@ defmodule Kernel.SpecialForms do
unambiguously identified by the operator `:.`. For example:
iex> quote do
...> Foo.bar()
...> Foo.bar
...> end
{{:., [], [{:__aliases__, [alias: false], [:Foo]}, :bar]}, [], []}
@@ -1694,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.
@@ -1743,34 +1625,34 @@ defmodule Kernel.SpecialForms do
end
#=> "This clause would match any value (x = 10)"
## Variable handling
## Variables handling
Notice that variables bound in a clause do not leak to the outer context:
Notice that variables bound in a clause "head" do not leak to the
outer context:
case data do
{:ok, value} -> value
:error -> nil
end
value
#=> unbound variable value
value #=> unbound variable value
When binding variables with the same names as variables in the outer context,
the variables in the outer context are not affected.
However, variables explicitly bound in the clause "body" are
accessible from the outer context:
value = 7
case lucky? do
false -> value = 13
true -> true
true -> true
end
value
#=> 7
value #=> 7 or 13
In the example above, `value` is going to be `7` regardless of the value of
`lucky?`. The variable `value` bound in the clause and the variable `value`
bound in the outer context are two entirely separate variables.
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`.
If you want to pattern match against an existing variable,
you need to use the `^/1` operator:
@@ -1779,7 +1661,7 @@ defmodule Kernel.SpecialForms do
case 10 do
^x -> "Won't match"
_ -> "Will match"
_ -> "Will match"
end
#=> "Will match"
@@ -1825,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`
@@ -1842,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
@@ -1928,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
@@ -1940,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
@@ -2086,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
@@ -2120,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
@@ -2132,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.
@@ -2152,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`.
File diff suppressed because it is too large Load Diff
+26 -55
View File
@@ -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} =
@@ -94,9 +94,6 @@ defmodule Kernel.Utils do
fields = :lists.map(mapper, fields)
enforce_keys = List.wrap(Module.get_attribute(module, :enforce_keys))
# TODO: Make it raise on v2.0
warn_on_duplicate_struct_key(:lists.keysort(1, fields))
foreach = fn
key when is_atom(key) ->
:ok
@@ -111,26 +108,13 @@ defmodule Kernel.Utils do
{struct, enforce_keys, Module.get_attribute(module, :derive)}
end
defp warn_on_duplicate_struct_key([]) do
:ok
end
defp warn_on_duplicate_struct_key([{key, _} | [{key, _} | _] = rest]) do
IO.warn("duplicate key #{inspect(key)} found in struct")
warn_on_duplicate_struct_key(rest)
end
defp warn_on_duplicate_struct_key([_ | rest]) do
warn_on_duplicate_struct_key(rest)
end
@doc """
Announcing callback for defstruct.
"""
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
@@ -173,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
"""
@@ -246,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
@@ -288,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
+129 -192
View File
@@ -1,10 +1,10 @@
defmodule Keyword do
@moduledoc """
Keyword lists are lists of two-element tuples, where the first
element of the tuple is an atom and the second element can be any
value, used mostly to work with optional values.
A set of functions for working with keywords.
## Examples
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.
For example, the following is a keyword list:
@@ -15,68 +15,55 @@ defmodule Keyword do
[exit_on_close: true, active: :once, packet_size: 1024]
The two syntaxes are completely equivalent. Like atoms, keyword
lists keys 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:
This is also the syntax that Elixir uses to inspect keyword lists:
iex> [{:active, :once}]
[active: :once]
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. Keyword lists
keys are always atoms. If you use quotes around the key when quoting
is not necessary, Elixir will warn.
Wrapping a keyword in quotes does not make it a string. Keywords are
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.
## Duplicate keys and ordering
A keyword may have duplicated keys so it is not strictly a key-value
data type. However most of the functions in this module behave exactly
as a key-value so they work similarly to the functions you would find
in the `Map` module. For example, `Keyword.get/3` will get the first
entry matching the given key, regardless if duplicated entries exist.
Similarly, `Keyword.put/3` and `Keyword.delete/2` ensure all duplicated
entries for a given key are removed when invoked. Note however that
keyword list operations need to traverse the list in order to find
keys, so these operations are slower than their map counterparts.
A handful of functions exist to handle duplicated keys, for example,
`get_values/2` returns all values for a given key and `delete_first/2`
deletes just one of the existing entries.
The functions in `Keyword` do not guarantee any property when 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.
## Call syntax
When keyword lists are passed as the last argument to a function, then
the square brackets around the keyword list can be omitted as well. For
example, the keyword list syntax:
String.split("1-0", "-", [trim: true, parts: 2])
can be written without the enclosing brackets whenever it is the last
argument of a function call:
Note that when keyword lists are passed as the last argument to a function,
if the short-hand syntax is used then the square brackets around the keyword list
can be omitted as well. For example, the following:
String.split("1-0", "-", trim: true, parts: 2)
Since tuples, lists, maps, and others are treated the same as function
calls in Elixir syntax, this property is also available to them:
is equivalent to:
iex> {1, 2, foo: :bar}
{1, 2, [{:foo, :bar}]}
String.split("1-0", "-", [trim: true, parts: 2])
iex> [1, 2, foo: :bar]
[1, 2, {:foo, :bar}]
A keyword may have duplicated keys so it is not strictly
a key-value store. However most of the functions in this module
behave exactly as a dictionary so they work similarly to
the functions you would find in the `Map` module.
iex> %{1 => 2, foo: :bar}
%{1 => 2, :foo => :bar}
For example, `Keyword.get/3` will get the first entry matching
the given key, regardless if duplicated entries exist.
Similarly, `Keyword.put/3` and `Keyword.delete/3` ensure all
duplicated entries for a given key are removed when invoked.
Note that operations that require keys to be found in the keyword
list (like `Keyword.get/3`) need to traverse the list in order
to find keys, so these operations may be slower than their map
counterparts.
A handful of functions exist to handle duplicated keys, in
particular, `Enum.into/2` allows creating new keywords without
removing duplicated keys, `get_values/2` returns all values for
a given key and `delete_first/2` deletes just one of the existing
entries.
The functions in `Keyword` do not guarantee any property when
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.
"""
@compile :inline_list_funcs
@@ -94,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
@@ -126,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)`
@@ -147,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)`,
@@ -160,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)
@@ -332,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
@@ -392,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
@@ -415,12 +402,13 @@ defmodule Keyword do
"""
@spec get_values(t, key) :: [value]
def get_values(keywords, key) when is_list(keywords) and is_atom(key) do
get_values(keywords, key, [])
end
fun = fn
{^key, val} -> {true, val}
{_, _} -> false
end
defp get_values([{key, value} | tail], key, values), do: get_values(tail, key, [value | values])
defp get_values([{_, _} | tail], key, values), do: get_values(tail, key, values)
defp get_values([], _key, values), do: :lists.reverse(values)
:lists.filtermap(fun, keywords)
end
@doc """
Returns all keys from the keyword list.
@@ -429,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]
"""
@@ -447,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]
"""
@@ -458,8 +446,24 @@ defmodule Keyword do
:lists.map(fn {_, v} -> v end, keywords)
end
@doc false
@deprecated "Use Keyword.fetch/2 + Keyword.delete/2 instead"
@doc """
Deletes the entries in the keyword list for a `key` with `value`.
If no `key` with `value` exists, returns the keyword list unchanged.
## Examples
iex> Keyword.delete([a: 1, b: 2], :a, 1)
[b: 2]
iex> Keyword.delete([a: 1, b: 2, a: 3], :a, 3)
[a: 1, b: 2]
iex> Keyword.delete([a: 1], :a, 5)
[a: 1]
iex> Keyword.delete([a: 1], :b, 5)
[a: 1]
"""
@spec delete(t, key, value) :: t
def delete(keywords, key, value) when is_list(keywords) and is_atom(key) do
case :lists.keymember(key, 1, keywords) do
true -> delete_key_value(keywords, key, value)
@@ -505,9 +509,17 @@ defmodule Keyword do
end
end
defp delete_key([{key, _} | tail], key), do: delete_key(tail, key)
defp delete_key([{_, _} = pair | tail], key), do: [pair | delete_key(tail, key)]
defp delete_key([], _key), do: []
defp delete_key([{key, _} | tail], key) do
delete_key(tail, key)
end
defp delete_key([{_, _} = pair | tail], key) do
[pair | delete_key(tail, key)]
end
defp delete_key([], _key) do
[]
end
@doc """
Deletes the first entry in the keyword list for a specific `key`.
@@ -629,10 +641,8 @@ defmodule Keyword do
## Examples
iex> Keyword.replace!([a: 1, b: 2, a: 3], :a, :new)
[a: :new, b: 2]
iex> Keyword.replace!([a: 1, b: 2, c: 3, b: 4], :b, :new)
[a: 1, b: :new, c: 3]
iex> Keyword.replace!([a: 1, b: 2, a: 4], :a, 3)
[a: 3, b: 2]
iex> Keyword.replace!([a: 1], :b, 2)
** (KeyError) key :b not found in: [a: 1]
@@ -641,19 +651,10 @@ defmodule Keyword do
@doc since: "1.5.0"
@spec replace!(t, key, value) :: t
def replace!(keywords, key, value) when is_list(keywords) and is_atom(key) do
replace!(keywords, key, value, keywords)
end
defp replace!([{key, _} | keywords], key, value, _original) do
[{key, value} | delete(keywords, key)]
end
defp replace!([{_, _} = e | keywords], key, value, original) do
[e | replace!(keywords, key, value, original)]
end
defp replace!([], key, _value, original) when is_atom(key) do
raise(KeyError, key: key, term: original)
case :lists.keyfind(key, 1, keywords) do
{^key, _} -> [{key, value} | delete(keywords, key)]
false -> raise KeyError, key: key, term: keywords
end
end
@doc """
@@ -700,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
@@ -711,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
@@ -740,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]
@@ -762,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
@@ -784,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 """
@@ -813,31 +814,30 @@ defmodule Keyword do
## Examples
iex> Keyword.update!([a: 1, b: 2, a: 3], :a, &(&1 * 2))
[a: 2, b: 2]
iex> Keyword.update!([a: 1, b: 2, c: 3], :b, &(&1 * 2))
[a: 1, b: 4, c: 3]
iex> Keyword.update!([a: 1], :a, &(&1 * 2))
[a: 2]
iex> Keyword.update!([a: 1, a: 2], :a, &(&1 * 2))
[a: 2]
iex> Keyword.update!([a: 1], :b, &(&1 * 2))
** (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
defp update!([{key, value} | keywords], key, fun, _original) do
defp update!([{key, value} | keywords], key, fun, _dict) do
[{key, fun.(value)} | delete(keywords, key)]
end
defp update!([{_, _} = e | keywords], key, fun, original) do
[e | update!(keywords, key, fun, original)]
defp update!([{_, _} = e | keywords], key, fun, dict) do
[e | update!(keywords, key, fun, dict)]
end
defp update!([], key, _fun, original) when is_atom(key) do
raise(KeyError, key: key, term: original)
defp update!([], key, _fun, dict) when is_atom(key) do
raise(KeyError, key: key, term: dict)
end
@doc """
@@ -892,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}
@@ -920,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
@@ -938,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
@@ -966,75 +961,16 @@ 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)}
:error -> {default, keywords}
{:ok, value} ->
{value, delete(keywords, key)}
:error ->
{default, keywords}
end
end
@doc """
Returns the first value for `key` and removes all associated antries in the keyword list,
raising if `key` is not present.
This function behaves like `pop/3`, but raises in cases the `key` is not present in the
given `keywords`.
## Examples
iex> Keyword.pop!([a: 1], :a)
{1, []}
iex> Keyword.pop!([a: 1, a: 2], :a)
{1, []}
iex> Keyword.pop!([a: 1], :b)
** (KeyError) key :b not found in: [a: 1]
"""
@doc since: "1.10.0"
@spec pop!(t, key) :: {value, t}
def pop!(keywords, key) when is_list(keywords) and is_atom(key) do
case fetch(keywords, key) do
{:ok, value} -> {value, delete(keywords, key)}
:error -> raise KeyError, key: key, term: keywords
end
end
@doc """
Returns all values for `key` and removes all associated entries in the keyword list.
It returns a tuple where the first element is a list of values 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, `{[], 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.
## Examples
iex> Keyword.pop_values([a: 1], :a)
{[1], []}
iex> Keyword.pop_values([a: 1], :b)
{[], [a: 1]}
iex> Keyword.pop_values([a: 1, a: 2], :a)
{[1, 2], []}
"""
@doc since: "1.10.0"
@spec pop_values(t, key) :: {[value], t}
def pop_values(keywords, key) when is_list(keywords) and is_atom(key) do
pop_values(:lists.reverse(keywords), key, [], [])
end
defp pop_values([{key, value} | tail], key, values, acc),
do: pop_values(tail, key, [value | values], acc)
defp pop_values([{_, _} = pair | tail], key, values, acc),
do: pop_values(tail, key, values, [pair | acc])
defp pop_values([], _key, values, acc),
do: {values, acc}
@doc """
Lazily returns and removes all values associated with `key` in the keyword list.
@@ -1059,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)}
@@ -1087,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}
@@ -1099,7 +1035,7 @@ defmodule Keyword do
## Examples
iex> Keyword.to_list(a: 1)
iex> Keyword.to_list([a: 1])
[a: 1]
"""
@@ -1109,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)
+158 -348
View File
@@ -1,6 +1,19 @@
defmodule List do
@moduledoc """
Linked lists hold zero, one, or more elements in the choosen order.
Functions that work on (linked) lists.
Many of the functions provided for lists, which implement
the `Enumerable` protocol, are found in the `Enum` module.
Additionally, the following functions and operators for lists are
found in `Kernel`:
* `++/2`
* `--/2`
* `hd/1`
* `tl/1`
* `in/2`
* `length/1`
Lists in Elixir are specified between square brackets:
@@ -50,84 +63,48 @@ 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.
Lists also implement the `Enumerable` protocol, so many functions to work with
lists are found in the `Enum` module. Additionally, the following functions and
operators for lists are found in `Kernel`:
* `++/2`
* `--/2`
* `hd/1`
* `tl/1`
* `in/2`
* `length/1`
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'
Even though the representation changed, the raw data does remain a list of
numbers, which can be handled as such:
iex> inspect('abc', charlists: :as_list)
"[97, 98, 99]"
iex> Enum.map('abc', fn num -> 1000 + num end)
[1097, 1098, 1099]
You can use the `IEx.Helpers.i/1` helper to get a condensed rundown on
charlists in IEx when you encounter them, which shows you the type, description
and also the raw representation in one single summary.
The rationale behind this behaviour is to better support
Erlang libraries which may return text as charlists
instead of Elixir strings. In Erlang, charlists are the default
way of handling strings, while in Elixir it's binaries. One
example of such functions is `Application.loaded_applications/0`:
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
@@ -135,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
@@ -183,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
@@ -204,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
@@ -269,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
@@ -289,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)
@@ -321,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
@@ -342,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
@@ -360,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
@@ -378,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
@@ -399,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
@@ -441,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
@@ -464,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
@@ -500,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')
@@ -533,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`.
@@ -607,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
@@ -645,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
@@ -678,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
@@ -761,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)
@@ -773,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
@@ -796,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.
@@ -807,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
@@ -857,8 +770,6 @@ defmodule List do
Inlined by the compiler.
The base needs to be between `2` and `36`.
## Examples
iex> List.to_integer('3FF', 16)
@@ -887,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
Unicode 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
@@ -912,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
@@ -925,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 Unicode code points, lists or
strings into a charlist.
Notice that this function expects a list of integers representing
Unicode code points. If you have a list of bytes, you must instead use
the [`:binary` module](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:
@@ -989,7 +837,7 @@ defmodule List do
#{inspect(list)}
"""
else
result when is_list(result) ->
result when is_binary(result) ->
result
{:error, encoded, rest} ->
@@ -1013,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])
@@ -1024,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
@@ -1079,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
@@ -1164,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
@@ -1178,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
@@ -1192,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
@@ -1206,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
+1 -2
View File
@@ -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)
@@ -24,8 +25,6 @@ defprotocol List.Chars do
end
defimpl List.Chars, for: Atom do
def to_charlist(nil), do: ''
def to_charlist(atom), do: Atom.to_charlist(atom)
end
+107 -325
View File
@@ -2,62 +2,16 @@ import Kernel, except: [to_string: 1]
defmodule Macro do
@moduledoc ~S"""
Macros are compile-time constructs that are invoked with Elixir's AST
as input and a superset of Elixir's AST as output.
Let's see a simple example that shows the difference between functions and macros:
defmodule Example do
defmacro macro_inspect(value) do
IO.inspect(value)
value
end
def fun_inspect(value) do
IO.inpect(value)
value
end
end
Now let's give it a try:
import Example
macro_inspect(1)
#=> 1
#=> 1
fun_inspect(1)
#=> 1
#=> 1
So far they behave the same, as we are passing an integer as argument.
But what happens when we pass an expresion:
macro_inspect(1 + 2)
#=> {:+, [line: 3], [1, 2]}
#=> 3
fun_inspect(1 + 2)
#=> 3
#=> 3
The macro receives the representation of the code given as argument,
while a function receives the result of the code given as argument.
A macro must return a superset of the code representation. See
`t:input/0` and `t:output/0` for more information.
To learn more about Elixir's AST and how to build them programmatically,
see `quote/2`.
Conveniences for working with macros.
## Custom Sigils
Macros are also commonly used to implement custom sigils. To create a custom
sigil, define a function with the name `sigil_{identifier}` that takes two
arguments. The first argument will be the string, the second will be a charlist
containing any modifiers. If the sigil is lower case (such as `sigil_x`) then
the string argument will allow interpolation. If the sigil is upper case
(such as `sigil_X`) then the string will not be interpolated.
To create a custom sigil, define a function with the name
`sigil_{identifier}` that takes two arguments. The first argument will be
the string, the second will be a charlist containing any modifiers. If the
sigil is lower case (such as `sigil_x`) then the string argument will allow
interpolation. If the sigil is upper case (such as `sigil_X`) then the string
will not be interpolated.
Valid modifiers include only lower and upper case letters. Other characters
will cause a syntax error.
@@ -105,94 +59,13 @@ defmodule Macro do
alias Code.Identifier
@typedoc "Abstract Syntax Tree (AST)"
@type t :: input
@type t :: expr | literal
@typedoc "The inputs of a macro"
@type input ::
input_expr
| {input, input}
| [input]
| atom
| number
| binary
@typedoc "Represents expressions in the AST"
@type expr :: {expr | atom, keyword, atom | [t]}
@typep input_expr :: {input_expr | atom, metadata, atom | [input]}
@typedoc "The output of a macro"
@type output ::
output_expr
| {output, output}
| [output]
| atom
| number
| binary
| captured_remote_function
| pid
@typep output_expr :: {output_expr | atom, metadata, atom | [output]}
@typedoc """
A keyword list of AST metadata.
The metadata in Elixir AST is a keyword list of values. Any key can be used
and different parts of the compiler may use different keys. For example,
the AST received by a macro will always include the `:line` annotation,
while the AST emitted by `quote/2` will only have the `:line` annotation if
the `:line` option is provided.
The following metadata keys are public:
* `:context` - Defines the context in which the AST was generated.
For example, `quote/2` will include the module calling `quote/2`
as the context. This is often used to distinguish regular code from code
generated by a macro or by `quote/2`.
* `:counter` - The variable counter used for variable hygiene. In terms of
the compiler, each variable is identified by the combination of either
`name` and `metadata[:counter]`, or `name` and `context`.
* `:generated` - Whether the code should be considered as generated by
the compiler or not. This means the compiler and tools like Dialyzer may not
emit certain warnings.
* `:keep` - Used by `quote/2` with the option `location: :keep` to annotate
the file and the line number of the quoted source.
* `:line` - The line number of the AST node.
The following metadata keys are enabled by `Code.string_to_quoted/2`:
* `:closing` - contains metadata about the closing pair, such as a `}`
in a tuple or in a map, or such as the closing `)` in a function call
with parens. The `:closing` does not delimit the end of expression if
there are `:do` and `:end` metadata (when `:token_metadata` is true)
* `:column` - the column number of the AST node (when `:columns` is true)
* `:delimiter` - contains the opening delimiter for sigils, strings,
and charlists as a string (such as `"{"`, `"/"`, `"'"`, and the like)
* `:format` - set to `:keyword` when an atom is defined as a keyword
* `:do` - contains metadata about the `do` location in a function call with
`do/end` blocks (when `:token_metadata` is true)
* `:end` - contains metadata about the `end` location in a function call with
`do/end` blocks (when `:token_metadata` is true)
* `:end_of_expression` - denotes when the end of expression effectively
happens. Available for all expressions except the last one inside a
`__block__` (when `:token_metadata` is true)
The following metadata keys are private:
* `:alias` - Used for alias hygiene.
* `:ambiguous_op` - Used for improved error messages in the compiler.
* `:import` - Used for import hygiene.
* `:var` - Used for improved error messages on undefined variables.
Do not rely on them as they may change or be fully removed in future versions
of the language. They are often used by `quote/2` and the compiler to provide
features like hygiene, better error messages, and so forth.
If you introduce custom keys into the AST metadata, please make sure to prefix
them with the name of your library or application, so that they will not conflict
with keys that could potentially be introduced by the compiler in the future.
"""
@type metadata :: keyword
@typedoc "A captured remote function in the format of &Mod.fun/arity"
@type captured_remote_function :: fun
@typedoc "Represents literals in the AST"
@type literal :: atom | number | binary | fun | {t, t} | [t]
@doc """
Breaks a pipeline expression into a list.
@@ -222,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
@@ -238,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
@@ -258,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, for example, `:foo |> (fn x -> x end)`.
# 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
@@ -277,26 +146,13 @@ defmodule Macro do
{call, line, List.insert_at([], integer, expr)}
end
def pipe(_expr, {op, _line, [arg]}, _integer) when op == :+ or op == :- do
raise ArgumentError,
"piping into a unary operator is not supported, please use the qualified name: " <>
"Kernel.#{op}(#{to_string(arg)}), instead of #{op}#{to_string(arg)}"
end
def pipe(expr, {op, line, args} = op_args, integer) when is_list(args) do
cond do
is_atom(op) and Identifier.unary_op(op) != :error ->
raise ArgumentError,
"cannot pipe #{to_string(expr)} into #{to_string(op_args)}, " <>
"the #{to_string(op)} operator can only take one argument"
is_atom(op) and Identifier.binary_op(op) != :error ->
raise ArgumentError,
"cannot pipe #{to_string(expr)} into #{to_string(op_args)}, " <>
"the #{to_string(op)} operator can only take two arguments"
true ->
{op, line, List.insert_at(args, integer, expr)}
def pipe(expr, {call, line, args} = call_args, integer) when is_list(args) do
if is_atom(call) and Identifier.binary_op(call) != :error do
raise ArgumentError,
"cannot pipe #{to_string(expr)} into #{to_string(call_args)}, " <>
"the #{to_string(call)} operator can only take two arguments"
else
{call, line, List.insert_at(args, integer, expr)}
end
end
@@ -306,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 """
@@ -342,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 """
@@ -490,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})
@@ -528,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
@@ -537,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
@@ -563,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 """
@@ -646,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
@@ -675,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
@@ -691,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()
@@ -753,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
@@ -895,16 +729,6 @@ defmodule Macro do
end
# All other calls
def to_string({target, meta, []} = ast, fun) do
target = call_to_string(target, fun)
if meta[:no_parens] do
fun.(ast, target)
else
fun.(ast, target <> "()")
end
end
def to_string({target, _, args} = ast, fun) when is_list(args) do
with :error <- unary_call(ast, fun),
:error <- binary_call(ast, fun),
@@ -912,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)
@@ -962,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
@@ -998,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)
@@ -1008,33 +827,20 @@ defmodule Macro do
false
end
defp interpolate(ast, fun), do: interpolate(ast, "\"", "\"", fun)
defp interpolate({:<<>>, _, [parts]}, left, right, _) when left in [~s["""\n], ~s['''\n]] do
<<left::binary, parts::binary, right::binary>>
end
defp interpolate({:<<>>, _, parts}, left, right, fun) do
defp interpolate({:<<>>, _, parts}, fun) do
parts =
Enum.map_join(parts, "", fn
{:"::", _, [{{:., _, [Kernel, :to_string]}, _, [arg]}, {:binary, _, _}]} ->
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [arg]}, {:binary, _, _}]} ->
"\#{" <> to_string(arg, fun) <> "}"
binary when is_binary(binary) ->
binary = inspect_no_limit(binary)
binary = binary_part(binary, 1, byte_size(binary) - 2)
escape_sigil(binary, left)
binary_part(binary, 1, byte_size(binary) - 2)
end)
<<left::binary, parts::binary, right::binary>>
<<?", parts::binary, ?">>
end
defp escape_sigil(parts, "("), do: String.replace(parts, ")", ~S"\)")
defp escape_sigil(parts, "{"), do: String.replace(parts, "}", ~S"\}")
defp escape_sigil(parts, "["), do: String.replace(parts, "]", ~S"\]")
defp escape_sigil(parts, "<"), do: String.replace(parts, ">", ~S"\>")
defp escape_sigil(parts, delimiter), do: String.replace(parts, delimiter, "\\#{delimiter}")
defp module_to_string(atom, _fun) when is_atom(atom) do
inspect_no_limit(atom)
end
@@ -1094,22 +900,25 @@ defmodule Macro do
:error
end
defp sigil_call({sigil, meta, [{:<<>>, _, _} = parts, args]} = ast, fun)
defp sigil_call({sigil, _, [{:<<>>, _, _} = parts, args]} = ast, fun)
when is_atom(sigil) and is_list(args) do
delimiter = Keyword.get(meta, :delimiter, "\"")
{left, right} = delimiter_pair(delimiter)
case Atom.to_string(sigil) do
<<"sigil_", name>> when name >= ?A and name <= ?Z ->
args = sigil_args(args, fun)
{:<<>>, _, [binary]} = parts
formatted = <<?~, name, left::binary, binary::binary, right::binary, args::binary>>
formatted =
if :binary.last(binary) == ?\n do
binary = String.replace(binary, ~s["""], ~s["\\""])
<<?~, name, ~s["""\n], binary::binary, ~s["""], sigil_args(args, fun)::binary>>
else
{left, right} = select_sigil_container(binary)
<<?~, name, left, binary::binary, right, sigil_args(args, fun)::binary>>
end
{:ok, fun.(ast, formatted)}
<<"sigil_", name>> when name >= ?a and name <= ?z ->
args = sigil_args(args, fun)
formatted = "~" <> <<name>> <> interpolate(parts, left, right, fun) <> args
{:ok, fun.(ast, formatted)}
{:ok, fun.(ast, "~" <> <<name>> <> interpolate(parts, fun) <> sigil_args(args, fun))}
_ ->
:error
@@ -1120,13 +929,17 @@ defmodule Macro do
:error
end
defp delimiter_pair("["), do: {"[", "]"}
defp delimiter_pair("{"), do: {"{", "}"}
defp delimiter_pair("("), do: {"(", ")"}
defp delimiter_pair("<"), do: {"<", ">"}
defp delimiter_pair("\"\"\""), do: {"\"\"\"\n", "\"\"\""}
defp delimiter_pair("'''"), do: {"'''\n", "'''"}
defp delimiter_pair(str), do: {str, str}
defp select_sigil_container(binary) do
cond do
:binary.match(binary, ["\""]) == :nomatch -> {?", ?"}
:binary.match(binary, ["\'"]) == :nomatch -> {?', ?'}
:binary.match(binary, ["(", ")"]) == :nomatch -> {?(, ?)}
:binary.match(binary, ["[", "]"]) == :nomatch -> {?[, ?]}
:binary.match(binary, ["{", "}"]) == :nomatch -> {?{, ?}}
:binary.match(binary, ["<", ">"]) == :nomatch -> {?<, ?>}
true -> {?/, ?/}
end
end
defp sigil_args([], _fun), do: ""
defp sigil_args(args, fun), do: fun.(args, List.to_string(args))
@@ -1298,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
@@ -1354,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
@@ -1369,10 +1180,10 @@ defmodule Macro do
elem(do_expand_once(ast, env), 0)
end
defp do_expand_once({:__aliases__, meta, _} = original, env) do
case :elixir_aliases.expand(original, env) do
defp do_expand_once({:__aliases__, _, _} = original, env) do
case :elixir_aliases.expand(original, env.aliases, env.macro_aliases, env.lexical_tracker) do
receiver when is_atom(receiver) ->
:elixir_env.trace({:alias_reference, meta, receiver}, env)
:elixir_lexical.record_remote(receiver, env.function, env.lexical_tracker)
{receiver, true}
aliases ->
@@ -1381,7 +1192,7 @@ defmodule Macro do
case :lists.all(&is_atom/1, aliases) do
true ->
receiver = :elixir_aliases.concat(aliases)
:elixir_env.trace({:alias_reference, meta, receiver}, env)
:elixir_lexical.record_remote(receiver, env.function, env.lexical_tracker)
{receiver, true}
false ->
@@ -1408,9 +1219,17 @@ defmodule Macro do
end
end
defp do_expand_once({atom, meta, context} = original, _env)
# Expand possible macro import invocation
defp do_expand_once({atom, meta, context} = original, env)
when is_atom(atom) and is_list(meta) and is_atom(context) do
{original, false}
if Macro.Env.has_var?(env, {atom, Keyword.get(meta, :counter, context)}) do
{original, false}
else
case do_expand_once({atom, meta, []}, env) do
{_, true} = exp -> exp
{_, false} -> {original, false}
end
end
end
defp do_expand_once({atom, meta, args} = original, env)
@@ -1433,15 +1252,9 @@ defmodule Macro do
case expand do
{:ok, receiver, quoted} ->
next = :elixir_module.next_counter(module)
next = :erlang.unique_integer()
{:elixir_quote.linify_with_context_counter(0, {receiver, next}, quoted), true}
{:ok, Kernel, op, [arg]} when op in [:+, :-] ->
case expand_once(arg, env) do
integer when is_integer(integer) -> {apply(Kernel, op, [integer]), true}
_ -> {original, false}
end
{:ok, _receiver, _name, _args} ->
{original, false}
@@ -1464,7 +1277,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 ->
@@ -1495,67 +1308,37 @@ defmodule Macro do
def operator?(name, arity) when is_atom(name) and is_integer(arity), do: false
@doc """
Returns `true` if the given quoted expression represents a quoted literal.
Atoms, numbers, and functions are always literals. Binaries, lists, tuples,
maps, and structs are only literals if all of their terms are also literals.
## Examples
iex> Macro.quoted_literal?(quote(do: "foo"))
true
iex> Macro.quoted_literal?(quote(do: {"foo", 1}))
true
iex> Macro.quoted_literal?(quote(do: {"foo", 1, :baz}))
true
iex> Macro.quoted_literal?(quote(do: %{foo: "bar"}))
true
iex> Macro.quoted_literal?(quote(do: %URI{path: "/"}))
true
iex> Macro.quoted_literal?(quote(do: URI.parse("/")))
false
iex> Macro.quoted_literal?(quote(do: {foo, var}))
false
Returns `true` if the given quoted expression is an AST literal.
"""
@doc since: "1.7.0"
@spec quoted_literal?(t) :: boolean
@spec quoted_literal?(literal) :: true
@spec quoted_literal?(expr) :: false
def quoted_literal?(term)
def quoted_literal?({:__aliases__, _, args}),
do: quoted_literal?(args)
def quoted_literal?({:%, _, [left, right]}),
do: quoted_literal?(left) and quoted_literal?(right)
def quoted_literal?({:%{}, _, args}), do: quoted_literal?(args)
def quoted_literal?({:{}, _, args}), do: quoted_literal?(args)
def quoted_literal?({left, right}), do: quoted_literal?(left) and quoted_literal?(right)
def quoted_literal?(list) when is_list(list), do: Enum.all?(list, &quoted_literal?/1)
def quoted_literal?(term),
do: is_atom(term) or is_number(term) or is_binary(term) or is_function(term)
def quoted_literal?(term) do
is_atom(term) or is_number(term) or is_binary(term) or is_function(term)
end
@doc """
Receives an AST node and expands it until it can no longer
be expanded.
Note this function does not traverse the AST, only the root
node is expanded.
This function uses `expand_once/2` under the hood. Check
it out for more information and examples.
"""
def expand(ast, env) do
expand_until({ast, true}, env)
def expand(tree, env) do
expand_until({tree, true}, env)
end
defp expand_until({ast, true}, env) do
expand_until(do_expand_once(ast, env), env)
defp expand_until({tree, true}, env) do
expand_until(do_expand_once(tree, env), env)
end
defp expand_until({ast, false}, _env) do
ast
defp expand_until({tree, false}, _env) do
tree
end
@doc """
@@ -1593,7 +1376,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)
+69 -80
View File
@@ -21,31 +21,31 @@ defmodule Macro.Env do
It contains the following fields:
* `aliases` - a list of two-element tuples, where the first
element is the aliased name and the second one the actual name
* `context` - the context of the environment; it can be `nil`
(default context), `:guard` (inside a guard) or `:match` (inside a match)
* `context_modules` - a list of modules defined in the current context
* `module` - the current module name
* `file` - the current file name as a binary
* `line` - the current line as an integer
* `function` - a tuple as `{atom, integer}`, where the first
element is the function name and the second its arity; returns
`nil` if not inside a function
* `functions` - a list of functions imported from each module
* `line` - the current line as an integer
* `macro_aliases` - a list of aliases defined inside the current macro
* `macros` - a list of macros imported from each module
* `module` - the current module name
* `context` - the context of the environment; it can be `nil`
(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
* `functions` - a list of functions imported from each module
* `macros` - a list of macros imported from each module
* `macro_aliases` - a list of aliases defined inside the current macro
* `context_modules` - a list of modules defined in the current context
* `lexical_tracker` - PID of the lexical tracker which is responsible for
keeping user info
The following fields are private to Elixir's macro expansion mechanism and
must not be accessed directly:
The following fields pertain to variable handling and must not be accessed or
relied on. To get a list of all variables, see `vars/1`:
* `contextual_vars`
* `current_vars`
* `lexical_tracker`
* `prematch_vars`
* `tracers`
* `unused_vars`
* `prematch_vars`
* `contextual_vars`
The following fields are deprecated and must not be accessed or relied on:
@@ -53,80 +53,69 @@ defmodule Macro.Env do
"""
@type aliases :: [{module, module}]
@type context :: :match | :guard | nil
@type context_modules :: [module]
@type file :: binary
@type functions :: [{module, [name_arity]}]
@type lexical_tracker :: pid | nil
@type line :: non_neg_integer
@type macro_aliases :: [{module, {term, module}}]
@type macros :: [{module, [name_arity]}]
@type name_arity :: {atom, arity}
@type file :: binary
@type line :: non_neg_integer
@type aliases :: [{module, module}]
@type macro_aliases :: [{module, {integer, module}}]
@type context :: :match | :guard | nil
@type requires :: [module]
@type variable :: {atom, atom | term}
@type functions :: [{module, [name_arity]}]
@type macros :: [{module, [name_arity]}]
@type context_modules :: [module]
@type lexical_tracker :: pid | nil
@type var :: {atom, atom | non_neg_integer}
@typep contextual_vars :: [atom]
@typep current_vars ::
{%{optional(variable) => {var_version, var_type}},
%{optional(variable) => {var_version, var_type}} | false}
@typep unused_vars ::
{%{optional({atom, var_version}) => non_neg_integer | false}, non_neg_integer}
@typep prematch_vars ::
{%{optional(variable) => {var_version, var_type}}, non_neg_integer}
| :warn
| :raise
| :pin
| :apply
@typep tracers :: [module]
@typep vars :: [var]
@typep var_type :: :term
@typep var_version :: non_neg_integer
@typep vars :: [variable]
@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]
@type t :: %{
__struct__: __MODULE__,
aliases: aliases,
context: context,
context_modules: context_modules,
contextual_vars: contextual_vars,
current_vars: current_vars,
file: file,
function: name_arity | nil,
functions: functions,
lexical_tracker: lexical_tracker,
line: line,
macro_aliases: macro_aliases,
macros: macros,
module: atom,
prematch_vars: prematch_vars,
unused_vars: unused_vars,
file: file,
line: line,
function: name_arity | nil,
context: context,
requires: requires,
tracers: tracers,
vars: vars
aliases: aliases,
functions: functions,
macros: macros,
macro_aliases: aliases,
context_modules: context_modules,
vars: vars,
unused_vars: unused_vars,
current_vars: current_vars,
prematch_vars: prematch_vars,
lexical_tracker: lexical_tracker,
contextual_vars: contextual_vars
}
# TODO: Remove :vars field on v2.0
def __struct__ do
%{
__struct__: __MODULE__,
aliases: [],
context: nil,
context_modules: [],
contextual_vars: [],
current_vars: {%{}, %{}},
file: "nofile",
function: nil,
functions: [],
lexical_tracker: nil,
line: 0,
macro_aliases: [],
macros: [],
module: nil,
prematch_vars: :warn,
file: "nofile",
line: 0,
function: nil,
context: nil,
requires: [],
tracers: [],
unused_vars: {%{}, 0},
vars: []
aliases: [],
functions: [],
macros: [],
macro_aliases: [],
context_modules: [],
vars: [],
unused_vars: %{},
current_vars: %{},
prematch_vars: :warn,
lexical_tracker: nil,
contextual_vars: []
}
end
@@ -143,22 +132,22 @@ 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: {read, _}}) do
Map.keys(read)
def vars(%{__struct__: Macro.Env, current_vars: current_vars}) do
Map.keys(current_vars)
end
@doc """
Checks if a variable belongs to the environment.
"""
@doc since: "1.7.0"
@spec has_var?(t, variable) :: boolean()
@spec has_var?(t, var) :: boolean()
def has_var?(env, var)
def has_var?(%{__struct__: Macro.Env, current_vars: {read, _}}, var) do
Map.has_key?(read, var)
def has_var?(%{__struct__: Macro.Env, current_vars: current_vars}, var) do
Map.has_key?(current_vars, var)
end
@doc """
@@ -180,8 +169,8 @@ defmodule Macro.Env do
env
end
def to_match(%{__struct__: Macro.Env, current_vars: {read, _}, unused_vars: {_, counter}} = env) do
%{env | context: :match, prematch_vars: {read, counter}}
def to_match(%{__struct__: Macro.Env, current_vars: vars} = env) do
%{env | context: :match, prematch_vars: vars}
end
@doc """
+45 -90
View File
@@ -1,9 +1,15 @@
defmodule Map do
@moduledoc """
Maps are the "go to" key-value data structure in Elixir.
A set of functions for working with maps.
Maps can be created with the `%{}` syntax, and key-value pairs can be
expressed as `key => value`:
Many functions for maps, which implement the `Enumerable` protocol,
are found in the `Enum` module. Additionally, the following functions
for maps are found in `Kernel`:
* `map_size/1`
Maps are the "go to" key-value data structure in Elixir. Maps can be created
with the `%{}` syntax, and key-value pairs can be expressed as `key => value`:
iex> %{}
%{}
@@ -85,19 +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.
Maps also implement the `Enumerable` protocol, so many functions to work with maps
are found in the `Enum` module. Additionally, the following functions for maps are
found in `Kernel`:
* `map_size/1`
"""
@type key :: any
@@ -172,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}
"""
@@ -213,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 """
@@ -265,9 +258,11 @@ defmodule Map do
iex> Map.fetch!(%{a: 1}, :a)
1
iex> Map.fetch!(%{a: 1}, :b)
** (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
@@ -341,8 +336,8 @@ defmodule Map do
in `map` unless `key` is already present.
This function is useful in case you want to compute the value to put under
`key` only if `key` is not already present, as for example, when the value is expensive to
calculate or generally difficult to setup and teardown again.
`key` only if `key` is not already present (e.g., the value is expensive to
calculate or generally difficult to setup and teardown again).
## Examples
@@ -383,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
@@ -421,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
@@ -634,31 +621,6 @@ defmodule Map do
end
end
@doc """
Returns and removes the value associated with `key` in `map` or raises
if `key` is not present.
Behaves the same as `pop/3` but raises if `key` is not present in `map`.
## Examples
iex> Map.pop!(%{a: 1}, :a)
{1, %{}}
iex> Map.pop!(%{a: 1, b: 2}, :a)
{1, %{b: 2}}
iex> Map.pop!(%{a: 1}, :b)
** (KeyError) key :b not found in: %{a: 1}
"""
@doc since: "1.10.0"
@spec pop!(map, key) :: {value, map}
def pop!(map, key) do
case :maps.take(key, map) do
{_, _} = tuple -> tuple
:error -> raise KeyError, key: key, term: map
end
end
@doc """
Lazily returns and removes the value associated with `key` in `map`.
@@ -685,9 +647,15 @@ defmodule Map do
"""
@spec pop_lazy(map, key, (() -> value)) :: {value, map}
def pop_lazy(map, key, fun) when is_function(fun, 0) do
case :maps.take(key, map) do
{_, _} = tuple -> tuple
:error -> {fun.(), map}
case map do
%{^key => value} ->
{value, delete(map, key)}
%{} ->
{fun.(), map}
other ->
:erlang.error({:badmap, other}, [map, key, fun])
end
end
@@ -702,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 """
@@ -742,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
@@ -870,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)
@@ -938,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)
+43 -59
View File
@@ -2,26 +2,14 @@ defmodule MapSet do
@moduledoc """
Functions that work on sets.
A set is a data structure that can contain unique elements of any kind,
without any particular order. `MapSet` is the "go to" set data structure in Elixir.
A set can be constructed using `MapSet.new/0`:
`MapSet` is the "go to" set data structure in Elixir. A set can be constructed
using `MapSet.new/0`:
iex> MapSet.new()
#MapSet<[]>
Elements in a set don't have to be of the same type and they can be
populated from an [enumerable](`t:Enumerable.t/0`) using `MapSet.new/1`:
iex> MapSet.new([1, :two, {"three"}])
#MapSet<[1, :two, {"three"}]>
Elements can be inserted using `MapSet.put/2`:
iex> MapSet.new([2]) |> MapSet.put(4) |> MapSet.put(0)
#MapSet<[0, 2, 4]>
By definition, sets can't contain duplicate elements: when
A set can contain any kind of elements, and elements in a set don't have to be
of the same type. By definition, sets can't contain duplicate elements: when
inserting an element in a set where it's already present, the insertion is
simply a no-op.
@@ -42,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,
@@ -57,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 """
@@ -117,19 +101,19 @@ defmodule MapSet do
end
defp new_from_list([], acc) do
Map.new(acc)
: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
Map.new(acc)
: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 """
@@ -163,33 +147,32 @@ defmodule MapSet do
@spec difference(t(val1), t(val2)) :: t(val1) when val1: value, val2: value
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 present in the second set.
# If the first set is less than twice the size of the second map,
# 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
map =
map1
|> :maps.iterator()
|> :maps.next()
|> Map.keys()
|> filter_not_in(map2, [])
%MapSet{map: map}
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))}
end
defp filter_not_in(:none, _map2, acc), do: Map.new(acc)
defp filter_not_in([], _map2, acc), do: :maps.from_list(acc)
defp filter_not_in({key, _val, iter}, map2, acc) do
if :erlang.is_map_key(key, map2) do
filter_not_in(:maps.next(iter), map2, acc)
else
filter_not_in(:maps.next(iter), map2, [{key, @dummy_value} | acc])
defp filter_not_in([key | rest], map2, acc) do
case map2 do
%{^key => _} -> filter_not_in(rest, map2, acc)
_ -> filter_not_in(rest, map2, [{key, @dummy_value} | acc])
end
end
@@ -209,15 +192,19 @@ defmodule MapSet do
{map1, map2} = order_by_size(map1, map2)
map1
|> :maps.iterator()
|> :maps.next()
|> Map.keys()
|> none_in?(map2)
end
defp none_in?(:none, _), do: true
defp none_in?([], _) do
true
end
defp none_in?({key, _val, iter}, map2) do
not :erlang.is_map_key(key, map2) and none_in?(:maps.next(iter), map2)
defp none_in?([key | rest], map2) do
case map2 do
%{^key => _} -> false
_ -> none_in?(rest, map2)
end
end
@doc """
@@ -241,7 +228,7 @@ defmodule MapSet do
# Elixir v1.5 change the map representation, so on
# version mismatch we need to compare the keys directly.
def equal?(%MapSet{map: map1}, %MapSet{map: map2}) do
map_size(map1) == map_size(map2) and all_in?(map1, map2)
map_size(map1) == map_size(map2) and map_subset?(Map.keys(map1), map2)
end
@doc """
@@ -275,7 +262,7 @@ defmodule MapSet do
"""
@spec member?(t, value) :: boolean
def member?(%MapSet{map: map}, value) do
:erlang.is_map_key(value, map)
match?(%{^value => _}, map)
end
@doc """
@@ -323,20 +310,19 @@ defmodule MapSet do
"""
@spec subset?(t, t) :: boolean
def subset?(%MapSet{map: map1}, %MapSet{map: map2}) do
map_size(map1) <= map_size(map2) and all_in?(map1, map2)
if map_size(map1) <= map_size(map2) do
map1
|> Map.keys()
|> map_subset?(map2)
else
false
end
end
defp all_in?(:none, _), do: true
defp map_subset?([], _), do: true
defp all_in?({key, _val, iter}, map2) do
:erlang.is_map_key(key, map2) and all_in?(:maps.next(iter), map2)
end
defp all_in?(map1, map2) when is_map(map1) and is_map(map2) do
map1
|> :maps.iterator()
|> :maps.next()
|> all_in?(map2)
defp map_subset?([key | rest], map2) do
match?(%{^key => _}, map2) and map_subset?(rest, map2)
end
@doc """
@@ -388,8 +374,7 @@ defmodule MapSet do
end
def slice(map_set) do
size = MapSet.size(map_set)
{:ok, size, &Enumerable.List.slice(MapSet.to_list(map_set), &1, &2, size)}
{:ok, MapSet.size(map_set), &Enumerable.List.slice(MapSet.to_list(map_set), &1, &2)}
end
def reduce(map_set, acc, fun) do
@@ -413,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
+165 -319
View File
@@ -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:
@@ -56,33 +55,19 @@ defmodule Module do
If the behaviour changes or `URI.HTTP` does not implement
one of the callbacks, a warning will be raised.
For detailed documentation, see the
[behaviour typespec documentation](typespecs.html#behaviours).
### `@impl`
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
@@ -97,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.
@@ -140,12 +123,12 @@ defmodule Module do
end
The Mix compiler automatically looks for calls to deprecated modules
and emit warnings during compilation.
and emit warnings during compilation, computed via `mix xref warnings`.
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:
@@ -178,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"
@@ -201,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
@@ -216,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
@@ -277,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
@@ -300,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
@@ -348,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"]
@@ -373,7 +348,7 @@ defmodule Module do
When just a module is provided, the function is assumed to be
`__after_compile__/2`.
Callbacks will run in the order they are registered.
Callbacks registered first will run last.
#### Example
@@ -381,7 +356,7 @@ defmodule Module do
@after_compile __MODULE__
def __after_compile__(env, _bytecode) do
IO.inspect(env)
IO.inspect env
end
end
@@ -397,11 +372,10 @@ defmodule Module do
When just a module is provided, the function/macro is assumed to be
`__before_compile__/1`.
Callbacks will run in the order they are registered. Any overridable
definition will be made concrete before the first callback runs.
A definition may be made overridable again in another before compile
callback and it will be made concrete one last time after all callbacks
run.
Callbacks registered first will run last. Any overridable definition
will be made concrete before the first callback runs. A definition may
be made overridable again in another before compile callback and it
will be made concrete one last time after after all callbacks run.
*Note*: unlike `@after_compile`, the callback function/macro must
be placed in a separate module (because when the callback is invoked,
@@ -439,6 +413,10 @@ defmodule Module do
* the list of quoted guards
* the quoted function body
Note the hook receives the quoted arguments and it is invoked before
the function is stored in the module. So `Module.defines?/2` will return
`false` for the first clause of every function.
If the function/macro being defined has multiple clauses, the hook will
be called for each clause.
@@ -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
@@ -482,23 +460,19 @@ defmodule Module do
below:
* `@compile :debug_info` - includes `:debug_info` regardless of the
corresponding setting in `Code.get_compiler_option/1`
corresponding setting in `Code.compiler_options/1`
* `@compile {:debug_info, false}` - disables `:debug_info` regardless
of the corresponding setting in `Code.get_compiler_option/1`
of the corresponding setting in `Code.compiler_options/1`
* `@compile {:inline, some_fun: 2, other_fun: 3}` - inlines the given
name/arity pairs. Inlining is applied locally, calls from another
name/arity pairs. Inlining is applied locally, calls from another
module are not affected by this option
* `@compile {:autoload, false}` - disables automatic loading of
modules after compilation. Instead, the module will be loaded after
it is dispatched to
* `@compile {:no_warn_undefined, Mod}` or
`@compile {:no_warn_undefined, {Mod, fun, arity}}` - does not warn if
the given module or the given `Mod.fun/arity` are not defined
You can see a handful more options used by the Erlang compiler in
the documentation for the [`:compile` module](http://www.erlang.org/doc/man/compile.html).
'''
@@ -506,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.
@@ -560,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
@@ -577,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`
@@ -607,10 +561,10 @@ 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} =
{value, binding, _env, _scope} =
:elixir.eval_quoted(quoted, binding, Keyword.put(opts, :module, module))
{value, binding}
@@ -641,8 +595,7 @@ defmodule Module do
Module.create(Hello, contents, Macro.Env.location(__ENV__))
Hello.world()
#=> true
Hello.world #=> true
## Differences from `defmodule`
@@ -669,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))
@@ -923,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
@@ -955,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
"""
@@ -971,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
@@ -993,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
@@ -1006,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
@@ -1035,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
@@ -1062,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_readonly!(__ENV__.function, module)
assert_not_compiled!(:make_overridable, module)
func = fn
{function_name, arity} = tuple
@@ -1083,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 ->
@@ -1126,7 +1063,7 @@ defmodule Module do
behaviour_definitions = bag_lookup_element(bag, {:accumulate, :behaviour}, 2)
cond do
Code.ensure_compiled(behaviour) != {:module, behaviour} ->
not Code.ensure_compiled?(behaviour) ->
{:error, "it was not defined"}
not function_exported?(behaviour, :behaviour_info, 1) ->
@@ -1168,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 """
@@ -1177,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 """
@@ -1203,68 +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
end
@doc """
Checks if the given attribute has been defined.
An attribute is defined if it has been registered with `register_attribute/3`
or assigned a value. If an attribute has been deleted with `delete_attribute/2`
it is no longer considered defined.
This function can only be used on modules that have not yet been compiled.
## Examples
defmodule MyModule do
@value 1
Module.register_attribute(__MODULE__, :other_value)
Module.put_attribute(__MODULE__, :another_value, 1)
Module.has_attribute?(__MODULE__, :value) #=> true
Module.has_attribute?(__MODULE__, :other_value) #=> true
Module.has_attribute?(__MODULE__, :another_value) #=> true
Module.has_attribute?(__MODULE__, :undefined) #=> false
Module.delete_attribute(__MODULE__, :value)
Module.has_attribute?(__MODULE__, :value) #=> false
end
"""
@doc since: "1.10.0"
@spec has_attribute?(module, atom) :: boolean
def has_attribute?(module, key) when is_atom(module) and is_atom(key) do
assert_not_compiled!(__ENV__.function, module)
{set, _bag} = data_tables_for(module)
:ets.member(set, key)
@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 """
@@ -1275,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_readonly!(__ENV__.function, module)
assert_not_compiled!(:delete_attribute, module)
{set, bag} = data_tables_for(module)
case :ets.lookup(set, key) do
@@ -1312,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
@@ -1323,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
@@ -1334,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_readonly!(__ENV__.function, module)
assert_not_compiled!(:register_attribute, module)
{set, bag} = data_tables_for(module)
if Keyword.get(options, :persist) do
@@ -1344,9 +1236,6 @@ defmodule Module do
if Keyword.get(options, :accumulate) do
:ets.insert_new(set, {attribute, [], :accumulate}) ||
:ets.update_element(set, attribute, {3, :accumulate})
else
:ets.insert_new(bag, {:warn_attributes, attribute})
:ets.insert_new(set, {attribute, nil, :unset})
end
:ok
@@ -1389,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
@@ -1426,11 +1316,11 @@ 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
defp compile_doc(table, line, kind, name, arity, args, _body, doc, doc_meta, env, impl) do
defp compile_doc(table, line, kind, name, arity, args, body, doc, doc_meta, env, impl) do
key = {doc_key(kind), name, arity}
signature = build_signature(args, env)
@@ -1526,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
@@ -1537,32 +1427,38 @@ defmodule Module do
:ok
end
defp check_behaviours(env, behaviours) do
defp check_behaviours(%{lexical_tracker: pid} = env, behaviours) do
Enum.reduce(behaviours, %{}, fn behaviour, acc ->
cond do
not is_atom(behaviour) ->
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
Code.ensure_compiled(behaviour) != {:module, behaviour} ->
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 ->
:elixir_env.trace({:require, [], behaviour, []}, env)
:elixir_lexical.record_remote(behaviour, nil, pid)
optional_callbacks = behaviour_info(behaviour, :optional_callbacks)
callbacks = behaviour_info(behaviour, :callbacks)
Enum.reduce(callbacks, acc, &add_callback(&1, behaviour, env, optional_callbacks, &2))
@@ -1576,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
@@ -1593,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
@@ -1601,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 =
@@ -1609,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
@@ -1631,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 ->
@@ -1644,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)
@@ -1761,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
@@ -1801,24 +1701,19 @@ 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
[{_, _, :accumulate}] ->
:lists.reverse(bag_lookup_element(bag, {:accumulate, key}, 2))
[{_, val, line}] when is_integer(line) ->
:ets.update_element(set, key, {3, :used})
[{_, val, nil}] ->
val
[{_, val, _}] ->
:ets.update_element(set, key, {3, nil})
val
[] when is_integer(line) ->
@@ -1838,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_readonly!(__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)
@@ -1863,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
@@ -1883,25 +1779,13 @@ 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, {:warn_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)
catch
:error, :badarg ->
:ets.insert(set, {key, value, line})
:ets.insert(bag, {:warn_attributes, key})
:ets.insert(bag, {:attributes, key})
else
:accumulate -> :ets.insert(bag, {{:accumulate, key}, value})
_ -> :ets.insert(set, {key, value, line})
@@ -1917,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) ->
@@ -1925,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
@@ -1980,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, :callback, :macrocallback] do
raise ArgumentError,
"attributes type, typep, opaque, spec, callback, and macrocallback " <>
"attributes type, typep, export_type, opaque, callback, and macrocallback" <>
"must be set directly via the @ notation"
end
@@ -2044,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
@@ -2078,35 +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_readonly!({function_name, arity}, module) do
case :elixir_module.mode(module) do
:all ->
:ok
:readonly ->
raise ArgumentError,
"could not call Module.#{function_name}/#{arity} because the module " <>
"#{inspect(module)} is in read-only mode (@after_compile)"
:closed ->
raise ArgumentError,
assert_not_compiled_message({function_name, arity}, module, "")
end
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
-317
View File
@@ -1,317 +0,0 @@
defmodule Module.Checker do
alias Module.ParallelChecker
@moduledoc false
def verify(module, cache) do
case prepare_module(module) do
{:ok, map} ->
undefined_and_deprecation_warnings = undefined_and_deprecation_warnings(map, cache)
infer_warnings = infer_definitions(map)
warnings = infer_warnings ++ undefined_and_deprecation_warnings
emit_warnings(warnings)
:error ->
[]
end
end
defp prepare_module({module, module_map}) when is_map(module_map) do
{:ok,
%{
module: module,
file: module_map.file,
definitions: module_map.definitions,
deprecated: module_map.deprecated,
no_warn_undefined: no_warn_undefined(module_map.compile_opts)
}}
end
defp prepare_module({module, binary}) when is_binary(binary) do
with {:ok, debug_info} <- debug_info(module, binary),
{:ok, checker_info} <- checker_chunk(binary) do
{:ok,
%{
module: module,
file: debug_info.file,
definitions: debug_info.definitions,
deprecated: checker_info.deprecated,
no_warn_undefined: checker_info.no_warn_undefined
}}
end
end
defp no_warn_undefined(compile_opts) do
for(
{:no_warn_undefined, values} <- compile_opts,
value <- List.wrap(values),
do: value
)
end
defp debug_info(module, binary) do
with {:ok, {_, [debug_info: chunk]}} <- :beam_lib.chunks(binary, [:debug_info]),
{:debug_info_v1, backend, data} <- chunk,
{:ok, info} <- backend.debug_info(:elixir_v1, module, data, []) do
{:ok, %{definitions: info.definitions, file: info.relative_file}}
else
_ -> :error
end
end
defp checker_chunk(binary) do
with {:ok, {_, [{'ExCk', chunk}]}} <- :beam_lib.chunks(binary, ['ExCk']),
{:elixir_checker_v1, contents} <- :erlang.binary_to_term(chunk) do
deprecated = Enum.map(contents.exports, fn {fun, map} -> {fun, map.deprecated_reason} end)
{:ok, %{deprecated: deprecated, no_warn_undefined: contents.no_warn_undefined}}
else
_ -> :error
end
end
defp infer_definitions(map) do
results = Module.Types.infer_definitions(map.file, map.module, map.definitions)
Enum.flat_map(results, fn {_function, reasons} -> reasons end)
end
defp undefined_and_deprecation_warnings(map, cache) do
state = %{
cache: cache,
file: map.file,
module: map.module,
no_warn_undefined: merge_no_warn_undefined(map),
function: nil,
warnings: []
}
state = check_definitions(map.definitions, state)
state.warnings
|> merge_warnings()
|> sort_warnings()
end
defp merge_no_warn_undefined(map) do
case Code.get_compiler_option(:no_warn_undefined) do
:all ->
:all
list when is_list(list) ->
map.no_warn_undefined ++ list
end
end
defp check_definitions(definitions, state) do
Enum.reduce(definitions, state, &check_definition/2)
end
defp check_definition({function, _kind, meta, clauses}, state) do
with_file_meta(%{state | function: function}, meta, fn state ->
Enum.reduce(clauses, state, &check_clause/2)
end)
end
defp with_file_meta(%{file: original_file} = state, meta, fun) do
case Keyword.fetch(meta, :file) do
{:ok, {meta_file, _}} ->
state = fun.(%{state | file: meta_file})
%{state | file: original_file}
:error ->
fun.(state)
end
end
defp check_clause({_meta, args, _guards, body}, state) do
state = check_expr(args, state)
check_expr(body, state)
end
# &Mod.fun/arity
defp check_expr({:&, meta, [{:/, _, [{{:., _, [module, fun]}, _, []}, arity]}]}, state)
when is_atom(module) and is_atom(fun) do
check_remote(module, fun, arity, meta, state)
end
# Mod.fun(...)
defp check_expr({{:., meta, [module, fun]}, _, args}, state)
when is_atom(module) and is_atom(fun) do
state = check_remote(module, fun, length(args), meta, state)
check_expr(args, state)
end
# %Module{...}
defp check_expr({:%, meta, [module, {:%{}, _meta, args}]}, state)
when is_atom(module) and is_list(args) do
state = check_remote(module, :__struct__, 0, meta, state)
check_expr(args, state)
end
# Function call
defp check_expr({left, _meta, right}, state) when is_list(right) do
state = check_expr(right, state)
check_expr(left, state)
end
# {x, y}
defp check_expr({left, right}, state) do
state = check_expr(right, state)
check_expr(left, state)
end
# [...]
defp check_expr(list, state) when is_list(list) do
Enum.reduce(list, state, &check_expr/2)
end
defp check_expr(_other, state) do
state
end
defp check_remote(module, fun, arity, meta, state) do
# TODO: In the future we may want to warn for modules defined
# in the local context
if Keyword.get(meta, :context_module, false) and state.module != module do
state
else
ParallelChecker.preload_module(state.cache, module)
check_export(module, fun, arity, meta, state)
end
end
defp check_export(module, fun, arity, meta, state) do
case ParallelChecker.fetch_export(state.cache, module, fun, arity) do
{:ok, :def, reason} ->
check_deprecated(module, fun, arity, reason, meta, state)
{:ok, :defmacro, reason} ->
state = warn(meta, state, {:unrequired_module, module, fun, arity})
check_deprecated(module, fun, arity, reason, meta, state)
{:error, :module} ->
if warn_undefined?(module, fun, arity, state) do
warn(meta, state, {:undefined_module, module, fun, arity})
else
state
end
{:error, :function} ->
if warn_undefined?(module, fun, arity, state) do
exports = ParallelChecker.all_exports(state.cache, module)
warn(meta, state, {:undefined_function, module, fun, arity, exports})
else
state
end
end
end
defp check_deprecated(module, fun, arity, reason, meta, state) do
if reason do
warn(meta, state, {:deprecated, module, fun, arity, reason})
else
state
end
end
# TODO: Do not warn inside guards
# TODO: Properly handle protocols
defp warn_undefined?(_module, :__impl__, 1, _state), do: false
defp warn_undefined?(_module, :module_info, 0, _state), do: false
defp warn_undefined?(_module, :module_info, 1, _state), do: false
defp warn_undefined?(:erlang, :orelse, 2, _state), do: false
defp warn_undefined?(:erlang, :andalso, 2, _state), do: false
defp warn_undefined?(_, _, _, %{no_warn_undefined: :all}) do
false
end
defp warn_undefined?(module, fun, arity, state) do
not Enum.any?(state.no_warn_undefined, &(&1 == module or &1 == {module, fun, arity}))
end
defp warn(meta, state, warning) do
{fun, arity} = state.function
location = {state.file, meta[:line] || 0, {state.module, fun, arity}}
%{state | warnings: [{__MODULE__, warning, location} | state.warnings]}
end
defp merge_warnings(warnings) do
Enum.reduce(warnings, %{}, fn {module, warning, location}, acc ->
locations = MapSet.new([location])
Map.update(acc, {module, warning}, locations, &MapSet.put(&1, location))
end)
end
defp sort_warnings(warnings) do
warnings
|> Enum.map(fn {{module, warning}, locations} -> {module, warning, Enum.sort(locations)} end)
|> Enum.sort()
end
defp emit_warnings(warnings) do
Enum.flat_map(warnings, fn {module, warning, locations} ->
message = module.format_warning(warning)
print_warning([message, ?\n, format_locations(locations)])
Enum.map(locations, fn {file, line, _mfa} ->
{file, line, message}
end)
end)
end
def format_warning({:undefined_module, module, fun, arity}) do
[
Exception.format_mfa(module, fun, arity),
" is undefined (module ",
inspect(module),
" is not available or is yet to be defined)"
]
end
def format_warning({:undefined_function, module, fun, arity, exports}) do
[
Exception.format_mfa(module, fun, arity),
" is undefined or private",
UndefinedFunctionError.hint_for_loaded_module(module, fun, arity, exports)
]
end
def format_warning({:deprecated, module, fun, arity, reason}) do
[
Exception.format_mfa(module, fun, arity),
" is deprecated. ",
reason
]
end
def format_warning({:unrequired_module, module, fun, arity}) do
[
"you must require ",
inspect(module),
" before invoking the macro ",
Exception.format_mfa(module, fun, arity)
]
end
defp format_locations([location]) do
format_location(location)
end
defp format_locations(locations) do
[
"Found at #{length(locations)} locations:\n",
Enum.map(locations, &format_location/1)
]
end
defp format_location({file, line, {module, fun, arity}}) do
file = Path.relative_to_cwd(file)
line = if line > 0, do: [Integer.to_string(line), ": "], else: []
mfa = Exception.format_mfa(module, fun, arity)
[" ", file, ?:, line, mfa, ?\n]
end
defp print_warning(message) do
IO.puts(:stderr, [:elixir_errors.warning_prefix(), message])
end
end
+14 -60
View File
@@ -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
-296
View File
@@ -1,296 +0,0 @@
defmodule Module.ParallelChecker do
@moduledoc false
@type cache() :: {pid(), :ets.tid()}
@type warning() :: term()
@type kind() :: :def | :defmacro
@doc """
Receives pairs of module maps and BEAM binaries. In parallel it verifies
the modules and adds the ExCk chunk to the binaries. Returns the updated
binaries and a list of warnings from the verification.
"""
@spec verify([{map(), binary()}], [{module(), binary()}], pos_integer()) :: [warning()]
def verify(compiled_modules, runtime_binaries, schedulers \\ nil) do
compiled_maps = Enum.map(compiled_modules, fn {map, _binary} -> {map.module, map} end)
check_modules = compiled_maps ++ runtime_binaries
schedulers = schedulers || max(:erlang.system_info(:schedulers_online), 2)
{:ok, server} = :gen_server.start_link(__MODULE__, [check_modules, self(), schedulers], [])
preload_cache(get_ets(server), check_modules)
start(server)
collect_results(length(check_modules), [])
end
defp collect_results(0, warnings) do
warnings
end
defp collect_results(count, warnings) do
receive do
{__MODULE__, _module, new_warnings} ->
collect_results(count - 1, new_warnings ++ warnings)
end
end
@doc """
Preloads a module into the cache. Call this function before any other
cache lookups for the module.
"""
@spec preload_module(cache(), module()) :: :ok
def preload_module({server, ets}, module) do
case :ets.lookup(ets, {:cached, module}) do
[{_key, _}] -> :ok
[] -> cache_module({server, ets}, module)
end
end
@doc """
Returns the export kind and deprecation reason for the given MFA from
the cache. If the module does not exist return `{:error, :module}`,
or if the function does not exist return `{:error, :function}`.
"""
@spec fetch_export(cache(), module(), atom(), arity()) ::
{:ok, kind(), binary() | nil} | {:error, :function | :module}
def fetch_export({_server, ets}, module, fun, arity) do
case :ets.lookup(ets, {:cached, module}) do
[{_key, true}] ->
case :ets.lookup(ets, {:export, {module, fun, arity}}) do
[{_key, kind, reason}] -> {:ok, kind, reason}
[] -> {:error, :function}
end
[{_key, false}] ->
{:error, :module}
end
end
@doc """
Returns all exported functions and macros for the given module from
the cache.
"""
@spec all_exports(cache(), module()) :: [{atom(), arity()}]
def all_exports({_server, ets}, module) do
# This is only called after we get a deprecation notice
# so we can assume it's a cached module
[{_key, exports}] = :ets.lookup(ets, {:all_exports, module})
exports
|> Enum.map(fn {function, _kind} -> function end)
|> Enum.sort()
end
def init([modules, send_results, schedulers]) do
ets = :ets.new(:checker_cache, [:set, :public, {:read_concurrency, true}])
state = %{
ets: ets,
waiting: %{},
send_results: send_results,
modules: modules,
spawned: 0,
schedulers: schedulers
}
{:ok, state}
end
def handle_call({:lock, module}, from, %{waiting: waiting} = state) do
case waiting do
%{^module => froms} ->
waiting = Map.put(state.waiting, module, [from | froms])
{:noreply, %{state | waiting: waiting}}
%{} ->
waiting = Map.put(state.waiting, module, [])
{:reply, true, %{state | waiting: waiting}}
end
end
def handle_call({:unlock, module}, _from, %{waiting: waiting} = state) do
froms = Map.fetch!(waiting, module)
Enum.each(froms, &:gen_server.reply(&1, false))
waiting = Map.delete(waiting, module)
{:reply, :ok, %{state | waiting: waiting}}
end
def handle_call(:get_ets, _from, %{ets: ets} = state) do
{:reply, ets, state}
end
def handle_cast(:start, %{modules: []} = state) do
{:stop, :normal, state}
end
def handle_cast(:start, state) do
{:noreply, spawn_checkers(state)}
end
def handle_info({__MODULE__, :done}, state) do
state = %{state | spawned: state.spawned - 1}
if state.spawned == 0 and state.modules == [] do
{:stop, :normal, state}
else
state = spawn_checkers(state)
{:noreply, state}
end
end
defp lock(server, module) do
:gen_server.call(server, {:lock, module}, :infinity)
end
defp unlock(server, module) do
:gen_server.call(server, {:unlock, module})
end
defp get_ets(server) do
:gen_server.call(server, :get_ets)
end
defp start(server) do
:gen_server.cast(server, :start)
end
defp preload_cache(ets, modules) do
Enum.each(modules, fn
{_module, map} when is_map(map) -> cache_from_module_map(ets, map)
{module, binary} when is_binary(binary) -> cache_from_chunk(ets, module, binary)
end)
end
defp spawn_checkers(%{modules: []} = state) do
state
end
defp spawn_checkers(%{spawned: spawned, schedulers: schedulers} = state)
when spawned >= schedulers do
state
end
defp spawn_checkers(%{modules: [{module, _} = verify | modules]} = state) do
parent = self()
ets = state.ets
send_results_pid = state.send_results
spawn_link(fn ->
warnings = Module.Checker.verify(verify, {parent, ets})
send(send_results_pid, {__MODULE__, module, warnings})
send(parent, {__MODULE__, :done})
end)
spawn_checkers(%{state | modules: modules, spawned: state.spawned + 1})
end
defp cache_module({server, ets}, module) do
if lock(server, module) do
cache_from_chunk(ets, module) || cache_from_info(ets, module)
unlock(server, module)
end
end
defp cache_from_chunk(ets, module) do
case :code.get_object_code(module) do
{^module, binary, _filename} -> cache_from_chunk(ets, module, binary)
_other -> false
end
end
defp cache_from_chunk(ets, module, binary) do
with {:ok, {_, [{'ExCk', chunk}]}} <- :beam_lib.chunks(binary, ['ExCk']),
{:elixir_checker_v1, contents} <- :erlang.binary_to_term(chunk) do
cache_chunk(ets, module, contents.exports)
true
else
_ -> false
end
end
defp cache_from_module_map(ets, map) do
exports =
[{{:__info__, 1}, :def}] ++
behaviour_exports(map) ++
definitions_to_exports(map.definitions)
deprecated = Map.new(map.deprecated)
cache_info(ets, map.module, exports, deprecated)
end
defp cache_from_info(ets, module) do
if Code.ensure_loaded?(module) do
exports = info_exports(module)
deprecated = info_deprecated(module)
cache_info(ets, module, exports, deprecated)
else
:ets.insert(ets, {{:cached, module}, false})
end
end
defp info_exports(module) do
Map.new(
[{{:__info__, 1}, :def}] ++
behaviour_exports(module) ++
Enum.map(module.__info__(:macros), &{&1, :defmacro}) ++
Enum.map(module.__info__(:functions), &{&1, :def})
)
rescue
_ -> Map.new(Enum.map(module.module_info(:exports), &{&1, :def}))
end
defp info_deprecated(module) do
Map.new(module.__info__(:deprecated))
rescue
_ -> %{}
end
defp cache_info(ets, module, exports, deprecated) do
exports =
Enum.map(exports, fn {{fun, arity}, kind} ->
reason = Map.get(deprecated, {fun, arity})
:ets.insert(ets, {{:export, {module, fun, arity}}, kind, reason})
{{fun, arity}, kind}
end)
:ets.insert(ets, {{:all_exports, module}, exports})
:ets.insert(ets, {{:cached, module}, true})
end
defp cache_chunk(ets, module, exports) do
exports =
Enum.map(exports, fn {{fun, arity}, %{kind: kind, deprecated_reason: reason}} ->
:ets.insert(ets, {{:export, {module, fun, arity}}, kind, reason})
{{fun, arity}, kind}
end)
:ets.insert(ets, {{:export, {module, :__info__, 1}}, :def, nil})
exports = [{{:__info__, 1}, :def} | exports]
:ets.insert(ets, {{:all_exports, module}, exports})
:ets.insert(ets, {{:cached, module}, true})
end
defp behaviour_exports(%{is_behaviour: true}), do: [{{:behaviour_info, 1}, :def}]
defp behaviour_exports(%{is_behaviour: false}), do: []
defp behaviour_exports(module) when is_atom(module) do
if {:behaviour_info, 1} in module.module_info(:functions) do
[{{:behaviour_info, 1}, :def}]
else
[]
end
end
defp definitions_to_exports(definitions) do
Enum.flat_map(definitions, fn {function, kind, _meta, _clauses} ->
if kind in [:def, :defmacro] do
[{function, kind}]
else
[]
end
end)
end
end
-387
View File
@@ -1,387 +0,0 @@
defmodule Module.Types do
@moduledoc false
import Module.Types.Helpers
alias Module.Types.{Expr, Pattern}
@doc """
Infer function definitions' types.
"""
def infer_definitions(file, module, defs) do
clauses = infer_signatures(file, module, defs)
infer_bodies(clauses)
end
defp infer_signatures(file, module, defs) do
Enum.map(defs, fn {{fun, _arity} = function, kind, meta, clauses} ->
stack = head_stack()
context = head_context(file, module, function)
clauses =
Enum.map(clauses, fn {_meta, params, guards, body} ->
def_expr = {kind, meta, [guards_to_expr(guards, {fun, [], params})]}
stack = push_expr_stack(def_expr, stack)
case of_head(params, guards, stack, context) do
{:ok, _signature, context} -> {:ok, {context, body}}
{:error, reason} -> {:error, reason}
end
end)
{function, clauses}
end)
end
defp infer_bodies(clauses) do
Enum.map(clauses, fn {function, clauses} ->
errors =
Enum.flat_map(clauses, fn
{:ok, {head_context, body}} ->
stack = body_stack()
context = body_context(head_context)
case Expr.of_expr(body, stack, context) do
{:ok, _type, _context} -> []
{:error, reason} -> [reason]
end
{:error, reason} ->
[reason]
end)
{function, errors}
end)
end
@doc false
def of_head(params, guards, stack, context) do
with {:ok, types, context} <-
map_reduce_ok(params, context, &Pattern.of_pattern(&1, stack, &2)),
# TODO: Check that of_guard/3 returns boolean() | :fail
{:ok, _, context} <- Pattern.of_guard(guards_to_or(guards), stack, context),
do: {:ok, lift_types(types, context), context}
end
@doc false
def head_context(file, module, function) do
%{
# File of module
file: file,
# Module of definitions
module: module,
# Current function
function: function,
# Expression variable to type variable
vars: %{},
# Type variable to expression variable
types_to_vars: %{},
# Type variable to type
types: %{},
# Trace of all variables that have been refined to a type,
# including the type they were refined to, why, and where
traces: %{},
# Counter to give type variables unique names
counter: 0,
# Track if a variable was infered from a type guard function such is_tuple/1
# or a guard function that fails such as elem/2, possible values are:
# `:guarded` when `is_tuple(x)`
# `:fail` when `elem(x, 0)`
# `:guarded_fail` when `is_tuple and elem(x, 0)`
guard_sources: %{}
}
end
@doc false
def head_stack() do
%{
# Stack of expression we have recursed through during inference,
# used for tracing
expr_stack: [],
# When false do not add a trace when a type variable is refined,
# useful when merging contexts where the variables already have traces
trace: true,
# Track if we are in a context where type guard functions should
# affect inference
type_guards_enabled?: true,
# Context used to determine if unification is bi-directional, :expr
# is directional, :pattern is bi-directional
context: :pattern
}
end
@doc false
def body_context(head_context) do
%{
# File of module
file: head_context.file,
# Module of definitions
module: head_context.module,
# Current function
function: head_context.function,
# Expression variable to type variable
vars: head_context.vars,
# Type variable to expression variable
types_to_vars: head_context.types_to_vars,
# Type variable to type
types: head_context.types,
# Trace of all variables that have been refined to a type,
# including the type they were refined to, why, and where
traces: head_context.traces,
# Counter to give type variables unique names
counter: head_context.counter
}
end
@doc false
def body_stack() do
%{
# Stack of expression we have recursed through during inference,
# used for tracing
expr_stack: [],
# When false do not add a trace when a type variable is refined,
# useful when merging contexts where the variables already have traces
trace: true,
# Context used to determine if unification is bi-directional, :expr
# is directional, :pattern is bi-directional
context: :expr
}
end
@doc """
Lifts type variables to their infered types from the context.
"""
def lift_types(types, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
{types, _context} = Enum.map_reduce(types, context, &do_lift_type/2)
types
end
@doc false
def lift_type(type, context) do
context = %{
types: context.types,
lifted_types: %{},
lifted_counter: 0
}
{type, _context} = do_lift_type(type, context)
type
end
## GUARDS
# TODO: Remove this and let multiple when be treated as multiple clauses,
# meaning they will be intersection types
defp guards_to_or([]) do
[]
end
defp guards_to_or(guards) do
Enum.reduce(guards, fn guard, acc -> {{:., [], [:erlang, :orelse]}, [], [guard, acc]} end)
end
defp guards_to_expr([], left) do
left
end
defp guards_to_expr([guard | guards], left) do
guards_to_expr(guards, {:when, [], [left, guard]})
end
## VARIABLE LIFTING
# Lift type variable to its infered (hopefully concrete) types from the context
defp do_lift_type({:var, var}, context) do
case Map.fetch(context.lifted_types, var) do
{:ok, lifted_var} ->
{{:var, lifted_var}, context}
:error ->
case Map.fetch(context.types, var) do
{:ok, :unbound} ->
new_lifted_var(var, context)
{:ok, type} ->
# Remove visited types to avoid infinite loops
# then restore after we are done recursing on vars
types = context.types
context = %{context | types: Map.delete(context.types, var)}
{type, context} = do_lift_type(type, context)
{type, %{context | types: types}}
:error ->
new_lifted_var(var, context)
end
end
end
defp do_lift_type({:tuple, types}, context) do
{types, context} = Enum.map_reduce(types, context, &do_lift_type/2)
{{:tuple, types}, context}
end
defp do_lift_type({:map, pairs}, context) do
{pairs, context} =
Enum.map_reduce(pairs, context, fn {key, value}, context ->
{key, context} = do_lift_type(key, context)
{value, context} = do_lift_type(value, context)
{{key, value}, context}
end)
{{:map, pairs}, context}
end
defp do_lift_type({:list, type}, context) do
{type, context} = do_lift_type(type, context)
{{:list, type}, context}
end
defp do_lift_type(other, context) do
{other, context}
end
defp new_lifted_var(original_var, context) do
types = Map.put(context.lifted_types, original_var, context.lifted_counter)
counter = context.lifted_counter + 1
type = {:var, context.lifted_counter}
context = %{context | lifted_types: types, lifted_counter: counter}
{type, context}
end
## ERROR FORMATTING
def format_warning({:unable_unify, left, right, expr, traces}) do
[
"incompatible types:\n\n ",
format_type(left),
" !~ ",
format_type(right),
"\n\n",
format_expr(expr),
format_traces(traces),
"Conflict found at"
]
end
defp format_expr(nil) do
[]
end
defp format_expr(expr) do
[
"in expression:\n\n ",
expr_to_string(expr),
"\n\n"
]
end
defp format_traces([]) do
[]
end
defp format_traces(traces) do
Enum.map(traces, fn
{var, {:type, type, expr, location}} ->
[
"where \"",
Macro.to_string(var),
"\" was given the type ",
Module.Types.format_type(type),
" in:\n\n # ",
format_location(location),
" ",
expr_to_string(expr),
"\n\n"
]
{var1, {:var, var2, expr, location}} ->
[
"where \"",
Macro.to_string(var1),
"\" was given the same type as \"",
Macro.to_string(var2),
"\" in:\n\n # ",
format_location(location),
" ",
expr_to_string(expr),
"\n\n"
]
end)
end
defp format_location({file, line}) do
file = Path.relative_to_cwd(file)
line = if line, do: [Integer.to_string(line)], else: []
[file, ?:, line, ?\n]
end
@doc false
def format_type({:union, types}) do
"#{Enum.map_join(types, " | ", &format_type/1)}"
end
def format_type({:tuple, types}) do
"{#{Enum.map_join(types, ", ", &format_type/1)}}"
end
def format_type({:list, type}) do
"[#{format_type(type)}]"
end
def format_type({:map, pairs}) do
case List.keytake(pairs, :__struct__, 0) do
{{:__struct__, struct}, pairs} ->
"%#{inspect(struct)}{#{format_map_pairs(pairs)}}"
nil ->
"%{#{format_map_pairs(pairs)}}"
end
end
def format_type({:atom, literal}) do
inspect(literal)
end
def format_type(atom) when is_atom(atom) do
"#{atom}()"
end
def format_type({:var, index}) do
"var#{index}"
end
defp format_map_pairs(pairs) do
Enum.map_join(pairs, ", ", fn {left, right} ->
"#{format_type(left)} => #{format_type(right)}"
end)
end
@doc false
def expr_to_string(expr) do
expr
|> reverse_rewrite()
|> Macro.to_string()
end
defp reverse_rewrite(guard) do
Macro.prewalk(guard, fn
{:., _, [:erlang, :orelse]} -> :or
{:., _, [:erlang, :andalso]} -> :and
{{:., _, [mod, fun]}, _, args} -> erl_to_ex(mod, fun, args)
other -> other
end)
end
defp erl_to_ex(mod, fun, args) do
case :elixir_rewrite.erl_to_ex(mod, fun, args) do
{Kernel, fun, args} -> {fun, [], args}
{mod, fun, args} -> {{:., [], [mod, fun]}, [], args}
end
end
end
-7
View File
@@ -1,7 +0,0 @@
defmodule Module.Types.Expr do
@moduledoc false
def of_expr(_expr, _stack, context) do
{:ok, :dynamic, context}
end
end
-126
View File
@@ -1,126 +0,0 @@
defmodule Module.Types.Helpers do
@moduledoc false
@doc """
Guard function to check if an AST node is a variable.
"""
defmacro is_var(expr) do
quote do
is_tuple(unquote(expr)) and
tuple_size(unquote(expr)) == 3 and
is_atom(elem(unquote(expr), 0)) and
is_atom(elem(unquote(expr), 2))
end
end
@doc """
Returns unique identifier for the current assignment of the variable.
"""
def var_name({_name, meta, _context}), do: Keyword.fetch!(meta, :version)
@doc """
Push expression to stack.
The expression stack is used to give the context where a type variable
was refined when show a type conflict error.
"""
def push_expr_stack(expr, stack) do
%{stack | expr_stack: [expr | stack.expr_stack]}
end
@doc """
Like `Enum.reduce/3` but only continues while `fun` returns `{:ok, acc}`
and stops on `{:error, reason}`.
"""
def reduce_ok(list, acc, fun) do
do_reduce_ok(list, acc, fun)
end
defp do_reduce_ok([head | tail], acc, fun) do
case fun.(head, acc) do
{:ok, acc} ->
do_reduce_ok(tail, acc, fun)
result when elem(result, 0) == :ok ->
result = Tuple.delete_at(result, 0)
do_reduce_ok(tail, result, fun)
{:error, reason} ->
{:error, reason}
end
end
defp do_reduce_ok([], acc, _fun), do: {:ok, acc}
@doc """
Like `Enum.unzip/1` but only continues while `fun` returns `{:ok, elem1, elem2}`
and stops on `{:error, reason}`.
"""
def unzip_ok(list) do
do_unzip_ok(list, [], [])
end
defp do_unzip_ok([{:ok, head1, head2} | tail], acc1, acc2) do
do_unzip_ok(tail, [head1 | acc1], [head2 | acc2])
end
defp do_unzip_ok([{:error, reason} | _tail], _acc1, _acc2), do: {:error, reason}
defp do_unzip_ok([], acc1, acc2), do: {:ok, Enum.reverse(acc1), Enum.reverse(acc2)}
@doc """
Like `Enum.map/2` but only continues while `fun` returns `{:ok, elem}`
and stops on `{:error, reason}`.
"""
def map_ok(list, fun) do
do_map_ok(list, [], fun)
end
defp do_map_ok([head | tail], acc, fun) do
case fun.(head) do
{:ok, elem} ->
do_map_ok(tail, [elem | acc], fun)
result when elem(result, 0) == :ok ->
result = Tuple.delete_at(result, 0)
do_map_ok(tail, [result | acc], fun)
{:error, reason} ->
{:error, reason}
end
end
defp do_map_ok([], acc, _fun), do: {:ok, Enum.reverse(acc)}
@doc """
Like `Enum.map_reduce/3` but only continues while `fun` returns `{:ok, elem, acc}`
and stops on `{:error, reason}`.
"""
def map_reduce_ok(list, acc, fun) do
do_map_reduce_ok(list, {[], acc}, fun)
end
defp do_map_reduce_ok([head | tail], {list, acc}, fun) do
case fun.(head, acc) do
{:ok, elem, acc} ->
do_map_reduce_ok(tail, {[elem | list], acc}, fun)
{:error, reason} ->
{:error, reason}
end
end
defp do_map_reduce_ok([], {list, acc}, _fun), do: {:ok, Enum.reverse(list), acc}
@doc """
Given a list of `[{:ok, term()} | {:error, term()}]` it returns a list of
errors `{:error, [term()]}` in case of at least one error or `{:ok, [term()]}`
if there are no errors.
"""
def oks_or_errors(list) do
case Enum.split_with(list, &match?({:ok, _}, &1)) do
{oks, []} -> {:ok, Enum.map(oks, fn {:ok, ok} -> ok end)}
{_oks, errors} -> {:error, Enum.map(errors, fn {:error, error} -> error end)}
end
end
end
-356
View File
@@ -1,356 +0,0 @@
defmodule Module.Types.Infer do
@moduledoc false
import Module.Types.Helpers
@doc """
Unifies two types and returns the unified type and an updated typing context
or an error in case of a typing conflict.
"""
def unify(source, target, stack, context) do
case do_unify(source, target, stack, context) do
{:ok, type, context} ->
{:ok, type, context}
{:error, reason} ->
if stack.context == :pattern do
case do_unify(target, source, stack, context) do
{:ok, type, context} ->
{:ok, type, context}
{:error, _} ->
{:error, reason}
end
else
{:error, reason}
end
end
end
defp do_unify(same, same, _stack, context) do
{:ok, same, context}
end
defp do_unify({:var, var}, type, stack, context) do
unify_var(var, type, stack, context, _var_source = true)
end
defp do_unify(type, {:var, var}, stack, context) do
unify_var(var, type, stack, context, _var_source = false)
end
defp do_unify({:tuple, sources}, {:tuple, targets}, stack, context)
when length(sources) == length(targets) do
result =
map_reduce_ok(Enum.zip(sources, targets), context, fn {source, target}, context ->
unify(source, target, stack, context)
end)
case result do
{:ok, types, context} -> {:ok, {:tuple, types}, context}
{:error, reason} -> {:error, reason}
end
end
defp do_unify({:list, source}, {:list, target}, stack, context) do
case unify(source, target, stack, context) do
{:ok, type, context} -> {:ok, {:list, type}, context}
{:error, reason} -> {:error, reason}
end
end
defp do_unify({:map, source_pairs}, {:map, target_pairs}, stack, context) do
# Since maps in patterns only support literal keys (excluding maps)
# we can do exact type match without subtype checking
unique_right_pairs =
Enum.reject(target_pairs, fn {key, _value} ->
:lists.keyfind(key, 1, source_pairs)
end)
unique_pairs = source_pairs ++ unique_right_pairs
# Build union of all unique key-value pairs between the maps
result =
map_reduce_ok(unique_pairs, context, fn {source_key, source_value}, context ->
case :lists.keyfind(source_key, 1, target_pairs) do
{^source_key, target_value} ->
case unify(source_value, target_value, stack, context) do
{:ok, value, context} -> {:ok, {source_key, value}, context}
{:error, reason} -> {:error, reason}
end
false ->
{:ok, {source_key, source_value}, context}
end
end)
case result do
{:ok, pairs, context} -> {:ok, {:map, pairs}, context}
{:error, reason} -> {:error, reason}
end
end
defp do_unify(source, :dynamic, _stack, context) do
{:ok, source, context}
end
defp do_unify(source, target, stack, context) do
if subtype?(source, target, context) do
{:ok, source, context}
else
error({:unable_unify, source, target}, stack, context)
end
end
defp unify_var(var, type, stack, context, var_source?) do
case Map.fetch!(context.types, var) do
:unbound ->
context = refine_var(var, type, stack, context)
if recursive_type?(type, [], context) do
if var_source? do
error({:unable_unify, {:var, var}, type}, stack, context)
else
error({:unable_unify, type, {:var, var}}, stack, context)
end
else
{:ok, {:var, var}, context}
end
var_type ->
context = trace_var(var, type, stack, context)
unify_result =
if var_source? do
unify(var_type, type, stack, context)
else
unify(type, var_type, stack, context)
end
case unify_result do
{:ok, var_type, context} ->
context = refine_var(var, var_type, stack, context)
{:ok, {:var, var}, context}
{:error, reason} ->
{:error, reason}
end
end
end
@doc """
Adds a variable to the typing context and returns its type variables.
If the variable has already been added, return the existing type variable.
"""
def new_var(var, context) do
case Map.fetch(context.vars, var_name(var)) do
{:ok, type} ->
{type, context}
:error ->
type = {:var, context.counter}
vars = Map.put(context.vars, var_name(var), type)
types_to_vars = Map.put(context.types_to_vars, context.counter, var)
types = Map.put(context.types, context.counter, :unbound)
traces = Map.put(context.traces, context.counter, [])
context = %{
context
| vars: vars,
types_to_vars: types_to_vars,
types: types,
traces: traces,
counter: context.counter + 1
}
{type, context}
end
end
@doc """
Set the type for a variable and add trace.
"""
def refine_var(var, type, stack, context) do
types = Map.put(context.types, var, type)
context = %{context | types: types}
trace_var(var, type, stack, context)
end
@doc """
Remove type variable and all its traces.
"""
def remove_var(var, context) do
types = Map.delete(context.types, var)
traces = Map.delete(context.traces, var)
%{context | types: types, traces: traces}
end
defp trace_var(var, type, %{trace: true, expr_stack: expr_stack} = _stack, context) do
line = get_meta(hd(expr_stack))[:line]
trace = {type, expr_stack, {context.file, line}}
traces = Map.update!(context.traces, var, &[trace | &1])
%{context | traces: traces}
end
defp trace_var(_var, _type, %{trace: false} = _stack, context) do
context
end
# Check if a variable is recursive and incompatible with itself
# Bad: `{var} = var`
# Good: `x = y; y = z; z = x`
defp recursive_type?({:var, var} = parent, parents, context) do
case Map.fetch!(context.types, var) do
:unbound ->
false
type ->
if type in parents do
not Enum.all?(parents, &match?({:var, _}, &1))
else
recursive_type?(type, [parent | parents], context)
end
end
end
defp recursive_type?({:list, type} = parent, parents, context) do
recursive_type?(type, [parent | parents], context)
end
defp recursive_type?({:tuple, types} = parent, parents, context) do
Enum.any?(types, &recursive_type?(&1, [parent | parents], context))
end
defp recursive_type?({:map, pairs} = parent, parents, context) do
Enum.any?(pairs, fn {key, value} ->
recursive_type?(key, [parent | parents], context) or
recursive_type?(value, [parent | parents], context)
end)
end
defp recursive_type?(_other, _parents, _context) do
false
end
@doc """
Checks if the first argument is a subtype of the second argument.
Only checks for simple and concrete types.
"""
def subtype?({:atom, boolean}, :boolean, _context) when is_boolean(boolean), do: true
def subtype?({:atom, atom}, :atom, _context) when is_atom(atom), do: true
def subtype?(:boolean, :atom, _context), do: true
def subtype?(:float, :number, _context), do: true
def subtype?(:integer, :number, _context), do: true
def subtype?({:tuple, _}, :tuple, _context), do: true
# TODO: Lift unions to unify/3?
def subtype?({:union, left_types}, {:union, _} = right_union, context) do
Enum.all?(left_types, &subtype?(&1, right_union, context))
end
def subtype?(left, {:union, right_types}, context) do
Enum.any?(right_types, &subtype?(left, &1, context))
end
def subtype?(left, right, _context), do: left == right
@doc """
Returns a "simplified" union using `subtype?/3` to remove redundant types.
Due to limitations in `subtype?/3` some overlapping types may still be
included. For example unions with overlapping non-concrete types such as
`{boolean()} | {atom()}` will not be merged or types with variables that
are distinct but equivalent such as `a | b when a ~ b`.
"""
# TODO: Translate union of all top types to dynamic()
def to_union(types, context) when types != [] do
if :dynamic in types do
:dynamic
else
case unique_super_types(flatten_union(types), context) do
[type] -> type
types -> {:union, types}
end
end
end
defp flatten_union(types) do
Enum.flat_map(types, fn
{:union, types} -> flatten_union(types)
type -> [type]
end)
end
# Filter subtypes
# `boolean() | atom()` => `atom()`
# `:foo | atom()` => `atom()`
# Does not unify `true | false` => `boolean()`
defp unique_super_types([type | types], context) do
types = Enum.reject(types, &subtype?(&1, type, context))
if Enum.any?(types, &subtype?(type, &1, context)) do
unique_super_types(types, context)
else
[type | unique_super_types(types, context)]
end
end
defp unique_super_types([], _context) do
[]
end
# Collect relevant information from context and traces to report error
defp error({:unable_unify, left, right}, stack, context) do
{fun, arity} = context.function
line = get_meta(hd(stack.expr_stack))[:line]
location = {context.file, line, {context.module, fun, arity}}
traces = type_traces(context)
common_expr = common_super_expr(traces) || hd(stack.expr_stack)
traces = simplify_traces(traces, context)
{:error, {Module.Types, {:unable_unify, left, right, common_expr, traces}, [location]}}
end
defp type_traces(context) do
Enum.flat_map(context.traces, fn {var_index, traces} ->
expr_var = Map.fetch!(context.types_to_vars, var_index)
Enum.map(traces, &{expr_var, &1})
end)
end
# Only use last expr from trace and tag if trace is for
# a concrete type or type variable
defp simplify_traces(traces, context) do
Enum.flat_map(traces, fn {var, {type, [expr | _], location}} ->
case type do
{:var, var_index} ->
var2 = Map.fetch!(context.types_to_vars, var_index)
[{var, {:var, var2, expr, location}}]
_ ->
[{var, {:type, type, expr, location}}]
end
end)
end
# Find first common super expression among all traces
defp common_super_expr([]) do
nil
end
defp common_super_expr([{_var, {_type, expr_stack, _location}} | traces]) do
Enum.find_value(expr_stack, fn expr ->
common? =
Enum.all?(traces, fn {_var, {_type, expr_stack, _location}} -> expr in expr_stack end)
if common? do
expr
end
end)
end
defp get_meta({_fun, meta, _args}) when is_list(meta), do: meta
defp get_meta(_other), do: []
end
-585
View File
@@ -1,585 +0,0 @@
defmodule Module.Types.Pattern do
@moduledoc false
import Module.Types.{Helpers, Infer}
@doc """
Return the type and typing context of a pattern expression or an error
in case of a typing conflict.
"""
# :atom
def of_pattern(atom, _stack, context) when is_atom(atom) do
{:ok, {:atom, atom}, context}
end
# 12
def of_pattern(literal, _stack, context) when is_integer(literal) do
{:ok, :integer, context}
end
# 1.2
def of_pattern(literal, _stack, context) when is_float(literal) do
{:ok, :float, context}
end
# "..."
def of_pattern(literal, _stack, context) when is_binary(literal) do
{:ok, :binary, context}
end
# <<...>>>
def of_pattern({:<<>>, _meta, args} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case reduce_ok(args, context, &of_binary(&1, stack, &2)) do
{:ok, context} -> {:ok, :binary, context}
{:error, reason} -> {:error, reason}
end
end
# left | []
def of_pattern({:|, _meta, [left_expr, []]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
of_pattern(left_expr, stack, context)
end
# left | right
def of_pattern({:|, _meta, [left_expr, right_expr]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case of_pattern(left_expr, stack, context) do
{:ok, left, context} ->
case of_pattern(right_expr, stack, context) do
{:ok, {:list, right}, context} ->
{:ok, to_union([left, right], context), context}
{:ok, right, context} ->
{:ok, to_union([left, right], context), context}
{:error, reason} ->
{:error, reason}
end
{:error, reason} ->
{:error, reason}
end
end
# []
def of_pattern([], _stack, context) do
{:ok, {:list, :dynamic}, context}
end
# [expr, ...]
def of_pattern(exprs, stack, context) when is_list(exprs) do
stack = push_expr_stack(exprs, stack)
case map_reduce_ok(exprs, context, &of_pattern(&1, stack, &2)) do
{:ok, types, context} -> {:ok, {:list, to_union(types, context)}, context}
{:error, reason} -> {:error, reason}
end
end
# left ++ right
def of_pattern(
{{:., _meta1, [:erlang, :++]}, _meta2, [left_expr, right_expr]} = expr,
stack,
context
) do
stack = push_expr_stack(expr, stack)
case of_pattern(left_expr, stack, context) do
{:ok, {:list, left}, context} ->
case of_pattern(right_expr, stack, context) do
{:ok, {:list, right}, context} ->
{:ok, {:list, to_union([left, right], context)}, context}
{:ok, right, context} ->
{:ok, {:list, to_union([left, right], context)}, context}
{:error, reason} ->
{:error, reason}
end
{:error, reason} ->
{:error, reason}
end
end
# _
def of_pattern({:_, _meta, atom}, _stack, context) when is_atom(atom) do
{:ok, :dynamic, context}
end
# var
def of_pattern(var, _stack, context) when is_var(var) do
{type, context} = new_var(var, context)
{:ok, type, context}
end
# {left, right}
def of_pattern({left, right}, stack, context) do
of_pattern({:{}, [], [left, right]}, stack, context)
end
# {...}
def of_pattern({:{}, _meta, exprs} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case map_reduce_ok(exprs, context, &of_pattern(&1, stack, &2)) do
{:ok, types, context} -> {:ok, {:tuple, types}, context}
{:error, reason} -> {:error, reason}
end
end
# left = right
def of_pattern({:=, _meta, [left_expr, right_expr]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
with {:ok, left_type, context} <- of_pattern(left_expr, stack, context),
{:ok, right_type, context} <- of_pattern(right_expr, stack, context),
do: unify(left_type, right_type, stack, context)
end
# %{...}
def of_pattern({:%{}, _meta, args} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
case of_pairs(args, stack, context) do
{:ok, pairs, context} -> {:ok, {:map, pairs_to_unions(pairs, context)}, context}
{:error, reason} -> {:error, reason}
end
end
# %_{...}
def of_pattern(
{:%, _meta1, [{:_, _meta2, var_context}, {:%{}, _meta3, args}]} = expr,
stack,
context
)
when is_atom(var_context) do
stack = push_expr_stack(expr, stack)
case of_pairs(args, stack, context) do
{:ok, pairs, context} ->
pairs = [{{:atom, :__struct__}, :atom} | pairs]
{:ok, {:map, pairs}, context}
{:error, reason} ->
{:error, reason}
end
end
# %var{...}
def of_pattern({:%, _meta1, [var, {:%{}, _meta2, args}]} = expr, stack, context)
when is_var(var) do
stack = push_expr_stack(expr, stack)
with {:ok, pairs, context} <- of_pairs(args, stack, context),
{var_type, context} = new_var(var, context),
{:ok, _, context} <- unify(var_type, :atom, stack, context) do
pairs = [{{:atom, :__struct__}, var_type} | pairs]
{:ok, {:map, pairs}, context}
end
end
# %Struct{...}
def of_pattern({:%, _meta1, [module, {:%{}, _meta2, args}]} = expr, stack, context)
when is_atom(module) do
stack = push_expr_stack(expr, stack)
case of_pairs(args, stack, context) do
{:ok, pairs, context} ->
pairs = [{{:atom, :__struct__}, {:atom, module}} | pairs]
{:ok, {:map, pairs}, context}
{:error, reason} ->
{:error, reason}
end
end
defp of_pairs(pairs, stack, context) do
map_reduce_ok(pairs, context, fn {key, value}, context ->
with {:ok, key_type, context} <- of_pattern(key, stack, context),
{:ok, value_type, context} <- of_pattern(value, stack, context),
do: {:ok, {key_type, value_type}, context}
end)
end
defp pairs_to_unions(pairs, context) do
# Maps only allow simple literal keys in patterns so
# we do not have to do subtype checking
Enum.reduce(pairs, [], fn {key, value}, pairs ->
case :lists.keyfind(key, 1, pairs) do
{^key, {:union, union}} ->
:lists.keystore(key, 1, pairs, {key, to_union([value | union], context)})
{^key, original_value} ->
:lists.keystore(key, 1, pairs, {key, to_union([value, original_value], context)})
false ->
[{key, value} | pairs]
end
end)
end
## GUARDS
# TODO: Some guards can be changed to intersection types or higher order types
@guard_functions %{
{:is_atom, 1} => {[:atom], :boolean},
{:is_binary, 1} => {[:binary], :boolean},
{:is_bitstring, 1} => {[:binary], :boolean},
{:is_boolean, 1} => {[:boolean], :boolean},
{:is_float, 1} => {[:float], :boolean},
{:is_function, 1} => {[:fun], :boolean},
{:is_function, 2} => {[:fun, :integer], :boolean},
{:is_integer, 1} => {[:integer], :boolean},
{:is_list, 1} => {[{:list, :dynamic}], :boolean},
{:is_map, 1} => {[{:map, []}], :boolean},
{:is_map_key, 2} => {[:dynamic, {:map, []}], :dynamic},
{:is_number, 1} => {[:number], :boolean},
{:is_pid, 1} => {[:pid], :boolean},
{:is_port, 1} => {[:port], :boolean},
{:is_reference, 1} => {[:reference], :boolean},
{:is_tuple, 1} => {[:tuple], :boolean},
{:<, 2} => {[:dynamic, :dynamic], :boolean},
{:"=<", 2} => {[:dynamic, :dynamic], :boolean},
{:>, 2} => {[:dynamic, :dynamic], :boolean},
{:>=, 2} => {[:dynamic, :dynamic], :boolean},
{:"/=", 2} => {[:dynamic, :dynamic], :boolean},
{:"=/=", 2} => {[:dynamic, :dynamic], :boolean},
{:==, 2} => {[:dynamic, :dynamic], :boolean},
{:"=:=", 2} => {[:dynamic, :dynamic], :boolean},
{:*, 2} => {[:number, :number], :number},
{:+, 1} => {[:number], :number},
{:+, 2} => {[:number, :number], :number},
{:-, 1} => {[:number], :number},
{:-, 2} => {[:number, :number], :number},
{:/, 2} => {[:number, :number], :number},
{:abs, 1} => {[:number], :number},
{:ceil, 1} => {[:number], :integer},
{:floor, 1} => {[:number], :integer},
{:round, 1} => {[:number], :integer},
{:trunc, 1} => {[:number], :integer},
{:element, 2} => {[:integer, :tuple], :dynamic},
{:hd, 1} => {[{:list, :dynamic}], :dynamic},
{:length, 1} => {[{:list, :dynamic}], :integer},
{:map_get, 2} => {[:dynamic, {:map, []}], :dynamic},
{:map_size, 1} => {[{:map, []}], :integer},
{:tl, 1} => {[{:list, :dynamic}], :dynamic},
{:tuple_size, 1} => {[:tuple], :integer},
{:node, 1} => {[{:union, [:pid, :reference, :port]}], :atom},
{:binary_part, 3} => {[:binary, :integer, :integer], :binary},
{:bit_size, 1} => {[:binary], :integer},
{:byte_size, 1} => {[:binary], :integer},
{:size, 1} => {[{:union, [:binary, :tuple]}], :boolean},
{:div, 2} => {[:integer, :integer], :integer},
{:rem, 2} => {[:integer, :integer], :integer},
{:node, 0} => {[], :atom},
{:self, 0} => {[], :pid},
{:bnot, 1} => {[:integer], :integer},
{:band, 2} => {[:integer, :integer], :integer},
{:bor, 2} => {[:integer, :integer], :integer},
{:bxor, 2} => {[:integer, :integer], :integer},
{:bsl, 2} => {[:integer, :integer], :integer},
{:bsr, 2} => {[:integer, :integer], :integer},
{:xor, 2} => {[:boolean, :boolean], :boolean},
{:not, 1} => {[:boolean], :boolean}
# Following guards are matched explicitly to handle
# type guard functions such as is_atom/1
# {:andalso, 2} => {[:boolean, :boolean], :boolean}
# {:orelse, 2} => {[:boolean, :boolean], :boolean}
}
@type_guards [
:is_atom,
:is_binary,
:is_bitstring,
:is_boolean,
:is_float,
:is_function,
:is_function,
:is_integer,
:is_list,
:is_map,
:is_number,
:is_pid,
:is_port,
:is_reference,
:is_tuple
]
@doc """
Refines the type variables in the typing context using type check guards
such as `is_integer/1`.
"""
def of_guard({{:., _, [:erlang, :andalso]}, _, [left, right]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
fresh_context = fresh_context(context)
with {:ok, left_type, left_context} <- of_guard(left, stack, fresh_context),
{:ok, right_type, right_context} <- of_guard(right, stack, fresh_context),
{:ok, context} <- merge_context_and(context, stack, left_context, right_context),
{:ok, _, context} <- unify(left_type, :boolean, stack, context),
{:ok, _, context} <- unify(right_type, :boolean, stack, context),
do: {:ok, :boolean, context}
end
def of_guard({{:., _, [:erlang, :orelse]}, _, [left, right]} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
fresh_context = fresh_context(context)
with {:ok, left_type, left_context} <- of_guard(left, stack, fresh_context),
{:ok, _right_type, right_context} <- of_guard(right, stack, fresh_context),
{:ok, context} <- merge_context_or(context, stack, left_context, right_context),
{:ok, _, context} <- unify(left_type, :boolean, stack, context),
do: {:ok, :boolean, context}
end
def of_guard({{:., _, [:erlang, guard]}, _, args} = expr, stack, context) do
stack = push_expr_stack(expr, stack)
{param_types, return_type} = guard_signature(guard, length(args))
type_guard? = type_guard?(guard)
# Only check type guards in the context of and/or/not,
# a type guard in the context of is_tuple(x) > :foo
# should not affect the inference of x
if not type_guard? or stack.type_guards_enabled? do
arg_stack = %{stack | type_guards_enabled?: type_guard?}
with {:ok, arg_types, context} <-
map_reduce_ok(args, context, &of_guard(&1, arg_stack, &2)),
{:ok, context} <- unify_call(param_types, arg_types, stack, context) do
{arg_types, guard_sources} =
case arg_types do
[{:var, index} | rest_arg_types] when type_guard? ->
guard_sources =
Map.update(context.guard_sources, index, [:guarded], &[:guarded | &1])
{rest_arg_types, guard_sources}
_ ->
{arg_types, context.guard_sources}
end
guard_sources =
Enum.reduce(arg_types, guard_sources, fn
{:var, index}, guard_sources ->
Map.update(guard_sources, index, [:fail], &[:fail | &1])
_, guard_sources ->
guard_sources
end)
{:ok, return_type, %{context | guard_sources: guard_sources}}
end
else
{:ok, return_type, context}
end
end
def of_guard(var, _stack, context) when is_var(var) do
type = Map.fetch!(context.vars, var_name(var))
{:ok, type, context}
end
def of_guard(expr, stack, context) do
# Fall back to of_pattern/3 for literals
of_pattern(expr, stack, context)
end
defp fresh_context(context) do
types = Map.new(context.types, fn {var, _} -> {var, :unbound} end)
traces = Map.new(context.traces, fn {var, _} -> {var, []} end)
%{context | types: types, traces: traces}
end
defp unify_call(params, args, stack, context) do
reduce_ok(Enum.zip(params, args), context, fn {param, arg}, context ->
case unify(param, arg, stack, context) do
{:ok, _, context} -> {:ok, context}
{:error, reason} -> {:error, reason}
end
end)
end
defp merge_context_and(context, stack, left, right) do
with {:ok, context} <- unify_new_types(context, stack, left),
{:ok, context} <- unify_new_types(context, stack, right) do
guard_sources = and_guard_sources(left.guard_sources, right.guard_sources)
guard_sources = merge_guard_sources([context.guard_sources, guard_sources])
{:ok, %{context | guard_sources: guard_sources}}
end
end
defp unify_new_types(context, stack, new_context) do
context = merge_traces(context, new_context)
reduce_ok(Map.to_list(new_context.types), context, fn
{_index, :unbound}, context ->
{:ok, context}
{index, new_type}, context ->
case unify({:var, index}, new_type, %{stack | trace: false}, context) do
{:ok, _, context} ->
{:ok, context}
{:error, reason} ->
{:error, reason}
end
end)
end
defp merge_guard_sources(sources) do
Enum.reduce(sources, fn left, right ->
Map.merge(left, right, fn _index, left, right -> join_guard_source(left, right) end)
end)
end
defp join_guard_source(left, right) do
sources = left ++ right
cond do
:fail in sources -> [:fail]
:guarded_fail in sources -> [:guarded_fail]
:guarded in sources -> [:guarded]
true -> []
end
end
defp and_guard_sources(left, right) do
Map.merge(left, right, fn _index, left, right ->
# When the failing guard function wont fail due to type check function before it,
# for example: is_list(x) and length(x)
if :guarded in left and :fail in right do
[:guarded_fail]
else
join_guard_source(left, right)
end
end)
end
defp merge_traces(context, new_context) do
traces =
:maps.fold(
fn index, new_traces, traces ->
:maps.update_with(index, &(new_traces ++ &1), new_traces, traces)
end,
context.traces,
new_context.traces
)
%{context | traces: traces}
end
defp merge_context_or(context, stack, left, right) do
context =
case {Map.to_list(left.types), Map.to_list(right.types)} do
{[{index, :unbound}], [{index, type}]} ->
refine_var(index, type, stack, context)
{[{index, type}], [{index, :unbound}]} ->
refine_var(index, type, stack, context)
{[{index, left_type}], [{index, right_type}]} ->
# Only include right side if left side is from type guard such as is_list(x),
# do not refine in case of length(x)
left_guard_sources = Map.get(left.guard_sources, index, [])
if :fail in left_guard_sources do
guard_sources = Map.put(context.guard_sources, index, [:fail])
context = %{context | guard_sources: guard_sources}
refine_var(index, left_type, stack, context)
else
guard_sources =
merge_guard_sources([
context.guard_sources,
left.guard_sources,
right.guard_sources
])
context = %{context | guard_sources: guard_sources}
refine_var(index, to_union([left_type, right_type], context), stack, context)
end
{left_types, _right_types} ->
Enum.reduce(left_types, context, fn {index, left_type}, context ->
left_guard_sources = Map.get(left.guard_sources, index, [])
if :fail in left_guard_sources do
guard_sources =
merge_guard_sources([
context.guard_sources,
left.guard_sources,
right.guard_sources
])
context = %{context | guard_sources: guard_sources}
refine_var(index, left_type, stack, context)
else
context
end
end)
end
{:ok, context}
end
# binary-pattern :: specifier
defp of_binary({:"::", _meta, [expr, specifiers]} = full_expr, stack, context) do
{expected_type, utf?} = collect_binary_type(specifiers) || {:integer, false}
stack = push_expr_stack(full_expr, stack)
# Special case utf specifiers with binary literals since they allow
# both integer and binary literals but variables are always integer
if is_binary(expr) and utf? do
{:ok, context}
else
with {:ok, type, context} <- of_pattern(expr, stack, context),
{:ok, _type, context} <- unify(type, expected_type, stack, context),
do: {:ok, context}
end
end
# binary-pattern
defp of_binary(expr, stack, context) do
case of_pattern(expr, stack, context) do
{:ok, type, context} when type in [:integer, :float, :binary] ->
{:ok, context}
{:ok, type, _context} ->
{:error, {:invalid_binary_type, type}}
{:error, reason} ->
{:error, reason}
end
end
# Collect binary type specifiers,
# from `<<pattern::integer-size(10)>>` collect `integer`
defp collect_binary_type({:-, _meta, [left, right]}),
do: collect_binary_type(left) || collect_binary_type(right)
defp collect_binary_type({:integer, _, _}), do: {:integer, false}
defp collect_binary_type({:float, _, _}), do: {:float, false}
defp collect_binary_type({:bits, _, _}), do: {:binary, false}
defp collect_binary_type({:bitstring, _, _}), do: {:binary, false}
defp collect_binary_type({:bytes, _, _}), do: {:binary, false}
defp collect_binary_type({:binary, _, _}), do: {:binary, false}
defp collect_binary_type({:utf8, _, _}), do: {:integer, true}
defp collect_binary_type({:utf16, _, _}), do: {:integer, true}
defp collect_binary_type({:utf32, _, _}), do: {:integer, true}
defp collect_binary_type(_), do: nil
defp guard_signature(name, arity) do
Map.fetch!(@guard_functions, {name, arity})
end
defp type_guard?(name) do
name in @type_guards
end
end
+2 -19
View File
@@ -13,23 +13,8 @@ defmodule Node do
@doc """
Turns a non-distributed node into a distributed node.
This functionality starts the `:net_kernel` and other related
processes.
This function is rarely invoked in practice. Instead, nodes are
named and started via the command line by using the `--sname` and
`--name` flags. If you need to use this function to dynamically
name a node, please make sure the `epmd` operating system process
is running by calling `epmd -daemon`.
Invoking this function when the distribution has already been started,
either via the command line interface or dynamically, will return an
error.
## Examples
{:ok, pid} = Node.start(:example, :shortnames, 15000)
This functionality starts the `:net_kernel` and other
related processes.
"""
@spec start(node, :longnames | :shortnames, non_neg_integer) :: {:ok, pid} | {:error, term}
def start(name, type \\ :longnames, tick_time \\ 15000) do
@@ -265,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
@@ -275,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
+42 -87
View File
@@ -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
+22 -32
View File
@@ -12,7 +12,7 @@ defmodule Path do
that require it (like `wildcard/2` and `expand/1`).
"""
@type t :: IO.chardata()
@type t :: :unicode.chardata()
@doc """
Converts the given path to an absolute one. Unlike
@@ -20,7 +20,7 @@ defmodule Path do
## Examples
### Unix-like operating systems
### Unix
Path.absname("foo")
#=> "/usr/local/foo"
@@ -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 """
@@ -195,7 +186,7 @@ defmodule Path do
## Examples
### Unix-like operating systems
### Unix
Path.type("/") #=> :absolute
Path.type("/usr/local/bin") #=> :absolute
@@ -223,7 +214,7 @@ defmodule Path do
## Examples
### Unix-like operating systems
### Unix
Path.relative("/usr/local/bin") #=> "usr/local/bin"
Path.relative("usr/local/bin") #=> "usr/local/bin"
@@ -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
@@ -522,7 +512,6 @@ defmodule Path do
do_join(left, right, os_type) |> remove_dir_sep(os_type)
end
defp do_join(left, "/", os_type), do: remove_dir_sep(left, os_type)
defp do_join("", right, os_type), do: relative(right, os_type)
defp do_join("/", right, os_type), do: "/" <> relative(right, os_type)
@@ -565,7 +554,7 @@ defmodule Path do
"""
@spec split(t) :: [binary]
# Work around a bug in Erlang on Unix-like operating systems
# Work around a bug in Erlang on UNIX
def split(""), do: []
def split(path) do
@@ -627,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.
@@ -693,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
+24 -51
View File
@@ -20,7 +20,7 @@ defmodule Port do
:ok
In the example above, we have created a new port that executes the
program `cat`. `cat` is a program available on Unix-like operating systems that
program `cat`. `cat` is a program available on UNIX systems that
receives data from multiple inputs and concatenates them in the output.
After the port was created, we sent it two commands in the form of
@@ -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,63 +117,37 @@ 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
will have its stdin and stdout channels closed but **it won't be automatically
terminated**.
While most Unix command line tools will exit once its communication channels
are closed, not all command line applications will do so. You can easily check
this by starting the port and then shutting down the VM and inspecting your
operating system to see if the port process is still running.
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 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:
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, you can wrap the application in a script that checks for stdin.
Here is such script that has been verified to work on bash shells:
#!/usr/bin/env bash
# Start the program in the background
exec "$@" &
pid1=$!
# Silence warnings from here on
exec >/dev/null 2>&1
# Read from stdin in the background and
# kill running program when stdin closes
exec 0<&0 $(
while read; do :; done
kill -KILL $pid1
) &
pid2=$!
# Clean up
wait $pid1
ret=$?
kill -KILL $pid2
exit $ret
Note the program above hijacks stdin, so you won't be able to communicate
with the underlying software via stdin (on the positive side, software that
reads from stdin typically terminates when stdin closes).
#!/bin/sh
"$@" &
pid=$!
while read line ; do
:
done
kill -KILL $pid
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"]])
"""
@@ -248,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
@@ -284,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
@@ -305,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.
"""
+46 -149
View File
@@ -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.
"""
@@ -708,6 +603,8 @@ defmodule Process do
@spec flag(:message_queue_data, :erlang.message_queue_data()) :: :erlang.message_queue_data()
@spec flag(:min_bin_vheap_size, non_neg_integer) :: non_neg_integer
@spec flag(:min_heap_size, non_neg_integer) :: non_neg_integer
@spec flag(:monitor_nodes, term) :: term
@spec flag({:monitor_nodes, term()}, term) :: term
@spec flag(:priority, priority_level) :: priority_level
@spec flag(:save_calls, 0..10000) :: 0..10000
@spec flag(:sensitive, boolean) :: boolean
@@ -724,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.
"""
@@ -737,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
@@ -748,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)
@@ -773,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.
"""

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