0

in python, I can show available membership of one object by doing this

s = "Hello World" print(s.__dir__())

And it will spit out all available member that s has. How to do this in scala? I google and search the documentation, and nothing to be found.

The reason why I need this because the IDE that I'm using lack tab completion. So it will be crucial for me to observe the availability by using something like __dir__() . Thanks.

Napitupulu Jon
  • 7,713
  • 3
  • 22
  • 23
  • you should look at [Getting public fields](http://stackoverflow.com/q/7457972/508064) – roterl Sep 09 '16 at 08:17
  • I've tried the link you gave and didn't get what I expect. I want all the properties and method of an object, in this case string. Can you elaborate more why the link will help? – Napitupulu Jon Sep 09 '16 at 08:28
  • 1
    What IDE are you using and why can't you use a better one? (BTW, the REPL has tab completion.) – jwvh Sep 09 '16 at 09:14
  • I'm using Jupyter with plugins from https://github.com/jupyter-incubator/sparkmagic. Yes, the REPL is nice. but I need to documented the output code. – Napitupulu Jon Sep 09 '16 at 09:41
  • 1
    @NapitupuluJon, In Scala (as it from Java) the way of getting the fields and methods is using [Reflection](http://docs.scala-lang.org/overviews/reflection/overview.html). that link have some simple example for that. – roterl Sep 09 '16 at 11:00

2 Answers2

0

You can use java reflection on the Class instance.

E.g. to get the fields

 s.getClass.getDeclaredFields

This will get you an array that you can make more readable like so

scala> s.getClass.getDeclaredFields.mkString("\n")
res3: String =
private final char[] java.lang.String.value
private int java.lang.String.hash
private static final long java.lang.String.serialVersionUID
private static final java.io.ObjectStreamField[] java.lang.String.serialPersistentFields
public static final java.util.Comparator java.lang.String.CASE_INSENSITIVE_ORDER

The Class exposes similar methods to instrospect your objects.

You can refer to the docs

pagoda_5b
  • 7,333
  • 1
  • 27
  • 40
  • 1
    This misses methods like `+`, and `##` which are added by the implicit conversion to `RichString`, `->` which is added by the implicit conversion to `ArrayAssoc`, and probably lots of others as well. – Jörg W Mittag Sep 09 '16 at 09:25
0

In Scala, this is both much harder and much simpler than in Python.

It is much harder, because of implicit conversions: for example, you can call the -> method on a String, even though neither String nor any of its superclasses has a -> method. But there is an implicit conversion from Any to a type that does have a -> method (called ArrowAssoc), and so it is legal to call the -> method on any object, because Scala will implicitly convert that object to ArrayAssoc. That's why you can not just ask an object about its available methods, because the object doesn't know which implicit conversions might be in scope.

It is, however, also much easier, because the set of available selectors can always be determined statically, without actually having to run the code.

The Scala REPL implements completion using the Presentation Compiler, the implementation is in the class scala.tools.nsc.interpreter.resentationCompilerCompleter. Maybe you can find some inspiration there. In general, the correct approach would be to use Scala Reflection and/or the Presentation Compiler.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653