首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐式转换中的隐式参数

隐式转换中的隐式参数
EN

Stack Overflow用户
提问于 2011-02-22 23:50:30
回答 1查看 1.2K关注 0票数 3

我试图在语言规范中找到应该告诉我这些类型的隐式转换不起作用的地方:

代码语言:javascript
复制
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
                          ^

基本上,我想要的就是将一个特定类型的列表赋给一个声明为另一种类型的变量,并让隐式转换生效。它显然不起作用。我可以想出解决这个问题的方法,但我只是讨厌我不理解这里工作的一般规则。有没有人?

EN

回答 1

Stack Overflow用户

发布于 2011-02-23 17:08:31

隐式视图本身可能需要隐式参数。例如:

代码语言:javascript
复制
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和这个脚本:

代码语言:javascript
复制
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]

然后,编译器解释:

代码语言:javascript
复制
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()))

我不太确定这是一个bug还是一个特性。但也许这个输出会帮助其他人进一步阐明这一问题。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5080406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档