Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_ (underscore) is not ignored inside templates #13804

Closed
metagn opened this issue Mar 30, 2020 · 6 comments · Fixed by #21192
Closed

_ (underscore) is not ignored inside templates #13804

metagn opened this issue Mar 30, 2020 · 6 comments · Fixed by #21192

Comments

@metagn
Copy link
Collaborator

metagn commented Mar 30, 2020

Example

  1. _ gets gensym'd if used as a variable name:
template foo() =
  let _ = 3

foo()
  1. _ is substituted if used as a parameter name for the template
template foo(_: int) =
  for _ in 1..3: # Error: identifier expected, but found '4'
    echo "hello"

foo(4)

Current Output

(2, 7) Hint: '_`gensym133006' is declared but not used [XDeclaredButNotUsed]
(5, 5) Error: identifier expected, but found '4'

Expected Output

doesn't warn or error, correctly does not gensym in the first case, and does not try to substitute in the second case

Workaround

template foo() =
  let _ {.inject.} = 3

foo()
$ nim -v
Nim Compiler Version 1.2.6
@Araq
Copy link
Member

Araq commented Mar 30, 2020

What's the use case though? Your example should use discard.

@SolitudeSF
Copy link
Contributor

template foo =
  let (_, a, b) = (3, 4, 5)

warns when unpacking a tuple

@demotomohiro
Copy link
Contributor

When 2 "_" are used in tuple unpacking in a template, it generate compile error.

#No compile error
let (_, _, x) = (1, 2, 3)
echo x

template test() =
  #Error: redefinition of '_`gensym3387051'; previous declaration here: f:\temp\testTU.nim(5, 8)
  let (_, _, x) = (1, 2, 3)
  echo x

test()

@metagn
Copy link
Collaborator Author

metagn commented Sep 26, 2020

Just ran into this bug again, but in a different way. Not only do templates not ignore _, they also try to substitute with it if it's the name of a parameter.

template foo(_: int) =
  for _ in 1..3: # Error: identifier expected, but found '4'
    echo "hello"

foo(4)

@metagn metagn changed the title _ (underscore) is gensym'd inside templates, therefore gives unused warning _ (underscore) is not ignored inside templates Sep 26, 2020
@ringabout
Copy link
Member

It can be used inside procs too.

proc main(_: int) = echo _

#13443 (comment)

@metagn
Copy link
Collaborator Author

metagn commented Oct 28, 2022

Fixing #13443 should fix second part (although should still be tested) so don't need to specifically fix it yet.

Edit: This might not be true, semtempl might still need to be patched

metagn added a commit to metagn/Nim that referenced this issue Dec 28, 2022
Araq pushed a commit that referenced this issue Jan 8, 2023
* underscores for routine parameters

fixes #13443, fixes #13804, refs #21121

* add changelog + more tests

* support generics and ensure inferred lambdas work
survivorm pushed a commit to survivorm/Nim that referenced this issue Feb 28, 2023
* underscores for routine parameters

fixes nim-lang#13443, fixes nim-lang#13804, refs nim-lang#21121

* add changelog + more tests

* support generics and ensure inferred lambdas work
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
* underscores for routine parameters

fixes nim-lang#13443, fixes nim-lang#13804, refs nim-lang#21121

* add changelog + more tests

* support generics and ensure inferred lambdas work
narimiran pushed a commit that referenced this issue Apr 25, 2023
narimiran pushed a commit that referenced this issue Apr 25, 2023
narimiran pushed a commit that referenced this issue Apr 25, 2023
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
* underscores for routine parameters

fixes nim-lang#13443, fixes nim-lang#13804, refs nim-lang#21121

* add changelog + more tests

* support generics and ensure inferred lambdas work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants