koka-lang / koka

Koka language compiler and interpreter
http://koka-lang.org
Other
3.16k stars 151 forks source link

Allow named arguments anywhere #491

Open TimWhiting opened 2 months ago

TimWhiting commented 2 months ago

The main motivation for this feature is to allow trailing lambdas to come after named arguments. I've run into this a lot.

f(1, x="something") fn()
   "something else"

(Currently an error).

Essentially, instead of thinking of formal parameters as named or fixed, we consider them required or optional, with implicit parameters being required parameters, but optional as arguments. Arguments are still thought of as named or fixed.

Subsumption proceeds as follows:

After matching the named arguments to formal parameters, the fixed arguments must match the ordering of the remaining parameters, with optional parameters staying optional.

In making these changes I noticed that the syntax/run test subdirectory was not actually being run, since it inherited a -l flag from the syntax directory. I moved those tests into a syntax/no-run subdirectory to prevent the conflicting flags.

Alternatively we could adjust the trailing lambda desugaring to put it prior to all named arguments. See PR #533

The main advantage of this particular solution is that it also allows for function "configuration" to happen prior to giving common arguments that would cause more indentation and visual clutter / noise if named.

div(
  classes="x,y,z", 
  div(
...
))

Could be written like this, but becomes noisy and causes deeper indentation (depends how you format it)

div(
  classes="x,y,z", 
  child=div(
...
))