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

Automatic type casting remains popular because it reduces the syntactic and cognitive overhead. I'm suggesting that a better solution would be to retain explicit casting, but minimise the overheads.

So, to take things a step further, lets use, say, the $ operator for casting. Then we have:

    >>> x = 1
    >>> y = "2"
    >>> print($x + y)
    3
    >>> print(x + $y)
    "12"
    >>> print($x + $y)
    CastError: ...
What do you think?


Python example with str and int are IMHO much better. They require only little more typing, but are very readable and explicit.


The proper way to do this is using something like TryParse() and ToString() and to handle the case of failing to parse the string as a number and maybe even specifying the locale for both operations. If you are absolutely sure that parsing can not fail you can use Parse() and omit the error handling.




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

Search: