Idiomdrottning’s homepage

Duck typing

Duck typing in programming means that if an object can fly, we can use it wherever we need an object that can fly, and if an object can quack, we can use it wherever we need an object that can quack. Contrasted with other typing systems that are more strict, like, “why are you asking an eagle to fly? I thought only doves could fly!”

Pasting from my own comment on 🦞:

Duck typing is perfect for hackers because we are our own customers ♥

We don’t need to spend sixteen hours telling the robot in excruciating, painful, inflexible, mindtrapping details like “so when I say I take a onion from the fridge, I mean a RootVegetable of the Allum variety, don’t worry, I’m not storing any parody newspapers in there” and “oh no I also need to store peanut butter in that same fridge and peanut butter is not a RootVegetable so now I’m down at the zoo with a razor and a template language trying to extend my fridge to also take peanut butter 💔

We can be like “take onion from fridge and put it in frying pan great ok thanks” while in an annotated language I’d need to create onion, fridge, frying pan as types and and implement versions of taking and putting specific to those types😭

Rather than experienced chefs, I meant people programming primarily for themselves and their friends. Duck typing is great for that situation because there’s no customer.

I meant a language like Lisp where you can just easily (fry (fridge 'onion)) and as long as the object the fridge returns on an ‘onion request message can work with whatever the fry is doing, you’re golden.

For example, this is a complete running brev program:

(define fridge (ctq onion alliumlicious peanut-butter crunchy))
(define (fry trait) (print "Mmm! Frying the " trait " ingredient!"))

(fry (fridge 'onion))
(fry (fridge 'peanut-butter))

Mmm! Frying the alliumlicious ingredient! Mmm! Frying the crunchy ingredient!

It’s not only the brevity that’s good. It’s the ease of extensibility, how I can fry lists and numbers and vectors and how I can do other things to stuff in the fridge:

(fry #(very curious))

(fridge 'dog 'friendly)

(define (admire quality) (print "You look " quality " today!"))

(admire (fridge 'dog))

You look friendly today!