language-design

Why isn't Array a generic type?

混江龙づ霸主 提交于 2019-12-17 03:01:25
问题 Array is declared: public abstract class Array : ICloneable, IList, ICollection, IEnumerable { I'm wondering why isn't it: public partial class Array<T> : ICloneable, IList<T>, ICollection<T>, IEnumerable<T> { What would be the issue if it was declared as a generic type? If it was a generic type, do we still need the non-generic one or could it derive from Array<T> ? Such as public partial class Array: Array<object> { 回答1: History What problems would arise if arrays became a generic type?

Why is The Iteration Variable in a C# foreach statement read-only?

半腔热情 提交于 2019-12-17 02:49:07
问题 As I understand it, C#'s foreach iteration variable is immutable. Which means I can't modify the iterator like this: foreach (Position Location in Map) { //We want to fudge the position to hide the exact coordinates Location = Location + Random(); //Compiler Error Plot(Location); } I can't modify the iterator variable directly and instead, I have to use a for loop for (int i = 0; i < Map.Count; i++) { Position Location = Map[i]; Location = Location + Random(); Plot(Location); i = Location; }

Why are references not reseatable in C++

一世执手 提交于 2019-12-17 02:33:38
问题 C++ references have two properties: They always point to the same object. They can not be 0. Pointers are the opposite: They can point to different objects. They can be 0. Why is there no "non-nullable, reseatable reference or pointer" in C++? I can't think of a good reason why references shouldn't be reseatable. Edit: The question comes up often because I usually use references when I want to make sure that an "association" (I'm avoiding the words "reference" or "pointer" here) is never

Why are C++ inline functions in the header?

瘦欲@ 提交于 2019-12-17 02:18:10
问题 NB This is not a question about how to use inline functions or how they work, more why they are done the way they are. The declaration of a class member function does not need to define a function as inline , it is only the actual implementation of the function. For example, in the header file: struct foo{ void bar(); // no need to define this as inline } So why does the inline implementation of a classes function have to be in the header file? Why can't I put the inline function the .cpp

Bounding generics with 'super' keyword

爱⌒轻易说出口 提交于 2019-12-16 22:12:12
问题 Why can I use super only with wildcards and not with type parameters? For example, in the Collection interface, why is the toArray method not written like this interface Collection<T>{ <S super T> S[] toArray(S[] a); } 回答1: super to bound a named type parameter (e.g. <S super T> ) as opposed to a wildcard (e.g. <? super T> ) is ILLEGAL simply because even if it's allowed, it wouldn't do what you'd hoped it would do, because since Object is the ultimate super of all reference types, and

Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

こ雲淡風輕ζ 提交于 2019-12-16 20:04:31
问题 Java doesn't allow multiple inheritance, but it allows implementing multiple interfaces. Why? 回答1: Because interfaces specify only what the class is doing, not how it is doing it. The problem with multiple inheritance is that two classes may define different ways of doing the same thing, and the subclass can't choose which one to pick. 回答2: One of my college instructors explained it to me this way: Suppose I have one class, which is a Toaster, and another class, which is NuclearBomb. They

“Least Astonishment” and the Mutable Default Argument

一曲冷凌霜 提交于 2019-12-14 03:58:54
问题 Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function to always return a list with only one element: [5] . The result is instead very different, and very astonishing (for a novice): >>> foo() [5] >>> foo() [5, 5] >>> foo() [5, 5, 5] >>> foo() [5, 5, 5, 5] >>> foo() A manager of mine once had his first encounter with this feature, and called it "a dramatic design flaw"

Why was the old empty throw specification rewritten with a new syntax `noexcept`?

别来无恙 提交于 2019-12-13 20:12:18
问题 The title says it all: why did C++ retire the perfectly satisfying, useful, empty throw specification throw() to replace it with another syntax, with the introduction of the new keyword noexcept ? The empty throw specification is the "only throw these enumerated exceptions guarantee" (written throw(X,Y,Z) ), but with zero enumerated exceptions: instead of throwing X , Y or Z (and derived types), you can throw the empty set: that is the guarantee for a function to never throw anything at the

The semantic of call/cc and “ensure” in Ruby

醉酒当歌 提交于 2019-12-13 15:41:22
问题 As I know so far, Ruby is the only mainstream language that supports both call/cc and try/catch/finally (written as begin/rescue/ensure/end block). I am not familiar with Ruby, but my intuitive tell me that there are potential conflicts of that two, since call/cc allows arbitrarily control flow and ensure require some guaranteed control flow (some code path MUST be executed in a pre-defined situation, namely leaving the containing block). So, are there any conflicts exists in the language? if

Why must C/C++ string literal declarations be single-line?

假如想象 提交于 2019-12-13 11:53:14
问题 Is there any particular reason that multi-line string literals such as the following are not permitted in C++? string script = " Some Formatted String Literal "; I know that multi-line string literals may be created by putting a backslash before each newline. I am writing a programming language (similar to C) and would like to allow the easy creation of multi-line strings (as in the above example). Is there any technical reason for avoiding this kind of string literal? Otherwise I would have