Groovy web console

subscribe to the feed Subscribe
to this
site
dynamic-dispatch (via #groovywebconsole)
tweet this snippet Tweet
this
script

dynamic-dispatch

Published 2 months ago by d.antoniuk
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
// dynamic method overloading - virtual dispatch on all method arguments (known in many languages as "multimethods")
def example(String s, Object i) {
   println ("string and object")
}

def example(String s, int i) {
   println ("string and int")
}

def example(Object s, Object i) {
   println ("object and object")
}

Object s = ""
Object i = 1;
example(s, i) // calls String, int variant