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

Touching result prevents returning with proc's body as an expression #21494

Closed
ZoomRmc opened this issue Mar 9, 2023 · 3 comments
Closed

Touching result prevents returning with proc's body as an expression #21494

ZoomRmc opened this issue Mar 9, 2023 · 3 comments

Comments

@ZoomRmc
Copy link
Contributor

ZoomRmc commented Mar 9, 2023

Description

func foo(): bool =
  result = true
  false

Nim Version

Verified for Nim 1.6.10 and 1.9.1.

Current Output

`Error: expression 'false' is of type 'bool' and has to be used (or discarded)`

Expected Output

Evaluate to `false`

Possible Solution

No response

Additional Information

Mixing different kinds of returns is not such a good style, but looks like it should be valid. If it's somehow not, the error message has to reflect that.

Seems to be related: #16855

@ghost
Copy link

ghost commented Mar 9, 2023

As far as I know that's intended, it was even documented somewhere, will try to find it.

@Araq
Copy link
Member

Araq commented Mar 9, 2023

Works as intended.

@Araq
Copy link
Member

Araq commented Mar 9, 2023

From the manual:

Void context

In a list of statements, every expression except the last one needs to have the
type void. In addition to this rule an assignment to the builtin result
symbol also triggers a mandatory void context for the subsequent expressions:

proc invalid*(): string =
  result = "foo"
  "invalid"  # Error: value of type 'string' has to be discarded
proc valid*(): string =
  let x = 317
  "valid"

@ZoomRmc ZoomRmc closed this as completed Mar 9, 2023
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

No branches or pull requests

2 participants