Concepts of Programming Languages: Static vs. Dynamic Typing Toni - - PowerPoint PPT Presentation

concepts of programming languages
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

Table of Contents

Motivation Typing Static Typing Dynamic Typing Comparison Static Typing

Advantages of Static Typing Disadvantages of Static Typing

Dynamic Typing

Advantages of Dynamic Typing Disadvantages of Dynamic Typing

Programming Concepts

Type Inference Subtyping Generics

Outlook and Conclusion Hybrid Languages Conclusion

  • T. Schumacher
  • 23. November 2015

2/31

slide-3
SLIDE 3

Motivation Quote 1:

Once syntactic verbosity [...] is removed from statically typed languages, there is absolutely no advantage in using a dynamically typed language. jooq.org/2014/12/11/the-inconvenient-truth-about-dynamic-vs-static-typing/

Quote 2:

With unit tests [...] the types will also get checked, so you may as well go for dynamic typing and benefit from its advantages. teamten.com/lawrence/writings/java-for-everything.html

  • T. Schumacher
  • 23. November 2015

3/31

slide-4
SLIDE 4

Motivation

◮ Widely discussed topic ◮ No exact/clear definitions

֒ → Which issues do we want to tackle?

◮ Distinguish statically and dynamically typed languages ◮ Knowing benefits and disadvatages of both ◮ When to use which technique

  • T. Schumacher
  • 23. November 2015

4/31

slide-5
SLIDE 5

Outline

Motivation Typing Static Typing Dynamic Typing Comparison Static Typing

Advantages of Static Typing Disadvantages of Static Typing

Dynamic Typing

Advantages of Dynamic Typing Disadvantages of Dynamic Typing

Programming Concepts

Type Inference Subtyping Generics

Outlook and Conclusion Hybrid Languages Conclusion

  • T. Schumacher
  • 23. November 2015

5/31

slide-6
SLIDE 6

Typing Definition: Type System

◮ Collection of type rules for a programming language ◮ Classifies expressions according to the kinds of values it compute ◮ Assigns type information to values

Definition: Type Checker

◮ Checks types of values for correctness ◮ Tracks type violation

Differentations:

◮ Strong / Weak ◮ Optional / Explicit ◮ Static / Dynamic

  • T. Schumacher
  • 23. November 2015

6/31

slide-7
SLIDE 7

Static Typing Definition: Static Typing

The type checker tries to assign objects to their particular type during the compile process.

  • T. Schumacher
  • 23. November 2015

7/31

slide-8
SLIDE 8

Static Typing Definition: Static Typing

The type checker tries to assign objects to their particular type during the compile process.

  • T. Schumacher
  • 23. November 2015

7/31

slide-9
SLIDE 9

Static Typing

◮ 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

  • T. Schumacher
  • 23. November 2015

8/31

slide-10
SLIDE 10

Dynamic Typing Definition: Dynamic Typing

Variables are associated with their contained values during run-time by tagging them with identifiers such as num, bool or fun.

  • T. Schumacher
  • 23. November 2015

9/31

slide-11
SLIDE 11

Dynamic Typing Definition: Dynamic Typing

Variables are associated with their contained values during run-time by tagging them with identifiers such as num, bool or fun.

  • T. Schumacher
  • 23. November 2015

9/31

slide-12
SLIDE 12

Dynamic Typing

֒ → Is inherently a restricted form of static typing with only a single type during compile-time

◮ Failure: partial or complete failure running the program ◮ E.g. Groovy, JavaScript, Objective-C, Perl, PHP

, Prolog, Python, Ruby and Smalltalk

  • T. Schumacher
  • 23. November 2015

10/31

slide-13
SLIDE 13

Static - Dynamic

◮ No clear boundaries between both ◮ Programming languages can’t be equated with typing techniques

֒ → Can use both static and dynamic type checking

◮ E.g. in static languages the main focus is the static type-checker and the

dynamic typing (if existing) is not superficial ֒ → Leads to controverse discussions about the topic

  • T. Schumacher
  • 23. November 2015

11/31

