News for hackers. Stuff that matters.
Re:Why static languages suffer from complexity by Too on Thursday January 20, 2022 @01:31AM (#30005526)
Typescript can do exactly what you ask for, “static duck typing”. Just define the input argument to be a interface with the 2 relevant attributes, any struct having at least these 2 attributes are now allowed, even if there is no explicit inheritance to it. This can be done inline in the function signature so doesn’t contribute to bloat.

Python can do the same using what they call Protocol. Here protocols need to be defined upfront.

This is usually called Structural typing, as opposed to nominal typing where classes inherit from a base.

There are no comments on this comment yet.