[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Modules that are functions
- From: Andrew Starks <andrew.starks@...>
- Date: Thu, 21 Nov 2013 21:43:33 -0600
On Thursday, November 21, 2013, Coda Highland wrote:
On Thu, Nov 21, 2013 at 2:58 PM, Petite Abeille
<petite.abeille@gmail.com> wrote:
>
> On Nov 21, 2013, at 11:32 PM, Kevin Martin <kev82@khn.org.uk> wrote:
>
>> 2) I can guarantee that at some point in the future, that module which currently only has one function will need another, and the rewrite will be horrific if not returning a table.
>
> Meh… behold the functable:
>
> local aFunction = function() return 'Yeah!' end
> debug.setmetatable( aFunction, { __index = function( self, aKey ) return aKey end } )
>
> print( aFunction() )
> print( aFunction.foo )
> print( aFunction.bar )
> print( aFunction.baz )
>
Or of course go the other way around and return a table that's also
callable, which is probably preferable for reflection.
/s/ Adam
++ for functable. You can pack version information into its fields.
If the function is also a field, the user can avoid the speed penalty, right from require:
" mymod = require("mymod").new"
There is no reason that you can't just do a function, though. If you're redistributing the module, a table is expect, which feels more sorted out.