Instructor: Stefan Mitsch
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);
typedef struct Node { int value; Node* next; } Node;Node* getNext (Node* x) { return x->next; }
println( 1 - 2 )println( "dog" - "cat" )
Dynamic type checking detects a failure
How do we know Scheme is dynamic?
#;> (- 5 "hello")Error in -: expected type number, got '"hello"'.
Type checker invoked before execution starts?
#;> (define (f) (- 5 "hello"))#;> (f)Error in -: expected type number, got '"hello"'.
f
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"
def f(i: Int, s: String) = if true then i else s
val x = 1