Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: adding union type

...

Number:

GEP-8

Title:

Static Type Checking

Version:

1 2

Type:

Feature

Status:

Draft

Leader:

Cédric Champeau

Created:

2011-10-08

Last modification:

2011-10-08 14

Abstract: Static Type Checking

...

Code

Java behaviour

Suggested behaviour for STC

Code Block
long x = 1;
int y = x;

Complains about possible loss of precision

"Standard Groovy" actually performs an implicit cast : int y=(int)x. So there is normally no reason to complain. However, it can be disturbing for the Java developer and may be a source of error.

Code Block
Set set = []

Unsupported

Should not complain

Code Block
Dimension d = [100,200]

Unsupported

Should not complain

Unification Types

In cases of for example "x instanceof A || x instanceof B" with A and B being unrelated we could still make an artificial union kind of type, that contains everthing present in A and B, to allow those kinds of method calls. The alternative to this is to allow only methods from Object here, which is less interesintg. This typing can also be used for multicatch, ensuring that a method call is only valid if it exists on each of the exceptions for the multicatch. In the current implementation (Okt-14-2011) the multicatch is already expanded at the point @StaticTypes will check. Meaning effectively this already represents a kind of union type, as the same code is in each catch block and thus the method call would fail, if the method is not available on each type. The proposed behaviour is therefore to align the instanceof case with multicatch.

References

Mailing-list discussions

...