language-design

Why not a memberinfo() reflection function for C# [duplicate]

微笑、不失礼 提交于 2019-12-01 17:45:11
This question already has an answer here: Why is there not a `fieldof` or `methodof` operator in C#? [closed] 4 answers There is sizeof() and typeof() , but why not a memberinfo() returning an instance of System.Reflection.MemberInfo for the part of code selected in order to aid in reflection code. Example: Program() { Type t = typeof(Foo); Foo foo = new Foo(); PropertyInfo pi = memberinfo(Foo.Name) as PropertyInfo; // or shall it be like this // PropertyInfo pi = memberinfo(foo.Name) as PropertyInfo; string name = pi.GetValue(foo, null); } I am trying to understand if there is a fundamental

Why does Ruby use its own syntax for safe navigation operator?

て烟熏妆下的殇ゞ 提交于 2019-12-01 16:24:12
Ruby 2.3.0 introduces the safe navigation syntax that eases the nil handling of chained method calls by introducing a new operator that only calls the method if value of previous statement is not nil . This is a feature that already exists for example in C#, Groovy and Swift. For example in Groovy , the syntax is foo?.bar which basically means that the result value is that of foo.bar unless foo is null , in which case the return value is also null and thus no exception is thrown. Also C# (called null-conditional operators ) and Swift (called optional-chaining expression ) use this notation. So

Why does Ruby use its own syntax for safe navigation operator?

本小妞迷上赌 提交于 2019-12-01 15:57:22
问题 Ruby 2.3.0 introduces the safe navigation syntax that eases the nil handling of chained method calls by introducing a new operator that only calls the method if value of previous statement is not nil . This is a feature that already exists for example in C#, Groovy and Swift. For example in Groovy, the syntax is foo?.bar which basically means that the result value is that of foo.bar unless foo is null , in which case the return value is also null and thus no exception is thrown. Also C#

Is there a limit to the length of identifier names in C++?

心不动则不痛 提交于 2019-12-01 15:15:29
Is there a length limit to the names of variables in C++? What is it? Does this have anything to do with the "64/32-bitness" of the machine? EDIT: Specifically, what is GCC's limit? section lex.name of the C++ standard says An identifier is an arbitrarily long sequence of letters and digits. However, variable names which share a very large number of initial characters may not be treated as separate variables, the exact number of initial characters used is implementation-specific. Annex B says: Because computers are finite, C++ implementations are inevitably limited in the size of the programs

What is the rationale in allowing `?` to be escaped?

江枫思渺然 提交于 2019-12-01 15:09:25
From 2.13.2/3 The double quote " and the question mark ? , can be represented as themselves or by the escape sequences \" and \? [...]. Simply put, the following: char x = '\?'; //or '\"' char y = '?'; //or '"' represent the same character. Why treat these two (especially ? ) differently than other characters? Steve Jessop \" gives consistency between single-quoted character literals and double-quoted string literals (they're defined to use the same escape sequences, as a result \' and \" can be used in both). I'm slightly guessing, but I reckon the committee just figured it was too much

Why is short-circuiting not the default behavior in VB?

情到浓时终转凉″ 提交于 2019-12-01 15:06:19
VB has operators AndAlso and OrElse , that perform short-circuiting logical conjunction. Why is this not the default behavior of And and Or expressions since short-circuiting is useful in every case. Strangely, this is contrary to most languages where && and || perform short-circuiting. Because the VB team had to maintain backward-compatibility with older code (and programmers!) If short-circuiting was the default behavior, bitwise operations would get incorrectly interpreted by the compiler. Why did we introduce AndAlso and OrElse? by Panopticon Central Our first thought was that logical

What is the rationale in allowing `?` to be escaped?

安稳与你 提交于 2019-12-01 14:51:14
问题 From 2.13.2/3 The double quote " and the question mark ? , can be represented as themselves or by the escape sequences \" and \? [...]. Simply put, the following: char x = '\?'; //or '\"' char y = '?'; //or '"' represent the same character. Why treat these two (especially ? ) differently than other characters? 回答1: \" gives consistency between single-quoted character literals and double-quoted string literals (they're defined to use the same escape sequences, as a result \' and \" can be used

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

梦想的初衷 提交于 2019-12-01 14:48:10
With the following code: case "GETSITES": case "SITESETUP": MessageBox.Show("Help! I am suffering from the Air-Conditioned Nightmare!!!"); // ... Will the MessageBox.Show be executed either the switch value is "GETSITES" or "SITESETUP" ? Or only if the switch value is "SITESETUP" ? Since "GETSITES" has no break, I'm thinking yes, but am not sure. UPDATE I guess the way I should have worded my question as: Are these two code fragments semantically equivalent? fragment 1 case 0: case 1: // bla bla bla; break; fragment 2(pseudo code) case 0, 1: // bla bla bla; break; What you are describing is

Why are arrays covariant but generics are invariant?

懵懂的女人 提交于 2019-12-01 14:47:54
From Effective Java by Joshua Bloch, Arrays differ from generic type in two important ways. First arrays are covariant. Generics are invariant. Covariant simply means if X is subtype of Y then X[] will also be sub type of Y[]. Arrays are covariant As string is subtype of Object So String[] is subtype of Object[] Invariant simply means irrespective of X being subtype of Y or not , List<X> will not be subType of List<Y>. My question is why the decision to make arrays covariant in Java? There are other SO posts such as Why are Arrays invariant, but Lists covariant? , but they seem to be focussed

Is there a limit to the length of identifier names in C++?

可紊 提交于 2019-12-01 14:16:49
问题 Is there a length limit to the names of variables in C++? What is it? Does this have anything to do with the "64/32-bitness" of the machine? EDIT: Specifically, what is GCC's limit? 回答1: section lex.name of the C++ standard says An identifier is an arbitrarily long sequence of letters and digits. However, variable names which share a very large number of initial characters may not be treated as separate variables, the exact number of initial characters used is implementation-specific. Annex B