Thoughts on optional commas

classic Classic list List threaded Threaded
62 messages Options
1234
Reply | Threaded
Open this post in threaded view
|

Thoughts on optional commas

Greg Hill-2
Occasionally I forget commas when I create large nested tables in Lua.  It
reminds me of when I was learning c and always forgetting to place semicolons at
the end of a line.  I'm so happy that semicolons are optional in Lua.  I wish
commas were too.

Could this work?

Example:

local node= {
    {template="window"
        {template="rect" x=10 y=100 alpha=1
           
            s1 = {color="red"}
            s2 = {color="blue" x="=x+1"}
            s3 = {color="blue" x=10}

        }
        {template="textbox" }
    }
}

Instead of traditionally:

local node= {
    {template="window",
        {template="rect", x=10, y=100, alpha=1,
           
            s1 = {color="red",},
            s2 = {color="blue", x="=x+1",},
            s3 = {color="blue", x=10,},
       

        },
        {template="textbox", },
    },
}

I always end up using trailing commas in my tables and it feels wasteful.

Any thoughts?




Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Duncan Cross
On Sat, Mar 3, 2012 at 1:11 PM, Greg <[hidden email]> wrote:
> Occasionally I forget commas when I create large nested tables in Lua.  It
> reminds me of when I was learning c and always forgetting to place semicolons at
> the end of a line.  I'm so happy that semicolons are optional in Lua.  I wish
> commas were too.
>
> Could this work?

Unfortunately it would introduce ambiguities. Lua supports a mix of
key-value table syntax and array-style table syntax, and also supports
optional parentheses in function calls when calling with a single
string or table literal parameter. So for example, if
commas/semicolons between table fields were optional, {a = b "test"}
could be either {a = b; [1] = "test"} or {a = b("test")}. There are
probably other ambiguities too.

-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Greg Hill-2
> Unfortunately it would introduce ambiguities. Lua supports a mix of
> key-value table syntax and array-style table syntax, and also supports
> optional parentheses in function calls when calling with a single
> string or table literal parameter. So for example, if
> commas/semicolons between table fields were optional, {a = b "test"}
> could be either {a = b; [1] = "test"} or {a = b("test")}. There are
> probably other ambiguities too.

I agree that it is unfortunate.  It seems sad to miss an opportunity to simplify
a language to accommodate an obscure part of the syntax.  

Personally I'd be in favor of dropping the option of no parentheses from
function calls if I could drop all commas from the language.  This would break
compatibility with existing code but the fix would be trivial.

I wonder if there are any other ambiguities?

- Greg



Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Patrick Rapin
> Personally I'd be in favor of dropping the option of no parentheses from
> function calls if I could drop all commas from the language.  This would break
> compatibility with existing code but the fix would be trivial.

A better compromise would be a allow removal of comma only after the
key-value syntax
  key = value
or (why not)
  ["key"] = value

But not for array syntax
 t = { 3 2 1 'Boom' }

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Robert Virding
I would personally not like as it makes the syntax inconsistent, in this case sometimes it is mandatory and sometimes it isn't. Consistency has a lot of benefits.

Robert

----- Original Message -----

> > Personally I'd be in favor of dropping the option of no parentheses
> > from
> > function calls if I could drop all commas from the language.  This
> > would break
> > compatibility with existing code but the fix would be trivial.
>
> A better compromise would be a allow removal of comma only after the
> key-value syntax
>   key = value
> or (why not)
>   ["key"] = value
>
> But not for array syntax
>  t = { 3 2 1 'Boom' }
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Greg Hill-2
> I would personally not like as it makes the syntax inconsistent, in this case
sometimes it is mandatory and
> sometimes it isn't. Consistency has a lot of benefits.

I too am a big fan of consistency.  We would be more consistent (IMHO) if we
dropped the option of no parentheses from function calls.  I think we could then
remove commas from the language (or at least make them optional).

function test(a, b, c)
   print "note"
   for i,v in ipairs(a) do
       b = {1, 2, 3, "boom", {4,5,6}, }
   end
   return a, b, 15, "ok"
end

could become


function test(a b c)
   print("note")
   for i v in ipairs(a) do
       b = {1 2 3 "boom" {4 5 6} }
   end
   return a b 15 "ok"
end

This feels much cleaner and consistent to me.

- Greg


Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Tim Mensch
On 3/3/2012 9:12 AM, Greg wrote:
> I too am a big fan of consistency. We would be more consistent (IMHO)
> if we dropped the option of no parentheses from function calls.

Feel free to create a patch and use it yourself, but don't expect a lot
of people to support losing such a core feature. Even if you never say:

foo "bar"

or

foo{ bar=1 }

...there are a lot of people who rely on that behavior. The latter is an
awesome way to get sparse named parameters for functions, and I'm sure
I'm not the only one who thinks so. ;) I even use the string syntax for
translations: I define a function "_t()" and then prefix every string
with it:

print( _t "this is a string that will be translated" )

Then later _t can look up the English and replace it with the
appropriate translation for the locale.

Also, breaking such a core behavior means that you're walling yourself
off from other Lua libraries. Which maybe you don't care about, and
that's fine. But many of us do. :)

Tim

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Philipp Janda
In reply to this post by Greg Hill-2
On 03.03.2012 17:12, Greg wrote:
>> I would personally not like as it makes the syntax inconsistent, in this case
> sometimes it is mandatory and
>> sometimes it isn't. Consistency has a lot of benefits.
>
> I too am a big fan of consistency.  We would be more consistent (IMHO) if we
> dropped the option of no parentheses from function calls.  I think we could then
> remove commas from the language (or at least make them optional).

{ this ("%s):format( "would also be ambiguous" ) }

>
> [...]
>
> - Greg
>

Philipp




Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Axel Kittenberger
In reply to this post by Greg Hill-2
> function test(a b c)
>   print("note")
>   for i v in ipairs(a) do
>       b = {1 2 3 "boom" {4 5 6} }
>   end
>   return a b 15 "ok"
> end
>
> This feels much cleaner and consistent to me.

Not possible with existing Lua without changing also some other
places, first it would have to get rid of function calls without
brackets syntax, and would have to replace key specification for
tables, for example : instead of =. Then it might work, but its more a
new dialect with changed syntax instead of a fix to Lua.

BTW: I find it tedious talking so much about syntax, when the
interesting stuff happens at semantics. This is not just this thread,
or this maillinglist, this goes through most discussions about
programming languages. If you don't like the syntax, just do a
source-to-source preprocessor, like Moonscript.

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Christopher Berardi
In reply to this post by Greg Hill-2
On Sat, Mar 03, 2012 at 04:12:36PM +0000, Greg wrote:
> function test(a b c)
>    print("note")
>    for i v in ipairs(a) do
>        b = {1 2 3 "boom" {4 5 6} }
>    end
>    return a b 15 "ok"
> end
>
> This feels much cleaner and consistent to me.

No, it doesn't seem better to me. I find it very confusing and
ambiguous. What if you start forgetting whitespace? What if it looked
like this:

function test(a b c)
   print("note")
   for i v in ipairs(a) do
       b = {"1"2"3""boom"{"4"5"6"}}
   end
   return "a""b"15"ok"
end

Do you leave commas out of lists when you write normally? Do you write
sentences like this:

I'm going to the grocery store, the library, the dry cleaners, and the gas station.

or like this:

I'm going to the grocery store the library the dry cleaners and the gas station.


--
Christopher Berardi
http://www.natoufa.com/

May grace and peace by yours in abundance.

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Petite Abeille

On Mar 4, 2012, at 12:39 AM, Christopher Berardi wrote:

> Do you leave commas out of lists when you write normally?

Oh, my… I was just waiting for someone to bring Lynne Truss into the conversation…

A panda walks into a café. He orders a sandwich, eats it, then draws a gun and proceeds to fire it at the other patrons.

"Why?" asks the confused, surviving waiter amidst the carnage.

The panda pauses on his way out, produces a badly punctuated wildlife manual, and tosses it over his shoulder.

"Well, I'm a panda," he says at the door. "Look it up."

The waiter turns to the relevant entry and, sure enough, finds an explanation:

"Panda. Large black-and-white bear-like mammal, native to China. Eats, shoots and leaves."
Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Greg Hill-2
In reply to this post by Tim Mensch
> Feel free to create a patch and use it yourself, but don't expect a lot
> of people to support losing such a core feature. Even if you never say:
>
> foo "bar"
>
> or
>
> foo{ bar=1 }


I agree that this is pretty nice.  It don't use it nearly as often as I use
commas.  My average Lua file has over 200 commas and not many instances of foo{
bar=1}.   I usually only this with require statements.  Ex: require "utils"

- Greg






Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Greg Hill-2
In reply to this post by Christopher Berardi
> Do you write sentences like this:

> I'm going to the grocery store the library the dry
> cleaners and the gas station.

Good point.  However I remember the day when I thought semicolons
were mandatory punctuation.  I'm glad in Lua that they are optional.  
I think that commas could still be used for clarity but could be
optional too.

- Greg





Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Greg Hill-2
In reply to this post by Axel Kittenberger
> Not possible with existing Lua without changing also some other
> places, first it would have to get rid of function calls without
> brackets syntax, and would have to replace key specification for
> tables, for example : instead of =. Then it might work, but its more a
> new dialect with changed syntax instead of a fix to Lua.

It is unfortunate that this change couldn't be a superset of Lua.  

> BTW: I find it tedious talking so much about syntax, when the
> interesting stuff happens at semantics.

Semantic are far more important in the big picture.  Lua is my favorite language
by far because of its clean syntax.  I'd even go as far as saying I adore the
syntax! :)  The commas just get to me after a while.  I have an average of over
200 commas in every Lua file that I feel are not needed - just like semicolons.
 I don't miss semicolons either.

- Greg


Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Tomas Lundell
In reply to this post by Greg Hill-2
> Occasionally I forget commas when I create large nested tables in Lua.  It
> reminds me of when I was learning c and always forgetting to place semicolons at
> the end of a line.  I'm so happy that semicolons are optional in Lua.  I wish
> commas were too.

In my opinion syntactical sugar is best used to enhance readability rather than aid beginners. I promise that you'll learn to remember the commas.

> I agree that it is unfortunate.  It seems sad to miss an opportunity to simplify a language to accommodate an obscure part of the syntax.

Perhaps it is obscure to you, but a lot of people use and like that syntactical sugar. It makes certain things cleaner, especially emulating named parameter calls:

create_box { width = 2, height = 3 }

as opposed to

create_box({width = 2 height = 3})
Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Greg Hill-2
> a lot of people use and like that syntactical sugar. It makes certain things
> cleaner, especially emulating named parameter calls:
> create_box { width = 2, height = 3 }
> as opposed
> tocreate_box({width = 2 height = 3})

I agree that this is a nice shortcut.  I just don't use it nearly as much as I
use commas.  In in this (contrived) function there are 13 commas.

function test(a, b, c)
   print "note"
   for i,v in ipairs(a) do
       b = {1, 2, 3, "boom", {4,5,6}, }
   end
   return a, b, 15, "ok"
end

My average Lua file has over 200 commas, but I only see a handful of
opportunities to use the create_box { width = 2, height = 3 } syntax.  I'm
trying to optimize by far my largest use case.

- Greg


Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Eike Decker

I omit brackets wherever possible and don't mind commas. Opposed to brackets, commas are typeable (on a German keyboard) without pressing shift keys. And it's also more conveniently positioned. And there's just one comma but two types of brackets (opening / closing), which is also more annoying.

Am 04.03.2012 04:23 schrieb "Greg" <[hidden email]>:
> a lot of people use and like that syntactical sugar. It makes certain things
> cleaner, especially emulating named parameter calls:
> create_box { width = 2, height = 3 }
> as opposed
> tocreate_box({width = 2 height = 3})

I agree that this is a nice shortcut.  I just don't use it nearly as much as I
use commas.  In in this (contrived) function there are 13 commas.

function test(a, b, c)
  print "note"
  for i,v in ipairs(a) do
      b = {1, 2, 3, "boom", {4,5,6}, }
  end
  return a, b, 15, "ok"
end

My average Lua file has over 200 commas, but I only see a handful of
opportunities to use the create_box { width = 2, height = 3 } syntax.  I'm
trying to optimize by far my largest use case.

- Greg


Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Axel Kittenberger
In reply to this post by Greg Hill-2
> Semantic are far more important in the big picture.  Lua is my favorite language
> by far because of its clean syntax.

Thats it what is IMHO is doing it wrong. You should pick your language
by semantics and not its syntax.

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Axel Kittenberger
In reply to this post by Eike Decker
On Sun, Mar 4, 2012 at 11:01 AM, Eike Decker <[hidden email]> wrote:
> I omit brackets wherever possible and don't mind commas. Opposed to
> brackets, commas are typeable (on a German keyboard) without pressing shift
> keys. And it's also more conveniently positioned. And there's just one comma
> but two types of brackets (opening / closing), which is also more annoying.

"And saving on typing is not a good reason - if your typing speed is
the main issue when you're
coding, you're doing something seriously wrong." - Linus Torvalds

Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on optional commas

Dirk Laurie-2
In reply to this post by Axel Kittenberger
Op 4 maart 2012 16:21 heeft Axel Kittenberger <[hidden email]> het
volgende geschreven:
>> Semantic are far more important in the big picture.  Lua is my favorite language
>> by far because of its clean syntax.
>
> Thats it what is IMHO is doing it wrong. You should pick your language
> by semantics and not its syntax.
>

If the syntax is too ugly, one is not tempted to find out
the semantics.  (That's why I never learnt Perl.)  You fall
in love with a language for the sake of its beautiful
syntax but you marry it for life because its semantics
satisfies your real needs so well.

1234