Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Status update

...

Number:

GEP-8

Title:

Static Type Checking

Version:

2 3

Type:

Feature

Status:

Draft

Leader:

Cédric Champeau

Created:

2011-10-08

Last modification:

2011-10-14 18

Abstract: Static Type Checking

...

The goal of this section is to provide code samples which demonstrates in what case the STC transformation will actually complain and what is the expected error message, and serves as a basis to future STC documentation. This section may not be up-to-date, and one should always take a look at the STC unit tests found in the src/test/groovy/transform/stc directory.

Open discussions

What to do on assignment

See the discussion on the mailing list. Here we must discuss what to do in those situations :

...

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.

Features

Feature

Example

Behaviour

Status

Method does not exist

Code Block
def method() { 
... 
}
methode() // typo

Complains about undefined method

Implemented

Property does not exist

Code Block
class A {
   int x
}
A obj = new A()
a.y = 2

Complains about undefined property "y"

Implemented

Assignment type checking

Code Block
int x=2
x = 'String'

Assigning a String to an int is forbidden

Implemented

Incompatible binary expressions

Code Block

'string'+1

Checks that arguments of a binary expression are compatible (here, no 'plus' method is available

Implemented

Possible loose of precision (1/2)

Code Block

long myLong = ...
int myInt = myLong

Complains about possible loose of precision

Implemented

Possible loose of precision (2/2)

Code Block

int myInt = 2L

Will not complain because '2' can be represented as an int

Implemented

Arrays components

Code Block
String[] arr = { '1','2','3' }
arr[2] = 200

Cannot assign an int value in an array of type String[]

Implemented

Method return type check

Code Block

String method() { 'Hello' }
int x = method() // return types don't match

Ensures that assignments are compatible with method return type

Implemented

Explicit return type checking

Code Block

int method() { 
   return 'String' // return type is not compatible
}

Ensures that returned value is compatible with declared return type

Implemented

Implicit return type checking

Code Block

int method() { 
   'String' // return type is not compatible
}

Ensures that returned value is compatible with declared return type

Implemented

Implicit toString()

Code Block

String method(String name) {
   StringBuilder sb = new StringBuilder()
   sb 'Hi ' << name << '!'
}

Implicit call to toString()

Implemented

Basic type inference

Code Block

def str = 'My string'
str.toUpperCase() // type of 'str' is inferred

Method calls as well as property access is checked against inferred type

Implemented

Basic flow analysis

Code Block

def o = new Object()
o = 1
o = 'Hello'
o.toUpperCase()

Last method call will not complain because type of 'o' at this point can be inferred

Implemented

Instance of

Code Block

def o
...
if (o instanceof String) {
   o.toUpperCase() // no explicit cast required
}

Casts should not be necessary when type can be inferred from a previous instanceof check

Implemented

DefaultGroovyMethods support

Code Block

'123'.toInteger() // toInteger() is a Groovy extension method

Method calls can be resolved against Groovy extension methods

In progress (no type inference for closure arguments)

with

Code Block

class A {
   int x
}
def a = new A()
a.with {
   x = 1
}

Static type checking should be aware of the "with" structure

In progress

Categories

Code Block

use (MyStringCategory) {
   'string'.methodInStringCategory()
}

Compiler should be aware that extension method is found in a category

N/A (support will be limited as category support is inherently dynamic

Open discussions

What to do on assignment

See the discussion on the mailing list. Here we must discuss what to do in those situations :

Code

Java behaviour

Suggested behaviour for STC

Code Block
Set set = []

Unsupported

Should not complain

Code Block
Dimension d = [100,200]

Unsupported

Should not complain

...

JIRA issues