View Source ExToolkit.Kernel (ExToolkit v0.9.13)
Basic language primitives to ease development flow.
Summary
Functions
Defines a module attribute and a function to get it. Inspired by attr_reader
from ruby.
Returns the atom :error
.
Wraps a given value in a tuple tagged with :error
.
Wraps a given value in a tuple tagged with :noreply
.
Returns the atom :ok
.
Wraps a given value in a tuple tagged with :ok
.
Determines the type of the given term.
Validates the given options against the given defaults.
Types
Functions
Defines a module attribute and a function to get it. Inspired by attr_reader
from ruby.
Examples
iex> defmodule ExampleModule do
...> require ExToolkit.Kernel
...> defattr foo: :bar
...> end
iex> ExampleModule.foo()
:bar
iex> defmodule ExampleModule2 do
...> require ExToolkit.Kernel
...> defattr name: "ExToolkit", version: "1.0.0"
...> end
iex> %{name: ExampleModule2.name(), version: ExampleModule2.version()}
%{name: "ExToolkit", version: "1.0.0"}
iex> defmodule ExampleModule3 do
...> require ExToolkit.Kernel
...> defattr [version: Version.parse!("1.0.1")]
...> end
iex> ExampleModule3.version()
%Version{major: 1, minor: 0, patch: 1}
@spec error() :: :error
Returns the atom :error
.
Example
iex> error()
:error
Wraps a given value in a tuple tagged with :error
.
Examples
iex> error("something went wrong")
{:error, "something went wrong"}
iex> error(404)
{:error, 404}
Wraps a given value in a tuple tagged with :noreply
.
Examples
iex> noreply("something went wrong")
{:noreply, "something went wrong"}
iex> noreply(404)
{:noreply, 404}
@spec ok() :: :ok
Returns the atom :ok
.
Example
iex> ok()
:ok
Wraps a given value in a tuple tagged with :ok
.
Examples
iex> ok(42)
{:ok, 42}
iex> ok("hello")
{:ok, "hello"}
Determines the type of the given term.
Examples
iex> type_of(3.14)
:float
iex> type_of(42)
:integer
iex> type_of(true)
:boolean
iex> type_of(:atom)
:atom
iex> type_of("string")
:binary
iex> type_of([1, 2, 3])
:list
iex> type_of({:ok, 1})
:tuple
iex> type_of(%{})
:map
iex> type_of(fn -> :ok end)
:function
iex> type_of(%URI{})
:struct
iex> type_of(%RuntimeError{})
:exception
iex> type_of(self())
:pid
iex> type_of(nil)
nil
Validates the given options against the given defaults.
Examples
iex> validate_opts!([foo: :bar], [foo: :baz])
%{foo: :bar}
iex> validate_opts!([], [foo: :baz])
%{foo: :baz}
iex> validate_opts!([foo: :bar, bar: :baz], %{foo: :bar})
** (ArgumentError) unknown keys [:bar] in [foo: :bar, bar: :baz], the allowed keys are: [:foo]
iex> validate_opts!(%{foo: :bar, bar: :baz}, %{foo: :bar, bar: :baz})
%{foo: :bar, bar: :baz}
iex> validate_opts!(%{foo: :bar}, [foo: :bar])
%{foo: :bar}
iex> validate_opts!(%{foo: :bar}, [foo: :baz, bar: :zad])
%{foo: :bar, bar: :zad}