[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Modules that are functions
- From: "John Hind" <john.hind@...>
- Date: Fri, 22 Nov 2013 09:22:47 -0000
> From: Craig Barnes <craigbarnes85@gmail.com>
> Subject: Re: Modules that are functions
> > 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.
>
> How so? It seems like a case of just changing:
>
> local fn = require "modname"
>
> to:
>
> local t = require "modname"
> local fn = t.fn
Of course you can just write this in one line:
local fn = require("modname").fn
This also works nicely for a library table with more than one entry:
local fn1 = require("modname").fn1
local fn2 = require("modname").fn2
Which is still very efficient because 'require' only loads the module the first time and caches the value it returns, so the second 'require' is just a table lookup.
I'm still thinking about whether if you have a module that has only one function it is better to return that function directly or in a single entry table and this has been a focussing discussion.
---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com