lua-users home
lua-l archive

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


On Mon, Dec 20, 2010 at 7:16 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> So how about more constrained version I suggested before, i.e. leave
> prefixexp in but extend it so that it can contain also literal
> constant?
>
>  - prefixexp ::= var | functioncall | ‘(’ exp ‘)’
> + prefixexp ::= var | functioncall | ‘(’ exp ‘)’ | String

It has exactly the same problem:

 a = "hellooo"
 (print or io.write)(a)

Moreover, it is too ad-hoc. We justify parentheses as the only
exception because everything else fits inside them. But to allow
parentheses plus strings, because that is what happens to be useful
right now, is not a good argument.

A real-life example of code that would break in a different fashion, that I (and others, I'm sure) have used many times is code like the following line:

-- Get stdout stream by running "cmd", convert to string, and trim it.
local cmd = io.popen "cmd":read "*a":gsub("^%s*(.-)%s*$", "%1")

The current policy allows the ':' operator to work as a pipe between these functions.  The proposed change would, unfortunately, try to call 'read' on the string "cmd" and gsub on the string "*a".  This would require changing such pipes to either break to multiple lines and separate assignments, or add a bunch of parentheses to get the original meaning:

local f = io.popen "cmd"
local str = f:read "*a"
local cmd = str:gsub("^%s*(.-)%s*$", "%1")
-- or
local cmd = ((io.popen "cmd"):read "*a"):gsub("^%s*(.-)%s*$", "%1")
--
Kevin Vermeer