https://github.com/demerphq/assign_when
Elixir macro to avoid duplicated code when conditionally updating a variable by allowing when on assignment statements
https://github.com/demerphq/assign_when
Last synced: 6 months ago
JSON representation
Elixir macro to avoid duplicated code when conditionally updating a variable by allowing when on assignment statements
- Host: GitHub
- URL: https://github.com/demerphq/assign_when
- Owner: demerphq
- License: apache-2.0
- Created: 2025-04-02T08:05:37.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-04-19T22:45:19.000Z (8 months ago)
- Last Synced: 2025-05-09T07:56:58.587Z (8 months ago)
- Language: Elixir
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# AssignWhen
Allows the use of `when` clauses on assignment statements as an alternative to using `if`,
avoiding the need for boilerplate `else` clauses which return the original variables when
the condition is `false`.
Statements like this:
```elixir
x = if condition(), do: whatever, else: x
```
can be replaced with code like this:
```elixir
x = whatever when condition()
```
For instance you can write
```elixir
x = 1 when x == nil
```
Exports no functions, just a macro. The macro does no validation,
but it does work on tuples and anything else that can be expressed
as THING = THING
Macro code originally by José Valim
## Installation
The package can be installed by adding `assign_when` to your list of dependencies
in `mix.exs`:
```elixir
def deps do
[
{:assign_when, "~> 0.1.0"}
]
end
```
The documentation for this package can be found at .