š¾ Archived View for idiomdrottning.org āŗ duck-typing captured on 2022-07-16 at 13:52:10. Gemini links have been rewritten to link to archived content
ā”ļø Next capture (2023-01-29)
-=-=-=-=-=-=-
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!