šŸ’¾ 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

View Raw

More Information

āž”ļø Next capture (2023-01-29)

-=-=-=-=-=-=-

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!