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; }
class Main () { public static void main (String[] args) { System.out.println( 1 - 2 ); System.out.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
class Typing01 { public static void main (String[] args) { int a = 5; String b = "hello"; System.out.println ("Result = " + (a - b)); } }
javac rejects code with (5 - "hello")
javac
(5 - "hello")
$ javac Typing01.java Typing01.java:5: error: bad operand types for binary operator '-' System.out.println ("Result = " + (a - b)); ^ first type: int second type: String
class Typing01 { public static void main (String[] args) { int a = 5; String b = "hello"; System.out.println ("Result = " + (a - (int)(Object)b)); } }
$ java Typing01 ClassCastException: class String cannot be cast to class Integer at Typing01.main(Typing01.java:5)
class Typing06 { public static void main (String[] args) { int a = 5; a = "hello"; } }
$ javac Typing06.java Typing06.java:4: error: incompatible types: String cannot be converted to int a = "hello"; ^
class Typing06 { public static void main (String[] args) { int a = 5; a = (int)(Object)"hello"; } }
$ java Typing06 ClassCastException: class String cannot be cast to class Integer at Typing06.main(Typing06.java:4)
var
class Typing06 { public static void main (String[] args) { var a = 5; a = "hello"; } }
#;> (define (main) (define a 5) (set! a "hello") (display a) ) #;> (main) "hello"
int f (int i, String s) { return true ? i : s; }
var x = 1;