testing - Scala: replace method at runtime -


let's have class

class original {   def originalmethod = 1 } 

now, let's have instance of it

val instance = new original 

is possible instance @ runtime replace originalmethod different method? (granted signature stays same)

for example, now, when calling instance.originalmethod following code called println("test"); 1

edit can't call new original. have existing instance of it, have modify.

edit 2 (@aleksey izmailov answer) nice solution, isn't i'm looking for. i'm thinking more in terms of testing - writing class "normally", without specifying functions variables rather methods

ps. stumbled on question when trying mimic mockito spy

this isn't possible on jvm — in either java or scala — not in way you're asking for.

see e.g. in java, given object, possible override 1 of methods?

being in scala instead of java doesn't gain additional leverage, since fundamentals of how classes , methods work in scala same java's. (this done both performance , interop reasons.)


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -