Search code examples
scaladateeclipse-juno

How to try this Scala code?


If I want to try this code to practice how Scala handles time and dates, should I create a new Scala application or just a new Scala class in Eclipse:

sealed abstract class SmartTime extends Ordered[SmartTime] { x =>
        def compare(y: SmartTime) = {
                x match {
                        case InfiniteFuture => 1
                        case InfinitePast => -1
                        case ConcreteTime(x) =>
                                y match {
                                        case InfiniteFuture => -1
                                        case InfinitePast => 1
                                        case ConcreteTime(y) => x compare y
                                }
                }
        }
}
case class ConcreteTime(t: Long) extends SmartTime
case object InfiniteFuture extends SmartTime
case object InfinitePast extends SmartTime

object Main {
        def main(args: Array[String]): Unit = {
                val y = ConcreteTime(100)
                val z = ConcreteTime(10)
                val x = InfiniteFuture
                val p = InfinitePast
                println(Vector(y, z, x, p).sortWith(_ < _))
        }
}

https://softwareengineering.stackexchange.com/questions/164843

I tried the date, calendar and time handling of python, java, and C++ and I thought neither was very good. Python was the best since it's relatively easy to state what you want with a timestamp and a timedelta but I think Java has made it confusing forcing us to use a Calendar object which we don't have to use in python. Now I want to try handling timestamps and time in Scala to learn Scala. The code above was posted and I would like to try it. Can you help me?

enter image description here

I can run the code and change it in Eclipse, now I want to try write I test case or try that no matter which date into the future you are, the infinite future is still ahead of that date.


Solution

  • If what you want really is the fastest way to know what the output of this program is without any hassle, then you can use http://www.simplyscala.com/, which is an online scala interpreter (scala 2.8). Just paste your code in the edit box and click "evaluate". Then enter Main.main(Array.empty) and click "evaluate" again. The main program will evaluate and you'll get your result.

    Then, if you really plan to learn scala, there are enough tutorials around the web regarding the use of Eclipse/IntelliJ/SBT with scala. A good book might help too.