Ignoring extra comma in code

If this won't break something else in the language I think that every expression that uses comma to list multiple items (function/method call, array elements, function/method definition) should allow for an extra comma without giving an error. Therefore the following expressions:

println(“hello”)

point[a,b]

{(x: Int, y: Int) -> x + y}

should do the same as these:

println(“hello”,)

point[a,b,]

{(x: Int, y: Int,) -> x + y}

Just think of commas as semicolons in css - last one is optional. If this can be done (and I can’t think of a case that would pose a problem) it would considerably simplify automated code generation (not that there aren’t other and better ways to generate code but the simpler the better).

2 Likes

Thanks for your suggestion

@abreslav any updates on this? I believe that this feature will simplify a lot of scenarios. So could we expect it in some future?

We’ve discussed it just a few days ago. No decision is made so far. To clarify: we are looking at the following scenarios (and not others):

  • Comment out the last item of a comma-separated list quickly for debug purposes
arrayOf(
  1,
  2,  // <-- not an error!
//  3
)
  • Meaningful diff when adding a new item at the end:
 arrayOf(
   1,
   2, 
+  3,
 )

instead of

 arrayOf(
   1,
-  2
+  2,
+  3
 )
  • Easily changing order of elements in simple text editors

I.e. cases like arrayOf(1, 2, 3,) are not considered a good practice.

Would this address your real-life use cases?

6 Likes

What about making all the commas optional?

So these would be valid:

arrayOf(1 2 3)

arrayOf(
  1
  2
  3
)

This would probably lead to issues with infix functions. I don’t like it. But allowing a trailing comma for varargs would be nice in some cases.

1 Like

This is similar to the trailing comma allowed in enumerations, which I’ve found to be convenient.

1 Like

Yes, that’s exactly what I’d like to see.

1 Like