Concepts of Programming Languages:
Static vs. Dynamic Typing Toni Schumacher
Institute for Software Engineering and Programming Languages
- 23. November 2015
- T. Schumacher
- 23. November 2015
1/31
Concepts of Programming Languages: Static vs. Dynamic Typing Toni - - PowerPoint PPT Presentation
Concepts of Programming Languages: Static vs. Dynamic Typing Toni Schumacher Institute for Software Engineering and Programming Languages 23. November 2015 T. Schumacher 23. November 2015 1/31 Table of Contents Motivation Typing Static
Institute for Software Engineering and Programming Languages
1/31
Advantages of Static Typing Disadvantages of Static Typing
Advantages of Dynamic Typing Disadvantages of Dynamic Typing
Type Inference Subtyping Generics
2/31
3/31
◮ Widely discussed topic ◮ No exact/clear definitions
◮ Distinguish statically and dynamically typed languages ◮ Knowing benefits and disadvatages of both ◮ When to use which technique
4/31
Advantages of Static Typing Disadvantages of Static Typing
Advantages of Dynamic Typing Disadvantages of Dynamic Typing
Type Inference Subtyping Generics
5/31
◮ Collection of type rules for a programming language ◮ Classifies expressions according to the kinds of values it compute ◮ Assigns type information to values
◮ Checks types of values for correctness ◮ Tracks type violation
◮ Strong / Weak ◮ Optional / Explicit ◮ Static / Dynamic
6/31
7/31
7/31
◮ Failure: compile attempt of the program code is canceled ◮ Considered as the origin of dynamic typing ◮ E.g. Ada, C, C++, Java, Fortran, Haskell, ML, Pascal, Perl and Scala
8/31
9/31
9/31
◮ Failure: partial or complete failure running the program ◮ E.g. Groovy, JavaScript, Objective-C, Perl, PHP
10/31
◮ No clear boundaries between both ◮ Programming languages can’t be equated with typing techniques
◮ E.g. in static languages the main focus is the static type-checker and the
11/31
Advantages of Static Typing Disadvantages of Static Typing
Advantages of Dynamic Typing Disadvantages of Dynamic Typing
Type Inference Subtyping Generics
12/31
◮ Earlier detection of programming mistakes ◮ More opportunities for compiler optimizations
◮ Better developing experience ◮ Better documentation in form of type annotations
13/31
◮ Too rigid ◮ Can’t handle changing requirements ◮ Code is less reusable ◮ Define some exceptions as dynamic errors (e.g. array-out-of-bound) ◮ Should be more complete
◮ Can’t handle a changing variable type
14/31
◮ Too rigid ◮ Can’t handle changing requirements ◮ Code is less reusable ◮ Define some exceptions as dynamic errors (e.g. array-out-of-bound) ◮ Should be more complete
◮ Can’t handle a changing variable type
1
2
14/31
◮ Too rigid ◮ Can’t handle changing requirements ◮ Code is less reusable ◮ Define some exceptions as dynamic errors (e.g. array-out-of-bound) ◮ Should be more complete
◮ Can’t handle a changing variable type
1
2
14/31
◮ Better for prototyping systems with changing or unknown requirements ◮ Allows programs to generate types and functionality based on run-time
15/31
◮ Better for prototyping systems with changing or unknown requirements ◮ Allows programs to generate types and functionality based on run-time
1
2
3
4
5
6
7
8
15/31
◮ Better interaction with systems or modules with unpredictable changing
◮ Important for data intensive programming
16/31
◮ Significantly more runtime errors
◮ More effort of writing exceptions ◮ Late detection of errors
◮ Type checker must check all classes during run-time
17/31
◮ Advantages and disadvantage of both typing techniques applied on
18/31
◮ Process of finding a type for a program within a given type system ◮ Type inference = dynamic typing ◮ Allows you to omit type information when declaring a variable
1
◮ Relies on the availability of static type information
◮ Only in statically typed languages like SML, Haskel, F# etc.
19/31
◮ Reflexive and transitive relation over types ◮ Satisfies subsumption:
◮ If a term has type A, which is a subtype of a type B, then the term also has
type B
◮ Ability to override existing super types with a related datatype ◮ Static type-checker has the type information needed to automatically lift
20/31
1
2
3
◮ Dynamic type-checker associates values with classes
◮ Very inefficient with dynamic type checker ◮ Construct of dynamic typing needs to be rebuild to implement subtyping
21/31
◮ Reference type that has one or more type parameters
◮ Specifying a type argument to declare and instantiate a constructed type ◮ Help to avoid writing the same code multiple times
◮ Type informations are at first available at runtime
1
22/31
◮ Write a new function for any element type and any kind of collection
1
2
3
4
5
6
23/31
◮ Write a new function for any element type and any kind of collection
1
2
3
4
5
6
23/31
◮ Better: dynamically scoped variables like arbitrary type T
1
2
3
24/31
Advantages of Static Typing Disadvantages of Static Typing
Advantages of Dynamic Typing Disadvantages of Dynamic Typing
Type Inference Subtyping Generics
25/31
◮ Dynamic type checking and static type checking appear to be
◮ Can coexist harmoniously ◮ Different techniques of solving this misery:
◮ Static type-checker verifies what it can and dynamic checks verify the rest ◮ Distinguish between statically typed and dynamically typed variables
26/31
1
2
3
4
5
6
7
8
9 10
11
12
13
27/31
28/31
29/31
◮ Dynamic typing for small programs and scripts (fast development, no
◮ Static typing mechanisms for applications relevant to security ◮ Fully expressive language supports the interplay between static and
◮ Static typing where possible and dynamic typing when needed
30/31
31/31