Nim: Support default arguments in sugar.`=>` and sugar.`->`

Created on 24 Sep 2020  路  3Comments  路  Source: nim-lang/Nim

Summary

The macros sugar.=> and sugar.-> do not have support for default arguments.

Description

The syntax would be like (a = 5) => echo a/(a = 5) -> void without a type annotation, and ((a: int) = 5) => echo a/((a: int) = 5) -> void with one, the reason being that (a: int = 5) does not parse as an expression.

Alternatives

Idk

Additional Information

  • I overlooked this in #14200 and I would have implemented it if I knew, but someone else doing it now would be greatly appreciated

Most helpful comment

Meh, I think at that point you can just write out proc... the lambda syntax sugar shouldn't support every single thing, especially when it requires those extra parens.

All 3 comments

Meh, I think at that point you can just write out proc... the lambda syntax sugar shouldn't support every single thing, especially when it requires those extra parens.

As of right now the sugar macros seem fine as-is, the only thing that I find slightly annoying about them is the lack of semicolon support for syntactic consistency's sake but this is a minor nitpick.

Default arguments seem kind of heavyweight for concise lambda syntax where the point is being able to write small, typically one-line functions. Having default argument support seems kinda weird when almost all of the time lambdas are used in a higher-order context - do procedures with default arguments even work correctly in higher-order functions?

Is the point of this more for concise function declaration (i.e. let foo = x => ... as opposed to proc foo(x :int) :auto = ...) as opposed to higher-orders? If so I would just say if you need default arguments, maybe just write a proc.

Ok, if someone really cares they can make a PR, closing. Implementing semicolons is another topic that might be a little difficult, for it to parse correctly, all colons would have to be replaced with a semicolon (StmtListExpr).

Was this page helpful?
0 / 5 - 0 ratings