Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-1733

Let boolean operators (||, &&) evaluate to the value instead of the boolean equivalent

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • None
    • None
    • syntax
    • None

    Description

      It would enable you to do some things a lot faster (eg. variable declaration) and would improve the current null-save navigation

      examples:

      def a = 0
      def b = null
      def c = "a string"
      def d = ""
      
      def val = a || c // c gets evalutated, because the boolean equivalent of a (0) is false
      
      assert a || b == null
      assert c || d == "a string"
      assert d || a == 0
      assert a || "another string" == "a string"
      assert d || "another string" == "another string"
      

      for null-save navigation:

      def val = aMap?.aProperty?.aSubProperty || "a default Value"
      

      or even for causal execution of code

      myobject.aMethodThatReturnsNullWhenItFails() || myobject.anotherMethod() 
      // myobject.anotherMethod gets only executed the result of the first call evalutes to false
      

      The current notation would not be affected in a negative way

      if(a || b){...} // would still work
      

      you would still have the possibility to get the boolean value by using !!

      eg.

      assert (0 || 15) instanceof Integer
      assert !!(0 || 15) instanceof Boolean
      

      The only language I know, which supports this currently is JavaScript

      Attachments

        Activity

          People

            guillaume Guillaume Sauthier
            ziegfried Siegfried Puchbauer
            Votes:
            3 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: