scala – By-name参数vs匿名函数
发布时间:2020-12-16 19:05:01  所属栏目:安全  来源:网络整理 
            导读:目前还不清楚的是匿名功能的优点是匿名功能在懒惰评估和其他好处方面有什么优势: def func1(a: = Int)def func2(a: () = Int) 什么时候应该使用第一个和第二个? 这不是What’s the difference between =,()=,and Unit=的副本 解决方法 在这两种情况下,懒惰
                
                
                
            | 
                         
 目前还不清楚的是匿名功能的优点是匿名功能在懒惰评估和其他好处方面有什么优势: 
  
  
  
def func1(a: => Int) def func2(a: () => Int) 什么时候应该使用第一个和第二个? 这不是What’s the difference between =>,()=>,and Unit=>的副本 解决方法
 在这两种情况下,懒惰是一样的,但存在微小的差异.考虑: 
  
  
  
        def generateInt(): Int = { ... }
def byFunc(a: () => Int) { ... }
def byName(a: => Int) { ... }
// you can pass method without
// generating additional anonymous function
byFunc(generateInt)
// but both of the below are the same
// i.e. additional anonymous function is generated
byName(generateInt)
byName(() => generateInt()) 
 具有调用名称的功能对于制作DSL很有用.例如: def measured(block: ? Unit): Long = {
  val startTime = System.currentTimeMillis()
  block
  System.currentTimeMillis() - startTime
}
Long timeTaken = measured {
  // any code here you like to measure
  // written just as there were no "measured" around
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
