Sunday, September 30, 2012

Accessing static final variable of java inner class

I've seen this nice trivia in the scala mailing list. Zang was asking "how do I access a final static variable of a java class ?", such as:

public class Foo {
 public class Bar {
     public static final String MY_STRING = "myString";
 }
}

Som brought the answer quickly, using:

null.asInstanceOf[Foo].Bar.MY_STRING

Sunday, September 23, 2012

ch.epfl.lamp.fjbg.JCode$$OffsetTooBigException

I've hit a OffsetTooBigException (java.lang.Error: ch.epfl.lamp.fjbg.JCode$$OffsetTooBigException: offset too big to fit in 16 bits: 36580) when trying to compile a long list of case. It is easily reproducable with this code:

def testCompile(test: String) {
  val PatternA = """a""".r
  val PatternB = """b""".r
  val PatternC = """c""".r
  val PatternD = """d""".r
  val PatternE = """e""".r
  val PatternF = """f""".r
  val PatternG = """g""".r
  val PatternH = """h""".r
  val PatternI = """i""".r
  val PatternJ = """j""".r
  test match {
    case PatternA(a) => a
    case PatternB(b) => b
    case PatternC(c) => c
    case PatternD(d) => d
    case PatternE(e) => e
    case PatternF(f) => f
    case PatternG(g) => g
    case PatternH(h) => h
    case PatternI(i) => i
    case PatternJ(j) => j
  }
}

The problem comes from the compiler creating for nested extractor huge sized bytecode. Apparently it is fixed for the 2.10 release. If you're not using it, the work around is easy, just cut the cases into pieces:

def testCompile(test: String) {
  val PatternA = """a""".r
  val PatternB = """b""".r
  val PatternC = """c""".r
  val PatternD = """d""".r
  val PatternE = """e""".r
  val PatternF = """f""".r
  val PatternG = """g""".r
  val PatternH = """h""".r
  val PatternI = """i""".r
  val PatternJ = """j""".r

  val firstPatternSet : PartialFunction[String, String] = {
    case PatternA(a) => a
    case PatternB(b) => b
    case PatternC(c) => c
    case PatternD(d) => d
    case PatternE(e) => e
  }

  val secondPatternSet: PartialFunction[String, String] = {
    case PatternF(f) => f
    case PatternG(g) => g
    case PatternH(h) => h
    case PatternI(i) => i
    case PatternJ(j) => j
  }

 firstPatternSet(test) orElse secondPatternSet(test)
}

Monday, September 17, 2012

Using SBT in Debug mode

When running sbt, you can pass options using SBT_OPTS. The following line allows to run sbt in debug mode, you can bind your debugger to port 5004. It suspend the server as long as the debugger is not bound. Not the option I would normally use, but this allows to check that sbt did get the proper options.

SBT_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5004" sbt

The really nice thing about it, is, because sbt doesn't spawn a new process when starting the console, you can also debug the scala console started from sbt.

Sunday, September 2, 2012

Showing scala code with orgmode

To code in scala I use either Eclipse or Intellij. To write my blog and do many other things I use Emacs and orgmode. To have syntax highlighting in orgmode, you need to have a mode for the language you are using (thanks to Bastien and Thorsten for their help). So to get a nicely colored code snippet in scala. First download scala-mode:

svn co http://lampsvn.epfl.ch/svn-repos/scala/scala-tool-support/trunk/src/emacs/ scala-mode

Then load it into emacs:

(add-to-list 'load-path "/path/to/scala-mode")
(require 'scala-mode-auto)

and now to create a scala snippets nicely exported, in orgmode:

#+begin_src scala
/** If given key is already in this map, returns associated value.
   *
   *  Otherwise, computes value from given expression `op`, stores with key
   *  in map and returns that value.
   *  @param  key the key to test
   *  @param  op  the computation yielding the value to associate with `key`, if
   *              `key` is previously unbound.
   *  @return     the value associated with key (either previously or as a result
   *              of executing the method).
   */
  def getOrElseUpdate(key: A, op: => B): B =
    get(key) match {
      case Some(v) => v
      case None => val d = op; this(key) = d; d
    }
#+end_src

will export like

/** If given key is already in this map, returns associated value.
   *
   *  Otherwise, computes value from given expression `op`, stores with key
   *  in map and returns that value.
   *  @param  key the key to test
   *  @param  op  the computation yielding the value to associate with `key`, if
   *              `key` is previously unbound.
   *  @return     the value associated with key (either previously or as a result
   *              of executing the method).
   */
  def getOrElseUpdate(key: A, op: => B): B =
    get(key) match {
      case Some(v) => v
      case None => val d = op; this(key) = d; d
    }

To create a blog post, I just export the file to html and copy/paste to blogger. You can see the difference with my previous post that was using the java highlighting.