nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.55k stars 1.47k forks source link

--experimental:views: path expressions constraint prevents useful code #17519

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago

Example

when true:
  proc bar(x: string): auto =
    x.len

  proc fn(a, b: var string): lent string =
    var ret: ptr string
    if a[0] <= b[0]: ret = a.addr
    else: ret = b.addr
    ret[]

  proc main() =
    var a = "abc"
    var b = "defgh"
    doAssert bar(fn(a, b)) == 3 # no copy is made
  main()

Current Output

nim r --experimental:views main

t12080.nim(11, 5) Error: cannot borrow from
  var ret: ptr string
  if a[0] <= b[0]:
    ret = addr(a)
  else:
    ret = addr(b)
  ret[], it is not a path expression; see https://nim-lang.github.io/Nim/manual_experimental.html#view-types-algorithm-path-expressions for details
      var ret: ptr string

Expected Output

works

Possible Solution

redesign --experimental:views from scratch, it's broken beyond repair; there's a much simpler design that doesn't have any of {.views}'s limitations and allows writing efficient code without getting in your way.

see also the many other issues plaguing {.views.} https://github.com/nim-lang/Nim/labels/view%20types, some of which are not fixable with this design.

Additional Information

1.5.1 8573160a44439ffe3fbbdd354188db9c976a2081

reduced case from code referenced in https://github.com/nim-lang/Nim/pull/17500#discussion_r601972687

Araq commented 3 years ago

What is this "much simpler design"? Your previous one wasn't it.