https://github.com/markmbaum/multiassign.jl
Assign the same value/expression to multiple variables with one terse and clear line
https://github.com/markmbaum/multiassign.jl
julia macro
Last synced: 25 days ago
JSON representation
Assign the same value/expression to multiple variables with one terse and clear line
- Host: GitHub
- URL: https://github.com/markmbaum/multiassign.jl
- Owner: markmbaum
- License: mit
- Created: 2021-11-02T19:42:53.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-12-27T19:06:49.000Z (over 4 years ago)
- Last Synced: 2025-01-11T11:52:54.172Z (over 1 year ago)
- Topics: julia, macro
- Language: Julia
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MultiAssign.jl
[](https://github.com/markmbaum/MultiAssign.jl/actions)
[](https://codecov.io/gh/markmbaum/MultiAssign.jl)
`MultiAssign` exports a macro for assigning a value/expression to multiple variables in a single terse and clear line. For example, you can write
```julia
@multiassign x, y, z = zeros(n)
```
instead of
```julia
x = zeros(n)
y = zeros(n)
z = zeros(n)
```
or
```julia
x, y, z = (zeros(n) for _ in 1:3)
```
Importantly, each of the variables is assigned with a distinct call to `zeros` in the example above. The resulting `x`, `y`, and `z` vectors are not copies of each other.
The macro should work wherever multiple variables should be assigned the same value. It simply generates identical assignments for each variable.
Type annotations will also work if you need them, except in global scope. For example
```julia
@multiassign a::Float64, b::Float32, c::Int64 = 0
```
will work inside a function, but not at the global scope of the REPL.