Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/pure/sugar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,22 @@ macro distinctBase*(T: typedesc): untyped =
while typeSym.typeKind == ntyDistinct:
typeSym = getTypeImpl(typeSym)[0]
typeSym.freshIdentNodes

template alias*(name: untyped, bodyOrExpr: untyped): untyped =
## Syntax sugar to create aliases.
##
## Notes:
## Contrary to `let`, aliases are not allocated in memory.
## This is useful to access deep nested object fields
## without incurring the cost of allocation.
runnableExamples:
type Node = ref object
left, right: Node
val: int

let t = Node(left: Node(left: Node(left: Node(val: 10))))
alias(t3, t.left.left.left)

doAssert t3.val == 10

template name(): untyped {.dirty.} = bodyOrExpr