2015-08-08 44 views
0

的代碼是從https://gist.github.com/jrudolph/66925來:爲什麼「河內塔」不在scala 2.11.4下工作?

object TowersOfHanoi { 
    import scala.reflect.Manifest 

    def simpleName(m:Manifest[_]):String = { 
    val name = m.toString 
    name.substring(name.lastIndexOf('$')+1) 
    } 

    trait Nat 
    final class _0 extends Nat 
    final class Succ[Pre<:Nat] extends Nat 

    type _1 = Succ[_0] 
    type _2 = Succ[_1] 
    type _3 = Succ[_2] 
    type _4 = Succ[_3] 

    case class Move[N<:Nat,A,B,C]() 

    implicit def move0[A,B,C](implicit a:Manifest[A],b:Manifest[B]):Move[_0,A,B,C] = { 
     System.out.println("Move from "+simpleName(a)+" to "+simpleName(b));null 
    } 

    implicit def moveN[P<:Nat,A,B,C](implicit m1:Move[P,A,C,B],m2:Move[_0,A,B,C],m3:Move[P,C,B,A]) 
    :Move[Succ[P],A,B,C] = null 

    def run[N<:Nat,A,B,C](implicit m:Move[N,A,B,C]) = null 

    case class Left() 
    case class Center() 
    case class Right() 

    def main(args:Array[String]){ 
    run[_2,Left,Right,Center] 
    } 
} 

我記得當我編譯它,它outputed像「從左向右移動」幾年前使用Scala 2.8

今天,我試着用斯卡拉2.11.4,它不輸出任何東西!

Scala有任何改變嗎?以及如何解決它?

回答

1

WFM(對我的作品)

scala> TowersOfHanoi.main(null) 
Move from Left to Right 
Move from Left to Center 
Move from Right to Center 
Move from Left to Right 
Move from Center to Left 
Move from Center to Right 
Move from Left to Right 

我會自稱不神交代碼本身 - 的implicits /類型是鈍角舊清單風格。 2.10+更喜歡TypeTags/ClassTags。

+0

你說得對,我忘了我需要'跑'它。我以爲我只需要**編譯**它:( – Freewind

+0

我注意到,不知道如何使用新功能,雖然 – Freewind

+0

http://stackoverflow.com/questions/31896390/how-to-use-new-scala -syntax-to-rewrite-towersofhanoi – Freewind