Skip to content

Instantly share code, notes, and snippets.

@dlwh
Forked from jorgeortiz85/gist:85662
Created March 25, 2009 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlwh/85668 to your computer and use it in GitHub Desktop.
Save dlwh/85668 to your computer and use it in GitHub Desktop.
implicit class RichString(s: String) extends RandomAccessSeq[Char] {
def apply(x: Int) = s.charAt(x);
// whatever
var foo = 3;
def twiddle = { foo += 1; foo }
}
yields
class RichString(s: String) extends RandomAccessSeq[Char] {
def apply(x: Int) = RichString.static$.apply(s,x);
// no forwarding for vars, or methods that reference them
var foo = 3;
def twiddle = { foo += 1; foo }
}
object RichString {
implicit def conversion(s: String) = new RichString(s);
private object static$ {
def apply(s: String, x: Int) = s.charAt(i);
def foo = 3
def twiddle = {
var $foo = foo
$foo += 1
$foo
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment