language-design

Python `None` passed into type-conversion functions

烈酒焚心 提交于 2020-01-04 15:15:15
问题 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

language without if's?

柔情痞子 提交于 2020-01-02 02:19:05
问题 A colleague said he heard of a language that did not have the concept of "if". Is that possible? If so, what language is it? 回答1: Besides perhaps Prolog, I don't know of any specific languages, but I can think of a few ways a language without if statements may work. In fact, you don't need loop constructs either. You obviously needs some way of conditional branches and looping. If, for example, you had the following features: functions , ML-style pattern matching on function arguments and

Why does the constness of a reference affect whether it can be initialized with a variable of a different type?

点点圈 提交于 2020-01-01 17:07:13
问题 Why does this code fail to compile? double d ; int & i1 = d // Compilation FAILS While this one does? double d ; const int & i = d // Compilation Succeeds Please, I am interested in knowing what was in mind of C++ designers that they allowed one behavior while disallowed another. I know in either case it's immoral, independent of technically possible or not. Also FYI I am using GCC on mac with "-O0 -g3 -Wall -c -fmessage-length=0" 回答1: Because it creates a temporary int where the double is

Why does CMake syntax have redundant parentheses everywhere?

十年热恋 提交于 2020-01-01 09:36:24
问题 CMake's if s go like this: if (condition) ... else if (...) ... else (...) ... endif (...) With else if (...) the (...) tests for a separate condition. Why else (...) and not just else ? Why endif (...) and not endif ? Cmake's functions go like this: function(funcname ...) ... endfunction(funcname ...) Why endfunction(funcname ...) and not simply endfunction ? I can omit the contents of the redundant parenthesis where they appear, like so: endif () . What's the purpose of this construct? 回答1:

Why doesn't a swap / exchange operator exist in imperative or OO languages like C/C++/C#/Java…?

梦想的初衷 提交于 2020-01-01 04:57:10
问题 I was always wondering why such a simple and basic operation like swapping the contents of two variables is not built-in for many languages. It is one of the most basic programming exercises in computer science classes; it is heavily used in many algorithms (e.g. sorting); every now and then one needs it and one must use a temporary variable or use a template/generic function. It is even a basic machine instruction on many processors, so that the standard scheme with a temporary variable will

Why aren't “and” and “or” operators in Python?

大憨熊 提交于 2020-01-01 01:17:34
问题 I wasn't aware of this, but apparently the and and or keywords aren't operators. They don't appear in the list of python operators. Just out of sheer curiosity, why is this? And if they aren't operators, what exactly are they? 回答1: Because they're control flow constructs. Specifically: if the left argument to and evaluates to False, the right argument doesn't get evaluated at all if the left argument to or evaluates to True, the right argument doesn't get evaluated at all Thus, it is not

Haskell “collections” language design

梦想的初衷 提交于 2020-01-01 01:17:09
问题 Why is the Haskell implementation so focused on linked lists? For example, I know Data.Sequence is more efficient with most of the list operations (except for the cons operation), and is used a lot; syntactically, though, it is "hardly supported". Haskell has put a lot of effort into functional abstractions, such as the Functor and the Foldable class, but their syntax is not compatible with that of the default list. If, in a project I want to optimize and replace my lists with sequences - or

Why no non-integral enums?

冷暖自知 提交于 2019-12-30 19:55:13
问题 Why is it that non-integral enums cannot be created? I want to know if this is a language design decision, or if there are issues with implementing this in the compiler. In other words, is it feasible to implement non-integral enums into the language, but there just isn't a justifiable need? Or if it isn't feasible but is justifiable, what impediment are in the way? Someone give me the skinny on what the reason or rationale is for not having this available in C#, pretty please. 回答1: Only the

Is empty case of switch in C# combined with the next non-empty one? [closed]

谁说我不能喝 提交于 2019-12-30 15:10:13
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . With the following code: case "GETSITES": case "SITESETUP": MessageBox.Show("Help! I am suffering from the Air-Conditioned Nightmare!!!"); // ... Will

Why not call nullptr NULL?

雨燕双飞 提交于 2019-12-30 01:39:06
问题 In C++11 the nullptr keyword was added as a more type safe null pointer constant, since the previous common definition of NULL as 0 has some problems. Why did the standards committee choose not to call the new null pointer constant NULL , or declare that NULL should be #define d to nullptr ? 回答1: Stephan T. Lavavej (member of the C++ standard committee) explained that once in a talk (55:35): While an implementation is allowed to #define NULL nullptr , it would break quite some uses like int i