150 likes | 399 Views
Quack, waddle. Barry Dorrans barryd@idunno.org. Duck Typing. If it walks like a duck and quacks like a duck, it’s a duck. And this means what?. A concentration on the aspects of the object, not the underlying type. And this means what?.
E N D
Quack, waddle Barry Dorrans barryd@idunno.org
Duck Typing If it walks like a duck and quacks like a duck, it’s a duck.
And this means what? A concentration on the aspects of the object, not the underlying type.
And this means what? Who cares what the type is; if it quacks and walks then we may treat it as a duck.
From Python; EAFP Easier to Ask Forgivenessthan Permission. No typeof(), isinstance(). Central to Ruby’s approach.
Object Substitution Substitution can occur if an object supports the methods and properties of the expected type in use in the accepting method.
Type Safety is the one true way Static types can slow productivity. .NET already has duck typing …
Liskoff Substitution Principle Subclasses can substitute base class, based on type. Duck typing substitutes on ability.
.NET foreach() quacks Provide a public method GetEnumerator that takes no parameters and returns a type that has two members: a method MoveNext that takes no parameters and return a Boolean, and a property Current with a getter that returns an Object.
So it’s C# 3.0 var then? var declarations are decided at compile time. var is type safe.
And what use is this? Very loose coupling. Type checking breaks encapsulation. Substituting Sealed classes. Testing & mock objects. Plugin versioning architectures.
Killer Beak Duck typing is not performant.
Library Availability www.deftflux.net/blog/page/Duck-Typing-Project.aspx Depends on http://www.codeplex.com/NGenerics
And yes;duck quacks do echo www.acoustics.salford.ac.uk/acoustics_info/duck/