2011-11-28 56 views
1

在下面的例子中,我不能隱瞞update從公開曝光:不可能讓內部對象的方法私立外

trait Order { 
    sealed trait EntryOption { 
    private[Order] def update(e: EntryOption): Unit 
    } 

    private case object EmptyEntry extends EntryOption { 
    def update(e: EntryOption) =() 
    } 

    trait Entry extends EntryOption 

    def test(a: Entry, b: EntryOption): Unit = a.update(b) 
} 

它失敗"error: object creation impossible, since method $line12$$read$Order$^date in trait EntryOption of type (e: Order.this.EntryOption)Unit is not defined「–編譯不管這應該是(?編譯器的bug),我嘗試沒有成功如下:

  • 另外,還要updateEmptyEntryprivate[Order]
  • 讓它protected –此休息方法test

的目標是有EntryOptionupdate無法訪問外部Order

編輯

如果我試探着改變trait Orderobject Order彙編,顯示潛在的編譯器錯誤?

+0

應該在EntryOption中更新受保護而不是私人? – david

+0

@david然後'test'方法無法訪問它 –

回答

0

愚蠢的變通辦法:

trait Order { 
    sealed trait EntryOption { 
    private[Order] def update(e: EntryOption): Unit 
    } 

    private sealed trait ScalaChokes extends EntryOption { 
    private[Order] final def update(e: EntryOption) =() 
    } 

    private case object EmptyEntry extends ScalaChokes 

    trait Entry extends EntryOption 
} 

我應該提交一個bug?

相關問題