tlent_from_var.nim 863 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. discard """
  2. output: '''x
  3. [10, 11, 12, 13]'''
  4. """
  5. # bug #14805
  6. type Foo = object
  7. a: string
  8. proc bar(f: var Foo): lent string =
  9. result = f.a
  10. var foo = Foo(a: "x")
  11. echo bar(foo)
  12. # bug #14878
  13. # proc byLentImpl[T](a: T): lent T = a
  14. proc byLentVar[T](a: var T): lent T =
  15. result = a
  16. # result = a.byLentImpl # this doesn't help
  17. var x = 3 # error: cannot take the address of an rvalue of type 'NI *'
  18. var xs = [10,11,12,13] # SIGSEGV
  19. let x2 = x.byLentVar
  20. let xs2 = xs.byLentVar
  21. echo xs2
  22. # bug #22138
  23. type Xxx = object
  24. type
  25. Opt[T] = object
  26. case oResultPrivate*: bool
  27. of false:
  28. discard
  29. of true:
  30. vResultPrivate*: T
  31. func value*[T: not void](self: Opt[T]): lent T {.inline.} =
  32. self.vResultPrivate
  33. template get*[T: not void](self: Opt[T]): T = self.value()
  34. method connect*(
  35. self: Opt[(int, int)]) =
  36. discard self.get()[0]