Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

While duck typing solves this problem on paper, how is a user to find out whether square really accepts a matrix?

From comments? They are again limited by the authors imagination. From reading the source?



> how is a user to find out whether square really accepts a matrix?

How do you find out that a function exists, what it accepts, and what it does normally?

> From comments? They are again limited by the authors imagination.

Sure, but an author which much experience with duck typing will typically write documentation that specified that expected methods (informal structural “types”) rather than nominal types.

With the static equivalent (protocols), it’s conceivable that this is also discoverable via tooling.


Using maths to either oppose or support generic duck typing is just pointless, because it's easy to come up with counterexamples where a mathematical operation would or would not behave as expected.

In quite a few cases where one would overload a mathematical operation to, say, sum two objects of certain type, it'd be better to explicitly type out the operation as a functor/lambda.


Yes, read the comments or the source code. Or the documentation. Or `help(square)`. Or just try `square(Matrix([1, 2], [3, 4]))` in the REPL.

How would you use type annotations to show that `square` accepts a matrix (as well as an int, float, rational number etc.)?


> How would you use type annotations to show that `square` accepts a matrix (as well as an int, float, rational number etc.)?

Semi-seriously: by specifying the input type is a Monoid.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: