Duck Typing?

classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|

Duck Typing?

Gavin Wraith
I hear the phrase "Duck Typing", in what I guess may be a
dismissive description of Lua's type system and possibly
a mysterious reference to "Duct Tape". But you can always
try to add typing to your own code, even if you cannot
enforce it in other's. For abstract datatypes with
constructors, I suggest

tag = function (t) return function (v)
      return { tag = t, value = v } end end

switch = function (case) return function (obj)
       return case[obj.tag] (obj.value) end end

and dually for structs with destructors, e.g.
for lazy streams

get = \ (stream)
      local v = stream.value
      stream.value = stream.update (v)
      return v, stream
      end

I would be interested to see examples of how to fake
types, type classes, and other such concepts within
Lua. And meanwhile, what about the etymological origins
of "Duck Typing"?
--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Francisco Olarte
Gavin:

On Tue, Jul 31, 2018 at 11:48 AM, Gavin Wraith <[hidden email]> wrote:
> I hear the phrase "Duck Typing", in what I guess may be a
> dismissive description of Lua's type system and possibly
> a mysterious reference to "Duct Tape".

It's not dismissive, and does not come from Duct Tape. It comes from
"if it walks like a duck, swims like a duck and quacks like a duck it
then must be a duck". I'm pretty suer wikipedia and the likes have
articles on it if you want to read more.

It describe a form of "typing" objects used in many languages which
have typing system similar to lua, where objects have a kind of
dictionary where you can add values/functions in many way, and where
functions/methods do not check "object inheritance", they just call (
or use or check ) the passed object has the methods/properties it
needs, and does not bother how it adquired them.

Francisco Olarte.

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Scott Morgan
In reply to this post by Gavin Wraith
On 31/07/18 10:48, Gavin Wraith wrote:
> I hear the phrase "Duck Typing", in what I guess may be a
> dismissive description of Lua's type system and possibly
> a mysterious reference to "Duct Tape".

https://en.wikipedia.org/wiki/Duck_typing

I'm looking after some ducklings this weekend, I'll ask them what their
preferred type system is.

Scott

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Gavin Wraith
In message <[hidden email]>
          Scott Morgan <[hidden email]> wrote:

>On 31/07/18 10:48, Gavin Wraith wrote:
>> I hear the phrase "Duck Typing", in what I guess may be a
>> dismissive description of Lua's type system and possibly
>> a mysterious reference to "Duct Tape".
>
>https://en.wikipedia.org/wiki/Duck_typing

Many thanks. It shows my age that I do not resort straightaway
to Wikipedia. "Duct tape" sounds like a Chelsea pronunciation
of "Duck type", if you are into UK accents.

--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Petite Abeille


> On Jul 31, 2018, at 1:32 PM, Gavin Wraith <[hidden email]> wrote:
>
> "Duct tape" sounds like a Chelsea pronunciation of "Duck type", if you are into UK accents.

http://lua-users.org/lists/lua-l/2008-09/msg00550.html

~10 years to the day :D



Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Gavin Wraith
In message <[hidden email]>
          Petite Abeille <[hidden email]> wrote:


>> On Jul 31, 2018, at 1:32 PM, Gavin Wraith <[hidden email]> wrote:
>>
>> "Duct tape" sounds like a Chelsea pronunciation of "Duck type", if you are into UK accents.
>
>http://lua-users.org/lists/lua-l/2008-09/msg00550.html
>
>~10 years to the day :D

Good lord, that is a shock! Loss of short term memory. I really had
forgotten. Thank you for your patience.
--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Sean Conner
In reply to this post by Gavin Wraith
It was thus said that the Great Gavin Wraith once stated:
>
> I would be interested to see examples of how to fake
> types, type classes, and other such concepts within
> Lua.

  The snippit of code I presented in another thread [1] is a form of duck
typing.  I'm working on a socket framework (to handle both TCP and TLS
connections via Lua coroutines) and I'm mimicking the Lua file operations.
Yes, I could create a FILE object from a TCP socket and use the normal Lua
functions, but that won't work for TLS.  So my own versions of read(),
write(), close(), etc).

  -spc

[1] http://lua-users.org/lists/lua-l/2018-07/msg00679.html

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Rena
In reply to this post by Gavin Wraith
On Tue, Jul 31, 2018, 07:33 Gavin Wraith, <[hidden email]> wrote:
In message <[hidden email]>
          Scott Morgan <[hidden email]> wrote:

>On 31/07/18 10:48, Gavin Wraith wrote:
>> I hear the phrase "Duck Typing", in what I guess may be a
>> dismissive description of Lua's type system and possibly
>> a mysterious reference to "Duct Tape".
>
>https://en.wikipedia.org/wiki/Duck_typing

Many thanks. It shows my age that I do not resort straightaway
to Wikipedia. "Duct tape" sounds like a Chelsea pronunciation
of "Duck type", if you are into UK accents.

--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

To try to explain it simply (assuming I understand it correctly): you don't check that an object is of a given type, just that it can do what you want. You tell it to go over there, and you don't care if it's a duck and flies there, or a cat and walks there. The idea being that you don't need to know about all the different types of objects there are; you just assume they have a "go_there" method, and if not, either you do something else or you complain that you can't use this object. "If it quacks like a duck, that's good enough for me."

For a real life example: you're given a data source and you use its `read` method to get data. It doesn't matter if that source is a file, a socket, a function, etc; all that matters is that you can call its `read` method and get some data back. If you explicitly check that it's a file, then I can't use a socket instead, even if it would have worked just fine.

In more strictly typed languages, this would be done by accepting any object that inherits from some base class or implements some interface, which guarantees it has a `read` method. In a dynamically typed language like Lua, you don't get the advantage of type safety (you might receive an incompatible object), but the code can be much simpler (you don't have to import class definitions and try to fit your object into them).

The one major gotcha is if you're given an object that seems compatible but isn't; eg the object has a `read` method, but it doesn't take the same parameters as you expect, or does something different (maybe tells you "yes, this message has been read"). This isn't very common but is something to watch out for.

(Aside: you can certainly implement a class/interface system using metatables, but you'll still have to check at runtime that you've received the right kind of object. It helps prevent the above mentioned problem, but does add complexity.)
Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

William Ahern
On Tue, Jul 31, 2018 at 12:17:08PM -0400, Rena wrote:
<snip>

> To try to explain it simply (assuming I understand it correctly): you don't
> check that an object is of a given type, just that it can do what you want.
> You tell it to go over there, and you don't care if it's a duck and flies
> there, or a cat and walks there. The idea being that you don't need to know
> about all the different types of objects there are; you just assume they
> have a "go_there" method, and if not, either you do something else or you
> complain that you can't use this object. "If it quacks like a duck, that's
> good enough for me."
>
> For a real life example: you're given a data source and you use its `read`
> method to get data. It doesn't matter if that source is a file, a socket, a
> function, etc; all that matters is that you can call its `read` method and
> get some data back. If you explicitly check that it's a file, then I can't
> use a socket instead, even if it would have worked just fine.
>
> In more strictly typed languages, this would be done by accepting any
> object that inherits from some base class or implements some interface,
> which guarantees it has a `read` method. In a dynamically typed language
> like Lua, you don't get the advantage of type safety (you might receive an
> incompatible object), but the code can be much simpler (you don't have to
> import class definitions and try to fit your object into them).

The cousin to duck typing in statically typed languages is structural
typing. In those systems you need neither a shared class nor a shared
interface, if by interface you mean an abstract type declaration on the
referent object. (The proximity with "base class" made it seem--noscitur a
sociis--as if you had Java-like OOP interfaces in mind, rather than the
looser meaning in duck and structural typing.)

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Lorenzo Donati-3
In reply to this post by Gavin Wraith
On 31/07/2018 16:53, Gavin Wraith wrote:

> In message <[hidden email]>
>           Petite Abeille <[hidden email]> wrote:
>
>
>>> On Jul 31, 2018, at 1:32 PM, Gavin Wraith <[hidden email]> wrote:
>>>
>>> "Duct tape" sounds like a Chelsea pronunciation of "Duck type", if you are into UK accents.
>>
>> http://lua-users.org/lists/lua-l/2008-09/msg00550.html
>>
>> ~10 years to the day :D
>
> Good lord, that is a shock! Loss of short term memory. I really had
> forgotten. Thank you for your patience.

You have all my sympathy!

During the years software developers have developed (pun intented!)
quite a remarkable and peculiar sense of humor when giving name to
something, like a new concept or technique.

