duck-typing

What are some advantages of duck-typing vs. static typing?

I'm researching and experimenting more with Groovy and I'm trying to wrap my mind around the pros and cons of implementing things in Groovy that I can't/don't do in Java. Dynamic programming is still just a concept to me since I've been deeply steeped static and strongly typed languages. Groovy gives me the ability to duck-type, but ...

Is there any point for interfaces in dynamic languages?

In static languages like Java you need interfaces because otherwise the type system just won't let you do certain things. But in dynamic languages like PHP and Python you just take advantage of duck-typing. PHP supports interfaces. Ruby and Python don't have them. So you can clearly live happily without them. I've been mostly doing my ...

from X import a versus import X; X.a

This is one of those semi-religious Python questions that I suspect has well reasoned responses lurking in the community. I've seen some Python programmers use the following style fairly consistently (we'll call it style 1): import some_module # Use some_module.some_identifier in various places. For support of this style you can cite ...

Could I improve this method with duck typing?

Hopefully I haven't misunderstood the meaning of "duck typing", but from what I've read, it means that I should write code based on how an object responds to methods rather than what type/class it is. Here's the code: def convert_hash(hash) if hash.keys.all? { |k| k.is_a?(Integer) } return hash elsif hash.keys.all? { |k| k.is_a...

Will C#4.0 dynamic objects have some facility for duck typing?

In C#4.0 we're going to get dynamic types, or objects whose "static type is dynamic", according to Anders. This will allow any method invocation resolution to happen at runtime rather than compile time. But will there be facility to bind the dynamic object to some sort of contract (and thereby also get full intellisense for it back), rat...

Are there any static duck-typed languages?

Can I specify interfaces when I declare a member? After thinking about this question for a while, it occurred to me that a static-duck-typed language might actually work. Why can't predefined classes be bound to an interface at compile time? Example: public interface IMyInterface { public void MyMethod(); } public class MyClass //D...

How is duck typing different from the old 'variant' type and/or interfaces?

I keep seeing the phrase "duck typing" bandied about, and even ran across a code example or two. I am way too lazy busy to do my own research, can someone tell me, briefly: the difference between a 'duck type' and an old-skool 'variant type', and provide an example of where I might prefer duck typing over variant typing, and provide a...

Duck type as syntactic sugar for reflection: Good or bad idea?

I've been thinking lately, would it be a good form of syntactic sugar in languages like Java and C#, to include a "duck" type as a method parameter type? This would look as follows: void myFunction(duck foo) { foo.doStuff(); } This could be syntactic sugar for invoking doStuff() via reflection, or it could be implemented different...

Uses for Dynamic Languages

My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent compile-time duck typing...

When you say Ruby is reflective, does this mainly refer to "duck typing"?

I was reading a text describing Ruby and it said the following: Ruby is considered a “reflective” language because it’s possible for a Ruby program to analyze itself (in terms of its make-up), make adjustments to the way it works, and even overwrite its own code with other code. I'm confused by this term 'reflective' - ...

C#. Add polymorphism for legacy code

Hello, Assume we have legacy classes, that can't be modified: class Foo { public void Calculate(int a) { } } class Bar { public void Compute(int a) {} } I want to write a helper with such signature: void Calc(object obj, int a); Notice, that the first argument is of type 'object'. The test code should be some like this: ...

How do I argue against Duck-typing in a strongly typed language like Java?

I work on a team of Java programmers. One of my co-workers suggests from time-to-time that I do something like "just add a type field" (usu. "String type"). Or code will be committed laden with "if (foo instanceof Foo){...} else if( foo instanceof Bar){...}". Josh Bloch's admonition that "tagged classes are a wan imitation of a proper ...

Besides dynamic typing, what makes Ruby "more flexible" than Java?

I've been using Java almost since it first came out but have over the last five years gotten burnt out with how complex it's become to get even the simplest things done. I'm starting to learn Ruby at the recommendation of my psychiatrist, uh, I mean my coworkers (younger, cooler coworkers - they use Macs!). Anyway, one of the things they...

C# and Interfaces - Explicit vs. Implicit

In C#, if a class has all the correct methods/signatures for an Interface, but doesn't explicitly implement it like: class foo : IDoo {} Can the class still be cast as that interface? ...

How can I tell if a python variable is a string or a list?

I have a routine that takes a list of strings as a parameter, but I'd like to support passing in a single string and converting it to a list of one string. For example: def func( files ): for f in files: doSomethingWithFile( f ) func( ['file1','file2','file3'] ) func( 'file1' ) # should be treated like ['file1'] How can ...

Is there a dream language that merges the benefits of dynamic and strong typing?

I would be interested to learn a language that handles objects internally as hashtables (like JavaScript) but could wrap them with strong types to offer the benefits of code completion/intellisense in design time. Here is how I wish this dream language to work: public class Lion { public void Roar() { Console.WriteLine("Aaarrgghh");} ...

Arguments for duck-typing in a strongly-typed OOP language?

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) ...

What's an example of duck typing in Java?

I just recently heard of duck typing and I read the Wikipedia article about it, but I'm having a hard time translating the examples into Java, which would really help my understanding. Would anyone be able to give a clear example of duck typing in Java and how I might possibly use it? ...

game design - handling bonuses / duck typing - python

Hi! I am currently faced with a design problem in my game design, not terrible but it bothers me enough so I want to ask others opinions :-) I am currently experimenting with pygame, I have developed a little space shooter and now I would like to handle some bonuses. Right now I have an abstract class Bonus from which derive all the b...

Duck typing - what about when you need a concrete type?

Say you are doing a calculator in a dynamic language (Python etc...) and you have an add method. def Add(x, y) print x + y Now if you were to pass in anything but a number that would be wrong, so you need some datatype checking. Is Duck Typing about objects as opposed to parameters like the above example? Could anyone explain fu...