Skip to content

Instantly share code, notes, and snippets.

@yutaono
Created November 19, 2014 06:12
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 yutaono/a9592e43c456dab6d09c to your computer and use it in GitHub Desktop.
Save yutaono/a9592e43c456dab6d09c to your computer and use it in GitHub Desktop.
trait Greeter {
val s: String
def greet(): String = s"greet! $s"
}
object Greeter {
implicit def convertInt(i: Int): Greeter = new Greeter { val s = i.toString }
}
import Greeter._
object Main extends App {
println(1.greet())
}
@yutaono
Copy link
Author

yutaono commented Nov 19, 2014

$ scalac Main.scala -Xprint:typer

[[syntax trees at end of                     typer]] // Main.scala
package <empty> {
  abstract trait Greeter extends scala.AnyRef {
    def /*Greeter*/$init$(): Unit = {
      ()
    };
    <stable> <accessor> def s: String;
    def greet(): String = scala.StringContext.apply("greet! ", "").s(Greeter.this.s)
  };
  object Greeter extends scala.AnyRef {
    def <init>(): Greeter.type = {
      Greeter.super.<init>();
      ()
    };
    implicit def convertInt(i: Int): Greeter = {
      final class $anon extends AnyRef with Greeter {
        def <init>(): anonymous class $anon = {
          $anon.super.<init>();
          ()
        };
        private[this] val s: String = i.toString();
        <stable> <accessor> def s: String = $anon.this.s
      };
      new $anon()
    }
  };
  import Greeter._;
  object Main extends AnyRef with App {
    def <init>(): Main.type = {
      Main.super.<init>();
      ()
    };
    scala.this.Predef.println(Greeter.convertInt(1).greet())
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment