lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sep 3, 2014, at 1:07 AM, Christopher Kappe <nox_diesque@web.de> wrote:

> Probably the interpreter thinks that dash here is a minus operator and so foo and fighter must be number variables with a likewise numerical result of the subtraction and thus the "key-is-a-string" syntactic sugar doesn't apply. But this wouldn't happen, if the "=" sign was processed first (?).
> 
> -- No necessary information below this line ;-)
> I bring this up because I really much would like to use such syntax. The background is that I like to create svg images with Lua scripts. This is easier, quicker and yields better results for me than struggling with programs like Inkscape, Draw or Dia. Especially the set of attributes for a svg tag is perfectly reflected by Lua tables. E.g. I can make a function call as
> svg.rectangle{ width=100, height=42, fill="black" }
> But attributes with a dash require the ugly syntax
> svg.rectangle{ fill="black", ["stroke-width"]=1.337 }
> 
> So I wonder what you think. Could this kind of syntax be allowed in any later version of the language?
> 
> Best regards,
> Nox
> 


As the Lua docs note, the point here is that the forms { foo = bar } in table constructors and x.foo in table field accesses are only valid when “foo” is a valid Lua identifier. These are of course merely syntactic sugar for the more general form. While you could tinker with the table constructor and make the name string more general, you would not be able to do this with the “.” syntax. The result would be a more complex language, which I for one feel isn’t justified by the modest benefit.

—Tim