groovy
  1. groovy
  2. GROOVY-4734

Object initializer not being called on object construction

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: 1.8-rc-2
    • Fix Version/s: 1.8-rc-4
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Perhaps related to GROOVY-4733.

      I would expect that this code prints "99", but it doesn't. It prints "9":

      class A {
              {
                      print f
      		}
              def f = 9 //
      		def other = { }
      		{
              	print f }
      }
      new A()
      

      This code, however, prints "99":

      class A {
              {
                      print f
      		}
      		{
              	print f }
              def f = 9 //
      		def other = { }
      }
      new A()
      

        Activity

        Hide
        Andrew Eisenberg added a comment -

        Why are you proposing that Groovy should disallow references as in the example I provide above? I know that Groovy is not Java, but disallowing such references would be an unnecessary departure (IMHO) from Java semantics.

        Despite what you said earlier, forward references in Java initializer blocks are allowed. Why should Groovy be any different?

        Show
        Andrew Eisenberg added a comment - Why are you proposing that Groovy should disallow references as in the example I provide above? I know that Groovy is not Java, but disallowing such references would be an unnecessary departure (IMHO) from Java semantics. Despite what you said earlier, forward references in Java initializer blocks are allowed. Why should Groovy be any different?
        Hide
        CÚdric Champeau added a comment -

        From what I read here, yes, the example you provide should not compile, neither in Java nor Groovy : http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#287406

        Show
        CÚdric Champeau added a comment - From what I read here, yes, the example you provide should not compile, neither in Java nor Groovy : http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#287406
        Hide
        Andrew Eisenberg added a comment - - edited

        Ummmm....yes. This does compile (in Java):

        class Test {
          { j = 9; }	
          int j = 1;
        }
        

        And this:

        class Test {
          static { j = 9; }	
          static int j = 1;
        }
        

        However, you are correct that this does not compile:

        class Test {
          int i = j;
          int j = 1;
        }
        
        Show
        Andrew Eisenberg added a comment - - edited Ummmm....yes. This does compile (in Java): class Test { { j = 9; } int j = 1; } And this: class Test { static { j = 9; } static int j = 1; } However, you are correct that this does not compile: class Test { int i = j; int j = 1; }
        Hide
        Andrew Eisenberg added a comment -

        Let me rephrase my previous comment.

        class Test {
          { j = 9; }	
          int j = 1;
        }
        

        works but only because the j variable is assigned but not used.

        This, however, does not work:

        class Test {
          { int i = j; }	
          int j = 1;
        }
        

        I am very surprised that Javac does not flag the first program as an error.

        I think the best thing to do right now is for me to come up with a very good example of what the problem is. I feel that we have gone off track here while we talk about initializers.

        Show
        Andrew Eisenberg added a comment - Let me rephrase my previous comment. class Test { { j = 9; } int j = 1; } works but only because the j variable is assigned but not used. This, however, does not work: class Test { { int i = j; } int j = 1; } I am very surprised that Javac does not flag the first program as an error. I think the best thing to do right now is for me to come up with a very good example of what the problem is. I feel that we have gone off track here while we talk about initializers.
        Hide
        Andrew Eisenberg added a comment -

        It seems that the real problem here was reported (and fixed) under GROOVY-4747. The other problems that I saw were actually confusions regarding how initializers work in Java and how Groovy parses code blocks. Thanks for your help in getting me to understand this.

        Show
        Andrew Eisenberg added a comment - It seems that the real problem here was reported (and fixed) under GROOVY-4747 . The other problems that I saw were actually confusions regarding how initializers work in Java and how Groovy parses code blocks. Thanks for your help in getting me to understand this.

          People

          • Assignee:
            Unassigned
            Reporter:
            Andrew Eisenberg
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: