コンストラクタ

クラスのコンストラクタの中では、valや、if..elseなどが(きっと他にもいろいろ)使えない模様。

実はこちらのページを参考にタイムコードを表すクラスと書いてみたのだけれど、
上記の制限(仕様?)にぶちあたりまして、
んでしょうがないから、ファクトリで対処した。

  class TimecodeFactory(rate:Any) {
    def make(hh:int, mm:int, ss:int, ff:int) = rate match {
      case "ntsc" =>
        val minutes = hh * 60 + mm
        new Timecode(17982 * (minutes/10) + 1798 * (minutes%10) + ss * 30 + ff)
      case r:Int =>
        val mrate = r * 60
        val hrate = mrate * 60
        new Timecode(hh * hrate + mm * mrate + ss * r + ff)
    }
  }
  class Timecode(frameRate:int) {
    class Tcode(hh:int, mm:int, ss:int, ff:int) {
      def hour = hh
      def minutes = mm
      def second = ss
      def frame = ff
      
      override def toString() = { hh + ": " + mm + ": " + ss + "." + ff }
    }
    
    def frame = frameRate	

    def ntsc():Tcode = 
	    new Tcode(frame / 17982 / 6,
	              frame / 17982 % 6 * 10 + (frame % 17982 - 2) / 1798,
	              ((frame % 17982 - 2) % 1798 + 2) / 30,
	              ((frame % 17982 - 2) % 1798 + 2) % 30
	             )

    def tc():Tcode = tc(30)
    def tc(s:int):Tcode = {
      val m = s * 60
      val h = m * 60
	    new Tcode(frame / h,
	              (frame % h) / m,
	              ((frame % h) % m) / s,
	              ((frame % h) % m) % s
	             )
	  }
  }