One of the things I have in my Scala startup file is the following.

def show[T](implicit thetype:scala.reflect.Manifest[T]) {
thetype.erasure.getMethods() foreach println
}
def help(x:AnyRef) {
x.getClass().getMethods() foreach println
}

These silly little functions are useful – at least, to me. Now when I can’t remember the name of a method I can type something like the following at the Scala prompt.

scala> help(Map())
public scala.Option scala.collection.immutable.EmptyMap.get(java.lang.Object)
public int scala.collection.immutable.EmptyMap.hashCode()
public boolean scala.collection.immutable.EmptyMap.equals(java.lang.Object)
...

The output isn’t very Scala-ish, but I could format it if I weren’t so lazy.

scala> help(scala.runtime.RichString)
public int scala.runtime.RichString$.$tag() throws java.rmi.RemoteException
public final boolean scala.runtime.RichString$.scala$runtime$RichString$$parseBoolean(java.lang.String) public final char scala.runtime.RichString.scalaruntimeRichString$$LF() public final char scala.runtime.RichString$.scala$runtime$RichString$$CR() public final char scala.runtime.RichString.scalaruntimeRichString$$FF()
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()

These functions aren’t in any way brilliant, or even clever, but they are short and ridiculously trivial to write.

Tagged with:

### 2 Responses to Silly Scala Tricks

1. wow this is eye boggling. It’s a bit complicated, is this really the standard way to do this?

2. KodeBlu says:

Well, it’s not so complicated once you start learning Scala. Scala is great, it has a rather sharp learning curve, but once you understand that it’s basically short-hand for those that already have a very strong understanding of programming concepts/object-oriented programming. Give it a try, Scala can make your life easier by not being so restrictive and verbose. It’s like being set free.