slide-14
SLIDE 14

Outline

Motivation Typing Static Typing Dynamic Typing Comparison Static Typing

Advantages of Static Typing Disadvantages of Static Typing

Dynamic Typing

Advantages of Dynamic Typing Disadvantages of Dynamic Typing

Programming Concepts

Type Inference Subtyping Generics

Outlook and Conclusion Hybrid Languages Conclusion

  • T. Schumacher
  • 23. November 2015

12/31

slide-15
SLIDE 15

Advantages of Static Typing

◮ Earlier detection of programming mistakes ◮ More opportunities for compiler optimizations

֒ → Increased runtime efficiency and reduced memory usage

◮ Better developing experience ◮ Better documentation in form of type annotations

  • T. Schumacher
  • 23. November 2015

13/31

slide-16
SLIDE 16

Disadvantages of Static Typing

◮ 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

֒ → Complex and overly complicated concepts added

◮ Can’t handle a changing variable type

  • T. Schumacher
  • 23. November 2015

14/31

slide-17
SLIDE 17

Disadvantages of Static Typing

◮ 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

֒ → Complex and overly complicated concepts added

◮ Can’t handle a changing variable type

Example: Changing variable type

1

employeeName = 9;

2

employeeName = "Steve";

  • T. Schumacher
  • 23. November 2015

14/31

slide-18
SLIDE 18

Disadvantages of Static Typing

◮ 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

֒ → Complex and overly complicated concepts added

◮ Can’t handle a changing variable type

Example: Changing variable type

1

employeeName = 9;

2

employeeName = "Steve"; ֒ → Type error

  • T. Schumacher
  • 23. November 2015

14/31

slide-19
SLIDE 19

Advantages of Dynamic Typing

◮ Better for prototyping systems with changing or unknown requirements ◮ Allows programs to generate types and functionality based on run-time

data ֒ → Much more flexible

  • T. Schumacher
  • 23. November 2015

15/31

slide-20
SLIDE 20

Advantages of Dynamic Typing

◮ Better for prototyping systems with changing or unknown requirements ◮ Allows programs to generate types and functionality based on run-time

data ֒ → Much more flexible

Example: Eval function in dynamic languages

1

function example(str){

2

var x = 10;

3

var y = 20;

4

var a = eval("x * y");

5

var b = eval("2 + 2");

6

var c = eval("x + 17");

7

var d = eval(str);

8

}

  • T. Schumacher
  • 23. November 2015

15/31

slide-21
SLIDE 21

Advantages of Dynamic Typing

◮ Better interaction with systems or modules with unpredictable changing

  • utput

◮ Important for data intensive programming

֒ → Indispensable for dealing with truly dynamic program behavior

  • T. Schumacher
  • 23. November 2015

16/31

slide-22
SLIDE 22

Disadvantages of Dynamic Typing

◮ Significantly more runtime errors

֒ → More costs in development process

◮ More effort of writing exceptions ◮ Late detection of errors

֒ → Complex troubleshooting and error fixing

◮ Type checker must check all classes during run-time

֒ → Worse execution time

  • T. Schumacher
  • 23. November 2015

17/31

slide-23
SLIDE 23

Programming Concepts

◮ Advantages and disadvantage of both typing techniques applied on

important programming concepts:

  • 1. Type Inference
  • 2. Subtyping
  • 3. Genercis
  • T. Schumacher
  • 23. November 2015

18/31

slide-24
SLIDE 24

Type Inference Definition: Type Inference

◮ 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

Example: Type inference in SML

1

fun fak(n) = if (n = 0) then 1 else n * fak(n-1);

◮ Relies on the availability of static type information

֒ → Redundant for dynamic languages

◮ Only in statically typed languages like SML, Haskel, F# etc.

  • T. Schumacher
  • 23. November 2015

19/31

slide-25
SLIDE 25

Subtyping Definition: Subtyping

◮ 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

inferred variables to required types

  • T. Schumacher
  • 23. November 2015

20/31

slide-26
SLIDE 26

Subtyping Example: Subtyped addition on nullable integers in C#

