当前位置:  开发笔记 > 编程语言 > 正文

如何在具有隐式参数的函数上调用Private

如何解决《如何在具有隐式参数的函数上调用Private》经验,为你挑选了1个好方法。

如何为这些类调用XYZ.doSomething方法:

XYZ.scala

object XYZ {

  private def doSomething(i : Int)(implicit s: String): String {
    s + i.toString
  }
}

XYZTest.scala

class XYZTest extends FunSpec with PrivateMethodTester {
  describe("SomeTest") {
    it("Can't find a private method named: doSomething :( ") {
      implicit lazy val someStr: String = "sahil"
      val doSomething = PrivateMethod[String]('doSomething)
      val myStr = XYZ invokePrivate doSomething(1)
      assert(myStr == "sahil1")
    }
  }
  describe("SomeTest") {
    it("This doesn't even compile :( ") {
      val doSomething = PrivateMethod[String]('doSomething)
      val myStr = XYZ invokePrivate doSomething(1)("sahil")
      assert(myStr == "sahil1")
    }
  }
}

vvg.. 5

正确答案是:

object XYZ {

  private def doSomething(i : Int)(implicit s: String): String = {
    s + i.toString
  }
}

class XYZTest extends FunSpec with PrivateMethodTester {
  describe("SomeTest") {
    it("Can't find a private method named: doSomething :( ") {
      implicit lazy val someStr: String = "sahil"
      val doSomething = PrivateMethod[String]('doSomething)
      val myStr = XYZ invokePrivate doSomething(1, someStr)
      assert(myStr == "sahil1")
    }
  }
}

关于invokePrivatemethod的简要视图表明它不支持implicits,但看起来像将currying参数列表视为一般参数序列

def invokePrivate[T](invocation : PrivateMethodTester.this.Invocation[T])

args顺序在哪里:

final class Invocation[T](val methodName : scala.Symbol, val args : scala.Any*)



1> vvg..:

正确答案是:

object XYZ {

  private def doSomething(i : Int)(implicit s: String): String = {
    s + i.toString
  }
}

class XYZTest extends FunSpec with PrivateMethodTester {
  describe("SomeTest") {
    it("Can't find a private method named: doSomething :( ") {
      implicit lazy val someStr: String = "sahil"
      val doSomething = PrivateMethod[String]('doSomething)
      val myStr = XYZ invokePrivate doSomething(1, someStr)
      assert(myStr == "sahil1")
    }
  }
}

关于invokePrivatemethod的简要视图表明它不支持implicits,但看起来像将currying参数列表视为一般参数序列

def invokePrivate[T](invocation : PrivateMethodTester.this.Invocation[T])

args顺序在哪里:

final class Invocation[T](val methodName : scala.Symbol, val args : scala.Any*)

推荐阅读
雨天是最美
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有