Static vs dynamic types

50 %
50 %
Information about Static vs dynamic types
Technology

Published on March 7, 2014

Author: parrt

Source: slideshare.net

Description

Static vs dynamic type systems in computer programming languages.

Computer Language Design and Implementation meetup March 6, 2014 Cat people vs dog people The great static vs dynamic typing debate Terence Parr University of San Francisco (You’re all wrong, I’m right)

Definitions (strong vs weak vs untyped) ● Static typing: compiler knows the type of every expression (using type inference here) var i = 3; var j = [“parrt”,”ollie”,”kay”]; ● Explicit static typing: coder gives type int i; List<String> a = new ArrayList<String>(); Explicit doesn’t mean “dirty” var dog : shit yes, a puppy dies every time you type this

Definitions Cont’d ● Dynamic typing: compiler ignores types, values’ types known only at runtime ● “Duck typing”: structural dynamic typing; i.e., the name (nominative) doesn’t matter, only the member list (Java uses interfaces) class A: {var name} class B: {var name} def f(x): {print x.name} f(new A()) f(new B())

Eric Lippert from stackoverflow ● “it is hard to maintain a large codebase, period” ● “there is a strong correlation between a language being dynamically typed and a language also lacking all the other facilities that make lowering the cost of maintaining a large codebase easier” ● So, to focus on static versus dynamic argument, let’s assume...

Assumptions ● Comparing two identical languages, one with and one w/o static typing ● Language has modern facilities to support large projects: modules, perhaps objects, encapsulation, data hiding, etc… ○ E.g., javascript missing lots of that ● Large projects not small scripts

Can we agree... ● Coder must know (partial) types statically or (s)he can’t access appropriate fields, funcs: o.name, o.save() ● With perfect human memory of your own code, no explicit types needed... ● ...but what if we call foreign library function or work on a team (i.e., all of us)? ● Must read lib code or doc to learn types

Reality ● Can’t remember type of all vars/funcs in large project (even solo project) ● We have to read lots of code every day ○ We work in teams ○ We use lots of libraries ○ Either we compute type or we read annotation ● Code always morphs over time ○ Explicit type info identifies key variable constraints ○ Code changes but do we update doc always?

Static typing burdens 1. Much faster to physically enter code w/o annotating variables with type info (don’t overestimate this cost) 2. Type annotations can clutter code 3. Harder for newbies to get rolling? 4. Type systems can add a lot of complexity to the language: Java’s generics, Scala, ... 5. Python (others) has structural type equivalence (duck typing) vs nominative (naming) for most static languages ○ Convenient if we care just that obj answers size() ○ Java requires creation of an interface

Dynamic typing burdens ● We exchange type annotation in code with unit/functional testing just to catch typos o. feild, o.methud(). More tests in general ● Should informally add type info in doc, a 2nd spec we must keep in sync ● Lippert: Need “good discipline about naming conventions, about division of responsibilities, about what the public surface of a given object is, and so on.”

Dynamic typing collateral damage ● Often less efficient ● Re-factoring challenging, dangerous, or impossible; can’t even rename safely ● Can’t do auto-completion, find usages/implementations, … ● Changes to type assumptions (in libs, code) introduce hidden landmines ● Can be more effort to read (computing

A compromise ● Annotate: fields (unless initialized), args, return values ● Generics only on built-in types? ● At the very least, use runtime generic type parameters to allow covariance List<? extends Animal> d=...; d.add(new Dog()); ● Type inference inside method bodies ● Allow plausible downcasting Jesus cries every time javac disallows this

Sample compromise class class Person { … } class User : Person { val BADID = 0 var id : string def permissions(name : string) : int { val m = Services.getFileManager() val f = m.getFile(name) return f.permissions(this.id) } } var p : Person = new User() var mask = p.permissions(new file(“...”)) // downcast

Speculation ● Static typing is seen as old-school. Younger programmers are always excited by the latest languages and these languages happen to be dynamically typed: JavaScript, Python, Ruby. Scala is the counterexample (others?) ● With experience, comes the pain of maintenance which motivates static typing ● I.e., there is a strong correlation between dynamic typing and youth

Summary ● Static + inferred typing is good, umkay? ● Dynamic typing is great for scripts but not large programs, umkay? ● Yes, we old farts rely more and more on static typing as our brains disintegrate ● People study this empirically see e.g., Andreas Stefik

Add a comment

Related presentations

Related pages

Introduction to Static and Dynamic Typing - SitePoint

Do you know the difference between static and dynamic typing? Premshree lifts the veil on these concepts, along with others that relate to data types, in ...
Read more

inheritance - Difference between Dynamic and Static type ...

Difference between Dynamic and Static type assignments in Java. ... the dynamic and static types for the following ... Static array vs. dynamic array in ...
Read more

Type system - Wikipedia

Static vs. Dynamic; ... Static and dynamic type checking in ... gradual typing uses a special type named dynamic to represent statically-unknown types, ...
Read more

Dynamic vs Static typing controversy - stackoverflow.com

Static types correspond to the mathematical notion of type theory, ... From Artima's Typing: Strong vs. Weak, Static vs. Dynamic article:
Read more

Programming Concepts: Static vs. Dynamic Type Checking ...

Programming Concepts: Static vs. Dynamic Type Checking. November 20, 2015. ... is a classification identifying one of various types of data.
Read more

Dynamic vs. Static Characters - ReadWriteThink

Dynamic vs. Static Characters Characters experience varying amounts of change over the course of a story. Two types of characters are • Static characters ...
Read more

What is dynamic and static? - Definition from WhatIs.com

In general, dynamic means energetic, capable of action and/or change, or forceful, while static means stationary or fixed.
Read more

Dynamic and Static IP Address Differences

Static IP address versus Dynamic: Should It Matter to You? When you sign up with an Internet Service Provider you will either end up with a static IP ...
Read more

Using Type dynamic (C# Programming Guide)

Visual C# 2010 introduces a new type, dynamic. The type is a static type, ... Conversions between dynamic objects and other types are easy.
Read more

Basic and Dynamic Disks (Windows) - msdn.microsoft.com

... basic disks and dynamic disks. Note that the storage types discussed ... Another difference between basic and dynamic disks is that dynamic ...
Read more