Sometimes /it is/ somewhat difficult to grasp or remember for people
coming from other scientific/engineering fields.

Although I've been programming since I was 13 (I'm 48 now) and I did it
professionally for a part of my life, I still (albeit rarely) forget
which is which in the SW dev zoo.

Some funny expressions off the top of my head:

bug [1]
shell (as command line interpreter/window)
duck typing
monkey patching
yoda conditions (if you don't know Star Wars saga you're lost!)
easter egg
eat your own dog food
to grok (an idea/concept)
spaghetti code
heisenbug
rubber ducking
to boot (a system)


[1] It's so widespread now that probably it doesn't sound funny anymore!
But it seems it's origin may be this:
http://boussejra.com/2016/11/18/military-terms-in-computer-science.html

Some related (and fun) links:

https://www.businessinsider.com/20-hilarious-programming-jargon-phrases-you-should-know-when-talking-to-engineers-2012-7?IR=T

https://www.buzzfeed.com/lukelewis/28-things-only-developers-will-find-funny

I can't remember exactly, but I once read something on these lines:
"Computer science and programming are a very young disciplines, so
developers are like kids in a kindergarten having fun naming things in
funny ways" :-D

BTW, not exactly related to terminology, but I stumbled on it while
googling for the other links and it's fun:

http://pmstories.com/2009/07/08/funny-quotes-computers-programming/


> --
> Gavin Wraith ([hidden email])
> Home page: http://www.wra1th.plus.com/
>
>


Cheers!

-- Lorenzo

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Lorenzo Donati-3
In reply to this post by Gavin Wraith
On 31/07/2018 16:53, Gavin Wraith wrote:

> In message <[hidden email]>
>           Petite Abeille <[hidden email]> wrote:
>
>
>>> On Jul 31, 2018, at 1:32 PM, Gavin Wraith <[hidden email]> wrote:
>>>
>>> "Duct tape" sounds like a Chelsea pronunciation of "Duck type", if you are into UK accents.
>>
>> http://lua-users.org/lists/lua-l/2008-09/msg00550.html
>>
>> ~10 years to the day :D
>
> Good lord, that is a shock! Loss of short term memory. I really had
> forgotten. Thank you for your patience.

You have all my sympathy!

During the years software developers have developed (pun intended!)
quite a remarkable and peculiar sense of humor when giving name to
something, like a new concept or technique.

Sometimes /it is/ somewhat difficult to grasp or remember for people
coming from other scientific/engineering fields.

Although I've been programming since I was 13 (I'm 48 now) and I did it
professionally for a part of my life, I still (albeit rarely) forget
which is which in the SW dev zoo.

Some funny expressions off the top of my head:

bug [1]
shell (as command line interpreter/window)
duck typing
monkey patching
yoda conditions (if you don't know Star Wars saga you're lost!)
easter egg
eat your own dog food
to grok (an idea/concept)
spaghetti code
heisenbug
rubber ducking
to boot (a system)


[1] It's so widespread now that probably it doesn't sound funny anymore!
But it seems it's origin may be this:
http://boussejra.com/2016/11/18/military-terms-in-computer-science.html

Some related (and fun) links:

https://www.businessinsider.com/20-hilarious-programming-jargon-phrases-you-should-know-when-talking-to-engineers-2012-7?IR=T

https://www.buzzfeed.com/lukelewis/28-things-only-developers-will-find-funny

I can't remember exactly, but I once read something on these lines:
"Computer science and programming are a very young disciplines, so
developers are like kids in a kindergarten having fun naming things in
funny ways" :-D

BTW, not exactly related to terminology, but I stumbled on it while
googling for the other links and it's fun:

http://pmstories.com/2009/07/08/funny-quotes-computers-programming/


> --
> Gavin Wraith ([hidden email])
> Home page: http://www.wra1th.plus.com/
>
>


Cheers!

-- Lorenzo

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Gavin Wraith
In reply to this post by Lorenzo Donati-3
In message <[hidden email]>
          Lorenzo Donati <[hidden email]> wrote:

>During the years software developers have developed (pun intented!)
>quite a remarkable and peculiar sense of humor when giving name to
>something, like a new concept or technique.
>
>Sometimes /it is/ somewhat difficult to grasp or remember for people
>coming from other scientific/engineering fields.
>
>Although I've been programming since I was 13 (I'm 48 now) and I did it
>professionally for a part of my life, I still (albeit rarely) forget
>which is which in the SW dev zoo.
>
>Some funny expressions off the top of my head:
>
>bug [1]
>shell (as command line interpreter/window)
>duck typing
>monkey patching
>yoda conditions (if you don't know Star Wars saga you're lost!)
>easter egg
>eat your own dog food
>to grok (an idea/concept)
>spaghetti code
>heisenbug
>rubber ducking
>to boot (a system)
>
>
>[1] It's so widespread now that probably it doesn't sound funny anymore!
>But it seems it's origin may be this:
>http://boussejra.com/2016/11/18/military-terms-in-computer-science.html
>
>Some related (and fun) links:
>
>https://www.businessinsider.com/20-hilarious-programming-jargon-phrases-you-should-know-when-talking-to-engineers-2012-7?IR=T
>
>https://www.buzzfeed.com/lukelewis/28-things-only-developers-will-find-funny
>
>I can't remember exactly, but I once read something on these lines:
>"Computer science and programming are a very young disciplines, so
>developers are like kids in a kindergarten having fun naming things in
>funny ways" :-D
>
>BTW, not exactly related to terminology, but I stumbled on it while
>googling for the other links and it's fun:
>
>http://pmstories.com/2009/07/08/funny-quotes-computers-programming/

Many thanks for these, Lorenzo. I have a disreputable weakness for words.

--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Sean Conner
In reply to this post by Lorenzo Donati-3
It was thus said that the Great Lorenzo Donati once stated:

>
> Some funny expressions off the top of my head:
>
> bug [1]
> shell (as command line interpreter/window)
> duck typing
> monkey patching
> yoda conditions (if you don't know Star Wars saga you're lost!)
> easter egg
> eat your own dog food
> to grok (an idea/concept)
> spaghetti code
> heisenbug
> rubber ducking
> to boot (a system)

  I've also heard of

ravioli code
16" rotary debugger [2]
carboard programmer
mung (or munge)

  -spc

> [1] It's so widespread now that probably it doesn't sound funny anymore!
> But it seems it's origin may be this:
> http://boussejra.com/2016/11/18/military-terms-in-computer-science.html

[2] In offical documentation no less!


Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Doug Currie
On Sun, Aug 5, 2018 at 6:04 PM Sean Conner <[hidden email]> wrote:
[...]
mung (or munge)

Ah, the TMRC is the source of so much jargon. It was a great privilege for me to experience that culture in the 1970s, long before Building 20 was dismantled. 


e
 
Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Dirk Laurie-2
In reply to this post by Sean Conner
2018-08-06 0:03 GMT+02:00 Sean Conner <[hidden email]>:
>   I've also heard of
>
> ravioli code
> 16" rotary debugger [2]
> carboard programmer
> mung (or munge)

While bringing myself to hep level on the second of these, I came
across the quote:

The most effective debugging tool is still careful thought, coupled
with judiciously placed print statements.

Brian Kernighan

"Unix for Beginners" (1979)

I think that Roberto said something very similar on this list, on a
thread dealing with Lua debugging tools, but can't find it.

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Javier Guerra Giraldez
On 6 August 2018 at 08:52, Dirk Laurie <[hidden email]> wrote:
> The most effective debugging tool is still careful thought, coupled
> with judiciously placed print statements.
>
> Brian Kernighan
>
> "Unix for Beginners" (1979)
>
> I think that Roberto said something very similar on this list, on a
> thread dealing with Lua debugging tools, but can't find it.

I remember on a round table, somebody from the audience asked him what
was his favorite debugger, the answer was ".... well.... print?"


--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Enrico Colombini
In reply to this post by Dirk Laurie-2
On 06-Aug-18 09:52, Dirk Laurie wrote:
> While bringing myself to hep level on the second of these, I came
> across the quote:
>
> The most effective debugging tool is still careful thought, coupled
> with judiciously placed print statements.

Having used a fair number of languages, I couldn't agree more. In fact,
that quote has been the subtitle my 'software' webpage for ages :-)

http://www.erix.it/about_erix/sw_design.html
(now a rather old page, I keep it for its archaeological value)

