ちょまどさんが、C#では 1 + "2" がエラーにならず、文字列 "12" が返るとポストしておられたのです。ふーん。では他の言語ではどうなるか気になるじゃないですか。というわけでレッツチャレンジ。
awk
Cygwin版GNU Awk 5.3.0
$ echo | awk '{a = 1 + "2"; print a}'
3
C
Cの場合、文字列はchar*型なので、そこに1を足すと2文字目へのポインタになります。
以下Cygwin版GCC 11.4.0
#include <stdio.h>
int main()
{
printf("%s", 1 + "12345");
}
結果。
2345
C++
C++も、Cと同様の現象が起こります。
以下Cygwin版GCC 11.4.0
#include <iostream>
int main() {
std::cout << (1 + "12345");
}
結果。
2345
Java
Debian GNU/Linux 12 (bookworm) 版OpenJDK 17.0.9
class Test {
public static void main(String[] args)
{
System.out.println((1 + "2").getClass());
System.out.println(1 + "2");
}
}
結果。
class java.lang.String
12
JavaScript
Windows版Edge 120.0.2210.61、Chrome 120.0.6099.71、Firefox 119.0.1、Android版Chrome 120.0.6099.43、iPad mini版Safariで試しましたが結果は同じでした。
<!DOCTYPE html>
<html lang="ja">
<body>
<div id="type"></div>
<div id="value"></div>
<script>
var i = 1 + "2";
document.getElementById("type").innerHTML = typeof i;
document.getElementById("value").innerHTML = i;
</script>
</body>
</html>
結果。
string
12
JScript
Windows 10版Windows Scripting Host 5.812
var i = 1 + "2";
WScript.echo(typeof i);
WScript.echo(i);
結果。
string
12
WSHでもVBScriptと結果が違うので注意。
Lisp
Windows版Emacs 26.3のEmacs Lisp
(print (+ 1 "2"))
エラー。
Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p "2")
+(1 "2")
(print (+ 1 "2"))
eval((print (+ 1 "2")) nil)
elisp--eval-last-sexp(t)
eval-last-sexp(t)
eval-print-last-sexp(nil)
funcall-interactively(eval-print-last-sexp nil)
call-interactively(eval-print-last-sexp nil nil)
command-execute(eval-print-last-sexp)
Perl
Cygwin版Perl 5.36.3
print 1 + "2";
結果。
3
PHP
Cygwin版PHP 7.3.7
<?php
$i = 1 + "2";
echo gettype($i);
echo "\n";
echo $i;
結果。
integer
3
Python
Cygwin版Python 3.9.16
i = 1 + "2"
print(type(i))
print(i)
エラー。
Traceback (most recent call last):
File "/cygdrive/c/Users/n-i-e/Documents/work/1plus2-test/test1.py", line 1, in <module>
i = 1 + "2"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
R
Debian GNU/Linux 12 (bookworm) 版R 4.2.2
エラー。
> print(1 + "2")
1 + "2" でエラー: 二項演算子の引数が数値ではありません
Ruby
Cygwin版Ruby 3.2.2
puts 1+"2"
エラー。
test1.rb:1:in `+': String can't be coerced into Integer (TypeError)
puts 1+"2"
^^^
from test1.rb:1:in `<main>'
sh
Cygwin版GNU bash, version 5.2.15(3)
#!/bin/bash
declare -i a
declare -l b
a=1
b="2"
echo $((a + b))
結果。
3
SQL
Access
Office 2016
Set adoxcat = CreateObject("ADOX.Catalog")
connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""test1.mdb"";Mode=ReadWrite"
Set dbo = CreateObject("ADODB.Connection")
adoxcat.Create connstr
dbo.Open connstr
dbo.Execute "CREATE TABLE test ([int] INTEGER PRIMARY KEY, [str] TEXT)"
dbo.Execute "INSERT INTO test VALUES (1, ""2"")"
Set rs = dbo.Execute("SELECT TypeName(1+""2"") AS [type], 1+""2"" AS [value] FROM test")
WScript.Echo rs("type")
WScript.Echo rs("value")
結果。
Double
3
MariaDB
Debian GNU/Linux 12 (bookworm) 版MariaDB 10.11.4
MariaDB [mysql]> set @a:=1+"2";
Query OK, 0 rows affected (0.001 sec)
MariaDB [mysql]> create temporary table test select @a;
Query OK, 1 row affected (0.018 sec)
Records: 1 Duplicates: 0 Warnings: 0
MariaDB [mysql]> desc test;
+-------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| @a | double | YES | | NULL | |
+-------+--------+------+-----+---------+-------+
1 row in set (0.017 sec)
MariaDB [mysql]> select * from test;
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.020 sec)
参考: https://stackoverflow.com/questions/1223211/get-the-type-of-a-variable-in-mysql
Microsoft SQL Server
Windows版SQL Server Developer Edition
select 1 + '2';
結果。
-----------
3
(1 行処理されました)
PostgreSQL
Debian GNU/Linux 12 (bookworm) 版PostgreSQL 15.5
postgres=# select pg_typeof(1+'2'), 1+'2';
pg_typeof | ?column?
-----------+----------
integer | 3
(1 行)
SQLite3
Cygwin版SQLite3 3.34.0
sqlite> SELECT typeof(1+"2"), 1+"2";
integer|3
VB.NET
Windows10付属のVisual Basic Compiler version 14.8.9037
Public Module Test1
Public Sub Main()
Dim a = 1 + "2"
Dim type = TypeName(a)
Console.WriteLine(type)
Console.WriteLine(a)
End Sub
End Module
結果。
Double
3
.NET FrameworkでもC#と結果が違うので注意。
VBA
Excel 2016
Sub Test1()
i = 1 + "2"
MsgBox (TypeName(i))
MsgBox i
End Sub
結果。
Double
3
VBScript
Windows 10版Windows Scripting Host 5.812
i = 1 + "2"
WScript.Echo TypeName(i)
WScript.Echo i
結果。
Double
3
WSHでもJScriptと結果が違うので注意。
以上!幸運を祈る。