2017-02-03 10 views
0

私はCourseraのScalaコースでFunctional Program Designの2週目にWater Pouringビデオをフォローしています。「値xはxyzのメンバーではありません」

私のコードはMartinのものとほとんど同じです。それはうまくコンパイルするようです。そして、ワークシートにクラスのインスタンスをインスタンス化することができます。

しかし、私がオブジェクトを検査すると、いくつかのメンバーが存在し、いくつかは存在しません。私は得ることはありません

object testing { 
    val problem = new Pouring(Vector(4, 7)) 

    problem.initialState // works 
    problem.glasses  // works 
    problem.moves  // works 
    problem.initialPath // fails 
    problem.pathSets  // fails 
} 

class Pouring(val capacity : Vector[Int]) { 

    type State = Vector[Int] 

    val initialState : State = capacity map (x => 0) 

    // Moves 
    trait Move { 
    def change(state : State) : State 
    } 
    case class Empty(glass: Int)  extends Move { 
    def change(state: State) = state updated(glass, 0) 
    } 
    case class Fill(glass: Int)   extends Move { 
    def change(state: State) = state updated(glass, capacity(glass)) 
    } 
    case class Pour(from: Int, to: Int) extends Move { 
    def change(state: State) = { 
     val amount = state(from) min (capacity(to) - state(to)) 
     state.updated(from, state(from) - amount) 
      .updated(to, state(to) + amount) 
    } 
    } 

    class Path(history: List[Move]) { 
    def endState : State = (history foldRight initialState)(_ change _) 
    def extend(move: Move) = new Path(move :: history) 
    override def toString = (history.reverse mkString " ") + " => " + endState 
    } 

    val initialPath = new Path(Nil) 

    val glasses = capacity.indices 

    val moves = 
    (for (g <- glasses) yield Empty(g)) ++ 
    (for (g <- glasses) yield Fill(g)) ++ 
    (for (from <- glasses ; to <- glasses if from != to) yield Pour(from,to)) 


    def from(paths: Set[Path]): Stream[Set[Path]] = 
    if (paths.isEmpty) Stream.empty 
    else { 
     val more = for { 
     path <- paths 
     move <- moves 
     } yield path.extend(move) 
     paths #:: from(more) 
    } 

    val pathSets = from(Set(initialPath)) 

} 

私のワークシートは十分に単純です:私は私のコードであるここでエラー「値initialPathがwater.Pouringのメンバーではありません」

を取得しますどうしましたか。 IDE(IntelliJ)がメンバー名を自動完成しました。なぜメンバーではないと不平を言うのですか?私はScalaのバグに遭遇しましたか?またはIntelliJのバグ?

IntelliJのは1つのハックは.ideaフォルダを削除するとのIntelliJで再度プロジェクトを再開することで、適切部材を示すされていない場合はすべてのヘルプは

+0

コードは正常に動作します。あなたはそれをREPLに貼り付けます。この問題はおそらくIntelliJがこのような動作をすることが知られています。 – pedrofurla

+0

ありがとう@pedrofurla - どうすればIntelliJの問題を解決できますか? –

+0

SBTを使用してください:http://www.scala-sbt.org/ – pedrofurla

答えて

0

どこかの行に沿って、予想される動作が変更されました。ビデオに書かれたコードは、IntelliJの最新のScalaワークシートでは動作しません。

コード全体でobject XXX { }ラッパーを削除するだけで済み、すべて正常に機能します。

0

をいただければ幸いです。問題を解決する可能性があります。それでも問題が解決しない場合は、intelliJのキャッシュを無効にしてみてください。 https://www.jetbrains.com/help/idea/2016.3/cleaning-system-cache.html

+0

両方を試しましたが、役に立たないです。さらに悪いことに、突然最新のアップデートの後、私はどんな種類の出力も得られませんでした。 –

+0

しかし、私は結局、他の誰かが、外側の "オブジェクト"定義を削除すると、それが動作する場所を見つけました。私はそれをして、今私は正しく起きている。ホウ –

関連する問題