1

int? a = null;

2

int? b = 1;

3

int? c = a + b;

◮ Dynamic type-checker associates values with classes

֒ → Exclude value types immediately

◮ Very inefficient with dynamic type checker ◮ Construct of dynamic typing needs to be rebuild to implement subtyping

  • T. Schumacher
  • 23. November 2015

21/31

slide-27
SLIDE 27

Generics Definition: Generics

◮ Reference type that has one or more type parameters

֒ → Parameterized type

◮ Specifying a type argument to declare and instantiate a constructed type ◮ Help to avoid writing the same code multiple times

Dynamic type checking:

◮ Type informations are at first available at runtime

֒ → Any collection or method is automatically generic ֒ → Create highly reusable libraries

Example: Generics in dynamically typed languages

1

new Set<object.getClass()>(object);

  • T. Schumacher
  • 23. November 2015

22/31

slide-28
SLIDE 28

Generics

Static type checking:

◮ Write a new function for any element type and any kind of collection

Example: Generics in statically typed languages

1

class Set{

2

public Set(boolean b) { ... }

3

public Set(int i) { ... }

4

.. other constructors.

5

}

6

new Set<Object>(object);

  • T. Schumacher
  • 23. November 2015

23/31

slide-29
SLIDE 29

Generics

Static type checking:

◮ Write a new function for any element type and any kind of collection

Example: Generics in statically typed languages

1

class Set{

2

public Set(boolean b) { ... }

3

public Set(int i) { ... }

4

.. other constructors.

5

}

6

new Set<Object>(object); ֒ → Endless number of types

  • T. Schumacher
  • 23. November 2015

23/31

slide-30
SLIDE 30

Generics

◮ Better: dynamically scoped variables like arbitrary type T

Example: Usage of Generics in C#

1

interface IEnumerator<T> {

2

T Current{get;}

3

} ֒ → Generics are not impossible in static typing ֒ → Much more easier to implement with dynamic type checking

  • T. Schumacher
  • 23. November 2015

24/31

slide-31
SLIDE 31

Outline

Motivation Typing Static Typing Dynamic Typing Comparison Static Typing

Advantages of Static Typing Disadvantages of Static Typing

Dynamic Typing

Advantages of Dynamic Typing Disadvantages of Dynamic Typing

Programming Concepts

Type Inference Subtyping Generics

Outlook and Conclusion Hybrid Languages Conclusion

  • T. Schumacher
  • 23. November 2015

25/31

slide-32
SLIDE 32

Hybrid Languages

◮ Dynamic type checking and static type checking appear to be

incompatible

◮ 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

  • T. Schumacher
  • 23. November 2015

26/31

slide-33
SLIDE 33

Hybrid Languages Example: Static and dynamic variables in C#

1

class ExampleClass{

2

public ExampleClass() { }

3

public void exampleMethod1(int i) {}

4

}

5

static void Main(string[] args){

6

ExampleClass ec = new ExampleClass();

7

//would cause compiler error

8

ec.exampleMethod1(10, 4);

9 10

dynamic dynamic_ec = new ExampleClass();

11

// no compiler error, but cause run-time exception.

12

dynamic_ec.exampleMethod1(10, 4);

13

}

  • T. Schumacher
  • 23. November 2015

27/31

slide-34
SLIDE 34

Conclusion

Questions?

  • T. Schumacher
  • 23. November 2015

28/31

slide-35
SLIDE 35

Conclusion

Static or Dynamic type checking?

  • T. Schumacher
  • 23. November 2015

29/31

slide-36
SLIDE 36

Conclusion

My opinion:

◮ Dynamic typing for small programs and scripts (fast development, no

major safety requirements)

◮ Static typing mechanisms for applications relevant to security ◮ Fully expressive language supports the interplay between static and

dynamic techniques

◮ Static typing where possible and dynamic typing when needed

xkcd.com

  • T. Schumacher
  • 23. November 2015

30/31

slide-37
SLIDE 37

Conclusion

Thank you for your attention.

  • T. Schumacher
  • 23. November 2015

31/31