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

In traits field assignment is not an expression

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.7
    • 2.3.8, 2.4.0-beta-4
    • None

    Description

      Assigning a value to a field in a trait doesn't produce a value as a result:

      trait A {
          def foo
          def bar() { foo = 42 }
      }
      class C implements A {}
      
      assert new C().bar() == null // should be 42
      

      Groovy treats assignments as expressions, returning the value from the RHS, but traits break that semantics.

      Using the AST Browser it can be seen that bar() calls A$Trait$Helper.bar(this), which in turn calls $self.A_foo$set(42) which returns void ($self is an instance of C) - hence null that bar() returns.

      Attachments

        Activity

          People

            melix Cédric Champeau
            dsrkoc Dinko Srkoc
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: