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

What's an example of a language that, when adding two strings/chars, parses them as integers first?



In perl you use + for addition and . for concatenation. So '3' + '4' converts the strings to integers and returns an integer, and 3 . 4 converts them to strings first and returns a string.


In addition to the answer of Perl and TCL, PHP would too, since the concatenate operator in PHP is "." - any dynamic and/or weak typing language that doesn't overload "+" for string concatenation will probably behave the same way.


How about TCL?

    % expr "3" + "4"
    7
    % expr "3" + 4
    7
    % expr 3 + 4
    7


Perl.

Altough its adition only apply to numbers, so that would be a bad example, it does automaticaly interpret digit strings as numbers.


> What's an example of a language that, when adding two strings/chars, parses them as integers first?

The cancerous piece of shit we know as Javascript.


No, Javascript will concatenate two strings that contain integers if you add them.

"1"+"1" //=> "11"


As 'croddin pointed out, this is flat-out wrong - and that attitude is hardly conducive to a constructive discussion.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

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

Search: