2011-02-22 17 views
3

私は暗黙の型変換のこれらの種類は動作しないことを私に伝えているはず言語仕様に場所を見つけるためにしようとしている:暗黙のパラメータ

scala> implicit def listToAlternativeList[F,T](xs: List[F])(implicit conv: (F) => T) = xs map conv 
listToAlternativeList: [F,T](xs: List[F])(implicit conv: (F) => T)List[T] 

scala> implicit def int2string(i: Int) = i.toString 
int2string: (i: Int)java.lang.String 

scala> val l = List(1, 2, 3) 
l: List[Int] = List(1, 2, 3) 

scala> val l2: List[String] = listToAlternativeList[Int,String](l) 
l2: List[String] = List(1, 2, 3) 

scala> val l2: List[String] = listToAlternativeList(l)    
l2: List[String] = List(1, 2, 3) 

scala> val l2: List[String] = l      
<console>:10: error: type mismatch; 
found : List[Int] 
required: scala.List[String] 
     val l2: List[String] = l 
         ^

基本的に、私が望むすべてがにあります他の型のものと宣言された変数に特定の型のListを割り当て、暗黙の変換を開始させます。明らかに機能しません。私はそれを回避する方法を思いつくことができますが、私はそれが私が仕事の一般的なルールを理解していないことを嫌っています。誰でも?

答えて

4

暗黙的なビューでは、暗黙のパラメータが必要な場合があります。例:

scala> implicit def listToStringList[A](as: List[A])(implicit f: A => String) = as map f 
listToStringList: [A](as: List[A])(implicit f: (A) => String)List[String] 

scala> implicit def i2s(i: Int) = i.toString 
i2s: (i: Int)java.lang.String 

scala> val l = List(1) 
l: List[Int] = List(1) 

scala> l: List[String] 
res0: List[String] = List(1) 

あなたのケースではどうなりますか?さて、このスクリプトでscala -Ytyper-debug -Xlog-implicitsとカーテンの後ろを見てみましょう:

implicit def listToList[A, B](as: List[A])(implicit f: A => B): List[B] = as map f 
implicit def i2s(i: Int): String = i.toString 
val l = List(1) 
l: List[String] 

コンパイラ、その後説明:

typing (l: List[String]), pt = ?, undetparams = List(), implicits-enabled = true, silent = true 
    typing List[String], pt = ?, undetparams = List(), implicits-enabled = true, silent = true 
     typing scala.package, pt = ?, undetparams = List(), implicits-enabled = true, silent = true 
     typing scala, pt = ?, undetparams = List(), implicits-enabled = true, silent = true 
     typed scala:type with underlying package scala, undetparams = List(), pt = ? 
     adapted scala:package scala to ?, List() 
     typed scala.package:type with underlying object package, undetparams = List(), pt = ? 
     adapted scala.package:object package to ?, List() 
     typing String, pt = ?, undetparams = List(), implicits-enabled = true, silent = true 
     typed scala.this.Predef.String:String, undetparams = List(), pt = ? 
     adapted scala.this.Predef.String:String to ?, List() 
    typed List[String]:List[String], undetparams = List(), pt = ? 
    adapted List[String]:List[String] to ?, List() 
    typing l, pt = List[String], undetparams = List(), implicits-enabled = true, silent = true 
    typed $anon.this.l:=> List[Int], undetparams = List(), pt = List[String] 
Beginning implicit search for $anon.this.l expecting (List[Int]) => List[String] looking for a view 
begin implicit search: ($anon.this.l,(List[Int]) => List[String],true,List()) 
typed impl for (List[Int]) => List[String]? listToList:(as: List[?])(implicit f: (?) => ?)List[?] orig info= [A,B](as: List[A])(implicit f: (A) => B)List[B]/List()/true/true/this.type/true 
typedImplicit0 typing$anon.this.listToList with wildpt = (List[Int]) => List[String] from implicit listToList:[A,B](as: List[A])(implicit f: (A) => B)List[B] 
    typing $anon.this.listToList, pt = ?, undetparams = List(), implicits-enabled = false, silent = false 
    typing $anon.this, pt = ?, undetparams = List(), implicits-enabled = false, silent = false 
    typed $anon.this:this.type with underlying java.lang.Object{}, undetparams = List(), pt = ? 
    adapted $anon.this:java.lang.Object{} to ?, List() 
    typed $anon.this.listToList:[A,B](as: List[A])(implicit f: (A) => B)List[B], undetparams = List(), pt = ? 
    adapted $anon.this.listToList:[A,B](as: List[A])(implicit f: (A) => B)List[B] to ?, List(type A, type B) 
    typing <argument>, pt = List[?], undetparams = List(), implicits-enabled = false, silent = false 
    typed <argument>:List[Int], undetparams = List(), pt = List[?] 
    adapted <argument>:List[Int] to List[?], List() 
    typing <argument>, pt = List[Int], undetparams = List(), implicits-enabled = false, silent = false 
    typed <argument>:List[Int], undetparams = List(), pt = List[Int] 
    adapted <argument>:List[Int] to List[Int], List() 
typed implicit $anon.this.listToList[Int, B](<argument>):(implicit f: (Int) => B)List[B], pt = (List[Int]) => List[String] 
adapted implicit method listToList:(as: List[Int])(implicit f: (Int) => B)List[B] to (List[Int]) => List[String] 
incompatible: (as: List[Int])(implicit f: (Int) => B)List[B] does not match (List[Int]) => List[String] 
Implicit search yielded: SearchResult(<empty>, TreeTypeSubstituter(List(),List())) 

を、私はこれはバグや機能であればかなりわかりません。しかし、おそらくこの出力は、他の誰かが問題をさらに明るくするのに役立ちます。

関連する問題