--
   Enrico

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Coda Highland
In reply to this post by Javier Guerra Giraldez
On Mon, Aug 6, 2018 at 3:09 AM, Javier Guerra Giraldez
<[hidden email]> wrote:

> On 6 August 2018 at 08:52, Dirk Laurie <[hidden email]> wrote:
>> The most effective debugging tool is still careful thought, coupled
>> with judiciously placed print statements.
>>
>> Brian Kernighan
>>
>> "Unix for Beginners" (1979)
>>
>> I think that Roberto said something very similar on this list, on a
>> thread dealing with Lua debugging tools, but can't find it.
>
> I remember on a round table, somebody from the audience asked him what
> was his favorite debugger, the answer was ".... well.... print?"
>

My boss was baffled why I didn't use a Javascript debugger instead of
log statements.

The way I see it... if it's not broke...?

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Rena
In reply to this post by Lorenzo Donati-3
On Sun, Aug 5, 2018 at 6:54 AM Lorenzo Donati
<[hidden email]> wrote:

>
> On 31/07/2018 16:53, Gavin Wraith wrote:
> > In message <[hidden email]>
> >           Petite Abeille <[hidden email]> wrote:
> >
> >
> >>> On Jul 31, 2018, at 1:32 PM, Gavin Wraith <[hidden email]> wrote:
> >>>
> >>> "Duct tape" sounds like a Chelsea pronunciation of "Duck type", if you are into UK accents.
> >>
> >> http://lua-users.org/lists/lua-l/2008-09/msg00550.html
> >>
> >> ~10 years to the day :D
> >
> > Good lord, that is a shock! Loss of short term memory. I really had
> > forgotten. Thank you for your patience.
>
> You have all my sympathy!
>
> During the years software developers have developed (pun intented!)
> quite a remarkable and peculiar sense of humor when giving name to
> something, like a new concept or technique.
>
> Sometimes /it is/ somewhat difficult to grasp or remember for people
> coming from other scientific/engineering fields.
>
> Although I've been programming since I was 13 (I'm 48 now) and I did it
> professionally for a part of my life, I still (albeit rarely) forget
> which is which in the SW dev zoo.
>
> Some funny expressions off the top of my head:
>
> bug [1]
> shell (as command line interpreter/window)
> duck typing
> monkey patching
> yoda conditions (if you don't know Star Wars saga you're lost!)
> easter egg
> eat your own dog food
> to grok (an idea/concept)
> spaghetti code
> heisenbug
> rubber ducking
> to boot (a system)
>
>
> [1] It's so widespread now that probably it doesn't sound funny anymore!
> But it seems it's origin may be this:
> http://boussejra.com/2016/11/18/military-terms-in-computer-science.html
>
> Some related (and fun) links:
>
> https://www.businessinsider.com/20-hilarious-programming-jargon-phrases-you-should-know-when-talking-to-engineers-2012-7?IR=T
>
> https://www.buzzfeed.com/lukelewis/28-things-only-developers-will-find-funny
>
> I can't remember exactly, but I once read something on these lines:
> "Computer science and programming are a very young disciplines, so
> developers are like kids in a kindergarten having fun naming things in
> funny ways" :-D
>
> BTW, not exactly related to terminology, but I stumbled on it while
> googling for the other links and it's fun:
>
> http://pmstories.com/2009/07/08/funny-quotes-computers-programming/
>
>
> > --
> > Gavin Wraith ([hidden email])
> > Home page: http://www.wra1th.plus.com/
> >
> >
>
>
> Cheers!
>
> -- Lorenzo
>

I've always found it a little amusing that a computer can contain many
different kinds of bugs at once:

- Bugs in the computer sense (software or even hardware defects)
- Insects
- Germs
- Spying devices
- Bugs Bunny?

English is such an interesting language.

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: Duck Typing?

Coda Highland
On Mon, Aug 6, 2018 at 3:24 PM, Rena <[hidden email]> wrote:

> I've always found it a little amusing that a computer can contain many
> different kinds of bugs at once:
>
> - Bugs in the computer sense (software or even hardware defects)
> - Insects
> - Germs
> - Spying devices
> - Bugs Bunny?
>
> English is such an interesting language.

So... a software error in the Boomerang app (classic cartoons service)
when it tries to phone home from an iPad that a toddler used as a
flyswatter?

/s/ Adam