Live data from Hacker News

Is Go Duck-Typed?

bionic.fullstory.com

1–10 of 89 posts

Re: Is Go Duck-Typed?

#3
Betteridge's law of headlines would say "no" (as would @mbell in this conversation). Curious, the author says "undefined". @mbell FTW!

Re: Is Go Duck-Typed?

#5
post #2

This is called Structural Typing[0] and is in contrast to Nominal Typing[1] (e.g. Java). 0: https://en.wikipedia.org/wiki/Structural_type_system 1: https://en.wikipedia.org/wiki/Nominal_type_system

Other notable examples include the OCaml object system which implements subclasses using row types (structural subtyping), meaning that subclasses are not technically subtypes, and TypeScript interfaces, which are also structurally typed.

Re: Is Go Duck-Typed?

#7
I like the histogram showing how much more frequent interfaces with just one or two methods are.

How frequent are problems where classes unintentionally use an interface due to identical signatures? Seems likely to happen in theory if so many interfaces have few functions.

Re: Is Go Duck-Typed?

#8
post #2

This is called Structural Typing[0] and is in contrast to Nominal Typing[1] (e.g. Java). 0: https://en.wikipedia.org/wiki/Structural_type_system 1: https://en.wikipedia.org/wiki/Nominal_type_system

I just recently found out about these two types of system. It's strange how people (like shown in the article) don't emphasize(know) it when talking about types in languages.

Interestingly, python has included structural subtyping in 3.8[1] as part of the typing module.

[1] https://www.python.org/dev/peps/pep-0544/

Re: Is Go Duck-Typed?

#9
post #7

I like the histogram showing how much more frequent interfaces with just one or two methods are. How frequent are problems where classes unintentionally use an interface due to identical signatures? Seems likely to happen in theory if so many interfaces have few functions.

In theory, yeah... in practice, as someone who uses Go and Python with mypy (which supports structural type-checking using the Protocol interface) professionally, I haven't seen it happen. It's easy to think of contrived examples like the one in the post, where Cat/Dog/Table all implement GetLegCount(), but in practice your interface methods usually aren't that simple, and interface signatures like

  func InsertFoo(*sqlx.DB, Foo) (int64, error)
are very unlikely to be unintentionally implemented.
Post reply on HN