lua-users home
lua-l archive

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



With the same reasons, there's bound to be an avalanche of "within the spirit of Lua" suggestions.

Why not just make a luaSub or MetaLua syntax mod out of it, and the ones alike? Portability, you may say. There's no way all suggestions will end up in core, and that's not necessarily a bad thing. In fact, I'd see many _existing_ syntax sugar be removable from the Lua core, and added as syntax front end features instead. This would allow explicitly stating when they are needed to be used (s.a. ":" class mechanism, which is arguable and has some unwanted side effects).

This is how you'd make the below syntax work in luaSub:

<<
local function NO_EQUALS
    p:replace( -0,0, "=", "true" )	-- appends to the scope
end

--
--  .field = alt{ ["["]= { exp,"]","=",exp },
--                  -->
--                ["["]= { exp, "]", alt{ ["="]= exp },
--                                        NO_EQUALS },
-- { "<name>", "=", exp }, -- must not be made keyed since -- exp -- 'exp' may also start with "<name>"
--              }
--
return function( syntax )

    local t= syntax.field
    assert( t[0]=="alt" )   -- just checking

    local exp= ref"exp"

    t["["]= { exp, "]", alt{ ["="]= exp,
                             NO_EQUALS } }
end
<<

Then you'd save this file in -say- 'lookup.luas' and refer to using it in your files by starting them with "#!luas -slookup".

Over and out.

-asko



John Hind kirjoitti 24.2.2008 kello 14:24:

About a year ago, Peter Jacobi suggested a syntactic sugar for sets:

planet = {["mercury"], ["venus"], ["earth"]} -- In Lua 5.1, Gives Error '='
expected

Would be compiled as:

planet = {["mercury"]=true, ["venus"]=true, ["earth"]=true}

http://lua-users.org/lists/lua-l/2007-03/msg00767.html

Of course, Peter's syntax could be further simplified to:

planet = {[mercury], [venus], [earth]} -- In Lua 5.1, Gives Error '='
expected

Then you could write:

local input = "pluto"

if planet[input] then
	print(input, " is a planet")
else
	print(input, " is not a planet")
end

I think this is a really nice idiom and entirely within the spirit of Lua.
It expresses sets using the standard table mechanism + syntax sugar in
exactly the same spirit that arrays and objects are implemented in Lua.

Of course, responses focussed on pointing out that you can already do this and various ways of expressing it within the existing language. But that's not the point. Having it in the basic language definition makes Lua better and more expressive at negligible cost. It also allows mixed idioms in data
description that are difficult to implement generically using factory
functions:

local mark = {name="Mark Jones", age=34, [male]}

I definitely vote this excellent suggestion be seriously considered for a
future version of Lua!

- John Hind