lua-users home
lua-l archive

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

> From: Craig Barnes <>
> 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.