Variable Typing CS 480/680 – Comparative Languages
Variable Typing2 Topics Typing Strong vs. weak Static vs. Dynamic
Variable Typing3 Strong typing Strongly typed – many definitions All variable types are defined by the language Conversions between variable types must be explicit C is (arguably) strongly typed Some people argue differently due to type casting/coercion int i = 2000; float* ptr = (float *) i; float f = *i;
Variable Typing4 Weak typing Weakly typed – C++, Perl, Ruby On the fly conversion between types Can be temporary or permanent > perl t.pl i = 003 i = 21 i = 21 dollars $i = "003"; print "i = $i\n"; $i = $i * 7; print "i = $i\n"; $i = $i. " dollars"; print "i = $i\n";
Variable Typing5 Static Typing Static typing – type violations are checked at compile time (C/C++) The compiler makes sure that you never write code that could place a floating point value into an integer, etc. int i; float j; i = j * 7; Compiler identifies type violation at compile-time. What response: Error? Warning? Coercion?
Variable Typing6 Static Typing (2) During compilation, the compiler keeps type information in the symbol table, which is discarded afterward. No run-time space or time overhead VariableTypeLocation iint0x2002 jfloat0x30FC
Variable Typing7 Dynamic Typing Variable type is checked at run time Type information is stored along with the variable contents 200030FC 2002ptr_to_fixnum 20040004 2006fixnum 2008F37B 200Afloat numptr val ratio
Variable Typing8 Typing examples DynamicStatic StrongAny examples? C/C++ (?) Fortran Pascal Weak Perl Python Scheme Can this be done?