Skip to content

Instantly share code, notes, and snippets.

@dlwh
Created March 25, 2009 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dlwh/85658 to your computer and use it in GitHub Desktop.
Save dlwh/85658 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment