language-features

How-to: short-circuiting inverted ternary operator implemented in, e.g. C#? Does it matter?

感情迁移 提交于 2019-12-05 18:49:44
Suppose you are using the ternary operator, or the null coalescing operator, or nested if-else statements to choose assignment to an object. Now suppose that within the conditional statement, you have the evaluation of an expensive or volatile operation, requiring that you put the result into a temporary variable, capturing its state, so that it can be compared, and then potentially assigned. How would a language, such as C#, for consideration, implement a new logic operator to handle this case? Should it? Are there existing ways to handle this case in C#? Other languages? Some cases of

Is there a valid usecase for redefining “define” in scheme/racket?

和自甴很熟 提交于 2019-12-05 12:55:17
I'm playing around with racket/scheme and it allows me to redefine for instance define and bind it as a value. > (define define 2) > define 2 In that scope I can no longer define anything using define since it is obviously bound to 2. This works for all "keywords" I tried it with ( if , cond etc.). However it is not possible to use define to specify my own definition function: > (define mydef define) stdin::14: define: not allowed in an expression context in: define === context === /usr/share/racket/collects/racket/private/norm-define.rkt:8:4: normalize-definition /usr/share/racket/collects

c# switch statement more limited than vb.net 'case' [closed]

风流意气都作罢 提交于 2019-12-05 12:10:55
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . I was reading an interesting article here and it made an interesting point about the 'case' statement in vb.net vs the 'switch'

Is it bad practice to use C features in C++?

纵饮孤独 提交于 2019-12-05 10:19:37
问题 For example printf instead of cout , scanf instead of cin , using #define macros, etc? 回答1: I wouldn't say bad as it will depend on the personal choice. My policy is when there is a type-safe alternatives is available in C++, use them as it will reduce the errors in the code. 回答2: It depends on which features. Using define macros in C++ is strongly frowned upon, and for a good reason. You can almost always replace a use of a define macro with something more maintainable and safe in C++

Smalltalk runtime features absent on Objective-C?

ⅰ亾dé卋堺 提交于 2019-12-05 09:16:56
I don't know well Smalltalk, but I know some Objective-C. And I'm interested a lot in Smalltalk. Their syntax are a lot different, but essential runtime structures (that means features) are very similar. And runtime features are supported by runtime. I thought two languages are very similar in that meaning, but there are many features on Smalltalk that absent on Objective-C runtime. For an example, thisContext that manipulates call-stack. Or non-local return that unwinds block execution. The block s. It was only on Smalltalk, anyway now it's implemented on Objective-C too. Because I'm not

ConditionalAttribute and other special classes

若如初见. 提交于 2019-12-05 07:54:03
The ConditionalAttribute can be used to remove calls to a marked method depending on the compiler symbols defined. I'm assuming we could not create this class ourselves since the compiler must be looking for it specifically. I was wondering what other classes there are that the compiler, or language uses that we could not code ourselves. The compiler looks for [ExtensionAttribute] to indicate extension methods (and classes containing extension methods). [DynamicAttribute] is used to indicate that a member should be treated as type dynamic (even though the member type itself will just be object

Structure tag and name, why does a local variable declared as name compile?

随声附和 提交于 2019-12-05 05:19:22
In some code I saw recently there was a structure defined like this: typedef struct tagMyStruct { int numberOne; int numberTwo; } MYSTRUCT; The way I understand this, tagMyStruct is the new data type and MYSTRUCT is a variable that is created right there. At another place, this was used like this: MYSTRUCT *pStruct = new MYSTRUCT; and it compiled fine with Visual Studio 2010. How is that valid C++? I thought MYSTRUCT was a variable and not a type? No. tagMyStruct is the name of the struct. In C, unlike C++, you must explicitly use the struct keyword every time you use the struct type. For

How does the C++ runtime determine the type of a thrown exception?

徘徊边缘 提交于 2019-12-05 05:02:11
If I do the following, how does the runtime determine the type of the thrown exception? Does it use RTTI for that? try { dostuff(); // throws something } catch(int e) { // .. } catch (const char * e) { // .. } catch (const myexceptiontype * e) { // .. } catch (myexceptiontype e) // is this the same as the previous handler? { // .. } See also: How is the C++ exception handling runtime implemented? Unlike the concerns asked in that other questions, the answer to this question can be answered entirely by means of the Standard. Here are the rules A handler is a match for an exception object of

C# - Indexer at its meaningful application

限于喜欢 提交于 2019-12-05 04:25:39
I understand indexer enable us to access a collection within a class as though the class itself were an array . Suppose we are developing a project ,where do we fit the real pratical usage (example may help) of such indexers ? Look to almost any of the collection classes in the framework for an example. If I were to retrieve an item from a hashtable in a language without an indexer, I would use syntax like: object o = table.getvalue(key); where an indexer allows somewhat simpler syntax: object o = table[key]; (purposely ignoring typing/generics issues in the samples above) You have several

Call/Return feature of classic C++(C with Classes), what modern languages have it?

依然范特西╮ 提交于 2019-12-05 02:29:59
On page 57 of The Design and Evolution of C++ , Dr. Stroustrup talks about a feature that was initially part of C with Classes, but it isn't part of modern C++(standard C++). The feature is called call/return . This is an example: class myclass { call() { /* do something before each call to a function. */ } return() { /* do something else after each call to a function. */ } ... }; I find this feature very interesting. Does any modern language have this particular feature? The modern C++ equivalent would be a sentry object: construct it at the beginning of a function, with its constructor