Let's say I have a class
class Original {
def originalMethod = 1
}
Now, let's say I have an instance of it
val instance = new Original
Is it now possible to do something to instance
at runtime to replace the originalMethod
with a different method? (granted the signature stays the same)
For example, now, when calling instance.originalMethod
the following code will be called println("test"); 1
EDIT
I can't call new Original
. I just have an existing instance of it, which I have to modify.
EDIT 2 (@Aleksey Izmailov answer) This is a nice solution, but isn't exactly what I'm looking for. I'm thinking more in terms of testing - writing a class "normally", without specifying functions as variables rather than methods
PS. I stumbled on this question when trying to mimic a Mockito spy