Overwriting an instance Codea function

I’m trying to find a way to overwrite a function in a class that specifically applies to an instance of that class. I can’t figure out how to pass it self so it can access it’s own variables. If it was a standard class method I could simply do:

foo = Foo()
foo.testFunc = newFunc(foo)

function newFunc(instance)
print(instance.x)
end

But how can I do this with a Codea function such as keyboard?


--# Main
function setup()
    showKeyboard()
    foo = Foo()
    --This is what I wan to do, but it can't access self.
    --foo.keyboard = new_keyboard

    
end


function keyboard(key)
    foo:keyboard(key)
end

--overwrite with this function
function new_keyboard(key)
    print(self.x.." Bar "..key)
end

--# Foo
Foo = class()

function Foo:init(x)
    -- you can accept and set parameters here
    self.x = "Foo"
end

--I want to overwrite this.
function Foo:keyboard(key)
    print(self.x.." "..key)
end


This is in regards to Cider 7 and needing to overwrite the keyboard function on specific textboxes to remove show and hide keyboard to allow custom keypad to work.

@Briarfox: If I am understanding you correctly, then I think what you need to do is make new_keyboard take ‘self’ as the first parameter:

function new_keyboard(self, key)
    print(self.x.." Bar "..key)
end

Remember that:

function Foo:keyboard(key)

is just shorthand for:

function Foo.keyboard(self, key)

Thus, it follows that all a function needs to do in order to be a “member function” is take the instance it operates on as the first parameter.

If that’s not what you are asking, then sorry, I misunderstood.

lol Thank you @toadkick That was simple. I had assumed self wouldn’t work as keyboard passes key as the first param. I overlooked how self worked :slight_smile:

Thanks