Paradigms in practice
How do modern languages approach object orientation?
On this page
Python provides classes and runtime method lookup, TypeScript checks public shapes structurally, and Rust combines data types with implementations and traits. To translate a design, identify the promised behaviour and who owns each value. A class-for-class translation can miss a language’s simpler representation.
Worked example
Rusttrait Label { fn label(&self) -> &str; }
struct Job { name: String }
impl Label for Job {
fn label(&self) -> &str { &self.name }
}
fn describe(item: &dyn Label) -> String {
format!("Queued: {}", item.label())
}
fn main() {
let job = Job { name: String::from("export") };
assert_eq!(describe(&job), "Queued: export");
}Explore class relationships in the OOP Playground