Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zevv/with
with macro for Nim
https://github.com/zevv/with
Last synced: 9 days ago
JSON representation
with macro for Nim
- Host: GitHub
- URL: https://github.com/zevv/with
- Owner: zevv
- Created: 2019-01-27T19:58:19.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-29T14:41:09.000Z (almost 2 years ago)
- Last Synced: 2024-08-04T03:05:19.735Z (3 months ago)
- Language: Nim
- Size: 448 KB
- Stars: 87
- Watchers: 4
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-nim - with - The `with` macro for Nim. (Language Features / Macros)
README
> _`with` is fun, but as its own project it should have a splash image like "Inspired by a forbidden(TM) JavaScript feature!"_
> -jrfondren`with` is a simple macro to replace the deprecated ``{.with.}`` pragma in Nim.
This macro allow you to access fields of an object or tuple without having to
repeatedly type its name. It works by creating tiny templates with the same
name as the field that expand to access to the field in the object.Example:
```nim
type Foo = ref object
first: int
second: string
third: floatvar foo = Foo(first: 1, second: "two", third: 3.0)
with foo:
echo first
if true:
third = float(first)
echo second
`````with`` also works with named tuples:
```nim
var foo = (first: 1, second: "two")
with foo:
first = 3
second = "six"
```Fields from the object can be shadowed by new declarations in the scope where
the new declaration is made. The object field can still be accessed by using its
full dot-expression notation:```nim
var foo = (first: 1, second: "two")
with foo:
assert first == 1
if true:
let first = "dragons"
assert first == "dragons"
assert foo.first == 1
assert first == 1
assert foo.first == 1
```For the brave, ``with`` blocks can be nested, or multiple objects can be passed
in a tuple constructor. Make sure that field names are unique in the nested
objects:```nim
var foo = (first: 1, second: "two")
var bar = (alpha: 42)with foo:
with bar:
first = alphawith (foo,bar):
first = alpha
```