2016-10-06 12 views
0

私はこのコードを使用することができません。このコードでは、円と四角形の境界ボックスが重なるかどうかを判断できるようになっています。ネストされた関数呼び出しでスカラ型の不一致が発生しました。

abstract class Shape 
    case class Circle(r: Double, x: Double, y: Double) extends Shape 
    case class Rectangle(llx: Double, lly: Double, w:Double, h:Double) extends Shape 


def boundingBox(s: Shape): Rectangle = s match { 
     case Rectangle(llx, lly, w, h) => Rectangle(llx, lly, w, h) 
     case Circle(r,x,y) => Rectangle(x-r, y-r, 2*r, 2*r) 
    } 


    def mayOverlap(s1: Shape, s2: Shape) = overlapRect(boundingBox(s1), boundingBox(s2)) 

    def overlapRect(r1: Rectangle, r2: Rectangle) = betweenLine(r1.llx, r1.w, r2.llx, r2.w) && betweenLine(r1.lly, r1.h, r2.lly, r2.h) 

    def betweenLine(x: Double, l: Double, a: Double, k: Double): Boolean = (a <= x+l) || (a+l <= x+l) 

mayOverlapを除くすべての機能が正しく動作しています。私は通訳にこれらの関数をロードすると、私は戻って、次のエラーを取得:

<console>:63: error: type mismatch; 
found : Rectangle(in object $iw) 
required: Rectangle(in object $iw) 
     def mayOverlap(s1: Shape, s2: Shape) = overlapRect(boundingBox(s1), boundingBox(s2)) 
                    ^
<console>:63: error: type mismatch; 
found : Rectangle(in object $iw) 
required: Rectangle(in object $iw) 
     def mayOverlap(s1: Shape, s2: Shape) = overlapRect(boundingBox(s1), boundingBox(s2)) 

を私はスカラ座に非常に新しいです、それは私が間の類似性の仮定を作ってるんだことも可能ですので、私ははるかにHaskellのに慣れています私はそれを単純化しようとしましたが、mayOverlapでネストされた関数呼び出しを全く作成できないようです。

+0

使用しているScalaのバージョンはどれですか?私はこれをREPLで再現することはできません。 –

答えて

0

ネストされたヘルパー関数が最初に来るように、関数の宣言を並べ替えることで問題を解決しました。

関連する問題