[[faqs:api]]
 

The Scala API

How do I use Enumerations?

This is an undocumented feature.

object WeekDays extends Enumeration(1) { // initial value
  final val Sunday, Monday, Tuesday = Value // from initial value
  final val Wednesday = Value(10) // a specific value
  final val Thursday, Friday, Saturday = Value // values after last assigned
  // {Sun.id==1 && Mon.id== 2 && Tue.id==3 && Wed.id==10 && Thu.id== 11 && Fri.id=12 && Sat.id== 13}  
 
  def isWorkDay( day: Value) = Monday <= day && day <= Friday
}
object UsesWeekDays {
  import WeekDays.{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} ;
  def plan( day: WeekDays.Value) = day match { case Sunday => "visit friends"
                                               case _ => "do whatever ..." }
  val mondayPlan = "Our plan on day " + Monday.id + " is " + plan( Monday)
  def wdayFromId( id: Int) = WeekDays( id)
  def printWorkDays = for( val day <- WeekDays; WeekDays.isWorkDay( day)) Console.println( day) ;
}
 

You can also assign names to print, e.g.: In a Roman context ...

object NamedWeekDays extends Enumeration(1, "dies Lunae", "dies Martis", 
               "dies Mercurii", "dies Iovis", "dies Veneris", "dies Saturni") {
 
  // from initial value, taking names from the constructor
  final val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday = Value
  // a specific value
  final val Sunday = Value( 0, "dies Solis") 
}
object UsesNamedWeekDays {
  import NamedWeekDays.{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} ;
  val msg = "Julius Caesar offered gifts to the Moon Goddess on " + Monday
  // {msg == "Julius Caesar offered gifts to the Moon Goddess on dies Lunae"}
}

Alternate way of entering enumerations

Is this very inefficient?

abstract class TokenType

object TokenTypes {
  case object Ident extends TokenType
  case object Keyword extends TokenType
  case object NumberLiteral extends TokenType
  case object StringLiteral extends TokenType
  case object Other extends TokenType
}

The advantage is that case classes that have TokenType parts have toString already implemented, including the TokenType part. The above way of specifying names for values uses two lists (values, names) that could get out of sync.

scala.testing.SUnit example

Scala Api Documentation SUnit example seems mangled

import scala.testing.SUnit; 
import SUnit._; 
  
object Main extends Application {
  // stupid expressly failed example
  val expected = "HELLO"
  val actual = "helloo".toUpperCase 
    
  class MyTest(n:String) extends TestCase(n) { 
     override def runTest() = n match { 
        case "myTest1" => {
           val msg = "expected: " + expected + "; actual: " + actual ;
           assertEquals( msg, expected, actual); 
           }
        case "myTest2" => assertTrue( "expressly false", false ); 
        } 
     } 
  val suite = new TestSuite( new MyTest("myTest1")) 
  suite.addTest( new MyTest("myTest2"))
  
  val r = new TestResult(); 
  suite.run(r)
  
  for(val tf <- r.failures()) { 
     Console.println( tf.toString()) 
  } 
}

How do I write a command line application with a main method?

Similar to Java, you need a method named main that accepts an array of strings. The Java VM requires the main method to be static but Scala does not have static; you use an object instead.

package org.mypackage
object MyMainClass {
  def main(args: Array[String]) {
    args.foreach(println)
  }
}

Earlier examples (including the testing example above) use the scala.Application trait, but as the scaladoc for Application notes, it has serious drawbacks.

 
faqs/api.txt · Last modified: 2010/02/11 09:10
 
Recent changes RSS feed Valid XHTML 1.0 Driven by DokuWiki