ngs-lang / ngs

Next Generation Shell (NGS)
https://ngs-lang.org/
GNU General Public License v3.0
1.46k stars 41 forks source link

[FR] Trailing closure syntax #552

Closed rdje closed 2 years ago

rdje commented 2 years ago

NGS supports Closures.

But it doesn't seem to have support for the so called trailing closure syntax.

It is when calling a function and the last argument of that function is a closure, one may pop it out of the function's parentheses and have it dangling outside of it after the closing parenthesis of the function, like this

  1. funcname(..., closure) <=> funcname(...) closure
  2. funcname(closure) <=> funcname() closure <=> funcname closure

When dangling outside of the function's argument list, the closure has the following form

{ code }

Ruby, Groovy, Kotlin, Swift and Raku support this syntactic sugar feature.

ilyash-b commented 2 years ago

But it doesn't seem to have support for the so called trailing closure syntax.

The Problem

I am thinking about this from time to time and don't know how to solve syntactic ambiguity that this would cause:

if CONDITION YES_BODY NO_BODY, for example: if 1 my_func() { A + 1 }. Is {A + 1} the NO_BODY here or last argument to my_func().

Closest Thing

NGS has a way to supply arguments after the my_func() parenthesis using with or do syntax:

test("my test") with { ... }
my_func(1,2) do { ... }

Thoughts

rdje commented 2 years ago

Ok then, it seems you already had this trailing closure syntax feature in mind for some time now.

So feel free to close my FR then.

ilyash-b commented 2 years ago

Let's break our heads on that if/when there is more demand.