import all symbols from a module

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
sswam
Prole
Posts: 1
Joined: Thu Jul 16, 2020 10:53 am

import all symbols from a module

Post by sswam »

I'm just learning a bit of Love2d and Lua, and I like it so far, but I'm not a fan of writing `love.graphics` 500 times in my program for no good reason. It makes the code more laborious to write, and harder to read. I also prefer not to import all the functions I might want to use one by one. So I wrote a little function to import symbols, much like python's `from math import *`. (I also wrote something similar for JavaScript a while ago.) I'm aware that this is frowned upon for big projects, but I think it's awfully convenient for experimenting and messing around.

Example:

util.lua:

Code: Select all

function use(module, ...)
    for k,v in pairs(module) do
        if _G[k] then
            io.stderr:write("use: skipping duplicate symbol ", k, "\n")
        else
            _G[k] = module[k]
        end
    end
end

io.stdout:setvbuf("no")

use(math)
use(love.graphics)
gprint = love.graphics.print
use(love.audio)
main.lua:

Code: Select all

require "util"

function love.load()
    whale = newImage("whale.png")
    sound = newSource("whale.ogg", "stream")
    play(sound)
    print(sin(pi/6))
end

function love.draw()
    setColor(1, 0, 0)
    circle("fill", 300, 300, 200)
    setColor(1, 1, 1)
    draw(whale, -40, -50)
    gprint("Hello Whaled!", 400, 300)
end
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: import all symbols from a module

Post by ReFreezed »

The problem with this approach is when there are name collisions, like with the print function in the example. A simple solution if you don't want to have "love.graphics" everywhere in the code is to use a shorter name:

Code: Select all

LG = love.graphics
function love.draw()
	print("Now drawing!")
	LG.print("Hello")
end
Both easy to write and read, and no name collisions.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: import all symbols from a module

Post by pgimeno »

My solution is to have this file called ns.lua:

Code: Select all

--[========================================================================[--

Abbreviate LÖVE namespaces as locals

Written by Pedro Gimeno Fortea, donated to the public domain.

Usage (copy-paste this line to every file needing the namespaces):

local la,le,lfs,lf,lg,li,lj,lk,lm,lmo,lp,ls,lsys,lth,lt,lw,ld,lto,lv = require'ns'()

--]========================================================================]--

return function() return
  love.audio, love.event, love.filesystem, love.font, love.graphics,
  love.image, love.joystick, love.keyboard, love.math, love.mouse,
  love.physics, love.sound, love.system, love.thread, love.timer, love.window,
  love.data, love.touch, love.video
end
This has the additional advantage that the namespaces are local, not global.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: import all symbols from a module

Post by milon »

pgimeno wrote: Thu Jul 16, 2020 7:05 pm My solution is to have this file called ns.lua:
...
This has the additional advantage that the namespaces are local, not global.
pgimeno, that's brilliant! Thanks for sharing.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: Google [Bot], Temperar and 210 guests