Hacker News new | past | comments | ask | show | jobs | submit login

Out of curiosity, how would you define my example stringify function in Haskell with "no cost" safety around the argument type?



The typeclass that provides "toString" in haskell is "Show". It defines a few functions, of which the important one is "show", which takes the original type and returns a string. A function that is equivalent to the java-ish example is:

    stringify s = show s
No type definition is necessary, because the compiler can correctly infer the correct, most generic type:

   stringify :: (Show s) => s -> String
Or, for any s in type class Show, a function that makes s into a String. Note that dispatch is done statically.

Modern type systems eliminate most of the cost of type safety through good generics and type inference. Mainstream statically typed languages are just 20-30 years behind the state of the art.

(note that the example actually can be reduced to: stringify = show)




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

Search: