LoginSignup
3
3

More than 5 years have passed since last update.

Scala with Datatype

Last updated at Posted at 2013-07-13

Scalaに代数型(datatype)を実装した

Scalaに代数型(datatype)を実装する その1
Scalaに代数型(datatype)を実装する その2
Scalaに代数型(datatype)を実装する その3

の記事のとおり、datatypeを実装したScalaを作ったので、GitHubに公開してみました。

コンパイル方法

まずは、GitHubからソースコードを取得します。

$ git clone https://github.com/JunSuzukiJapan/scala

次はコンパイルします。

$ cd scala
$ ant

これで、15〜20分ぐらいでコンパイルされます。

出来上がった実行ファイルは、

build/pack/bin

にあります。

動作確認

以下のコードをテキストエディタで入力して動作確認してみましょう。

datatypeSample.scala
object datatypeSample {

  datatype Week = Mon | Tue | Wed | Thi | Fri | Sat | Sun

  val day: Week = Wed()
  val result = day match {
    case Mon() => "月曜日"
    case Tue() => "火曜日"
    case Wed() => "水曜日"
    case Thi() => "木曜日"
    case Fri() => "金曜日"
    case Sat() => "土曜日"
    case Sun() => "日曜日"
  }

  datatype Week2 {
    def isWorkday: Boolean = true
    def isHolyday: Boolean = ! isWorkday
  }
  = Mon2 | Tue2 | Wed2 | Thi2 | Fri2 | Sat2 {
      override def isWorkday = false
    }
  | Sun2 {
      override def isWorkday = false
    }

  datatype Fig =
    Rect(val x: Double, val y: Double){
      def area(): Double = x * y
    }
  | Circle(val r: Double){
      val pi = 3.14
      def area(): Double = r * r * pi
  }

  def main(args: Array[String]): Unit = {
    println("result = " + result)

    val day2: Week2 = Sun2()
    println("Sun2.isWorkday = " + day2.isWorkday)
    println("Sun2.isHolyday = " + day2.isHolyday)

    val rect = Rect(3.0, 5.0)
    val area1 = rect.area()
    val circle = Circle(10.0)
    val area2 = circle.area()
    println("rect area = " + area1)
    println("circle area = " + area2)
  }
}

実行方法は以下。

$ build/pack/bin/scala datatypeSample.scala
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3