Nim: proc reference assignments should be required to be explicit.

Created on 4 Feb 2015  路  7Comments  路  Source: nim-lang/Nim

You should be able to assign the return value of a 0-argument function like so:

var foo = bar

Currently, doing this will assign the proc reference of bar to foo. IMO this is unexpected behavior, as the amount of times you want a proc reference to bar is very small compared to the amount of times you want the return value of bar. proc reference assignments should be required to be explicit like so:

var foo: proc = bar
Documentation Medium Priority

Most helpful comment

I actually think that var foo = bar assigning the proc is quite natural if one comes from functional programming. If one wants the returned value, var foo = bar() is two characters away.

Regarding documentation, the manual states

Function calls with no arguments still needs () to distinguish between a call and the function itself as a first class value.

Unless someone disagrees, I suggest to close this

All 7 comments

Eh...I like it like this. If bar has side effects, that may be wanted. The only time I usually see this in programming languages is pure ones (e.g. Haskell).

No way.

  1. It's clearly documented ... somewhere.
  2. Would break too much code.

My main reason for creating this issue is that this behavior conflicts with the behavior in some other parts of the language:

proc getNum: int =
    return 2

proc addFive(a: int): int =
    return a + 5

# Notice the compiler accepts addFive instead of addFive(),
# but not getNum instead of getNum().
echo getNum().addFive # Prints 7 as expected.

I originally wrote something like that, saw that I could use addFive without the parenthesis, extrapolated that behavior and proceeded to do var foo = bar in another part of my program. This of course didn't work and it was really frustrating to debug, because I was expecting var foo = bar to work given that I could use a call as above.

Would break too much code.

I'll admit that I don't have usage stats for this behavior, but I think its worth the break to make this change pre-1.0. It is a matter of preference though.

I know several other languages where Nim's behavior is used.

Some is this simply unavoidable, and this would probably break more code that every other recent change combined.

Yes, it is inconsistent with leaving out parens elsewhere. However, let p: proc = ... probably wants the proc types as well, which sounds like a pain imo.

@reactormonk a good way to see the impact of this would be to modify the compiler to follow the new behavior, and see just how much breaks.

As for the idea in general, I'm on the fence. I would rather just have an error or warning printed which states that the assignment is ambiguous.

I actually think that var foo = bar assigning the proc is quite natural if one comes from functional programming. If one wants the returned value, var foo = bar() is two characters away.

Regarding documentation, the manual states

Function calls with no arguments still needs () to distinguish between a call and the function itself as a first class value.

Unless someone disagrees, I suggest to close this

Was this page helpful?
0 / 5 - 0 ratings