language-design

Why are arrays covariant but generics are invariant?

*爱你&永不变心* 提交于 2019-12-19 11:57:29
问题 From Effective Java by Joshua Bloch, Arrays differ from generic type in two important ways. First arrays are covariant. Generics are invariant. Covariant simply means if X is subtype of Y then X[] will also be sub type of Y[]. Arrays are covariant As string is subtype of Object So String[] is subtype of Object[] Invariant simply means irrespective of X being subtype of Y or not , List<X> will not be subType of List<Y>. My question is why the decision to make arrays covariant in Java? There

How to yield empty generator?

…衆ロ難τιáo~ 提交于 2019-12-19 05:17:28
问题 I have a method which takes a generator plus some additional parameters and yields a new generator: function merge(\Generator $carry, array $additional) { foreach ( $carry as $item ) { yield $item; } foreach ( $additional as $item ) { yield $item; } } The usual use case for this function is similar to this: function source() { for ( $i = 0; $i < 3; $i++ ) { yield $i; } } foreach ( merge(source(), [4, 5]) as $item ) { var_dump($item); } But the problem is that sometimes I need to pass empty

Why does Java use -D to indicate system properties?

那年仲夏 提交于 2019-12-19 05:17:22
问题 Why is the flag that indicates a System property in Java -D ? Surely there is some semantics to this letter choice, but I can't guess what it is. 回答1: It is short for setting a system define. "define" debug to yes -Ddebug=yes There is some historical context, as other compilers use similar flags. For example, gcc uses -D to set a preprocessor define. gcc -D debug=yes test.c will compile test.c with a preprocessing environment where the preprocessor variable debug is set to yes . 回答2: "defines

Why dont languages allow overloading of methods by return value?

我怕爱的太早我们不能终老 提交于 2019-12-18 16:08:09
问题 c, java and many other languages do not pay attention to return values. int i = func() float f = func() int func() { return 5 } float func() { return 1.3} Why isnt the above legal? Does it make it more difficult to program int i = func(func(func(func2(func3())))) //you dont know what you are getting Is it hard to write a compiler? are there more language unambiguity? Is there a language that can do the above? 回答1: What about this case? class A implements Foo { /*...*/ } class B implements Foo

Why dont languages allow overloading of methods by return value?

a 夏天 提交于 2019-12-18 16:08:03
问题 c, java and many other languages do not pay attention to return values. int i = func() float f = func() int func() { return 5 } float func() { return 1.3} Why isnt the above legal? Does it make it more difficult to program int i = func(func(func(func2(func3())))) //you dont know what you are getting Is it hard to write a compiler? are there more language unambiguity? Is there a language that can do the above? 回答1: What about this case? class A implements Foo { /*...*/ } class B implements Foo

Uses for Dynamic Languages

喜欢而已 提交于 2019-12-18 15:54:15
问题 My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent compile-time duck typing), I'm curious what the benefits are of dynamic languages even when you have those. The bottom line is that, if I'm going to learn Python, I want to learn it

Pointer syntax in C: why does * only apply to the first variable?

孤者浪人 提交于 2019-12-18 14:17:42
问题 The following declaration in C: int* a, b; will declare a as type int* and b as type int . I'm well aware of this trap, but what I want to know is why it works this way. Why doesn't it also declare b as int* , as most people would intuitively expect? In other words, why does * apply to the variable name, rather than the type? Sure you could write it this way to be more consistent with how it actually works: int *a, b; However, I and everyone I've spoken to think in terms of a is of type

In C#, why is “int” an alias for System.Int32?

痴心易碎 提交于 2019-12-18 13:52:54
问题 Since C# supports Int8 , Int16 , Int32 and Int64 , why did the designers of the language choose to define int as an alias for Int32 instead of allowing it to vary depending on what the native architecture considers to be a word ? I have not had any specific need for int to behave differently than the way it does, I am only asking out of pure encyclopedic interest. I would think that a 64-bit RISC architecture could conceivably exist which would most efficiently support only 64-bit quantities,

Javascript apparent madness [duplicate]

大城市里の小女人 提交于 2019-12-18 13:17:23
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Conflicting boolean values of an empty JavaScript array What is the rationale behind the fact that [ ([] == false), ([] ? 1 : 2) ] returns [true, 1] ? In other words an empty list is logically true in a boolean context, but is equal to false . I know that using === solves the issue, but what is the explanation behind this apparently totally illogical choice? In other words is this considered a mistake in the

Why has Python decided against constant references?

流过昼夜 提交于 2019-12-18 12:51:24
问题 Note: I'm not talking about preventing the rebinding of a variable. I'm talking about preventing the modification of the memory that the variable refers to, and of any memory that can be reached from there by following the nested containers. I have a large data structure, and I want to expose it to other modules, on a read-only basis. The only way to do that in Python is to deep-copy the particular pieces I'd like to expose - prohibitively expensive in my case. I am sure this is a very common