LoginSignup
2
1

More than 1 year has passed since last update.

いくつかの言語で、空白類の挙動

Posted at

これは何?

https://qiita.com/universato/items/c1f97e1843e010ed8dd8
という記事を見て、私も遊んでみた。

JavaScript

ちゃんと調べてないけど、Unicode で空白となっている文字は空白だと思う。

なので、唯一の(私調べ)目に見える空白文字、 (U+1680、オガム文字の空白) を半角空白の代わりに使える。

JavaScript
consta=()=>{
    console.log("hello"+", "+"spaces");
};
a(  );

ちなみに手元の環境では「format on save」で全部半角空白にされてしまうので、この機能を OFF にする必要がある。

go

仕様書 を見ると

Tokens form the vocabulary of the Go language. There are four classes: identifiers, keywords, operators and punctuation, and literals. White space, formed from spaces (U+0020), horizontal tabs (U+0009), carriage returns (U+000D), and newlines (U+000A), is ignored except as it separates tokens that would otherwise combine into a single token.

とある。

一方。
トークン(変数名とか)になれるのは、ユニコードで「Letter」になっているものだけ。なので、空白類は変数名の一部になれない。

C#

仕様書 を見ると

whitespace:

     Any character with Unicode class Zs

     Horizontal tab character (U+0009)
     
Vertical tab character (U+000B)
     
Form feed character (U+000C)

となっている。
Unicode class Zs
 とは何かと思うと Space Separator のことで、オガム文字の空白もその一員になっている。
したがって下記のコードは

C#
usingSystem;

public class Hello{
  publicstaticvoidMain(){  
    Console.WriteLine("hello spaces!");
  }
}

普通に動く。

Java

Java の場合、仕様書 をみると。
Unicode が成熟する前に作られているからだとおもうんだけど、Unicode の空白類とは関係なく

White space is defined as the ASCII space character, horizontal tab character, form feed character, and line terminator characters

と、突然 ASCII space caracter が登場する。

一方、変数名などに使える文字としては「 any Unicode character that is a "Java letter" 」ということになっている。その Java letter ってなんだよということだけど、

The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII dollar sign ($, or \u0024) and underscore (_, or \u005f). The dollar sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

とのこと。$ って変数名に使えるんだ知らなかった。

J#

全く調べてないし、今後も調べないと思うけど、 J# は .NET の上で動くので、変数名に漢字とか普通に使えたんじゃないかなぁと想像する。そうじゃないと困りそう。

まったくどうでもいいのだけれど。

zig

zig は仕様書を読んでもよくわからなかった。
試してみると、変数名にも空白にも オガム文字の空白 は使えなかった。

Swift

仕様書 を見ると、

inline-space → U+0009 or U+0020

とあり、オガム文字の空白は空白には使え無さそうだけど

swift
let  漢字変数  =  "hello spaces"
print(  漢字変数  )

は、普通に動く。

Groovy

仕様書 を見ると

'\u0100' to '\uFFFE'

が変数名に使えるということになっているようにみえるので U+1680 も変数名に使えそうだが、wandbox で試すとそうなっていない。

とはいえ、this."foo bar baz" のようにすれば、空白でも変数名などに使える。

思うこと

変な空白はエラーにしてくれたほうが嬉しいなと思う。
Unicode 的に空白だったとしてもオガム文字の空白を意図して使う人は稀だと思うし、禁止しても誰も困らないと思う。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1