Type systems differ in when they catch type errors.
Statically Typed (e.g., Java, C++, Rust)
Types checked at compile time
Type errors caught before runtime
Type inference can reduce declarations
Guarantees have language-specific limits
fn greet(name:String) ->String{format!("Hello, {}!", name)}// Won't compile: type mismatchgreet(42);// Error at compile time
Dynamically Typed (e.g., Python, JavaScript)
Types checked at runtime
Type errors appear during execution
More flexible, less verbose
No compile-time type guarantees
def greet(name: str) ->str:returnf"Hello, {name.upper()}!"# Works with correct typegreet("alice") # "Hello, ALICE!"# Wrong type fails at runtimegreet(42) # Runtime error: 'int' object has no attribute 'upper'
Every language needs boundary checks: static types alone do not validate incoming data.
from pydantic import BaseModelfrom datetime import datetimeclass Event(BaseModel): event_id: int# Coerces string to int timestamp: datetime # Parses ISO 8601 strings is_public: bool# Coerces to bool attendees: list[str] # Coerces tuple to list
from pydantic import ValidationErrortry: user = SignupForm( username="ab", # Too short email="not-an-email", # Invalid format )except ValidationError as e: issues = [ {"loc": err["loc"], "msg": err["msg"]}for err in e.errors() ]print(issues) # Two field errors