language-features

What are the differences between “generic” types in C++ and Java?

大城市里の小女人 提交于 2019-11-26 15:36:14
Java has generics and C++ provides a very strong programming model with template s. So then, what is the difference between C++ and Java generics? Alexandru Nedelcu There is a big difference between them. In C++ you don't have to specify a class or an interface for the generic type. That's why you can create truly generic functions and classes, with the caveat of a looser typing. template <typename T> T sum(T a, T b) { return a + b; } The method above adds two objects of the same type, and can be used for any type T that has the "+" operator available. In Java you have to specify a type if you

What is the tilde (~) in the enum definition?

心不动则不痛 提交于 2019-11-26 15:00:28
I'm always surprised that even after using C# for all this time now, I still manage to find things I didn't know about... I've tried searching the internet for this, but using the "~" in a search isn't working for me so well and I didn't find anything on MSDN either (not to say it isn't there) I saw this snippet of code recently, what does the tilde(~) mean? /// <summary> /// Enumerates the ways a customer may purchase goods. /// </summary> [Flags] public enum PurchaseMethod { All = ~0, None = 0, Cash = 1, Check = 2, CreditCard = 4 } I was a little surprised to see it so I tried to compile it,

Hidden Features of F#

橙三吉。 提交于 2019-11-26 15:00:26
问题 This is the unabashed attempt of a similar C# question. So what are your favorite F# hidden (or not) features? Most of the features I've used so far aren't exactly hidden but have been quite refreshing. Like how trivial it is to overload operators compared to say C# or VB.NET. And Async<T> has helped me shave off some real ugly code. I'm quite new to the language still so it'd be great to learn what other features are being used in the wild. 回答1: User defined numeric literals can be defined

What is the difference between “new Number(…)” and “Number(…)” in JavaScript?

自闭症网瘾萝莉.ら 提交于 2019-11-26 14:48:45
问题 In Javascript, one of the reliable ways to convert a string to a number is the Number constructor: var x = Number('09'); // 9, because it defaults to decimal Inspired by this question, I started wondering — what is the difference between the above and: var x =new Number('09'); Number certainly looks better, but it seems like a slightly inappropriate use of a constructor. Are there any side effects or any difference to using it without the new ? If there is no difference, why not, and what is

Python intern for non-strings

泄露秘密 提交于 2019-11-26 14:45:16
问题 Why is Python's intern built-in only for strings? It should be possible to extend intern to classes that are hashable and comparable, right? 回答1: The purpose of interning things is to be able to compare them by comparing their memory address; you ensure that you never create two objects with the same value (when the program requests the creation of a second object with the same value as an existing object, it instead receives a reference to the pre-existing object). This requires that the

Why doesn&#39;t a python dict.update() return the object?

六眼飞鱼酱① 提交于 2019-11-26 14:24:44
I 'm trying to do : award_dict = { "url" : "http://facebook.com", "imageurl" : "http://farm4.static.flickr.com/3431/3939267074_feb9eb19b1_o.png", "count" : 1, } def award(name, count, points, desc_string, my_size, parent) : if my_size > count : a = { "name" : name, "description" : desc_string % count, "points" : points, "parent_award" : parent, } a.update(award_dict) return self.add_award(a, siteAlias, alias).award But if felt really cumbersome in the function, and I would have rather done : return self.add_award({ "name" : name, "description" : desc_string % count, "points" : points, "parent

Why C# doesn&#39;t implement indexed properties?

混江龙づ霸主 提交于 2019-11-26 14:20:08
I know, I know... Eric Lippert's answer to this kind of question is usually something like " because it wasn't worth the cost of designing, implementing, testing and documenting it ". But still, I'd like a better explanation... I was reading this blog post about new C# 4 features , and in the section about COM Interop, the following part caught my attention : By the way, this code uses one more new feature: indexed properties (take a closer look at those square brackets after Range.) But this feature is available only for COM interop; you cannot create your own indexed properties in C# 4.0 .

Hidden features of HTML

青春壹個敷衍的年華 提交于 2019-11-26 13:54:32
HTML being the most widely used language (at least as a markup language) has not gotten its due credit. Considering that it has been around for so many years, things like the FORM / INPUT controls have still remained same with no new controls added. So at least from the existing features, do you know any features that are not well known but very useful. Of course, this question is along the lines of: Hidden Features of JavaScript Hidden Features of CSS Hidden Features of C# Hidden Features of VB.NET Hidden Features of Java Hidden Features of classic ASP Hidden Features of ASP.NET Hidden

What is a maximum number of arguments in a Python function?

自作多情 提交于 2019-11-26 13:09:28
问题 It\'s somewhat common knowledge that Python functions can have a maximum of 256 arguments. What I\'m curious to know is if this limit applies to *args and **kwargs when they\'re unrolled in the following manner: items = [1,2,3,4,5,6] def do_something(*items): pass I ask because, hypothetically, there might be cases where a list larger than 256 items gets unrolled as a set of *args or **kwargs . 回答1: WFM >>> fstr = 'def f(%s): pass' % (', '.join(['arg%d' % i for i in range(5000)])) >>> exec

The written versions of the logical operators

不打扰是莪最后的温柔 提交于 2019-11-26 13:06:00
This is the only place I've ever seen and , or and not listed as actual operators in C++. When I wrote up a test program in NetBeans, I got the red underlining as if there was a syntax error and figured the website was wrong, but it is NetBeans which is wrong because it compiled and ran as expected. I can see ! being favored over not but the readability of and && or seems greater than their grammatical brothers. Why do these versions of the logical operators exist and why does seemingly no one use it? Is this truly valid C++ or some sort of compatibility with C that was included with the