在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在Java中(实际上,我最近在不同的SO帖子上写过这个):
int x = 1 + + + + + + + + + + + + + 1;
System.out.println(x);
其他回答
这并不是一个奇怪的特性,但从类型安全的角度来看,它确实令人恼火:c#中的数组协方差。
class Foo { }
class Bar : Foo { }
class Baz : Foo { }
Foo[] foo = new Bar[1];
foo[0] = new Baz(); // Oh snap!
我相信这是从Java继承而来的(双关语)。
在c#中,这至少应该生成一个编译器警告,但它没有:
public int Something
{
get { return Something; }
set { Something = value; }
}
当被调用时,它会导致你的应用程序崩溃,你不会得到一个好的堆栈跟踪,因为它是一个StackOverflowException。
Perl的sub没有真正的参数列表,只有@_数组。同时,sub会自动变平传入的参数。
我不明白为什么这是一个持久的功能;这反映了几年前我不得不在TI-86 BASIC上做的事情,因为这门语言没有足够的特色。
在Bash中,变量可以显示为标量和数组:
$ a=3
$ echo $a
3
$ echo ${a[@]} # treat it like an array
3
$ declare -p a # but it's not
declare -- a="3"
$ a[1]=4 # treat it like an array
$ echo $a # acts like it's scalar
3
$ echo ${a[@]} # but it's not
3 4
$ declare -p a
declare -a a='([0]="3" [1]="4")'
$ a=5 # treat it like a scalar
$ echo $a # acts like it's scalar
5
$ echo ${a[@]} # but it's not
5 4
$ declare -p a
declare -a a='([0]="5" [1]="4")'
KSH做同样的事情,但是使用排版而不是声明。
当你在zsh中这样做时,你得到的是子字符串赋值而不是数组:
$ a=3
$ a[2]=4 # zsh is one-indexed by default
$ echo $a
34
$ a[3]=567
$ echo $a
34567
$ a[3]=9
$ echo $a
34967
$ a[3]=123 # here it overwrites the first character, but inserts the others
$ echo $a
3412367
$ a=(1 2 3)
$ echo $a
1 2 3 # it's an array without needing to use ${a[@]} (but it will work)
$ a[2]=99 # what about assignments?
$ echo $a
1 99 3
INTERCAL可能是最奇怪的语言特征的最佳汇编。我个人最喜欢的是COMEFROM语句,它(几乎)与GOTO相反。
COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement. The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal. A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the label that control will mysteriously jump to another point of the program.