Scorpio

Scorpio

349k Downloads

Suggestion: sealed functions

BannZay opened this issue ยท 2 comments

commented

First of all, I am learning Scorpio (and PLoop) so my suggestion might be inapropriate because I am new to it.

Problem:

I was working on my addon and got traped into a 14x Scorpio\Modules\Core.lua:343: script ran too long . It took from me really decent amount of time to figure out what happened.

Simplified version of the addon (you might skip the code) :

Scorpio "MyCustomAddon" "1.0.0"
 
BlinkDelay = 1
 
function OnLoad()
	_Addon:SetSavedVariable("ScorpioTest_DB")
		:UseConfigPanel()
	
	_MyBlinkingFrame = Frame("MyBlinkingFrame")

	local customFrameVisibilitySubject = Subject()
	Style[_MyBlinkingFrame].Visible = customFrameVisibilitySubject 
	BeginBlinkingFor(customFrameVisibilitySubject)
 end
 
__Async__()
function BeginBlinkingFor(subject)
	local visible = false
	
	while true do 
		visible = not visible
		subject:OnNext(visible)
		Delay(BlinkDelay)
		print(visible)
	end
end

__Config__(_Config, "blinkDelay", RangeValue[{ 0.2, 2.8, 0.1 }], 0.2)
function Delay(value) 
	BlinkDelay = value
end

As you might notice I defined function with "Delay" name, hence I accidentally have overriden inherited function for async operations.
So the main thread stuck in a infinite loop.

Solution:
So the suggestion is to make some sort of notifications/errors for development time when user tries to override really important stuff.
Perphaps some kind of sealed functions ?

Hope it makes sense and technically possible.

commented

It's encourage to override the object's original method by inherited classes or object itself, so normally I won't block that.

You can do that by create your own Module like

Scorpio "MyAddon" ""

_G.MyAddon = class "MyAddon" (function(_ENV)
    inherit "Scorpio"  -- inherit the Scorpio to create your own addon system

    -- We need override the __newindex method, but that's abstract, 
    -- so can't use super to access it, need use the real function directly.
    local setEnvvalue = Environment.SaveValue

    function __newindex(self, key, value)
        if type(value) == "function" and Scorpio[key] then
            error("The " .. key .. " is already used, try another one", 2)
        end
        
        -- Save the value with attribute and other features
        return setEnvvalue(self, key, value)
    end
end)

Then you can use it in other files like

-- You can use it just like how use Scorpio
MyAddon "Test" ""

-- This will pass
__Async__()
function Check()
    print(coroutine.running())
end

-- This will fail
function Delay()
end

But that's not really needed, when you are familiar with Scorpio, there is not many methods that you should care about. Just use Scorpio.Delay to call the original one.

commented

yeah, but still would be cool to have some sort of "keywords". Using "Scorpio.Delay" everywhere is not really a convinient option.

Anyway, thanks for the reply.