Luca Franceschini
PhD seminars series @ DIBRIS
23 May 2019
(stolen title)
fun doCoolStuff(a, b) {
// bla bla
var x = a.method();
var y = b.anotherMetod();
return a+b;
}
fun doCoolStuff(a: String, b: Int): Int {
// bla bla with types
var x: Int = a.method();
var y: Int = b.anotherMetod();
return x+y;
}
fun doCoolStuff(a, b) {
if (a !is String)
throw Exception("gimme a string")
if (a !is Int)
throw Exception("gimme a number")
// bla bla
var x = a.method();
var y = b.anotherMetod();
return a+b;
}
fun doCoolStuff(a: String, b: Int): Int {
// bla bla with types
var x: Int = a.method();
var y: Int = b.anotherMetod();
return x+y;
}
/**
* {a} must be of type String.
* {b} must be of type Int.
* {return}s an object of type Int.
*/
fun doCoolStuff(a, b) {
if (a !is String)
throw Exception("gimme a string")
if (a !is Int)
throw Exception("gimme a number")
// bla bla
var x = a.method();
var y = b.anotherMetod();
return a+b;
}
fun doCoolStuff(a: String, b: Int): Int {
// bla bla with types
var x: Int = a.method();
var y: Int = b.anotherMetod();
return x+y;
}
... and maybe some not-that-interesting unit tests
fun doCoolStuff(a, b) {
// bla bla
var x = a.method();
var y = b.anotherMetod();
return a+b;
}
fun doCoolStuff(a: String, b: Int): Int {
// bla bla with types
var x: Int = a.method();
var y: Int = b.anotherMetod();
return x+y;
}
The Python way: "If it walks like a duck and it quacks like a duck, then it must be a duck"
bartender.shot()
gun.shot()
// some stuff
writer.write(myInt);
// some more
// some stuff
writer.write(7);
// some more
["What to Know Before Debating Type Systems", Chris Smith, 2011]
"You take the blue pill, the story ends, you wake up in your Python IDE and write whatever you want to write. You take the red pill, you stay in Type theory, and I show you how deep the rabbit hole goes." (~Matrix)
fun doCoolStuff(a: String, b: Int): Int {
// bla bla with types
var x: Int = a.method();
var y: Int = b.anotherMetod();
return x+y;
}
fun <T> length(l: List<T>): Int {
// somehow count elements...
return size;
}
fun foo(l: List<Apple>) { ... }
// should this work?
foo(listOf<GreenApple>());
// should this work?
foo(listOf<Fruit>());
// Java
void foo(List<? extends Apple> l) { ... }
// this now works
foo(listOf<GreenApple>());
// this doesn't
foo(listOf<Fruit>());
// C#
void foo<T>(T obj) where T : new() {
// here I can do this
var x = new T();
}
// Java
void <T> sort(
List<T extends Comparable<T>> l) {
// sorting algorithm
}
data Tree a = Leaf
| Node a (Tree a) (Tree a)
"dad"
"son"
let stringTree = Node "dad"
Leaf
(Node "son" Leaf Leaf)
data Vec : Nat -> Type -> Type where ...
u = ["foo"]
v = [1, 2, 3]