Readplace

Duck typing

Wikimedia Foundation, Inc. 3 min read
View original
Summary (TL;DR)
Duck typing is a programming concept where an object's suitability is determined by the presence of certain methods and properties, rather than its explicit type. It applies the duck test: if it walks like a duck and quacks like a duck, it's a duck. This differs from nominative typing, which requires explicit type declarations. Duck typing is dynamic and runtime-based, unlike static structural typing. It can be implemented in statically typed languages through runtime checks, templates, or reflection. It offers flexibility by allowing objects to be used as long as they implement required methods at runtime, without needing adapters or explicit interfaces.