> I would expect many languages to do something similar.Of the common dynamic languages I can think of, the only ones that do something similar are Perl/Perl6 and PHP, and even they are still saner than JS.
$ $((1+\2))
bash: 1+\2: syntax error: operand expected (error token is "\2")
$ $((1-\2))
bash: 1-\2: syntax error: operand expected (error token is "\2")
$ ruby -e"1+'2'"
-e:1:in `+': String can't be coerced into Integer (TypeError)
from -e:1:in `'
$ ruby -e"1-'2'"
-e:1:in `-': String can't be coerced into Integer (TypeError)
from -e:1:in `'
$ python3 -c"1+'2'"
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for +: 'int' and 'str'
$ python3 -c"1-'2'"
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for -: 'int' and 'str'
>>> exit()
$ python2 -c"1+'2'"
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for +: 'int' and 'str'
$ python2 -c"1-'2'"
Traceback (most recent call last):
File "", line 1, in
TypeError: unsupported operand type(s) for -: 'int' and 'str'
$ perl -e'print 1+"2"."\n"'
3
$ perl -e'print 1-"2"."\n"'
-1
$ perl6 -e"say 1+'2'"
3
$ perl6 -e"say 1-'2'"
-1
$ php -r'echo 1+"2","\n";'
3
$ php -r'echo 1-"2","\n";'
-1
$ racket -e '(+ 1 "2")'
+: contract violation
expected: number?
given: "2"
argument position: 2nd
other arguments...:
1
$ racket -e '(- 1 "2")'
-: contract violation
expected: number?
given: "2"
argument position: 2nd
other arguments...:
1
$ sbcl
This is SBCL 1.3.18, an implementation of ANSI Common Lisp.
More information about SBCL is available at .
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
* (+ 1 "2")
debugger invoked on a TYPE-ERROR in thread
#:
The value
"2"
is not of type
NUMBER
when binding SB-KERNEL::Y
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [ABORT] Exit debugger, returning to top level.
(SB-KERNEL:TWO-ARG-+ 1 "2") [external]
0] 0
* (- 1 "2")
debugger invoked on a TYPE-ERROR in thread
#:
The value
"2"
is not of type
NUMBER
when binding SB-KERNEL::Y
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [ABORT] Exit debugger, returning to top level.
(SB-KERNEL:TWO-ARG-- 1 "2") [external]
0] 0
* (exit)
$ erl
Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V8.3 (abort with ^G)
1> 1+"2".
** exception error: an error occurred when evaluating an arithmetic expression
in operator +/2
called as 1 + "2"
2> 1-"2".
** exception error: an error occurred when evaluating an arithmetic expression
in operator -/2
called as 1 - "2"
3> halt().
$ elixir -e '1+"2"'
** (ArithmeticError) bad argument in arithmetic expression
:erlang.+(1, "2")
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/code.ex:170: Code.eval_string/3
$ elixir -e '1-"2"'
** (ArithmeticError) bad argument in arithmetic expression
:erlang.-(1, "2")
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/code.ex:170: Code.eval_string/3