Finglas
Finglas

Reputation: 15707

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 further?

Edit

By objects I mean:

Person.Quack()
Duck.Quack()

With no care about what gets passed into methods.

Upvotes: 1

Views: 199

Answers (1)

Amok
Amok

Reputation: 1269

Duck typing is about not caring what the objects you're working with are as long as they support the necessary operations. So if + is string concatenation then passing strings to Add would be fine. If dates support the + operation then passing dates would be fine as well.

Upvotes: 2

Related Questions