Clojure's 'let' equivalent in Scala

后端 未结 6 1995
不思量自难忘°
不思量自难忘° 2020-12-29 22:50

Often I face following situation: suppose I have these three functions

def firstFn: Int = ...
def secondFn(b: Int): Long = ...
def thirdFn(x: Int, y: Long, z:         


        
6条回答
  •  自闭症患者
    2020-12-29 23:34

    Stick with the original form:

    def calculate(a: Long) = {
      val first = firstFn
      val second = secondFn(first)
    
      thirdFn(first, second, second + a)
    }
    

    It's concise and clear, even to Java developers. It's roughly equivalent to let, just without limiting the scope of the names.

提交回复
热议问题