在您看来,最令人惊讶、奇怪、奇异或真正的“WTF”语言特性是什么?
每个答案只限一个特性。
在您看来,最令人惊讶、奇怪、奇异或真正的“WTF”语言特性是什么?
每个答案只限一个特性。
a[10]
这非常常见。
然而,较少人知道的形式(确实有效!)是:
10[a]
这意味着与上面的内容相同。
在JavaScript中:
'5' + 3 gives '53'
相比之下
'5' - 3 gives 2
+
非常糟糕。 - Matteo Rivareturn
{
id : 1234,
title : 'Tony the Pony'
};
返回 是一个语法错误,因为在 undefined
return
后的换行符上进行了诡异的隐式分号插入。然而,以下代码将按预期工作:
return {
id : 1234,
title : 'Tony the Pony'
};
更糟糕的是,这个在Chrome中也能工作:
return /*
*/{
id : 1234,
title : 'Tony the Pony'
};
这是一个相同问题的变体,它不会产生语法错误,只是悄无声息地失败了:
return
2 + 2;
JavaScript真值表:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
" \t\r\n" == 0 // true
==
的作用是什么? - Chris S==
的含义和===
一样就好了,然后再有另一个运算符,比如说~=
,允许类型强制转换。 - TM.C和C++中的三字符序列。
int main() {
printf("LOL??!");
}
这将打印LOL|
,因为三字符组??!
被转换为|
。
Java中的自动装箱和整数缓存:
Integer foo = 1000;
Integer bar = 1000;
foo <= bar; // true
foo >= bar; // true
foo == bar; // false
//However, if the values of foo and bar are between 127 and -128 (inclusive)
//the behaviour changes:
Integer foo = 42;
Integer bar = 42;
foo <= bar; // true
foo >= bar; // true
foo == bar; // true
快速查看Java源代码将会发现以下内容:
/**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Integer(int)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
注意: IntegerCache.high
默认为 127
,除非通过属性进行设置。foo = new Integer(42)
),否则 foo 和 bar 将从缓存中检索相同的整数对象,因此在比较引用相等性时它们将是 true 而不是 false。比较 Integer 值的正确方法是使用 .equals;
。IntegerCache.high
的 - 可修改的值,但仅在前一行,他们决定硬编码数字128(而不是使用IntegerCache.high + 1)。 - monokrome引用Neil Fraser的话(请查看该页面底部):
try {
return true;
} finally {
return false;
}
(在Java中,但行为显然在JavaScript和Python中是相同的)。结果留给读者练习。
编辑:只要我们谈论这个话题,请考虑这个:
try {
throw new AssertionError();
} finally {
return false;
}
return
放在 finally
语句块中。 - jfsfinally
中无法返回结果,以下代码会怎样执行:bool x = true; try { return x; } finally { x = false; }
该代码将返回布尔值 true
。尽管在 finally
语句块中将变量 x
设置为 false
,但是由于在 try
语句块中已经返回了 true
,因此 return
语句的结果不会受到后续代码的影响。 - Chris LutzAPL (除了全部内容), 具备只需一行即可编写任何程序的能力。
例如,Conway's Game of Life 在 APL 中只需要一行代码:
替代文本 http://catpad.net/michael/APLLife.gif
如果那一行让你困惑,那就没有什么比这更让人困惑的了!
以下是一个视频
C++ 模板可以用来做很奇怪的事情,最好的例子是通过"多维模拟字面量"来展示,该技术使用模板计算“绘制”形状的面积。以下代码是一个有效的 C++ 代码,用于表示一个 3x3 的矩形。
#include"analogliterals.hpp"
using namespace analog_literals::symbols;
unsigned int c = ( o-----o
| !
! !
! !
o-----o ).area;
或者,另一个带有3D立方体的示例:
assert( ( o-------------o
|L \
| L \
| L \
| o-------------o
| ! !
! ! !
o | !
L | !
L | !
L| !
o-------------o ).volume == ( o-------------o
| !
! !
! !
o-------------o ).area * int(I-------------I) );
Perl内置的许多变量:
$#
— 不是注释!$0
、$$
和$?
— 与同名shell变量相同$ˋ
、$&
和$'
— 奇怪的匹配变量$"
和$,
— 列表和输出字段分隔符的奇怪变量$!
— 类似于errno
的数字,但是像strerror(errno)
一样是一个字符串$_
— 隐形变量,总是被使用但从来没有显示出来$#_
— 最后一个子例程参数的索引号...也许@_
— 当前函数的(非)名称...也许$@
— 最近抛出的异常%::
— 符号表$:
、$^
、$~
、$-
和$=
— 与输出格式有关的东西$.
和$%
— 输入行号、输出页码$/
和$\
— 输入和输出记录分隔符$|
— 输出缓冲控制器$[
— 将数组基数从0-based改为1-based或42-based:WHEEE!$}
— 奇怪地什么也没有!$<
、$>
、$(
、$)
— 真实和有效的UID和GID@ISA
— 当前包的直接超类的名称$^T
— 脚本启动时间(时代秒)$^O
— 当前操作系统名称$^V
— Perl版本号还有更多变量可以使用。在这里阅读完整列表。
perldoc perlvar
,就能编写 Perl 6 代码,那肯定会很感激。(尽管我承认有一半的时间我检查它时想着,“我知道有一个特殊变量可以为我完成这个任务,但我不记得是哪一个…” =P) - Chris Lutzuse English;
的问题在于它会影响正则表达式的性能。这不是我编造的。详情请见:http://perldoc.perl.org/English.html#PERFORMANCE。 - David Webb/$foo[bar]/
中,[bar]
部分是字符类还是数组@foo
的下标?请查看perldata
中的Grep以获取令人恐惧的答案。 - j_random_hacker