Thursday, November 20, 2008

Custom constructs in Scala

I ran accross a disscussion in the Scala list today, related to zero-parameter-function-values. One of the answers pointed to an interesting wiki enty about by-name parameters. Which walked through creating a home-made while loop in Scala (very DSL like). I had read about this (or seen it somewhere) but had not experimented with it, until now.




1 def myWhile(condition: => Boolean)(body: => Unit): Unit = {
2
3 if (condition) {
4
5 body;
6 myWhile(cond)(body)
7 }
8 }
9
10 var x = 0;
11
12 myWhile(x < 5) {
13
14 println(x)
15 x += 1
16 }


This is pretty cool, but I'm not sure at this point what I can do with a custom while loop (Scala has a perfectly useful one already). Then, I remembered something about creating constructs similar to the using statement in C#, for calling the dispose method of disposable types.




1 trait Disposable {
2 def dispose();
3 }
4
5 def using(resource: Disposable)(body: => Unit): Unit = {
6 body
7 resource.dispose()
8 }
9
10
11 class MyResource extends Disposable {
12
13 def foo() {
14 println("doing something with my resource")
15 }
16
17 override def dispose() {
18 println("closing resource")
19 }
20 }
21
22 val myResource = new MyResource
23
24 using(myResource) {
25 myResource.foo();
26 }


Now that is pretty cool. There might already be something in scala do to this "using" statement and I'm not quite sure where I can using this now, but it's nice to be able to create statements and constructs for increased readability.

Sunday, October 5, 2008

Drop Down Lists in the Lift framework

Ok, I need to add a select box to a web page. This should be farely easy in any modern web framework, but I'm using Lift, a new framework written in Scala.

Anyway, Lift has a singleton object (a common concept in Scala) with numerous utility functions for generating HTML elements (it's SHtml). This object has the select, selectObj, and select_* methods, which can generate a simple select box.

The first one was easy, it is designed to display name/value pairs of strings. The second one was much more difficult (due to my inexperience with Scala).

As I discovered, scala has a type called Option which allows for a an optional argument or value without having to check for null or deal with null exceptions.

Lift has it's own option like type called Can. As far as I can see, this provides similar functionality and is used throughout the framework.

In this particular instance the selectObj method was expecting a list of objects/values; a default value which is actually a Can holding the value or Empty; and a function to execute when the form is submitted.

After researching (David Pollak and Programming In Scala) I finally discovered how to use the selectObj method and more importantly learned about the Option type in scala and the Can type in Lift.

Adding a dop down list with objects and having that object returned to you is not so easy in most frameworks that I have delt with in the past (Java and .NET). Here is the code that I ended up writing, which is super cool:


1 def displayCourseList():NodeSeq = {
2
3 val c1 = new Course("111", "item 1")
4 val c2 = new Course("222", "item 2")
5
6 val courses = List((c1, c1.name), (c2, c2.name))
7
8 <span>
9 { SHtml.selectObj[Course](courses, Full(course1), course => print(course.number) }
10 </span>
11
12 }

Tuesday, February 26, 2008

Initial Entry

This is my first blog entry in Blogger. I've crossed over into the realm of google data (ouch!!!). changed

I will be disabling my hosted server this week, to save money during difficult times, in hopes to be self contained once again.

Total Pageviews

Labels