language-design

Why can't you have multiple interfaces in a bounded wildcard generic?

a 夏天 提交于 2020-01-26 11:23:37
问题 I know there's all sorts of counter-intuitive properties of Java's generic types. Here's one in particular that I don't understand, and which I'm hoping someone can explain to me. When specifying a type parameter for a class or interface, you can bound it so that it must implement multiple interfaces with public class Foo<T extends InterfaceA & InterfaceB> . However, if you're instantiating an actual object, this doesn't work anymore. List<? extends InterfaceA> is fine, but List<? extends

Why the static methods values() and valueOf() in Enum are added by the compiler and not explicitly declared in the Enum class?

有些话、适合烂在心里 提交于 2020-01-24 09:39:30
问题 In reading through the Javadoc of Enum I can see a signature of valueOf() that is not what I usually use. Also the javadoc for values() method is also missing. I read that these methods are actually generated by the compiler automatically. So my question is why are these methods not declared as static methods in the Enum class itself? What is the need for the methods to be included like the way they are by the compiler? Why this extra layer of abstraction? 回答1: These methods have to be

Do default constructors for private inner classes have a formal parameter?

邮差的信 提交于 2020-01-22 08:43:30
问题 Caveat #1: This is actually a potential two-parter: First, does the constructor for a private inner class have a formal parameter? If yes, why does the JLS say it doesn't? And if no, how/why not? Caveat #2: This question is not for speculation. I'm looking for authoritative answers only. Default constructors are defined in JLS 8.8.9, which states (in part): The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly

Compiler error when Calling a generic method with no actual argument but with explicit type parameter

独自空忆成欢 提交于 2020-01-17 05:42:05
问题 From the book "Java Generic and Collections", section 1.4 there is this code sniplet class Lists { public static <T> List<T> toList(T... arr) { List<T> list = new ArrayList<T>(); for (T elt : arr) list.add(elt); return list; } } Then there is this statement: When a type parameter is passed to a generic method invocation, it appears in angle brackets to the left, just as in the method declaration. The Java grammar requires that type parameters may appear only in method invocations that use a

languages that always had triple equals

不想你离开。 提交于 2020-01-14 07:47:07
问题 What popular programming languages were intentionally designed from the beginning to have both === and == (and require the programmer to figure out which one to use). Javascript, PHP, ruby (and probably others) have a triple equals operator today. But, it is not clear if this was a deliberate design decision, or happened only by accident (perhaps because the language started with double equals, but at some point it was discovered that double equals wasn't quite doing what people wanted it to

Typed FP: Tuple Arguments and Curriable Arguments

我的未来我决定 提交于 2020-01-12 07:35:10
问题 In statically typed functional programming languages, like Standard ML, F#, OCaml and Haskell, a function will usually be written with the parameters separated from each other and from the function name simply by whitespace: let add a b = a + b The type here being " int -> (int -> int) ", i.e. a function that takes an int and returns a function which its turn takes and int and which finally returns an int. Thus currying becomes possible. It's also possible to define a similar function that

Why does C# allow for an abstract class with no abstract members?

守給你的承諾、 提交于 2020-01-12 04:19:25
问题 The C# spec, section 10.1.1.1, states: An abstract class is permitted (but not required) to contain abstract members. This allows me to create classes like this: public abstract class A { public void Main() { // it's full of logic! } } Or even better: public abstract class A { public virtual void Main() { } } public abstract class B : A { public override sealed void Main() { // it's full of logic! } } This is really a concrete class; it's only abstract in so far as one can't instantiate it.

Javascript: why Object.keys(someobject), rather than someobject.keys?

和自甴很熟 提交于 2020-01-10 14:18:13
问题 I frequently get an array of an objects keys using: Object.keys(someobject) I'm comfortable doing this. I understand that Object is the Object constructor function, and keys() is a method of it, and that keys() will return a list of keys on whatever object is given as the first parameter. My question is not how to get the keys of an object - please do not reply with non-answers explaining this. My question is, why isn't there a more predictable keys() or getKeys() method, or keys instance

Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc

雨燕双飞 提交于 2020-01-09 04:38:10
问题 I'm bewildered by all the built-in Mathematica functions that purport to prevent evaluation in some way: Unevaluated , Defer , Hold , and over half a dozen of the form Hold* . The Mathematica documentation just explains each function in isolation without explaining why you would choose one or the other. Can anyone offer a coherent explanation of all these functions? The whole thing seems like a convoluted mess to me. Relating it all to Lisp macros might be a good place to start. Most of the

Python `None` passed into type-conversion functions

我们两清 提交于 2020-01-04 15:15:28
问题 What is the rationale for the design decision for None type being passed into type-conversion functions? bool(None) returns False - which makes perfect sense str(None) returns 'None' which is not okay - Returning an empty string would be a better choice as below >>> bool(None) False >>> bool(str(None)) #Returning empty string will make it consistent by returning False True >>> bool('') False And list(None) , dict(None) , tuple(None) , int(None) , float(None) return Type errors - From which if