language-design

what does a php function return by default?

两盒软妹~` 提交于 2019-12-17 10:57:40
问题 If I return nothing explicitly, what does a php function exactly return? function foo() {} What type is it? What value is it? How do I test for it exactly with === ? Did this change from php4 to php5? Is there a difference between function foo() {} and function foo() { return; } (I am not asking how to test it like if (foo() !=0) ... ) 回答1: null null if(foo() === null) - Nope. You can try it out by doing: $x = foo(); var_dump($x); 回答2: Not returning a value from a PHP function has the same

Why can't I inherit from int in C++?

拈花ヽ惹草 提交于 2019-12-17 10:39:12
问题 I'd love to be able to do this: class myInt : public int { }; Why can't I? Why would I want to? Stronger typing. For example, I could define two classes intA and intB , which let me do intA + intA or intB + intB , but not intA + intB . "Ints aren't classes." So what? "Ints don't have any member data." Yes they do, they have 32 bits, or whatever. "Ints don't have any member functions." Well, they have a whole bunch of operators like + and - . 回答1: Neil's comment is pretty accurate. Bjarne

Why can't we define a variable inside an if statement?

陌路散爱 提交于 2019-12-17 09:06:15
问题 Maybe this question has been answered before, but the word if occurs so often it's hard to find it. The example doesn't make sense (the expression is always true), but it illustrates my question. Why is this code valid: StringBuilder sb; if ((sb = new StringBuilder("test")) != null) { Console.WriteLine(sb); } But this code isn't: if ((StringBuilder sb = new StringBuilder("test")) != null) { Console.WriteLine(sb); } I found a similar question regarding a while statement. The accepted answer

Why was IEnumerable<T> made covariant in C# 4?

那年仲夏 提交于 2019-12-17 08:33:26
问题 In earlier versions of C# IEnumerable was defined like this: public interface IEnumerable<T> : IEnumerable Since C# 4 the definition is: public interface IEnumerable<out T> : IEnumerable Is it just to make the annoying casts in LINQ expressions go away? Won't this introduce the same problems like with string[] <: object[] (broken array variance) in C#? How was the addition of the covariance done from a compatibility point of view? Will earlier code still work on later versions of .NET or is

Why are empty expressions legal in C/C++?

↘锁芯ラ 提交于 2019-12-17 06:52:09
问题 int main() { int var = 0;; // Typo which compiles just fine } 回答1: This is the way C and C++ express NOP. 回答2: How else could assert(foo == bar); compile down to nothing when NDEBUG is defined? 回答3: It's obviously so we can say things like for(;;) { // stuff } Who could live without that? 回答4: I'm no language designer, but the answer I'd give is "why not?" From the language design perspective, one wants the rules (i.e. the grammar) to be as simple as possible. Not to mention that "empty

Why are slice and range upper-bound exclusive?

懵懂的女人 提交于 2019-12-17 04:30:16
问题 Disclaimer: I am not asking if the upper-bound stop argument of slice() and range() is exclusive or how to use these functions. Calls to the range and slice functions, as well as the slice notation [start:stop] all refer to sets of integers. range([start], stop[, step]) slice([start], stop[, step]) In all these, the stop integer is excluded. I am wondering why the language is designed this way. Is it to make stop equal to the number of elements in the represented integer set when start equals

Why are private fields private to the type, not the instance?

余生长醉 提交于 2019-12-17 03:23:06
问题 In C# (and many other languages) it's perfectly legitimate to access private fields of other instances of the same type. For example: public class Foo { private bool aBool; public void DoBar(Foo anotherFoo) { if (anotherFoo.aBool) ... } } As the C# specification (sections 3.5.1, 3.5.2) states access to private fields is on a type, not an instance. I've been discussing this with a colleague and we're trying to come up with a reason why it works like this (rather than restricting access to the

Why don't languages raise errors on integer overflow by default?

烈酒焚心 提交于 2019-12-17 03:22:26
问题 In several modern programming languages (including C++, Java, and C#), the language allows integer overflow to occur at runtime without raising any kind of error condition. For example, consider this (contrived) C# method, which does not account for the possibility of overflow/underflow. (For brevity, the method also doesn't handle the case where the specified list is a null reference.) //Returns the sum of the values in the specified list. private static int sumList(List<int> list) { int sum

Why function template cannot be partially specialized?

最后都变了- 提交于 2019-12-17 03:03:01
问题 I know the language specification forbids partial specialization of function template. I would like to know the rationale why it forbids it? Are they not useful? template<typename T, typename U> void f() {} //allowed! template<> void f<int, char>() {} //allowed! template<typename T> void f<char, T>() {} //not allowed! template<typename T> void f<T, int>() {} //not allowed! 回答1: AFAIK that's changed in C++0x. I guess it was just an oversight (considering that you can always get the partial

Why function template cannot be partially specialized?

混江龙づ霸主 提交于 2019-12-17 03:02:47
问题 I know the language specification forbids partial specialization of function template. I would like to know the rationale why it forbids it? Are they not useful? template<typename T, typename U> void f() {} //allowed! template<> void f<int, char>() {} //allowed! template<typename T> void f<char, T>() {} //not allowed! template<typename T> void f<T, int>() {} //not allowed! 回答1: AFAIK that's changed in C++0x. I guess it was just an oversight (considering that you can always get the partial