mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 01:44:37 +00:00
* spec for view types * spec additions * refactoring; there are two different kinds of views * refactorings and spec additions * enforce that view types are initialized * enforce borrowing from the first formal parameter * enforce lifetimes for borrowing of locals * typo in the manual * clarify in the implementation what a borrow operation really is
24 lines
546 B
Nim
24 lines
546 B
Nim
discard """
|
|
errormsg: "cannot borrow"
|
|
nimout: '''tcannot_borrow.nim(21, 7) Error: cannot borrow meh; what it borrows from is potentially mutated
|
|
tcannot_borrow.nim(22, 3) the mutation is here'''
|
|
line: 21
|
|
"""
|
|
|
|
|
|
{.experimental: "views".}
|
|
|
|
type
|
|
Foo = object
|
|
field: string
|
|
|
|
proc valid(s: var seq[Foo]) =
|
|
let v: lent Foo = s[0] # begin of borrow
|
|
echo v.field # end of borrow
|
|
s.setLen 0 # valid because 'v' isn't used afterwards
|
|
|
|
proc dangerous(s: var seq[Foo]) =
|
|
let meh: lent Foo = s[0]
|
|
s.setLen 0
|
|
echo meh.field
|