Allow arguments from main scope without upvaluing to listeners.

Bit of a wordy title but it is as it sounds. Allow the ability to pass in arguments to a listener from the main scope without the usage of upvaluing. Something like this:

local function Listener(EventArg, Arg2)
-- Do something with this.
end

local Arg2 = "Arg2"

engine:on("event", Listener, Arg2) 
--Arg2 would be passed to Listener as the second argument. The first would be whatever the connection passes.

Makes sense right?

Hey utrain,

Sorry for the late reply. We’ve been busy pushing stuff up for release.

I found this to be quite interesting. Definitely something that we’ll look into. However, upvalues are traditionally “safe-guards” against referencing variables outside it’s scope. This functionality is equivalent to C’s static scope (I believe don’t quote me on it).

Personally, I think a better method would be to establish a listener and have it invoke a return instance instead of creating a new one. For instance (no pun intended), something like this:

local function Listener(EventArg, parentInstance) return end

engine:on("event", function(instance) 
   -- code
end)

parentInstance is the instance of which is being listened for. Instance is the returned parentInstance. This way, it’s easier to identify which instance you’re listening for.

How does upvaluing safe-guard outside of its scope? I’d think it’d actually allow a scope to see variables that is outside of its scope (i.e scope A encloses scope B, scope B can see scope A’s variables because of upvaluing).

It explains it a bit more here:
http://www.lua.org/manual/5.1/manual.html#2.6


Besides that, I don’t understand how the listener you suggested would work. Are you using a instance to tag a listener for future reference? I don’t exactly understand the solution you have in mind.

This was more from an internal-standpoint. I don’t know if I like the idea of variables (i.e possibly dead references) being passed around. However, that’s completely on me for my own communication error. I think what I meant was a crude comparison between Lua’s upvaluing & C’s static scoping.

The example I provided was pseudo-code to demonstrate a possible implementation we could use. Look back at it as you made a decent point, where’s the feasibility? Well, I rewrote it to fit better.

local instance = teverse:construct(xyz) 

instance:bindListener("name", function(args) 
    return 
end)

instance:Fire(args)

The idea is that if the listener resolves to True, the event is fired. If not, the event enters a null state and retries.

1 Like

Looking at it this way makes it much clearer what you are trying to do. Though, I have to question this part:

I don’t think it is best for the even to retry if the listener doesn’t resolve to true. Couldn’t that lead to a loop of calls in which the listener returns false? It would be better just to fire the event again within the listener instead of outside of it. Something like this perhaps:

local event = teverse:construct("event")
event:bindListener("listener", function(args)
    if args == true then
        return
    else
        event:fire("listener")
    end
end

event:fireAll()
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.