Giter Site home page Giter Site logo

Comments (4)

mihacooper avatar mihacooper commented on June 2, 2024 2

Hope, problem solved, if not feel free to open another issue

from effil.

adrfantini avatar adrfantini commented on June 2, 2024

If instead of _timersContainerClass I use everywhere, including in the class definition, effil.G._timersContainerClass, everything seems to be working:

Class:

effil = require('effil')

effil.G._timersContainerClass = effil.table()

-- Metamethods
function effil.G._timersContainerClass:__index(key)
    local value = effil.rawget(self, key) -- workaround for effil __index problem in this effil 0.1.0, see #139
    if value ~= nil then
        return value
    end
    return effil.G._timersContainerClass[key]
end

-- Object creation method.
function effil.G._timersContainerClass:new()
    local outer = effil.table()
    effil.setmetatable(outer, self)
    return outer
end

function effil.G._timersContainerClass:getParent()
    return tostring(effil.G._timersContainerClass)
end

Code:

effil = require('effil')

pl = effil.table()

require('timersTEST2')

t = effil.G._timersContainerClass:new()

function checkExpired(t)
    effil = require "effil" -- MUST be global (?)
    print('INSIDE CLASS', effil.G._timersContainerClass) -- OK, xyz as outside
    print('INSIDE PARENT1', effil.G._timersContainerClass:getParent()) -- OK, xyz
    print('INSIDE PARENT2', t:getParent()) -- OK, xyz
end

print('OUTSIDE CLASS', effil.G._timersContainerClass) -- OK, xyz
print('OUTSIDE PARENT1', effil.G._timersContainerClass:getParent()) -- OK, xyz
print('OUTSIDE PARENT2', t:getParent()) -- OK, xyz

effil.sleep(1, 's')

thread = effil.thread(checkExpired)(t)

effil.sleep(1, 's')

thread:status() -- Shows where it failed: in __index while trying t:getParent()

But this is not what I want, since I cannot use effil.G to export the class.
I do not want to export every class I use to every thread I defined, since there are many.

from effil.

mihacooper avatar mihacooper commented on June 2, 2024

Hello, adrfantini!

The problem caused by _G in your function's upvalue again.
You have declared your class global variable:

_timersContainerClass = effil.table()

And after that references it in function:

function _timersContainerClass:getParent()
    return tostring(_timersContainerClass)
end

So, _timersContainerClass:getParent has _G in it's upvalues. But it does not work in Lua, docs here.

To fix the problem you should declare it as a local variable
local _timersContainerClass = effil.table().
And it's better always (where it's possible) do that.

After that you can expose _timersContainerClass like that:
_timersContainerClass = require 'timers' - preferable way (make return _timersContainerClass in your timers module)
Or like that:
_G._timersContainerClass = _timersContainerClass - at the end of 'timers' module. Not really cool, but still works

from effil.

adrfantini avatar adrfantini commented on June 2, 2024

Once again, thank you very much for the time you've taken to dispel my doubts. It's quite clear now!

from effil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.