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

The question posed by colanderman's expression is valid if and only if there is a type containing both 5 and "foo".

Here's Haskell's answer:

    Prelude> \ a b -> a == b && b == 5 && a == "foo"

        No instance for (Num [Char]) arising from the literal `5'
        Possible fix: add an instance declaration for (Num [Char])
        In the second argument of `(==)', namely `5'
        In the first argument of `(&&)', namely `b == 5'
        In the second argument of `(&&)', namely `b == 5 && a == "foo"'
Here, the typechecker took the most general type of 5, namely some type belonging to the Num typeclass, and discovered that the type of "foo", which is [Char], isn't an instance of the Num typeclass, or, in Java parlance, doesn't implement the Num interface. This means there is no type containing both 5 and "foo", hence — type error.

You're asking about implicit conversions, which are an undisputable evil. However, attempting to provide answers to invalid questions also isn't very good.

You may have already seen the rather entertaining Wat talk, by Gary Bernhardt: https://www.destroyallsoftware.com/talks/wat

The central theme of this talk is not just implicit conversions — it's surprising answers to invalid questions. A much better way to handle an invalid question is to reject it as early as possible — Fail Fast. With a static type system, this can be very early indeed.

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