https://github.com/qqwy/elixir-coerce
Automatic coercion of types that can be promoted to each-other for Elixir.
https://github.com/qqwy/elixir-coerce
Last synced: 7 months ago
JSON representation
Automatic coercion of types that can be promoted to each-other for Elixir.
- Host: GitHub
- URL: https://github.com/qqwy/elixir-coerce
- Owner: Qqwy
- Created: 2017-07-18T21:37:57.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-04-10T06:59:36.000Z (over 2 years ago)
- Last Synced: 2024-03-14T19:54:09.268Z (over 1 year ago)
- Language: Elixir
- Size: 27.3 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Coerce
[](https://hex.pm/packages/coerce)
[](https://travis-ci.org/Qqwy/elixir-coerce)Coerce allows defining coercions between data types.
These are standardized conversions of one kind of data to another.
A coercion can be defined using the `defcoercion` macro.The code that coercion is compiled to attempts to ensure that the result
is relatively fast (with the possibility for further optimization in the future).
Coerce does _not_ come with built-in coercions, instead allowing libraries that build on top of it
to define their own rules.## Examples
```elixir
iex> require Coerce
iex> Coerce.defcoercion(Integer, Float) do
iex> def coerce(int, float) do
iex> {int + 0.0, float}
iex> end
iex> end
iex> Coerce.coerce(1, 2.3)
{1.0, 2.3}
iex> Coerce.coerce(1.4, 42)
{1.4, 42.0}```
```elixir
iex> require Coerce
iex> Coerce.defcoercion(BitString, Atom) do
iex> def coerce(str, atom) do
iex> {str, inspect(atom)}
iex> end
iex> end
iex> Coerce.coerce("foo", Bar)
{"foo", "Bar"}
iex> Coerce.coerce("baz", :qux)
{"baz", ":qux"}
```## Installation
The package can be installed
by adding `coerce` to your list of dependencies in `mix.exs`:```elixir
def deps do
[
{:coerce, "~> 1.0"}
]
end
```Documentation can
be found at [https://hexdocs.pm/coerce](https://hexdocs.pm/coerce).## Changelog
- 1.0.1 - Coercion implementation modules no longer show up in generated documentation.
- 1.0.0 - First feature-complete stable release