페이지

2015년 11월 2일 월요일

Scala2e Chapter4 Classes and Objects 4.5 The Application trait

Scala provides a trit, scala.Application

import ChecksumAccumulator.calculate

object FallWinterSpringSummer extends Application {

   for(season <- fall="" list="" p="" spring="" winter="">       println(season + ": " + calculae(season))
}


Then instead of writing a main method, you place the code you would have put in the main method directly between the curly braces of the singleton object. That's it. You can compile and run this application just like any other.

Inheriting from Application is shorter than writing an explicit main method, but it also has some shortcomings.
First, you can't use this trait if you need to access command-line arguments, because the args array isn't available.
Second, because of some restrictions in the JVM threading model, you need an explicit main method if your program is multi-threaded.
Finally, some implementations of the JVM do not optimize the initialization code of an object which is executed by the Application trait. So you should inherit from Application only when your program is relatively simple and single-threaded.

댓글 없음: