Groovy 2: Type checking to the rescue!
Cédric Champeau, VMware
SpringOne2GX, October 17th, 2012
@CedricChampeau

About me

Past : Groovy contributor

Present: Core Groovy committer

Follow me

About Groovy 2

Modularity

JDK 7 enhancements

Static theme

Static type checking

Goal

Turn the compiler grumpy

Static Type Checking

It's optional

Triggered using @TypeChecked annotation

Annotate a class or a method

Static type checking basics

STC basics (cont'd)

STC basics (cont'd)

STC basics (cont'd)

Multiple assignments

Type checking options

if set on a class, type checks:

if set on a method, type checks:

You can prevent some code from beeing type checked:

Type inference (1/3)

def with list literals

Type inference (2/3)

Verify that declared types are compatible with assignments

Type inference (3/3)

LUB = Lowest Upper Bound (also called Least Upper Bound)

Instanceof type inference

Type checked Grooviness (1/4)

Groovy constructors

Map-style constructors

Type checked Grooviness (2/4)

Closure return type inference

Closure arguments

Type checked Grooviness (3/4)

Lists

Maps

Type checked Grooviness (4/4)

Compatible with @AST transformations

True as long as the AST transformation runs before the INSTRUCTION_SELECTION phase

Flow typing

What's cooking?

Planned for Groovy 2.1

Wishlist

Static type checking gotchas

Dynamic vs Static

Static type checking in a dynamic language?!

Closure parameter types

What's wrong with this code?

Is the role of the receiver the same in this code?

The type checker doesn't know about the type of "it"

An enhancement proposal exists, nothing implemented yet

Closure shared variables (1/2)

Closure shared variables (2/2)

This is allowed

Closure delegates and strategy

In 2.0

In 2.1 (upcoming)

Method selection gotchas (1/3)

Dynamic vs static

What's wrong with this (statically checked) code?

Method selection gotchas (2/3)

Step 1: you need to help the compiler

Method selection gotchas (3/3)

Step 2: you can remove the casts!

Static compilation

Why do you want static compilation?

Results from a survey before we started working on static compilation

Why static compilation?

If you do static type checking...

Benefits

But you loose...

Static compilation in Groovy 2

Static compilation is optional

But there are important differences

Static compilation: how to use

Just use the @CompileStatic annotation instead of @TypeChecked

Generated bytecode is very close (if not equal) to what Java produces

CompileStatic and semantics

Does it follow the semantics of dynamic Groovy?

Short answer: no

Long answer: as close as possible

Problem is the dynamic dispatch

Static method dispatch (1/2)

What does the following code outputs?

Static method dispatch (2/2)

Be careful!

Compat with extension modules (1/2)

Static compilation (thus static type checking) is compatible!

Define an extension Write the descriptor file

Compat with extension modules (2/2)

Static compilation (thus static type checking) is compatible!

Use it in your statically compiled code!

Inside the type checker

Inside the type checker (1/2)

How does it work?

Inside the type checker (2/2)

Node metadata

Static compilation

Testing the type checker (1/2)

ASTTest transformation

Testing the type checker (2/2)

ASTTest transformation

Q&A

Thank you!

Slides written using deck.js

http://melix.github.com/talks/s2gx-typechecking/slides.html

/

#