lua-users home
lua-l archive

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


On Mon, Mar 7, 2011 at 10:38,  <jgiors@threeeyessoftware.com> wrote:
>> -------- Original Message --------
>> Date: Mon, 7 Mar 2011 09:19:27 -0600
>> From: Paul Bonser <misterpib@gmail.com>
>> Subject: Re: Why are Lua Modules Only a Runtime Component of the
>>       Language?
>> To: Lua mailing list <lua-l@lists.lua.org>
>> Message-ID:
>>       <AANLkTi=AcZLCBA-wxOjUaQcPj0pkJOQZ69x0FocuRebu@mail.gmail.com>
>> Content-Type: text/plain; charset=ISO-8859-1
>>
>> On Mon, Mar 7, 2011 at 9:06 AM, Jon Akhtar <akhtar@mindspring.com> wrote:
>> > Is there a compelling reason for not making 'require' and 'module'
>> > keywords in the language.
>>
>> Is there a compelling reason TO make them keywords?
>>
>> >
>> > Since I am working on a static analysis tool for Lua, handling Lua
>> > modules is something I need to do. In doing so, I find myself
>> > continually nagged by this question of why modules have a second class
>> > status. Why is 'require' merely a function with the name 'require'?
>>
>> For the same reason that classes aren't "first-class" in Lua, I'd say.
>>
>> >
>> > The only reasons I can come up with is that modules (like almost
>> > everything in Lua) are optional, and may not be included in embedded
>> > Lua, and that they were built using lower level api's like setfenv.
>>
>> Right, it seems to be the "Lua way" to include a simpler, lower-level
>> functionality that allows you to implement specific, higher-level
>> functionality, but not to force any specific implementation on you.
>>
>> >
>> > That is fair enough. I was just wondering if adding the module system
>> > as a language (grammar) feature vs a library had been considered and
>> > if anyone had any additional insight on the matter.
>>
>> It's part of the standard library, which is implemented in C. I would
>> say keeping the grammar as simple as possible is a pretty valuable
>> goal.
>>
>> Why add something to the grammar when it works fine as a function? For
>> static analysis just assume that when someone says "require" or
>> "module" that they are using the standard functions. If they aren't,
>> they either know what they are doing or are asking for trouble anyway,
>> right? :P
>>
>> >
>> > Thanks In Advance,
>> >
>> > Jon Akhtar
>> >
>>
>> --
>> Paul Bonser
>> http://probablyprogramming.com
>> http://twitter.com/pib
>
> +1 all of the replies
>
> In addition, it is nice that it is possible to do things like the
> following:
>
> --[[]]  -- Note: Untested code snippet for expository purpose only.
> --[[]]  -- Re-load module "myModuleName"
> --[[]]  package.loaded[ "myModuleName" ] = nil
> --[[]]  require "myModuleName"
>
> Also, if require were changed into a keyword, what would that mean for
> package.path? Would it be converted to a function? Would that function
> also be a keyword? What about cpath?
>
> It starts getting pretty ugly, though I suppose the data members of the
> "package" table could continue to be supported as-is, even if "require"
> and "module" became keywords. However, it seems strange (at least to me)
> to have keywords whose behavior depends on runtime variables stored in a
> global table.
>
> John Giors
> http://ThreeEyesSoftware.com
> jgiors@ThreeEyesSoftware.com
>
>
>
>

Having things be functions means you can change their behaviour by
replacing them with other functions, which is one of the things that
makes Lua so flexible and extensible. For example like James said
about only loading particular modules, or perhaps you want some
debugging info:
local _require = require
function require(mdl)
    print("Loading module:", mdl)
    return _require(mdl)
end

In C/C++ I often find myself wishing I could globally redefine malloc,
free, new and delete to print debugging info to track down
memory-related bugs... in Lua you can do this kind of thing as easily
as shown above!

-- 
Sent from my toaster.