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

Some minor changes required regarding unary overloading operators and numbers

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: groovy-runtime
    • Labels:
      None

      Description

      There are some minor discrepancies in how Groovy is dealing with overloading for unaryMinus and unaryPlus with numbers. Here is one example:

      class IntCat {
        static unaryPlus(Integer i) {"P"}
        static unaryMinus(Integer i) {"M"}
      }
      use (IntCat) {
        println 1.unaryPlus()   // => P
        println 1.unaryMinus()  // => M
        println (+1)            // => 1
        println (-1)            // => -1
      }
      

      We either need to not bypass operator overriding or document the reasons why we want a special case here.

      And here is another:

      println 1.unaryMinus()  // => -1
      println 1.unaryPlus()   // => MME unaryPlus
      

      We should provide the same hook for both (or potentially not provide this for either and provide documentation to justify why a special case would be needed).

      Note also that it is a little strange for Strings too.

      def foo = 'foo'
      println (-foo)   // => MME negative
      println (+foo)   // => MME positive
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              paulk Paul King
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: