Skip to content

Commit

Permalink
byRef => byAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jan 19, 2020
1 parent 653ab94 commit 051a689
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
This simplifies code by reducing need for if-else branches around intermediate maybe nil values.
Eg: `echo ?.n.typ.kind`

- Added `sugar.byRef` allowing a ref syntax for lvalue expressions
- Added `sugar.byAddr` allowing a ref syntax for lvalue expressions

## Library changes

Expand Down
8 changes: 4 additions & 4 deletions lib/pure/sugar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ when (NimMajor, NimMinor) >= (1, 1):

proc splitDefinition*(def: NimNode): tuple[lhs: NimNode, rhs: NimNode, exported: bool] {.since: (1,1).} =
## allows library constructs such as:
## `byRef: a2=expr`
## `byRef: a2*=expr` (to indicate `export`)
## `byAddr: a2=expr`
## `byAddr: a2*=expr` (to indicate `export`)
doAssert def.kind == nnkStmtList and def.len == 1
let def2 = def[0]
case def2.kind
Expand All @@ -383,13 +383,13 @@ proc splitDefinition*(def: NimNode): tuple[lhs: NimNode, rhs: NimNode, exported:
else: doAssert false, $def2.kind
expectKind(result.lhs, nnkIdent)

macro byRef*(def: untyped): untyped {.since: (1,1).} =
macro byAddr*(def: untyped): untyped {.since: (1,1).} =
## Defines a ref alias for lvalue expressions. The expression is evaluated
## only once, and any side effects will only be evaluated once, at declaration
## time.
runnableExamples:
var x = @[1,2,3]
byRef: x1=x[1]
byAddr: x1=x[1]
x1+=10
doAssert type(x1) is int and x == @[1,12,3]

Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/msugar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ type Foo = object
bar: Bar

var foo*: Foo
byRef: barx*=foo.bar.x
byAddr: barx*=foo.bar.x
12 changes: 6 additions & 6 deletions tests/stdlib/tsugar.nim
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import sugar
import macros

block byRefBlock:
block byAddrBlock:
var count = 0
proc identity(a: int): auto =
block: count.inc; a
var x = @[1,2,3]
byRef: x1=x[identity(1)] # the lvalue expression is evaluated only here
byAddr: x1=x[identity(1)] # the lvalue expression is evaluated only here
doAssert count == 1
x1 += 10
doAssert type(x1) is int # use x1 just like a normal variable
doAssert x == @[1,12,3]
doAssert count == 1 # count has not changed
doAssert compiles (block: byRef: x2=x[0])
doAssert not compiles (block: byRef: x2=y[0])
doAssert compiles (block: byAddr: x2=x[0])
doAssert not compiles (block: byAddr: x2=y[0])
# correctly does not compile when using invalid lvalue expression

block byPtrfBlock:
type Foo = object
x: string
proc fun(a: Foo): auto =
doAssert not compiles (block: byRef: x=a.x)
doAssert not compiles (block: byAddr: x=a.x)
let foo = Foo(x: "asdf")
fun(foo)

# test byRef with export
# test byAddr with export
import ./msugar
barx += 10
doAssert $foo == "(bar: (x: 10))"
Expand Down

0 comments on commit 051a689

Please sign in to comment.