【问题标题】:implicit conversion with by-name parameter using Validation.fromTryCatch使用 Validation.fromTryCatch 使用按名称参数进行隐式转换
【发布时间】:2013-12-03 22:15:31
【问题描述】:

试图制作将函数a: => T转换为Validation[Throwable, String]的隐式方法

//works correctly
val t3 = test3
t3.fold(e => println("got error: " + e), s => println("got success: " + s.toString))

def test3: Validation[Throwable, String] ={
    Validation.fromTryCatch{
      throw new RuntimeException()
      "success"
    }
}

//does not work, exception is being thrown
val t4 = test4
implicit def trycatch2v[T](a: => T): Validation[Throwable, T]  = Validation.fromTryCatch(a)

def test4: Validation[Throwable, String] ={
      throw new RuntimeException()
      "success"
}

为什么我的implicit def 没有被调用?

【问题讨论】:

    标签: scala scalaz


    【解决方案1】:

    这是与 Try 相同的场景。

    def test3: Try [String] = Try { throw new RuntimeException(); "success" }
    
    test3 // works fine.
    
    
    implicit def toTry[T](a: => T): Try[T] = { println("convert to try!"); Try(a) }
    
    def test4: Try[String] = { throw new RuntimeException(); "success"}
    
    
    { throw new RuntimeException(); "success"  }.getOrElse("failed") // works fine.
    
    // type checker finds that the return type of block is String,
    //   which does not have a getOrElse 
    // so it falls back on implicit conversion for the block. as follows:
    
    toTry( {throw new RuntimeException(); "success"  }).getOrElse("failed") 
    
    
    test4 // gives runtime exception
    
     // test4 has an *expected type* of Try[String]
     // The return type of block is String ("success")
     // So implicit conversion kicks in from ':=> String' to 'Try[String]' 
     // this conversion is not for the outer block but for the inner block.
    
     // so in essence, it is converted to:
    
     def test4: Try[String] = { throw new RuntimeException(); toTry({"success"}) }
    

    请参阅 https://issues.scala-lang.org/browse/SI-3237 了解更多详情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多