Skip to content

Silent error when (re)defining result #868

@ReneSac

Description

@ReneSac

One of my first errors when I started using nimrod was redefining result. At first I gave up and started using "return" as I would do in any language, but latter I went back and discovered what I was doing wrong. The main problem is that this fails silently, causing the function to return a non-expected result:

proc ret2(): int =
  result = 2

proc ret2NOT(): int =
  var result = 2

echo ret2()     # Prints 2
echo ret2NOT()  # Prints 0

EDIT:

I discovered the real problem:
Renaming "var result" to "var foo" I saw that that if you forget to return a value from a proc, it will return a value anyway because "result" was implicitly initialized. This hides programmer errors. I see no situation where this behavior would be useful, especially because if you just write "proc ret2NOT(): int" the compiler will complain about missing implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions