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

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


当前回答

Processing (processing.org)是一种基于Java的语言。简单来说,处理编译器是将特定于处理的语法转换为Java的Java预处理器。

由于语言的设计,它有一些惊喜:

Processing的类被编译成Java内部类,这会引起一些麻烦,比如私有变量并不是真正私有的

class Foo {
  private int var = 0; // compiles fine
}

void setup() {
  Foo f = new Foo();
  print(f.var); // but does not causes compile error
}

同样缺少draw()函数会导致事件处理程序不被调用:

// void draw() {} // if you forgot to include this
void mousePressed() {
  print("this is never called");
}

其他回答

Python的三元操作符

在Python中,C三元操作符(c++示例:bool isNegative = i < 0 ?True: false;)可用作语法糖:

>>> i = 1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is positive'
>>> i = -1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is negative!'

这并不奇怪,而是一种特征。奇怪的是,与C中的顺序(条件?答:b)。

在Java中可以抛出任何可抛出的东西。

class YourBoss extends Throwable {
}
public class Main{
  public void main(String[] s) throws YourBoss {
   try{
    throw new YourBoss();
   }catch(Exception e){
   }catch(Error e){
   }
  }
}

javascript:

parseInt('06'); // 6
parseInt('08'); // 0

在c#中: A = cond ?B: c; 如果“b”和“c”是“赋值不兼容”,你永远不会得到结果,即使“a”是对象。 它是ms中最常用和最愚蠢的实现运算符。有关比较,请参阅D语言中的实现(关于类型推断的注意)。

С#:

var a = Double.Parse("10.0", CultureInfo.InvariantCulture); // returns 10
var b = Double.Parse("10,0", CultureInfo.InvariantCulture); // returns 100

在不变区域性中,逗号不是小数点符号,而是组分隔符。

据我所知,对于一些地区的新手程序员来说,这是一个常见的错误。