is groovy static or dynamic

64
Copyright © 2013 Russel Winder 1 Is Groovy Static or Dynamic? Russel Winder email: [email protected] xmpp: [email protected] twitter: @russel_winder web: http://www.russel.org.uk

Upload: russel-winder

Post on 29-Nov-2014

425 views

Category:

Technology


3 download

DESCRIPTION

A session at Groovy and Grails eXchange 2013 investigating whether a language designed as a dynamic language really can be statically typed and compiled.

TRANSCRIPT

Page 1: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 1

Is Groovy Static or Dynamic?

Russel Winder

email: [email protected]: [email protected]

twitter: @russel_winderweb: http://www.russel.org.uk

Page 2: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 2

?

Page 3: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 3

Prelude

Page 4: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 4

Debate [d be t]ɪˈ ɪn 1. (Government, Politics & Diplomacy) a formal discussion, as in a legislative body, in which opposing arguments are put forward 2. discussion or dispute 3. (Philosophy) the formal presentation and opposition of a specific motion, followed by a votevb 1. to discuss (a motion), esp in a formal assembly 2. to deliberate upon (something) he debated with himself whether to go

[from Old French debatre to discuss, argue, from Latin battuere]

debater n

Collins English Dictionary – Complete and Unabridged © HarperCollins Publishers 1991, 1994, 1998, 2000, 2003.

Page 5: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 5

foment [f m nt]əˈ ɛvb (tr) 1. to encourage or instigate (trouble, discord, etc.); stir up 2. (Medicine) Med to apply heat and moisture to (a part of the body) to relieve pain and inflammation

[from Late Latin f ment re, from Latin f mentum a poultice, ultimately from ō ā ōfov re to foster]ē

fomentation [ f m n te n]ˌ əʊ ɛ ˈ ɪʃə nfomenter n

Usage: Both foment and ferment can be used to talk about stirring up trouble: he was accused of fomenting/fermenting unrest. Only ferment can be used intransitively or as a noun: his anger continued to ferment (not foment); rural areas were unaffected by the ferment in the cities

Collins English Dictionary – Complete and Unabridged © HarperCollins Publishers 1991, 1994, 1998, 2000, 2003

Page 6: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 6

fermentn [ f m nt]ˈ ɜː ɛ 1. (Life Sciences & Allied Applications / Biochemistry) any agent or substance, such as a bacterium, mould, yeast, or enzyme, that causes fermentation 2. (Life Sciences & Allied Applications / Biochemistry) another word for fermentation 3. commotion; unrestvb [f m nt]əˈ ɛ 1. (Life Sciences & Allied Applications / Biochemistry) to undergo or cause to undergo fermentation 2. to stir up or seethe with excitement

[from Latin fermentum yeast, from ferv re to seethe]ēfermentable adjfermentability nfermenter n

Usage: See at foment

Collins English Dictionary – Complete and Unabridged © HarperCollins Publishers 1991, 1994, 1998, 2000, 2003

Page 7: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 7

The intention of this session is to

foment debate.

Page 8: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 8

Let the fomentation begin.

Page 9: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 9

No not the fermentation.

Page 10: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 10

Starting Point

Page 11: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 11

2003

Groovy is created as thedynamic symbiote to Java.

Page 12: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 12

Dynamic languages have no typed variables.

Page 13: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 13

static totallyDynamic(x, y) { x * y}

Page 14: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 14

Page 15: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 15

Duck typing:

The receiver of the message decides the meaning of the message.

Page 16: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 16

Operators are not messages to areceiving object in Java…

Page 17: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 17

…Java isn't really an object-oriented language.

Page 18: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 18

Fortunately, Groovy decided to follow Smalltalk,and have a meta-object protocol.

Page 19: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 19

Groovy is truly object-oriented,message passing is the operational semantic,

and all variables are references to objects.

Page 20: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 20

x * y

transforms to

x.plus(y)

to realize message passing with method call.

Page 21: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 21

Interesting Issue

Page 22: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 22

Page 23: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 23

Objects clearly have static types.

Page 24: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 24

Java allows for despatch by signature,

i.e. methods can be overloaded.

Page 25: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 25

Page 26: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 26

Adding types to parameters is requiredfor despatch by signature.

Page 27: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 27

static runtimeTyping(Number x, Number y) { x * y}

Page 28: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 28

Groovy is an optionally typed language:

variables, not just function and method parameters, may or may not be given a type.

Page 29: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 29

Adding types works in harmony withmethod overloading.

Page 30: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 30

All type checking is at run time.

This is harmonious with duck typing.

Page 31: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 31

Being up to date

Page 32: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 32

2013

Groovy is the dynamic symbiote to Java,and

Groovy can now be the static language replacement for Java.

Page 33: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 33

@TypeChecked

@CompileStatic

Page 34: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 34

@TypeCheckedstatic staticTyping(x, y) { x * y}

Page 35: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 35

Compile time interface conformance,

no duck typing.

Page 36: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 36

✘✘

Page 37: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 37

Groovy now has C++-style message passing.

Page 38: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 38

@TypeCheckedstatic staticTyping(Number x, Number y) { x * y}

Page 39: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 39

Enforce don't ask.

Page 40: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 40

A totally different view of how computation proceeds: a different computational model.

Page 41: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 41

class ExpandoDynamic { static void main(args) { def x = new Expando() x.multiply = { y -> x.data * y } x.data = 'XX' def y = 2 assert x * 2 == 'XXXX' }}

Page 42: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 42

@TypeCheckedclass ExpandoStatic { static void main(args) { def x = new Expando() x.multiply = { y -> x.data * y } x.data = 'XX' def y = 2 assert x * 2 == 'XXXX' }}

Page 43: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 43

Should a language support two disparate computational models?

Page 44: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 44

final class Thing { final x Thing(x) { this.x = x } Thing multiply(Thing y) { new Thing(x * y.x) } boolean equals(Thing y) { x == y.x } boolean equals (Object y) { x == y } String toString() { x.toString() }}

x = new Thing('XX')y = new Thing(2)z = new Thing(2.0)

assert x * y == 'XXXX'assert x * y == new Thing('XXXX')assert x * z == 'XXXX'assert x * z == new Thing('XXXX')

assert y * z == 4.0assert y * z == new Thing(4.0)

assert z * y == 4.0assert z * y == new Thing(4.0)

Page 45: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 45

@TypeCheckedfinal class Thing { final Integer x_i final String x_s Thing(Integer x) { this.x_i = x ; assert this.x_s == null } Thing(String x) { this.x_s = x ; assert this.x_i == null } Thing multiply(Thing y) { if (x_i == null) { new Thing(StringGroovyMethods.multiply(x_s, (Number)y.x)) } else { new Thing(x_i * y.x_i) } } Object getX() { x_i == null ? x_s : x_i } boolean equals(Thing y) { x_i == null ? x_s == y.x_s : x_i == y.x_i } boolean equals (Object y) { x == y } String toString() { x.toString() }}

x = new Thing('XX')y = new Thing(2)

assert x * y == 'XXXX'assert x * y == new Thing('XXXX')

Page 46: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 46

Should a language do one thing well?

Page 47: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 47

class Duck { static main(args) { def total = 1 def data = [2, 3] try { total += data.sum() } catch (MissingMethodException mme) { total = 0 } assert total == 6 }}

Page 48: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 48

@TypeCheckedclass NotDuck { static void main(String[] args) { Integer total = 1 List data = [2, 3] total += (Integer)data.sum() assert total == 6 }}

Page 49: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 49

Should a language be all things…

Page 50: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 50

Page 51: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 51

…or should we embrace polyglot programming?

Page 52: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 52

Groovy + { JavaScalaCeylonKotlin

Page 53: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 53

Tooling and IDE support is a big issue.

Page 54: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 54

Endnote

Page 55: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 55

This is philosophizing.

Page 56: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 56

Personal experience useful.

We are allowed to debate and (dis)agree.

Create a personal view.

Page 57: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 57

What is really needed is some objective research.

Page 58: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 58

Page 59: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 59

Psychology of programming.

PPIG.

Page 60: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 60

foment [f m nt]əˈ ɛvb (tr) 1. to encourage or instigate (trouble, discord, etc.); stir up 2. (Medicine) Med to apply heat and moisture to (a part of the body) to relieve pain and inflammation

[from Late Latin f ment re, from Latin f mentum a poultice, ultimately from ō ā ōfov re to foster]ē

fomentation [ f m n te n] nˌ əʊ ɛ ˈ ɪʃəfomenter n

Usage: Both foment and ferment can be used to talk about stirring up trouble: he was accused of fomenting/fermenting unrest. Only ferment can be used intransitively or as a noun: his anger continued to ferment (not foment); rural areas were unaffected by the ferment in the cities

Collins English Dictionary – Complete and Unabridged © HarperCollins Publishers 1991, 1994, 1998, 2000, 2003

Page 61: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 61

fermentn [ f m nt]ˈ ɜː ɛ 1. (Life Sciences & Allied Applications / Biochemistry) any agent or substance, such as a bacterium, mould, yeast, or enzyme, that causes fermentation 2. (Life Sciences & Allied Applications / Biochemistry) another word for fermentation 3. commotion; unrestvb [f m nt]əˈ ɛ 1. (Life Sciences & Allied Applications / Biochemistry) to undergo or cause to undergo fermentation 2. to stir up or seethe with excitement

[from Latin fermentum yeast, from ferv re to seethe]ēfermentable adjfermentability nfermenter n

Usage: See at foment

Collins English Dictionary – Complete and Unabridged © HarperCollins Publishers 1991, 1994, 1998, 2000, 2003

Page 62: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 62

?

Page 63: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 63

Is Groovy Static or Dynamic?

Russel Winder

email: [email protected]: [email protected]

twitter: @russel_winderweb: http://www.russel.org.uk

Page 64: Is Groovy static or dynamic

Copyright © 2013 Russel Winder 64

Groovy is Static or Dynamic

Russel Winder

email: [email protected]: [email protected]

twitter: @russel_winderweb: http://www.russel.org.uk