【问题标题】:Recreating the if/else in groovy: giving multiple closures arguments to a function在 groovy 中重新创建 if/else:为函数提供多个闭包参数
【发布时间】:2015-02-13 09:31:16
【问题描述】:

在尝试用 groovy 中的闭包重新发明 if/else 语法时,我无法让它发挥作用。我认为在括号外放置多个闭包是不允许的,但它可能是别的东西。

如果不允许,您将如何重现 if/else 语法?这是一个思想实验,所以不要告诉我这个实现的效率低下。

我的代码:

void ifx(boolean condition, Closure action){
  ["${true.toString()}": action].get(condition.toString(), {})()
}

void ifx(boolean condition, Closure action, Closure elsex){
  ["${true.toString()}": action, "${false.toString()}": elsex].get(condition.toString())()
}

void elsex(Closure action){
    action()
}

ifx(1==2){
    println("1")
} ifx(1==3){
    println("2")
} elsex{
    println("3")
}

错误信息:

java.lang.NullPointerException:无法在 null 上调用方法 ifx() 对象

【问题讨论】:

    标签: groovy closures first-order-logic


    【解决方案1】:

    沿着这些思路工作:

    更新带有闭包以避免全局状态:

    def ifx( outerCondition, outerBlock ) {
      boolean matched = false
      def realIfx
      realIfx = { condition, block ->
        if (condition) {
          matched = true
          block()
        }
        [ifx: realIfx, elsex: { elseBlock -> if(!matched) elseBlock() }]
      }
    
      realIfx outerCondition, outerBlock
    }
    

    还有一些测试:

    def result
    
    ifx(1 == 2) {
      result = 1
    } ifx(1 == 3) {
      result = 2
    } elsex {
      result = 3
    }
    
    assert result == 3
    result = null
    
    
    ifx (1 == 2) {
      result = 1
    } ifx (2 == 2) {
      result = 2
    } elsex {
      result = 3
    }
    
    assert result == 2
    result = null
    
    ifx (true) {
      result = 1
    } ifx (2 == 1) {
      result = 2
    } elsex {
      result = 3
    }
    
    assert result == 1
    

    【讨论】:

      【解决方案2】:

      ifx (1==2) {} ifx(1==3) {} elsex {} 是一个转换为ifx(1==2,{}).ifx(1==3,{}).elsex({}) 的命令链表达式。由于 void 转换为 null,因此应该清楚第二个 ifx 调用随后会因 NPE 而失败。为了实现 if/else 之类的事情,我可能会做以下事情

      void ifx(boolean condition, Closure ifBlock, Closure elseBlock) {
       ....
      }
      ifx (1==2) {...}{...}
      

      表示根本不使用 else 关键字。如果你想保持你的想法,你必须返回一些你可以调用 elsex 和 ifx 的东西。或者如果不是 ifx,那么在第一个 ifx 之后添加一个换行符

      【讨论】:

        猜你喜欢
        • 2016-06-27
        • 1970-01-01
        • 1970-01-01
        • 2020-10-29
        • 2021-10-23
        • 1970-01-01
        • 2022-01-17
        • 2018-07-23
        • 1970-01-01
        相关资源
        最近更新 更多