lua-users home
lua-l archive

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


I was going to note that Smalltalk had gotten away without macros. Note that
Smalltalk does one other clever thing, however. There's a way to have a
block return a value to the code that invoked it, but there is also a way to
have it return a value from the method invocation in which it was created.
(I don't remember whether this means that Smalltalk has first class
continuations.)

Ruby doesn't have macros as far as I know. It seems to be a little bit more
convenient than Lua for defining some constructs mostly because there seems
to be an easy way to change the environment in which a block of code
executes.

Scheme got by without macros for a long time. On the other hand, Paul Graham
cites macros as one of the great strengths of Common LISP.

Once one gets past optimization, I think the key use for macros is making
common idioms more natural to write. For example, it's hard to argue that
the following is in any way natural to write:

    local function helper( file, ... )
        pcall( io.close, file )
        return pcall2call( ... )
            -- pcall2call is a function which takes pcall results and turns
            -- them back into either call results or an exception throw
    end

    function doWithOpenFile( path, fn, ... )

        local file = assert( io.open( path ) )

        return helper( file, pcall( fn, file, ... ) )

    end

    doWithOpenFile( "/MyFile.txt", function( src, dst )
        for line in src:lines() do
            dst:write( line )
            dst:write( "\n" )
        end
    end, destinationFile )

Instead one wants to write something like:

    withOpenFile( "/MyFile.txt" ) do( src )

        for line in src:lines() do
            destinationFile :write( line )
            destinationFile :write( "\n" )
        end

    end

Macros can potentially help here though it's possible that the 80/20 rule
applies and minor syntactic and semantic extensions to Lua could handle most
of the cases.

Mark