lqt icon indicating copy to clipboard operation
lqt copied to clipboard

Don't use Globals

Open daurnimator opened this issue 13 years ago • 5 comments

Well behaved lua modules should not set globals; Please fix lqt so it no longer does.

For compatability, older apps can:

local function shallowcopy ( dst , ... ) for i , src in ipairs({...}) do for k , v in pairs ( src ) do dst [ k ] = v end end return dst end

local Qt = shallowcopy ( {} , require"qtcore" , require"qtgui" , ) setmetatable(_G,{__index=Qt} )

daurnimator avatar Nov 13 '11 12:11 daurnimator

This is on my (private) TODO list, for Lua 5.2 - introduce a loader module, which will handle dependencies and be used to import the real libraries.

I imagine something along these lines:

local core, gui, network, xml = require 'lqt' {'core', 'gui', 'network', 'xml'}

Or if you want them global (I do not consider Qt a "module", but a "framework", therefore globals are fine for my application), something like this:

require 'lqt':import{'core', 'gui'} -- this will bring everything in 'core' and 'gui' to _ENV

Saner proposals are welcome :)

mkottman avatar Nov 13 '11 12:11 mkottman

Why not just: local qt = require"qt" qt.load("core") qt.load("gui")

accept a vararg as well: qt.load("core","gui","network" )

For the import thing; tell the user to just use: setmetatable(_G,{__index=qt})

daurnimator avatar Nov 13 '11 12:11 daurnimator

In my opinion, qt:import() is easier to read/understand than setmetatable(_G,{__index=qt}) for those users, who do not know Lua yet (but want to use Lqt). Of course, it would do just that.

Also, there is a question of whether to put all classes into one module qt, or separate them into separate qtcore, qtgui, ...

mkottman avatar Nov 13 '11 13:11 mkottman

You can easily have one loader dll: "qt" and it has the load function that operates as above. and (eg) qtcore just gets passed a table: and it loads all it's classes into it. Other modules would obviously tell the loader dll if they have dependancies (which it would then resolve).

daurnimator avatar Nov 13 '11 13:11 daurnimator

Also i'm not sure about the import.... setting an __index on the global environment isn't a normal thing to do and SHOULD look out of place/strange ==> only 1 module can do it in your whole application.

For users that don't know lua yet; they can use it as boilerplate: and later when they want to learn more: they can figure out what it does/means.

daurnimator avatar Nov 13 '11 13:11 daurnimator