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.59k stars 1.47k forks source link

Error in const block inside proc #12172

Open Wh1teDuke opened 5 years ago

Wh1teDuke commented 5 years ago

Test

const TEST = block:
  var test: array[5, string]
  test

proc test =
  const TEST = block:
    var test: array[5, string] # Error here
    test

Error

Test.nim(7, 9) Error: cannot evaluate at compile time: test

>nim -v
Nim Compiler Version 0.20.99 [Linux: i386]
Compiled at 2019-08-29
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: d0e5bd2305db719b0c9acb6a017c8aa579a2f246
active boot switches: -d:release
Araq commented 5 years ago

Regression?

Wh1teDuke commented 5 years ago

Not that I'm aware of. Const block inside a proc doesn't play well

proc test =
  const TEST = block:
    let i = 0 # Error here too
    i
timotheecour commented 4 years ago

see also duplicate issue that involves templates (the more common case where you'll encounter this bug) but it's the same problem

https://github.com/nim-lang/Nim/issues/13795

raising to medium priority, it keeps arising in various forms (mostly via templates, eg toSeq)

jrfondren commented 3 years ago
import strutils

proc example =
  # ... imagine lots of code ...
  # suddenly, a long string!
  when true:
    # some indentation levels
    const str = """
      This is an example.
      It's indented to look nice.
      <-- but no spaces, please.
    """.dedent
    echo str
  when false:
    discard # more code

example()

It's disappointing that this requires a let

ZoomRmc commented 1 year ago

Just bumped into this. It's a bit annoying as this prevents limiting the scope of a constant.