Am I doing OOP wrong?

local SLP = {}
local methods = require(script:WaitForChild("methods"))

SLP.newNetwork = function(settings)
	local network = {}
	
	setmetatable(network, methods)
	
	return network
end

return SLP

What methods looks like:

local methods = {}
local activation_function = script.Parent.Parent.Parent:WaitForChild("ActivationFunction")

function methods:propagateFoward(new_input)

end

return methods

How the main script is using the modules:

local NNLibrary = require(script:WaitForChild("NNLibrary"))

local types = NNLibrary.getNetworkTypes("FFN")
local SingleLayerPerceptron = types.SingleLayerPerceptron

local network_setting = {3, 4, "stepFunction"}
local network = SingleLayerPerceptron.newNetwork(network_setting)

network:propagateFoward({1, 4, 6})

Error: ServerScriptService.test:9: attempt to call missing method ‘propagateFoward’ of table

I think the problem is how you set metatable. Using __index, this makes the module check the indexed table, if the method is not found in the first table.

local SLP = {}
local methods = require(script:WaitForChild("methods"))

SLP.newNetwork = function(settings)
    local network = {}

    setmetatable(network, {__index = methods})

    return network
end

return SLP
2 Likes

You can alternatively do this (this is a more common practice)

local SLP = {}
SLP.__index = SLP

local methods = require(script:WaitForChild("methods"))

SLP.newNetwork = function(settings)
    local network = {}

    setmetatable(network, methods)

    return network
end

return SLP
1 Like

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