language-design

Why are await and async valid variable names?

社会主义新天地 提交于 2019-12-17 23:29:48
问题 I was experimenting with how / is interpreted when around different keywords and operators, and found that the following syntax is perfectly legal: // awaiting something that isn't a Promise is fine, it's just strange to do: const foo = await /barbaz/ myFn() Error: Uncaught ReferenceError: await is not defined It looks like it tries to parse the await as a variable name ..? I was expecting await is only valid in async function or maybe something like Unexpected token await To my horror, you

Why does the implicit copy constructor calls the base class copy constructor and the defined copy constructor doesn't?

穿精又带淫゛_ 提交于 2019-12-17 23:19:45
问题 Consider a class hierarchy where A is the base class and B derives from A . If the copy constructor is not defined in B , the compiler will synthesize one. When invoked, this copy constructor will call the base class copy constructor (even the synthesized one, if none has been provided by the user). #include <iostream> class A { int a; public: A() { std::cout << "A::Default constructor" << std::endl; } A(const A& rhs) { std::cout << "A::Copy constructor" << std::endl; } }; class B : public A

Duck typing, must it be dynamic?

让人想犯罪 __ 提交于 2019-12-17 22:34:12
问题 Wikipedia used to say* about duck-typing: In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. (* Ed. note: Since this question was posted, the Wikipedia article has been edited to remove the word "dynamic".) It says about structural typing: A structural

Why there is no sleep functionality in javascript when there is setTimeout and setInterval?

巧了我就是萌 提交于 2019-12-17 19:46:43
问题 Why there no such function in javascript that sets a timeout for its continuation, saves the necessary state (the scope object and the execution point), terminates the script and gives the control back to the browser? After the timeout expires the browser would load back the execution context and continues the script, and we would have a real non browser blocking sleep functionality that would work even if the JS engine is single threaded. Why there is still no such functionality in

Why can't namespaces be template parameters?

强颜欢笑 提交于 2019-12-17 18:49:15
问题 I understand that namespaces cannot be template parameters. See the question, "template specialized on a namespace": Given: namespace A { class Foo; class Bar; } namespace B { class Foo; class Bar; } I want to template a class on the namespace A or B such that the following works: template<name> class C { name::Foo* foo; name::Bar* bar; }; I was wondering why this is the case. I understand that templates aren't structures, but is there a technical limitation to the compiler's design? Or is

Why does PHP's call_user_func() function not support passing by reference?

十年热恋 提交于 2019-12-17 16:35:37
问题 Why don't the function handling functions like call_user_func() support passing parameters by reference? The docs say terse things like "Note that the parameters for call_user_func() are not passed by reference." I assume the PHP devs had some kind of reason for disabling that capability in this case. Were they facing a technical limitation? Was it a language design choice? How did this come about? EDIT: In order to clarify this, here is an example. <?php function more(&$var){ $var++; }

Why are there no ||= or &&= operators in C#?

心不动则不痛 提交于 2019-12-17 16:34:04
问题 We have equivalent assignment operators for all Logical operators, Shift operators, Additive operators and all Multiplicative operators. Why did the logical operators get left out? Is there a good technical reason why it is hard? 回答1: Why did the logical operators get left out? Is there a good technical reason why it is hard? They didn't . You can do &= or |= or ^= if you want. bool b1 = false; bool b2 = true; b1 |= b2; // means b1 = b1 | b2 The || and && operators do not have a compound form

Why can't C# member names be the same as the enclosing type name?

橙三吉。 提交于 2019-12-17 16:31:47
问题 In C#, the following code doesn't compile: class Foo { public string Foo; } The question is: why? More exactly, I understand that this doesn't compile because (I quote): member names cannot be the same as their enclosing type Ok, fine. I understand that, I won't do it again, I promise. But I really don't understand why the compiler refuses to take any field having the same name as an enclosing type. What is the underlying issue that prevents me to do that? 回答1: Strictly speaking, this is a

Function arguments (in Python for example)

守給你的承諾、 提交于 2019-12-17 16:31:40
问题 What are [function] arguments? What are they used for? I started learning Python very recently; I'm new to programming and I apologize for this basic question. In every Python tutorial I go through they talk about arguments . I have looked for the answer to this question and have found many answers but they are just a little too hard for me to understatnd. I may just be missing some conceptual background. So... when I define a function, what are the things in parenthesis used for? Example:

Why does C# not allow generic properties?

拈花ヽ惹草 提交于 2019-12-17 10:59:11
问题 I was wondering why I can not have generic property in non-generic class the way I can have generic methods. I.e.: public interface TestClass { IEnumerable<T> GetAllBy<T>(); //this works IEnumerable<T> All<T> { get; } //this does not work } I read @Jon Skeet's answer, but it's just a statement, which most probably is somewhere in the specifications. My question is why actually it is that way? Was kind of problems were avoided with this limitation? 回答1: Technically, the CLR supports only