在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
c++中我最喜欢的一个是“公共抽象具体内联析构函数”:
class AbstractBase {
public:
virtual ~AbstractBase() = 0 {}; // PACID!
virtual void someFunc() = 0;
virtual void anotherFunc() = 0;
};
我是从Scott Meyers的《Effective c++》中偷来的。看到一个方法既是纯虚拟的(通常意味着“抽象”),又是内联实现的,这看起来有点奇怪,但这是我发现的确保对象被多态破坏的最佳和最简洁的方法。
其他回答
在Python中:
i = 1
++i
print i
输出“1”。行'++i'的计算结果为+(+i) (Python不支持增量操作符)
C# has a feature called "extension methods", which are roughly analogous to Ruby mix-ins - Essentially, you can add a method to any pre-existing class definition (for instance, you oould add "reverse()" to String if you were so inclined). That alone is fine- The "Weird" part is that you can add these extension methods, with a method body and everything, to an interface. On the one hand, this can be handy as a way to add a single method to a whole swath of classes which aren't part of the same inheritance tree. On the other, you're adding fleshed out methods to interfaces, essentially breaking the very definition of an interface.
在PHP中,你可以使用符号和字符串文字或包含变量名的变量引用变量,例如:
${'foo'} = 'test';
echo $foo;
这将打印“test”。这种行为的奇怪之处在于,你也可以使用非字符串作为变量名,例如:
${array()} = 'test';
echo ${array()};
${NULL} = 'test';
echo ${NULL};
现在我们有了名为array()的变量,甚至还有NULL!所有包含字符串"test"。
在PHP中,字符串和函数指针一样好:
$x = "foo";
function foo(){ echo "wtf"; }
$x(); # "wtf"
不幸的是,这并不管用:
"foo"();
Haskell's use of Maybe and Just. Maybe a is a type constructor that returns a type of Just a, but Maybe Int won't accept just an Int, it requires it to be a Just Int or Nothing. So in essence in haskell parlance Just Int is about as much of an Int as an apple is an orange. The only connection is that Just 5 returns a type of Maybe Interger, which can be constructed with the function Just and an Integer argument. This makes sense but is about as hard to explain as it can theoretically be, which is the purpose of haskell right? So is Just really JustKindaLikeButNotAtAll yea sorta, and is Maybe really a KindaLooksLikeOrIsNothing, yea sorta again.
-- Create a function that returns a Maybe Int, and return a 5, which know is definitly Int'able
> let x :: Maybe Int; x = 5;
<interactive>:1:24:
No instance for (Num (Maybe Int))
arising from the literal `5' at <interactive>:1:24
Possible fix: add an instance declaration for (Num (Maybe Int))
In the expression: 5
In the definition of `x': x = 5
> Just 5
Just 5
it :: Maybe Integer
-- Create a function x which takes an Int
> let x :: Int -> Int; x _ = 0;
x :: Int -> Int
-- Try to give it a Just Int
> x $ Just 5
<interactive>:1:4:
Couldn't match expected type `Int' against inferred type `Maybe t'
In the second argument of `($)', namely `Just 5'
In the expression: x $ Just 5
In the definition of `it': it = x $ Just 5
祝你好运读到这篇文章,我希望它是正确的。