News for hackers. Stuff that matters.
Re:Is Go Duck-Typed? by twistedpairs on Thursday March 05, 2020 @07:30AM (#22492979)
Is it just me or is the wikipedia article for duck typing really bad?

The example they have is not illustrative or explanatory at all.

  class Duck:

      def fly(self):  

          print("Duck flying")  



  class Sparrow:

      def fly(self):  

          print("Sparrow flying")  


  class Whale:

      def swim(self):  

          print("Whale swimming")  


  for animal in Duck(), Sparrow(), Whale():

      animal.fly()

output:

  Duck flying  

  Sparrow flying  

  AttributeError: 'Whale' object has no attribute 'fly'  

This moreso shows a dynamic typing issue.
Re:Is Go Duck-Typed? by jolux on Thursday March 05, 2020 @09:43AM (#22493865)
Duck typing really only makes sense in the context of dynamic typing.
Re:Is Go Duck-Typed? by twistedpairs on Thursday March 05, 2020 @09:54AM (#22493956)
That's a given. That comment doesn't further the conversation in any way though?
Re:Is Go Duck-Typed? by jolux on Thursday March 05, 2020 @10:16AM (#22494177)
You said it was a dynamic typing issue, not a duck typing one, when in fact the two are inseparable in the example you gave. That is absolutely an example of duck typing, you’re calling the same method on three different types with no subtyping relationship with a single invocation.