Skip to content

Commit f4f357e

Browse files
authored
Merge pull request #647 from rpatters1/RGP-mixin-fallback-call
Implement fallback_call
2 parents c23e52d + a80ead7 commit f4f357e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/library/mixin.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ function mixin_public.eachentry(region, layer)
10221022
local c = mixin.FCMNoteEntryCell(measure, region:CalcStaffNumber(slotno))
10231023
c:SetLoadLayerMode(layertouse)
10241024
c:Load()
1025-
return function ()
1025+
return function()
10261026
while true do
10271027
i = i + 1;
10281028
local returnvalue = c:GetItemAt(i - 1)

src/mixin/__FCMBase.lua

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- Author: Robert Patterson
2+
-- Date: January 20, 2024
3+
--[[
4+
$module __FCMBase
5+
6+
## Summary of Modifications
7+
- Add method _FallbackCall to gracefully allow skipping missing methods in earlier Lua versions
8+
]] --
9+
local mixin = require("library.mixin")
10+
local mixin_helper = require("library.mixin_helper")
11+
12+
local class = {Methods = {}}
13+
local methods = class.Methods
14+
15+
16+
--[[
17+
% _FallbackCall
18+
19+
Checks the existence of a class method before calling it. If the method exists, it returns
20+
as expected. If the method does not exist, it returns the fallback_value. This function allows
21+
a script to call a method that does not exist in earlier versions of Lua (specifically, in JW Lua)
22+
and get a default return value in that case.
23+
24+
@ self (userdata) The class instance on which to call the method.
25+
@ method_name (string) The name of the method to return.
26+
@ fallback_value (any) The value that will be returned if the method does not exist. If this value is `nil`, the function returns `self`.
27+
@ additional_parameters (...) The additional parameters of the method.
28+
]]
29+
function methods:_FallbackCall(method_name, fallback_value, ...)
30+
if not self[method_name] then
31+
if fallback_value ~= nil then
32+
return fallback_value
33+
end
34+
return self
35+
end
36+
37+
return self[method_name](self, ...)
38+
end
39+
40+
return class

0 commit comments

Comments
 (0)