在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在Perl中,你可以做到:
my $test = "Hello World";
substr($test, 0, 5) = "Goodbye";
print $test;
这在其他语言中可行吗?
其他回答
这并不是一个真正的语言特性,而是一个实现缺陷:一些早期的Fortran编译器通过使用常量池来实现常量。所有参数都是通过引用传递的。如果你调用一个函数,例如。
f(1)
编译器会将常量池中常量1的地址传递给函数。 如果您为函数中的参数赋值,则会在程序中全局地更改该值(在本例中为1)。引起了一些挠头。
我最喜欢的奇怪的C语言是5[“Hello World”],但因为已经发布了,我最喜欢的第二个奇怪的是Windows版本结构初始化黑客:
void someWindowsFunction() {
BITMAPINFOHEADER header = {sizeof header};
/* do stuff with header */
}
这条微妙的线条达到了以下效果:
Declares a BITMAPINFOHEADER structure Concisely sets the "size" member of the structure, without hardcoding a size constant (since many Window structures, including BITMAPINFOHEADER, follow the convention of specifying the size of the structure as the first member} Declares the version of the structure (since many Windows structures, including BITMAPINFOHEADER, identify their version by the declared size, following the convention that structures definitions are append-only) Clears all other members of the structure (a C standard behavior when a structure is incompletely initialized).
我为客户端编写了一种编程语言(用于实验驱动定制硬件),其中包含一些定制类型(Curl, Circuit,…),每种类型只有2个值。它们可以隐式地转换为布尔值,但是(在客户机的请求下)可以在运行时更改此类类型常量的确切布尔值。
例如: Curl类型有2个可能的值:CW和CCW(顺时针和逆时针)。在运行时,你可以通过一个简单的赋值语句改变布尔值:
ccw := true
所以你可以改变所有这些类型值的布尔值。
两个字:多重继承。这毫无意义,只会制造麻烦。
编辑——我指的是c++中的MI,而不是Java和其他语言中的mixins之类的。
Algol通过名称传递(使用C语法说明):
int a[3] = { 1, 2, 3 };
int i = 1;
void f(int j)
{
int k;
k = j; // k = 2
i = 0;
k = j; // k = 1 (!?!)
}
int main()
{
f(a[i]);
}