简体   繁体   English

按名称的参数或函数?

[英]By-name parameters or functions?

I found out that by-name parameters and functions are very similar. 我发现按名称命名的参数和函数非常相似。 What is the advantage the first ones against the second ones? 第一个与第二个相比有什么优势?

object ws1 {
  def myByFunc(f: () => Int) = f()                //> myByFunc: (f: () => Int)Int
  def myByName(f: => Int) = f                     //> myByName: (f: => Int)Int

  def testFunc(a: Int) = a * a                    //> testFunc: (a: Int)Int

  myByFunc(() => testFunc(123))                   //> res0: Int = 15129
  myByName(testFunc(123))                         //> res1: Int = 15129

}

UPDATE : Regarding the question of how many times each of them executes: 更新 :关于每个执行多少次的问题:

object ws1 {
  def myByFunc(f: () => Int) = {
    println("111111111 myByFunc")
    f()
  }                                               //> myByFunc: (f: () => Int)Int

  def myByName(f: => Int) = {
    println("22222222 myByName")
    f
  }                                               //> myByName: (f: => Int)Int

  def testFunc(a: Int) = a * a                    //> testFunc: (a: Int)Int

  myByFunc(() => testFunc(123))                   //> 111111111 myByFunc
                                                  //| res0: Int = 15129
  myByName(testFunc(123))                         //> 22222222 myByName
                                                  //| res1: Int = 15129
}

For some reason they both execute 1 time. 由于某种原因,它们都执行1次。 Why? 为什么?

UPDATE2: Neither of them is evaluated: UPDATE2:两者均未评估:

object ws1 {
  def myByFunc(f: () => Int) = {
    println("111111111 myByFunc")
  }                                               //> myByFunc: (f: () => Int)Unit

  def myByName(f: => Int) = {
    println("22222222 myByName")
  }                                               //> myByName: (f: => Int)Unit

  def testFunc(a: Int) = {
    println("hello from test func")
    123456
  }                                               //> testFunc: (a: Int)Int

  myByFunc(() => testFunc(123))                   //> 111111111 myByFunc
  myByName(testFunc(123))                         //> 22222222 myByName
}

It's seems similar but the intent is different : 看起来很相似,但目的不同:

  • () => Int is a function , more precisely Function0 : it take no parameter and return an Int . () => Int是一个函数 ,更准确地说是Function0 :它不带任何参数并返回一个Int The execution of the function is triggred by call of f(). 通过调用f()触发函数的执行

  • => Int is a parameter , passed with a call-by-name technique : The evaluation will be lazy. => Int是一个参数 ,与呼叫按姓名技术通过: 评价将是懒。 The code can never be evaluated : def myByName(f: => Int) = 1 or be evaluated multiple times : def myByName(f: => Int) = f * f . 该代码永远不能被评估: def myByName(f: => Int) = 1或被多次评估: def myByName(f: => Int) = f * f

For a more complete explanation, take a look at this question : What's the difference between => , ()=>, and Unit=> 要获得更完整的解释,请看以下问题: =>,()=>和Unit =>有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM