在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
C + + 1 xλ的:
[] (int x) { std::cout << x << std::endl; } ();
它们可能被滥用在一些奇怪的语法中:
[](){}();[]{[]{}();}();
这是完全有效的c++ 1x。
其他回答
在Java中从文本文件中读取一行。
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("filename"));
String str;
str = in.readLine();
if (str != null) {
...
}
} catch (IOException e) {
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {}
}
啊。虽然我承认这并不奇怪……只是邪恶。: -)
更短、更习惯的版本:
try {
BufferedReader in = new BufferedReader(new FileReader("filename"));
try {
String str = in.readLine();
while (str != null) {
str = in.readLine();
}
} finally {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
在Matlab中,以下内容可能会让你感到惊讶,特别是如果你习惯了Python:
>> not true
ans =
0 0 0 0
>> not false
ans =
0 0 0 0 0
这里有两个奇怪的特征。第一个是a b被解释为a('b'),因此非真被解释为not('true')。第二个奇怪的特征是没有任何字符返回0(大概是因为在matlab中没有false或true,只有0或1)。
c++最恼人的解析:
struct S
{
S() {} //default constructor
};
int main() {
S s(); // this is not a default construction, it declares a function named s that takes no arguments and returns S.
}
在MATLAB(交互式数组语言,目前是TIOBE 20)中,有一个关键字end来表示数组的最后一个元素(它对应于NumPy -1)。这是一个众所周知的MATLAB语法:
myVar = myArray(end)
要从数组中间获取一个元素,通常可以这样写:
myVar = myArray( ceil( length(myArray)/2 ) )
令人惊讶的是,关键字end根本不是一个关键字,而是一种变量:
myVar = myArray( ceil( end/2 ) )
这缺少了一个奇怪的特性:Python没有switch语句(尽管存在变通方法)。