How to investigate objects/types/etc. from Scala REPL?

前端 未结 4 1783
长发绾君心
长发绾君心 2020-12-22 23:59

I\'ve been working with Scala for a while now and have written a 10,000+ line program with it, but I\'m still confused by some of the inner workings. I came to Scala from Py

相关标签:
4条回答
  • 2020-12-23 00:14

    Javap works, but you are pointing it to scala.Predef.List, which is a type, not a class. Point it instead to scala.collection.immutable.List.

    Now, for the most part just entering a value and seeing what the result's type is is enough. Using :type can be helpful sometimes. I find that use getClass is a really bad way of going about it, though.

    Also, you are sometimes mixing types and values. For example, here you refer to the object :::

    scala> `::`.getClass res79: java.lang.Class[_ <: object
    scala.collection.immutable.::] = class
    scala.collection.immutable.$colon$colon$
    

    And here you refer to the class :::

    scala> classOf[`::`[Int]] res81: java.lang.Class[::[Int]] = class
    scala.collection.immutable.$colon$colon
    

    Objects and classes are not the same thing, and, in fact, there's a common pattern of objects and classes by the same name, with a specific name for their relationship: companions.

    Instead of dir, just use tab completion:

    scala> "abc".
    +                     asInstanceOf          charAt                codePointAt           codePointBefore       codePointCount
    compareTo             compareToIgnoreCase   concat                contains              contentEquals         endsWith
    equalsIgnoreCase      getBytes              getChars              indexOf               intern                isEmpty
    isInstanceOf          lastIndexOf           length                matches               offsetByCodePoints    regionMatches
    replace               replaceAll            replaceFirst          split                 startsWith            subSequence
    substring             toCharArray           toLowerCase           toString              toUpperCase           trim
    
    scala> "abc".compareTo
    compareTo             compareToIgnoreCase
    
    scala> "abc".compareTo
                                 def compareTo(String): Int
    

    If you enter the power mode, you'll get way more information, but that's hardly for beginners. The above shows types, methods, and method signatures. Javap will decompile stuff, though that requires you to have a good handle on bytecode.

    There's other stuff in there -- be sure to look up :help, and see what's available.

    Docs are only available through the scaladoc API. Keep it open on the browser, and use its search capability to quickly find classes and methods. Also, note that, as opposed to Java, you don't need to navigate through the inheritance list to get the description of the method.

    And they do search perfectly fine for symbols. I suspect you haven't spent much time on scaladoc because other doc tools out there just aren't up to it. Javadoc comes to mind -- it's awful browsing through packages and classes.

    If you have specific questions Stack Overflow style, use Symbol Hound to search with symbols.

    Use the nightly Scaladocs: they'll diverge from whatever version you are using, but they'll always be the most complete. Besides, right now they are far better in many respects: you can use TAB to alternate between frames, with auto-focus on the search boxes, you can use arrows to navigate on the left frame after filtering, and ENTER to have the selected element appear on the right frame. They have the list of implicit methods, and have class diagrams.

    I've made do with a far less powerful REPL, and a far poorer Scaladoc -- they do work, together. Granted, I skipped to trunk (now HEAD) just to get my hands on tab-completion.

    0 讨论(0)
  • 2020-12-23 00:24

    Note that scala 2.11.8 New tab-completion in the Scala REPL can facilitate the type exploration/discovery.

    It now includes:

    • CamelCase completion:
      try:
      (l: List[Int]).rroTAB,
      it expands to:
      (l: List[Int]).reduceRightOption

    • Find members by typing any CamelCased part of the name:
      try:
      classOf[String].typTAB, to get getAnnotationsByType, getComponentType and others

    • Complete bean getters without typing get:
      try:
      (d: java.util.Date).dayTAB

    • Press TAB twice to see the method signature:
      try:
      List(1,2,3).partTAB,
      which completes to:
      List(1,2,3).partition;
      press TAB again to display:
      def partition(p: Int => Boolean): (List[Int], List[Int])

    0 讨论(0)
  • 2020-12-23 00:27

    You mentioned an important point which Scala lacks a bit: the documentation.

    The REPL is a fantastic tool, but it is not as fantastic at it can be. There are too much missing features and features which can be improved - some of them are mentioned in your post. Scaladoc is a nice tool, too, but is far away to be perfect. Furthermore lots of code in the API is not yet or too less documented and code examples are often missing. The IDEs are full ob bugs and compared to the possibilities Java IDEs show us they look like some kindergarten toys.

    Nevertheless there is a gigantic difference of Scalas current tools compared to the tools available as I started to learn Scala 2-3 years ago. At that time IDEs compiled permanently some trash in the background, the compiler crashed every few minutes and some documentation was absolutely nonexistent. Frequently I got rage attacks and wished death and corruption to Scala authors.

    And now? I do not have any of these rage attacks any more. Because the tools we currently have are great although the are not perfect!

    There is docs.scala-lang.org, which summarizes a lot of great documentation. There are Tutorials, Cheat-sheets, Glossaries, Guides and a lot of more great stuff. Another great tools is Scalex, which can find even the weirdest operator one can think of. It is Scalas Hoogle and even though it is not yet as good as his great ideal, it is very useful.

    Great improvements are coming with Scala2.10 in form of Scalas own Reflection library:

    // needs Scala2.10M4
    scala> import scala.reflect.runtime.{universe => u}
    import scala.reflect.runtime.{universe=>u}
    
    scala> val t = u.typeOf[List[_]]
    t: reflect.runtime.universe.Type = List[Any]
    
    scala> t.declarations
    res10: Iterable[reflect.runtime.universe.Symbol] = SynchronizedOps(constructor List, method companion, method isEmpty, method head, method tail, method ::, method :::, method reverse_:::, method mapConserve, method ++, method +:, method toList, method take, method drop, method slice, method takeRight, method splitAt, method takeWhile, method dropWhile, method span, method reverse, method stringPrefix, method toStream, method removeDuplicates)
    

    Documentation for the new Reflection library is still missing, but in progress. It allows one to use scalac in an easy way inside of the REPL:

    scala> u reify { List(1,2,3) map (_+1) }
    res14: reflect.runtime.universe.Expr[List[Int]] = Expr[List[Int]](immutable.this.List.apply(1, 2, 3).map(((x$1) => x$1.$plus(1)))(immutable.this.List.canBuildFrom))
    
    scala> import scala.tools.reflect.ToolBox
    import scala.tools.reflect.ToolBox
    
    scala> import scala.reflect.runtime.{currentMirror => m}
    import scala.reflect.runtime.{currentMirror=>m}
    
    scala> val tb = m.mkToolBox()
    tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@32f7fa37
    
    scala> tb.parseExpr("List(1,2,3) map (_+1)")
    res16: tb.u.Tree = List(1, 2, 3).map(((x$1) => x$1.$plus(1)))
    
    scala> tb.runExpr(res16)
    res18: Any = List(2, 3, 4)
    

    This is even greater when we want to know how Scala code is translated internally. Formerly wen need to type scala -Xprint:typer -e "List(1,2,3) map (_+1)" to get the internally representation. Furthermore some small improvements found there way to the new release, for example:

    scala> :type Predef
    scala.Predef.type
    

    Scaladoc will gain some type-hierarchy graph (click on type-hierarchy).

    With Macros it is possible now, to improve error messages in a great way. There is a library called expecty, which does this:

    // copied from GitHub page
    import org.expecty.Expecty
    
    case class Person(name: String = "Fred", age: Int = 42) {
      def say(words: String*) = words.mkString(" ")
    }
    
    val person = Person()
    val expect = new Expecty()
    
    // Passing expectations
    
    expect {
      person.name == "Fred"
      person.age * 2 == 84
      person.say("Hi", "from", "Expecty!") == "Hi from Expecty!"
    }
    
    // Failing expectation
    
    val word1 = "ping"
    val word2 = "pong"
    
    expect {
      person.say(word1, word2) == "pong pong"
    }
    
    /*
    Output:
    
    java.lang.AssertionError:
    
    person.say(word1, word2) == "pong pong"
    |      |   |      |      |
    |      |   ping   pong   false
    |      ping pong
    Person(Fred,42)
    */
    

    There is a tool which allows one to find libraries hosted on GitHub, called ls.implicit.ly.

    The IDEs now have some semantic highlighting, to show if a member is a object/type/method/whatever. The semantic highlighting feature of ScalaIDE.

    The javap feature of the REPL is only a call to the native javap, therefore it is not a very featue-rich tool. You have to fully qualify the name of a module:

    scala> :javap scala.collection.immutable.List
    Compiled from "List.scala"
    public abstract class scala.collection.immutable.List extends scala.collection.AbstractSeq implements scala.collection.immutable.LinearSeq,scala.Product,scala.collection.LinearSeqOptimized{
    ...
    

    Some time ago I have written a summary of how Scala code is compiled to Bytecode, which offers a lot of things to know.

    And the best: This is all done in the last few months!

    So, how to use all of these things inside of the REPL? Well, it is not possible ... not yet. ;)

    But I can tell you that one day we will have such a REPL. A REPL which shows us documentation if we want to see it. A REPL which let us communicate with it (maybe like lambdabot). A REPL which let us do cool things we still cannot imagine. I don't know when this will be the case, but I know that a lot of stuff was done in the last years and I know even greater stuff will be done in the next years.

    0 讨论(0)
  • 2020-12-23 00:35

    You need to pass fully qualified class name to javap.

    First take it using classOf:

    scala> classOf[List[_]]
    res2: java.lang.Class[List[_]] = class scala.collection.immutable.List
    

    Then use javap (doesn't work from repl for me: ":javap unavailable on this platform.") so example is from a command line, in repl, I believe, you don't need to specify classpath:

    d:\bin\scala\scala-2.9.1-1\lib>javap -classpath scala-library.jar "scala.collection.immutable.List"
    

    But I doubt this will help you. Probably you're trying to use techniques you used to use in dynamic languages. I extremely rarely use repl in scala (while use it often in javascript). An IDE and sources are my all.

    0 讨论(0)
提交回复
热议问题