在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在Bash中,变量可以显示为标量和数组:
$ a=3
$ echo $a
3
$ echo ${a[@]} # treat it like an array
3
$ declare -p a # but it's not
declare -- a="3"
$ a[1]=4 # treat it like an array
$ echo $a # acts like it's scalar
3
$ echo ${a[@]} # but it's not
3 4
$ declare -p a
declare -a a='([0]="3" [1]="4")'
$ a=5 # treat it like a scalar
$ echo $a # acts like it's scalar
5
$ echo ${a[@]} # but it's not
5 4
$ declare -p a
declare -a a='([0]="5" [1]="4")'
KSH做同样的事情,但是使用排版而不是声明。
当你在zsh中这样做时,你得到的是子字符串赋值而不是数组:
$ a=3
$ a[2]=4 # zsh is one-indexed by default
$ echo $a
34
$ a[3]=567
$ echo $a
34567
$ a[3]=9
$ echo $a
34967
$ a[3]=123 # here it overwrites the first character, but inserts the others
$ echo $a
3412367
$ a=(1 2 3)
$ echo $a
1 2 3 # it's an array without needing to use ${a[@]} (but it will work)
$ a[2]=99 # what about assignments?
$ echo $a
1 99 3
其他回答
来自边远吗?
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)。
我肯定会给Perl提供多个可怕的例子:
if(!$#var)
or
if($mystring =~ m/(\d+)/) {
INTERCAL可能是最奇怪的语言特征的最佳汇编。我个人最喜欢的是COMEFROM语句,它(几乎)与GOTO相反。
COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement. The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal. A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the label that control will mysteriously jump to another point of the program.
Perl中的字符串数学非常奇怪。
$ perl -E '$string = "a"; $string++; say $string'
b
$ perl -E '$string = "abc"; $string++; say $string'
abd
$ perl -E '$string = "money"; $string++; say $string'
monez
$ perl -E '$string = "money"; $string--; say $string'
-1