在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

Perl的$[(已弃用),这在之前关于通用Perl变量的另一篇文章中提到过,但它值得特别提到,并有更好的解释。对$[的更改仅限于当前范围。更多的信息以及如何使用它的含义;)可以在$[中找到,请访问http://www.perlmonks.org/index.pl/?node_id=480333

其他回答

在Scala中,没有操作符,只有方法。所以a + b - c实际上等于a +(b) -(c)在这里,它等于Smalltalk。但是,与Smalltalk不同的是,它考虑了优先级。规则基于第一个字符,因此一个假设的方法*+将优先于一个叫做+*的方法。做了一个例外,以便任何以=结尾的方法都将具有与==——含义相同的优先级!!和!=(非假设方法)具有不同的优先级。

所有ASCII字母的优先级最低,但所有非ASCII (unicode)字符的优先级最高。如果你写了一个比较两个int型的方法,那么2 + 2 = 1 + 3将会编译并为真。如果你用葡萄牙语写é,那么2 + 2 é 1 + 3会导致错误,因为它会看到2 + (2 é 1) + 3。

而且,在Scala中操作符的WTF中,所有以:结尾的方法都是右关联的,而不是左关联的。这意味着1::2::Nil等价于Nil.::(2).::(1)而不是1.::(2).::(Nil)。

雅达利基本:

你可以在不写循环的情况下用字符填充字符串:

10 DIM A$(100)
20 A$(1)=" ":A$(100)=" ":A$(2)=A$

A very tiny thing that annoyed me in COBOL was that there was no dedicated modulo operation. Instead you could do a division specifying that you only wanted whole number results and store the rest in a different variable. Since COBOL is very sensitive when it comes to variables that means that you ended up with a variable you didn't really need, i.e. the actual result of the division. This is the story of how I once named a variable "USELESS" - that was the most appropriate name I could think of.

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

还有很多这样的东西。点击这里阅读完整列表。

有向图和替代标记

C (ISO/IEC 9899:1999, 6.4.6/3)和c++ (ISO/IEC 14882:2003, 2.5)有一个很少使用的特性,被C称为“有向图”,被c++称为“替代令牌”。它们不同于三trigraph,主要是因为包含它们的字符串字面量永远不会被不同地解释。

%:include <stdio.h>

int main() <%
    int a<:10:> = <%0%>;
    printf("Here's the 5th element of 'a': %d\n", a<:4:>);
    puts("Evil, eh? %:>");
    return 0;
%>

c++有更多,包括and, or, and not,它们被要求表现为&&,||和!C语言也有这些,但是需要包含<iso646.h>来使用它们,将它们视为宏而不是令牌。c++头文件<ciso646>实际上是一个空文件。

值得注意的是,GCC实现了对这种奇怪的语言特性的支持,但许多其他编译器在尝试编译上述代码段时都阻塞并死亡。