jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • groovy
  • GROOVY-3823

Interface containing field without initialization expression incorrectly compiles

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Duplicate
  • Affects Version/s: 1.7-beta-2
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

For me, this INCORRECTLY compiles:

interface I {
  String s
}

Execution in 1.7beta2 groovyconsole gives:

groovy> interface I {
groovy>   String s
groovy> }
groovy> println I.s

null

I smell a bug...

For Java, the rule is: "Every field in the body of an interface must have an initialization expression,..."
(http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html)

Issue Links

is related to

Bug - A problem which impairs or prevents the functions of the product. GROOVY-3830 java.lang.IncompatibleClassChangeError thrown with Interface containing variable

  • Major - Major loss of function.
  • Closed - The issue is considered finished, the resolution is correct. Issues which are not closed can be reopened.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
blackdrag blackdrag added a comment - 15/Oct/09 7:15 AM

I reduced the priority to "minor", because this is no bug that is causing any problems. I am even tempted to change this into an improvement. Even though we want to be near Java, we don't have to follow all rules. We don't require final local variables for inner classes and don't make full generics checks. We do not even have static type based method preselection.

So for me the first question is why we absolutely have to do this check. Can you answer this?

Show
blackdrag blackdrag added a comment - 15/Oct/09 7:15 AM I reduced the priority to "minor", because this is no bug that is causing any problems. I am even tempted to change this into an improvement. Even though we want to be near Java, we don't have to follow all rules. We don't require final local variables for inner classes and don't make full generics checks. We do not even have static type based method preselection. So for me the first question is why we absolutely have to do this check. Can you answer this?
Hide
Permalink
alpheratz added a comment - 15/Oct/09 5:35 PM

Happy with the lower priority.

One very common use for interfaces is to encapsulate runtime constants/environmental dependencies,etc. In this usage, I'm not sure that I want 'blank finals' in my interface.

Interestingly, I COULD take an opposite stance and make some sort of case for having blank finals in an interface...for precisely the same reason: capture values that are constant for the duration of this run, but which may not be universally so. If this were to happen, would one have to give an interface a constructor like an enum? Or setters that look like:

setX(x) {
  if (x.initialised)
    barf somehow
  this.x = x
  x.initialised = true
}

We already have this:

abstract class AC {
  String s
}

Your comment suggests that there is/should be no distinction?

if you don't check, what to do about:

interface II {
  Integer i
  Integer 2i = i * 2
}

Currently (1.7b2), one gets:

groovy> interface II {
groovy>   Integer i
groovy>   Integer i2 = i * 2
groovy> }
groovy> println II.i
groovy> println II.i2

Exception thrown

java.lang.IncompatibleClassChangeError: Found interface II, but class was expected

	at II.<clinit>(ConsoleScript0:3)

	at ConsoleScript0.class$(ConsoleScript0)

	at ConsoleScript0.$get$$class$II(ConsoleScript0)

	at ConsoleScript0.run(ConsoleScript0:6)

Blank final interfaces may not be such a bad idea if done deliberately. At the moment, one just gets a shock and assumes that one has found a bug...which one has, even if it may eventually lead to an improved idea.

Show
alpheratz added a comment - 15/Oct/09 5:35 PM Happy with the lower priority. One very common use for interfaces is to encapsulate runtime constants/environmental dependencies,etc. In this usage, I'm not sure that I want 'blank finals' in my interface. Interestingly, I COULD take an opposite stance and make some sort of case for having blank finals in an interface...for precisely the same reason: capture values that are constant for the duration of this run, but which may not be universally so. If this were to happen, would one have to give an interface a constructor like an enum? Or setters that look like:
setX(x) {
  if (x.initialised)
    barf somehow
  this.x = x
  x.initialised = true
}
We already have this:
abstract class AC {
  String s
}
Your comment suggests that there is/should be no distinction? if you don't check, what to do about:
interface II {
  Integer i
  Integer 2i = i * 2
}
Currently (1.7b2), one gets:
groovy> interface II {
groovy>   Integer i
groovy>   Integer i2 = i * 2
groovy> }
groovy> println II.i
groovy> println II.i2

Exception thrown

java.lang.IncompatibleClassChangeError: Found interface II, but class was expected

	at II.<clinit>(ConsoleScript0:3)

	at ConsoleScript0.class$(ConsoleScript0)

	at ConsoleScript0.$get$$class$II(ConsoleScript0)

	at ConsoleScript0.run(ConsoleScript0:6)
Blank final interfaces may not be such a bad idea if done deliberately. At the moment, one just gets a shock and assumes that one has found a bug...which one has, even if it may eventually lead to an improved idea.
Hide
Permalink
blackdrag blackdrag added a comment - 16/Oct/09 6:43 AM

In Groovy variables have always an value. So your blanks in the interface will have a value of null or 0, depending on if it is a primitive type or not. Of course you are usually not defining a field in an interface to have a null value. You can get that more easily by using null directly. But for the sake of incremental development I think that it makes sense to allow someone to define some fields in the interface and think about the right values later.

Things like an interface constructor is not intended, no. As for your IncompatibleClassChangeError,could you pleae fill a new issue for this?

Show
blackdrag blackdrag added a comment - 16/Oct/09 6:43 AM In Groovy variables have always an value. So your blanks in the interface will have a value of null or 0, depending on if it is a primitive type or not. Of course you are usually not defining a field in an interface to have a null value. You can get that more easily by using null directly. But for the sake of incremental development I think that it makes sense to allow someone to define some fields in the interface and think about the right values later. Things like an interface constructor is not intended, no. As for your IncompatibleClassChangeError,could you pleae fill a new issue for this?
Hide
Permalink
Roshan Dawrani added a comment - 13/Nov/09 11:10 AM

Is there anything to be done on this issue? The IncompatibleClassChangeError was solved through GROOVY-3830 and the interface fields are correctly having their initial values - null/0/false

interface I {
  String s
  int i
  boolean b
}
println I.s // null
println I.i // 0
println I.b // false

So, what remains to be done in this JIRA?

Show
Roshan Dawrani added a comment - 13/Nov/09 11:10 AM Is there anything to be done on this issue? The IncompatibleClassChangeError was solved through GROOVY-3830 and the interface fields are correctly having their initial values - null/0/false
interface I {
  String s
  int i
  boolean b
}
println I.s // null
println I.i // 0
println I.b // false
So, what remains to be done in this JIRA?
Hide
Permalink
blackdrag blackdrag added a comment - 13/Nov/09 5:50 PM

I close it as duplicate of GROOVY-3830 then

Show
blackdrag blackdrag added a comment - 13/Nov/09 5:50 PM I close it as duplicate of GROOVY-3830 then

People

  • Assignee:
    blackdrag blackdrag
    Reporter:
    alpheratz
Vote (0)
Watch (0)

Dates

  • Created:
    14/Oct/09 10:51 PM
    Updated:
    13/Nov/09 5:50 PM
    Resolved:
    13/Nov/09 5:50 PM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.