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

> As a matter of fact, if/switch statements are _also_ polymorphic, often used in more procedural code.

Polymorphism has a straightforward meaning, that the same code is operating on different data types. Switch statements (if's are just a special case) are running different code on different types, so they're monomorphic.

> The more functional way to match on different type constructors is a fine example of a polymorphic function.

I think I see where you're coming from. Here's an example of what you're talking about.

    data Foo = Foo Int | Bar String | Qux

    what :: Foo -> Foo
    what (Foo num) = Bar $ show num
    what (Bar str) = Qux
    what Qux = Foo 0
That's not polymorphic; there's a single type there, and any given code is strictly acting on a single type of data. It's just a nice way of writing a case.

And, in fact, you find out it's not magic because it gets just as messy and confusing once you have to nest logic as with imperative programming, for example, if Foo contained another ADT, I'd often have to use a case statement or equivalent to manage it.



Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

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

Search: