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

前端 未结 4 1799
长发绾君心
长发绾君心 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条回答
  •  Happy的楠姐
    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.

提交回复
热议问题