2

'val'と'case'は型システムにどのようにそしてなぜ影響しますか?(特に分散)

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> class E[-A]
defined class E

scala> class F[-A](val f: E[A] => Unit)
<console>:6: error: contravariant type A occurs in covariant position in type => (E[A]) => Unit of value f
class F[-A](val f: E[A] => Unit)
                       ^  
scala> case class C[-A](f: E[A] => Unit)
<console>:6: error: contravariant type A occurs in covariant position in type => (E[A]) => Unit of value f
   case class C[-A](f: E[A] => Unit)

scala> class F[-A](f: E[A] => Unit)    
defined class F
4

4 に答える 4

4

このことを考慮:

trait Equal[-A] { def eq(a1: A, a2: A): Boolean }
val e = new Equal[Option[Int]] { 
    def eq(a1: Option[Int], a2: Option[Int]) = a1 forall (x => a2 forall (x ==)) 
}

// Because Equal is contra-variant, Equal[AnyRef] is a subtype of Equal[String]
// Because T => R is contra-variant in T, Equal[AnyRef] => Unit is a supertype
// of Equal[String] => Unit
// So the follow assignment is valid
val f: Equal[AnyRef] => Unit = (e1: Equal[String]) => println(e1.eq("abc", "def"))


// f(e) doesn't compile because of contra-variance
// as Equal[Option[Int]] is not a subtype of Equal[AnyRef]

// Now let's tell Scala we know what we are doing
class F[-A](val f: Equal[A @uncheckedVariance] => Unit)

// And then let's prove we are not:
// Because F is contra-variant, F[Option[Int]] is a subtype of F[AnyRef]
val g: F[Option[Int]] = new F(f)

// And since g.f is Equal[Option[Int]] => Unit, we can pass e to it.
g.f(e) // compiles, throws exception

f外に見えない場合F、この問題は発生しません。

于 2011-02-24T00:39:56.980 に答える
2

差異とは何かを尋ねていますか?分散が何であるかを知っている場合、これは自明です。「val」または「case」のない例には、Aを含む外部から見えるメンバーがないため、分散エラーを引き起こすことはできません。

于 2011-02-23T23:24:42.910 に答える
1

'val'は、フィールドが外部から見えることを意味します。検討:

val f: E[Any] => Unit = { ... }
val broken: F[Int] = new F[Any](f) // allowed by -A annotation
val f2: E[Int] => Unit = broken.f // must work (types match)
val f3: E[Int] => Unit = f // type error

基本的に、明示的に行動せずにfを安全にキャストすることはできませんでした。これは、fが表示されている場合にのみ機能します。つまり、valとして定義するか、caseクラスを使用する場合です。

于 2011-02-24T00:40:28.307 に答える
0

これは、コンソールに出力するだけの反変の「出力チャネル」です。

class OutputChannel[-T] {   
  def write(t:T) = println(t); 
}

ここでそれが実行されています:

val out:OutputChannel[Any] = new OutputChannel[Any]
out.write(5)

まだ面白いものはありません。共変性のすばらしい点は、この出力チャネルをTの任意のサブクラスを受け入れるチャネルに安全に割り当てることができることです。

val out2:OutputChannel[String] = out
out2.write("five")
out2.write(55) //wont compile

ここで、出力チャネルに履歴追跡を追加した場合を想像してみてください。これまでに送信されたもののリストを返します。

//!!! as you've seen code like this won't compile w/ contravariant types!!!!
class OutputChannel[-T] {   
  var history:List[T] = Nil
  def write(t:T) = { 
    history = history :+ t;  
    println(t); 
  } 
}

上記がコンパイルされた場合、文字列ベースの出力チャネルのユーザーは問題を抱えることになります。

//history(0) is an Int - runtime exception (if scala allowed it to compile)
val firstStringOutputted:String = out2.history(0) 

共変性はこの型の「狭まり」(つまり、ここではAnyからString)を可能にするため、型システムは、私が行ったこの「履歴」フィールドや、あなたが持っていた「f」フィールドなど、型Tの値を公開できません。

他の有名な「反主流論者」は、関数とコンパレータです。

val strHashCode:String => Int = { s:Any => s.hashCode }  //function which works with any object
val strComp:Comparator<String> = new HashCodeComparator()   //comparator object which works with any object
于 2011-02-24T02:45:58.103 に答える