views:

254

answers:

3

Is there a case where you wrote something in such a language (e.g. C#, Java), and missed duck typing? (See this question for arguments against duck typing)

+1  A: 

That other question had little to do with duck typing. Anyway, assuming this doesn't get closed I'd say the one time I really miss duck typing is when trying to test classes with big API's. We need a separate framework to create mocks of them while in another programming language you could conceivably just pass in a self written class that implements the bare basics of what you need.

For instance, try to mock a JDBC ResultSet in java without a framework, it's a bit of a pain.

wds
+3  A: 

Every time you need to work with code that you do not own, and that does not have proper abstraction (HttpContext anyone?). As you can't have a method of yours accept IHttpContext, since HttpContext type does not have that kind of abstraction, you have to settle for an Adapter and/or Factory and such. Would have been extremely nicer if you could have defined the IHttpContext contract in your code, make it look like the HttpContext, set your method to accept IHttpContext, and have a true, real HttpContext object passed in to duck into IHttpContext.

Ken Egozi
First answer of yours I see :)
ripper234
you should be looking harder :)
Ken Egozi
+2  A: 

Never. Been using Java since the '90's and Python since '01 or so.

Here's why I never missed duck typing in Java.

The "Duck Typing in Java Question" is really about an absolute failure to understand Polymorphism. If you ever think you need any kind of run-time type identification or "isinstance" functionality, you've failed to grasp Polymorphism and you're doing it wrong.

See the Programmer Ignorance Pet Peeve question. Failure to grasp polymorphism is a huge problem and leads to this "duck typing in Java" mistake.

If you understand polymorphism, you don't need duck typing and you don't miss it when switching between Python and Java.

On a related note, I only use Python's isinstance() as part of an assertion to make a function that requires integers blow up when it gets a non-integer. isinstance() is sometimes used with attempts in Java to do duck-typing-like things.

The point is that I'm old (52) and not very smart. So I have to keep to a "strong-ish" class hierarchy in Python or I get confused. I always left a space in a Python design for refactoring into more strict class hierarchy if it become necessary.

S.Lott
Using isinstance is not duck typing, in fact it subverts duck typing.class A { method foo() { ... } }class B { method foo() { ... } }function not_duck_typed(x) { if( x isinstance A ) { x.foo() } }function duck_typed(x) { x.foo() }The who point of duck typing is you can pass an instance of either A or B into duck_typed, despite the fact that they don't share a common anecestor or formal interface. Duck typing is still polymorphism.
Logan Capaldo
You're confusing run-time type identification/isinstance with duck typing. In Java, interfaces are defined explicitly - in duck typed languages they're defined implcitly by the methods an object exposes.
Joe Gauterin