Last active
August 29, 2015 14:06
-
-
Save melix/5bc7c5db7ab986924181 to your computer and use it in GitHub Desktop.
Fat fingers correction in Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.codehaus.groovy.reflection.ClassInfo | |
import org.codehaus.groovy.runtime.MethodRankHelper | |
trait FatFingers { | |
def methodMissing(String name, args) { | |
def ci = ClassInfo.getClassInfo(this.class) | |
def methods = [*ci.metaClass.methods,*ci.metaClass.metaMethods] | |
def sugg = MethodRankHelper.rankMethods(name,args,methods) | |
if (sugg) { | |
sugg[0].invoke(this, args) | |
} else { | |
super.methodMissing(name, args) | |
} | |
} | |
} | |
class Boo implements FatFingers { | |
void boo() { println "Boo" } | |
} | |
def b = new Boo() | |
b.booo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. I'd always wanted to design a language where you could suppress compiler errors and warnings by adding more ! characters to your statements. If the compiler complained about b.booo() not existing, you could call b.booo!() and force it to call some similarly named method. This code would work well for that.