Instructor:
How should we support programmers in interpreting memory content correctly? How should we support programmers in applying operations correctly?
What is difficult about the code below? How can we improve the language to better support programmers?
void* getNext(void* x) { return *(x+4);}void* n = malloc(12); *n = 42; *(n+4) = NULL;void* nn = getNext(n);
typedef struct Node { int value; Node* next; } Node;Node* getNext (Node* x) { return x->next; }Node* n = malloc(sizeof(Node)); n->value = 42; n->next = NULL;Node* nn = getNext(n);
How does Python overcome missing static types?
functools.reduce(function, iterable, [initial, ]/)
typedef struct Node { int value; Node* next; } Node;Node* getNext (Node* x) { return x->next; }
println( 1 - 2 )println( "dog" - "cat" )
#;> (define (f) (- 5 "hello"))
#;> (f)Error in -: expected type number, got '"hello"'.
var x = 5 - "hello"; // x === NaN
Java
int a = 5;String b = "hello";System.out.println ("Result = " + (a - b));
Scala
val a = 5val b = "hello"println(s"Result = ${a-b}")
(5 - "hello")
error: bad operand types for binary operator '-' System.out.println ("Result = " + (a - b)); ^ first type: int second type: String
int a = 5;String b = "hello";System.out.println ("Result = " + (a - (int)(Object)b));
val a = 5val b = "hello"println(s"Result = ${a-b.asInstanceOf[Int]}")
ClassCastException: class String cannot be cast to class Integer
int a = 5;a = "hello";
var a = 5a = "hello"
incompatible types: String cannot be converted to int a = "hello"; ^
int a = 5;a = (int)(Object)"hello";
var a = 5a = "hello".asInstanceOf[Int]
var a = 5;a = "hello";
error: incompatible types: String cannot be converted to int a = "hello"; ^
Scheme
#;> (define (main) (define a 5) (set! a "hello") (display a) )#;> (main)"hello"
Javascript
> function f() { var a = 5; a = "hello"; console.log(a); }> f()"hello"
def f(i: Int, s: String) = if true then i else s
val x = 1
Static type checking
Dynamic type checking
50min (5min student activity)
let students discuss among themselves about the meaning of the code without the types