“With” statement equivalent for Scala?

前端 未结 6 885
野的像风
野的像风 2021-02-13 10:31

Idle pondering from a Scala learner perhaps, but ... in my tinkerings I\'ve written the following:

( n.child.size > 0 ) && ( n.child.filter( ! _.isIns         


        
6条回答
  •  终归单人心
    2021-02-13 10:58

    If you just want to limit the scope of your intermediate variable, you can also just create a block around the predicate:

    val n = getNodeByMagic()
    val passesTest = {
        val child = n.child
        child.length == 0 && !child.filter(_.isInstanceOf[Text]).isEmpty
    }
    // child is not defined outside of the block
    

提交回复
热议问题