English 中文(简体)
scala: tracing implicits selection and other code magics
原标题:

When trying to figure how a library works, implicit conversions are confusing. For example, looking at an expression like val foo: Foo = 1 , what converts 1 to Foo?

Is it possible to instruct the scala library (or REPL) to print out the code paths that are executing while evaluating an expression?

最佳回答

You can add "-Xprint:typer" to the compiler command line (or "-Ybrowse:typer" for a swing GUI browser) to see the code with the conversions explicitly applied.

问题回答

As an alternative to printing out the conversions, one must realize implicits can t just come out of the blue. You have to bring them into scope in some way. The alternatives are:

  1. Explicit import statement. Watch out for import x.y._ when y is an object, as this is the only way to bring in an implicit into scope.
  2. The object companion of the class that is being converted into something else.
  3. The object companion of the target class, as long as that target is made explicit somehow (such as in your example).

Note that the object scala.Predef is all imported into scope by default, which is how Scala s default implicits get into scope.

scalac -print prints the code after implicit type conversions where applied.

class A{
    val x : String = "hi" drop 1 
}

Will result in:

package <empty> {
  class A extends java.lang.Object with ScalaObject {
    @remote def $tag(): Int = scala.ScalaObject$class.$tag(A.this);
    private[this] val x: java.lang.String = _;
    <stable> <accessor> def x(): java.lang.String = A.this.x;
    def this(): A = {
      A.super.this();
      A.this.x = scala.this.Predef.forceRandomAccessCharSeq(
        scala.this.Predef.stringWrapper("hi").drop(1));
      ()
    }
  }
}




相关问题
Is there assembler REPL under linux?

Recently I ve started plaing with assembler under linux, there s good debuger, but comming from Ruby I m missing simple REPL that would let me enter a line of assembler code and see the result on ...

C# REPL tools; quick console-like compiling tool

Often times, I start a new instance of Visual Studio, just to create a console application that has some output and/or input. It s a temporary sandbox I use to test a method or something else and ...

How to play with Specs matchers in Scala REPL?

While debugging or exploring spec features it would be more advantageous to type them in REPL (Scala interpreter) rather then in file with spec and run it with something like maven. What is the ...

octave: load many functions from single file

How can I put multiple functions in one file and later get access to all of them in the octave interpreter ? I don t want to have a thousand files and want to group functions together. I d like ...

Clojure emacs slime + swank directory question

I m using emacs with clojure-swank and slime and trying to set my development environment. And I ran into a problem. When I start a repl I m stuck in an unknown directory preventing me to load my ...

scala: tracing implicits selection and other code magics

When trying to figure how a library works, implicit conversions are confusing. For example, looking at an expression like val foo: Foo = 1 , what converts 1 to Foo? Is it possible to instruct the ...

热门标签