Static and Dynamic Typing
When a program combines values, its language determines which combinations make sense. Static typing enforces type restrictions through analysis before execution. Dynamic typing enforces type restrictions using the types of values encountered during execution. This distinction helps you predict when an incompatible operation will be reported and what a successful test run actually establishes.
When type restrictions are enforced
A type describes a category of values and the operations permitted on them. Consider a teaching language with integer values of type Int and text values of type Text. Its addition operator accepts two integers and produces an integer:
Here, operand types matter. The integer 5 and the text "five" belong to different categories. In this language, addition does not automatically convert text into an integer.
With static typing, the language analyzes the program's type restrictions before running its statements. If that analysis rejects the program, execution does not begin. The analysis need not calculate the actual results of the program to identify an incompatible operation.
With dynamic typing, an addition checks the values available when that addition executes. An incompatible addition can therefore fail after earlier statements have already completed. Conversely, an incompatible operation on an execution path that is never taken may produce no error during that run.
Compare the same program
For a concrete comparison, suppose the static variant of our teaching language requires each variable to retain its declared type. The dynamic variant allows a variable to hold values of different types at different times. Both variants use the integer-only addition rule above.
Example: A counter initially stores the integer 5. A later assignment replaces it with the text "five", followed by an attempt to add 3.
Event | Static variant | Dynamic variant |
|---|---|---|
Store an integer in an integer variable | Permitted | Permitted |
Assign text to that same variable | Rejected before execution | Permitted during execution |
Add an integer after the text assignment | Program has already been rejected | Fails when this addition executes |
The dynamic assignment is permitted because that variant places no fixed type restriction on the variable. The later addition fails because its current value is text. Changing what a variable holds does not change which operand types an operation accepts.
This example uses particular assignment and addition rules. Other languages may permit additional combinations, such as joining two text values. Read the language's stated rules before predicting an outcome.
What the distinction does and does not promise
Static typing does not require every type to be written explicitly in source code. A language can establish some types without written labels; the details of that process are a separate topic. Similarly, dynamic typing does not require every variable to change types during a program.
Neither model determines whether an implementation translates code ahead of time or executes it through an interpreter. Those implementation choices do not, by themselves, establish when type restrictions are enforced.
Passing static analysis also does not establish that a program produces the intended answer. A program can use compatible types yet calculate the wrong total. Depending on the language, additional failures, such as an out-of-range array access, may still require checks during execution.
What to remember
- Identify whether the relevant type restriction is enforced before execution or during execution.
- Separate a variable's current value from the restrictions on operations using that value.
- Check the language's rules rather than assuming automatic conversions.
- Type correctness is narrower than overall program correctness.
Example: A receipt value changes type
A receipt application temporarily replaces a numeric subtotal with text. A type error occurs when an assignment or operation violates the language's restrictions on permitted types. Compare two variants of a teaching language: the static variant fixes each variable's declared type and analyzes every statement before execution; the dynamic variant allows variables to hold different value types and checks operations when they execute.
In both variants, + accepts only two integers, performs no conversion, and returns their integer sum. print displays its argument. A dynamic type error stops execution immediately; static rejection prevents all execution. These rules fully specify the examples below.
Static version:
var subtotal: Int = 18
print(subtotal + 6)
subtotal = "18"
print(subtotal + 2)Dynamic version:
var subtotal = 18
print(subtotal + 6)
subtotal = "18"
print(subtotal + 2)Part a: Analyze the static version
Does the static version begin executing? Identify the incompatible statement and state whether anything is printed.
The declaration fixes the permitted type of subtotal as ________. The assignment on the third line supplies a value of type ________, because quotation marks make "18" text rather than an integer.
The assignment therefore violates the static variant's restriction. Analysis rejects the complete program before its first statement executes. The earlier print statement does not run merely because it appears before the incompatible assignment.
Answer: The third line causes static rejection. Execution never begins, so nothing is printed.
Part b: Trace the dynamic version
What does the dynamic version print before it stops, and how many additions complete successfully?
The first assignment stores the integer 18. The first addition receives two integers, so it completes:
The application prints 24. The next assignment stores the text "18", which is permitted in this dynamic variant.
At the final addition, the current type of subtotal is ________, while the type of 2 is ________. This pair does not satisfy the integer-only addition rule. The error occurs when the final addition executes, after the earlier output has already appeared.
Answer: The application prints 24, then stops with a type error on the final line. Exactly 1 addition completes successfully; the failed addition is an attempt, not a completed operation.
Part c: Repair the stored value
Replace the third line of the dynamic version with subtotal = 30. What two numbers are printed, and how many additions complete successfully?
The first assignment and print statement are unchanged, so the first output remains:
The replacement assignment stores the integer 30, rather than text. The final addition now receives two integers and completes:
Answer: The outputs are 24 followed by 32, and 2 additions complete successfully. The repair works by supplying a permitted operand type; it does not depend on an automatic text-to-integer conversion.
A developer is choosing types for application data and operations. Match each type constructor to the description of the structure it specifies.
A specification of the accepted argument types and the result type of a callable operation
A finite collection of explicitly named alternatives, each with no associated data
A composite containing one component from each of several specified sets of values
A composite selecting one named alternative and carrying only the data associated with that alternative
Function type
Product type
Tagged union
Enumeration
Three independent scripts run in a dynamically typed teaching language. Variables may hold integers or text. The + operator accepts only two integers, performs no conversion, and stops the script on a type error. Each assignment or print line counts as one statement. Count additions inside print as well as assignments.
For each script, enter the exact number of additions that finish successfully and the number of the first failing statement. Enter none if no statement fails. Count only additions that actually complete.
Script A:
x = 7
x = x + 5
print(x + 3)Script B:
x = 8
print(x + 4)
x = "8"
print(x + 1)Script C:
x = "9"
x = 9
print(x + 2)| Script | Successful additions | First failing statement |
| A | ||
| B | ||
| C |