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

Imagine you have two functions that you only call with lists of integers:

    def summarize1[A](list: List[A]) = ...
    def summarize2(list: List[Int]) = ...
Even though you don't call summarize1 polymorphically, the signature tells you that it's only looking at the structure of the list rather than what's inside it - a reader can immediately see that it's going to do something like return the length of the list. Whereas summarize2 could do other things, e.g. sum the elements of the list, add and subtract alternating elements...

A list is a pretty simple structure, these kinds of guarantees become more useful when the structure itself is complicated. E.g. in the project I'm currently working on I have a tree-like datatype that only ever contains Strings, but I've written it generically so I can be confident that my structural functions only work on the structure, and I have a clear separation between functions that operate on the structure of the tree and functions that operate on the content of the tree.



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

Search: