在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
Java中有标记的break和continue语句。
它们允许你通过一次突破突破多个控制块。
其他回答
Forth的控制结构有些奇怪。首先,因为它是一种反向波兰符号语言,条件在IF之前,如:
x 0 = IF
现在,要关闭条件块,使用关键字THEN:
x 0 = IF ." Equals zero!" THEN
现在真正的WTF开始了。IF所做的是编译一个有条件的前向跳转,并将跳转偏移量的地址放在堆栈上。当找到THEN时,它从堆栈中弹出该地址,计算实际偏移量,然后编译它。另一方面,ELSE编译一个条件向前跳转,从堆栈中弹出一个地址,将一个新地址压入堆栈,计算弹出地址的偏移量,然后编译该偏移量。这意味着语法是这样的:
x 0 = IF ." Equals zero!" ELSE ." Not equal to zero!" THEN
第一个和第二个语句是这样编译的:
x LITERAL 0 = (0BRANCH) LITERAL offset SLITERAL" Equals zero!" (DOTQ)
x LITERAL 0 = (0BRANCH) LITERAL offset SLITERAL" Equals zero!" (DOTQ) BRANCH LITERAL offset SLITERAL" Not equal to zero!" (DOTQ)
更奇怪的是,这种行为并不隐藏。它是该语言的ANSI规范的一部分,可以通过构造自定义流控制结构或以有趣的方式组合它们来自由利用。例如,Forth的WHILE循环:
BEGIN x 10 < WHILE x 1+ to x REPEAT
BEGIN和WHILE之间的部分是任意代码,因此实际上可以在单个控制结构中让代码在条件测试之前和之后执行。这是故意的,但下面的内容虽然是允许的,但却不允许:
BEGIN DUP 2 > WHILE DUP 5 < WHILE DUP 1+ REPEAT 123 ELSE 345 THEN
它利用了每个控制流字的工作方式来组合两个WHILE语句,并为每个出口添加不同的后循环代码来引导。为了表明我不是在开玩笑,我只是从互联网上的一个代码中复制了这个小片段,并进行了一些小的修改来简化它。
Perl的许多内置变量:
$# — not a comment! $0, $$, and $? — just like the shell variables by the same name $ˋ, $&, and $' — weird matching variables $" and $, — weird variables for list- and output-field-separators $! — like errno as a number but strerror(errno) as a string $_ — the stealth variable, always used and never seen $#_ — index number of the last subroutine argument... maybe @_ — the (non)names of the current function... maybe $@ — the last-raised exception %:: — the symbol table $:, $^, $~, $-, and $= — something to do with output formats $. and $% — input line number, output page number $/ and $\ — input and output record separators $| — output buffering controller $[ — change your array base from 0-based to 1-based to 42-based: WHEEE! $} — nothing at all, oddly enough! $<, $>, $(, $) — real and effective UIDs and GIDs @ISA — names of current package’s direct superclasses $^T — script start-up time in epoch seconds $^O — current operating system name $^V — what version of Perl this is
还有很多这样的东西。点击这里阅读完整列表。
作为一名NHibernate爱好者,当我从Smalltalk听到be时,我非常激动……如。
a become: b
它直接将a对象更改为b,这使得编写惰性初始化代理变得很简单,因为所有对a的引用现在都将引用b。非常简洁!
我认为这是一种奇怪的语言特征,因为据我所知,没有其他语言具有这种能力。
Java;使所有对象实例都是互斥对象。
C和c++中的三联体。
int main() {
printf("LOL??!");
}
这将打印LOL|,因为trigraph ??!转换为|。