Richard Searle's Blog

Thoughts about software

Archive for July, 2011

Exhaustive Scala Enum

Posted by eggsearle on July 24, 2011

This interesting post describes how to define an enum in Scala such that the compiler will flag a failure to exhaustively cover all the options. It also provides an implementation of values, which is often very useful.

However, I found the gist did not function under either Scala 2.8.1 or 2.9.0-1. The compiler would falsely indicate that not all options were covered.  Using objects rather than vals resolved the problem.

Arguably this is a clearer representation since Enum values are singletons.  It also avoids some of the “boilerplate” nature of the vals implementation, while retaining the compiler checking.

object Foos extends Enum {
  sealed trait EnumVal extends Value
  object F extends EnumVal { val name = "F" }
  object X extends EnumVal { val name = "X" }
}

Unfortunately, this implementation does not populate the values until each object is explicitly referenced.

Posted in Scala | Leave a Comment »

Controlling jquery tab plugin add

Posted by eggsearle on July 4, 2011

The jquery tab plugin provides a means to dynamically add a tab, but without any evident mechanism to determine the id of the tab. My use case required the population of that panel using Lift  AJAX, which requires the id to identify the  html element to be modified.

Only the tabTemplate accepts parameters, so that mechanism does not suffice. The template mechanism is now depreciated, which would eliminate it from consideration even if the the panelTemplate accepted parameters.

The assigned id turns out to be the fragment id iff a fragment URL is specified on the add call. Specifying a full URL causes a sequential, jquery generated id to be assigned.

Dynamically creating the tab and populating can thus be done using

JsRaw("$('#tabs').tabs( 'add', '#" + id + "', 'title' )") &
SetHtml(id, <i>Id is{id}</i>)

Posted in jquery, lift | Leave a Comment »