The Microsoft Corporation is in the process of developing the next major version of the C# language. This article contains information regarding four key new features, including generics, iterators, anonymous methods, and partial types.
The Microsoft Corporation is in the process of developing the next major version of the C# language. This article contains information regarding four key new features, including generics, iterators, anonymous methods, and partial types.
just need a Standard cross platform set of APIs.
Doh. A lot of these features are kinda lame. Now we’re going to have a whole bunch of C# yuppies going on about how C# also has templates, lambdas, and iterators, just like C++! Don’t get me wrong. C# is a simpler language than C++, and one of the things that make it useful is that it’s rather limited, so it’s easy to learn, and economical to maintain. However, it irks me when people try to compare C# (and Java) to C++ (like this article does at one point!) and go “it has all the power, but is easier and cleaner!” Articles like this (that make comparisons to C++ without pointing out the inherent limitations of the language) just give fodder to the yuppies.
First, let me say one nice thing about C#. The C# CLI has a built-in compiler, so you can distribute a template class as precompiled IL code, while in C++, you have to distribute the source in a header file. It reduces coupling, but note that it really doesn’t make sense in environments where the runtime has no access to a compiler.
Generics — C# generics pale in comparison to C++ templates. They’re basically good for a small subset of generic programming tasks, and nothing else. You can do “containers of T” and “generic functions on type T” and not much else. In contrast, C++ templates are a Turing complete meta-language. The syntax is kinda hairy (because it wasn’t designed to be a meta language) but the power is definately there, and stuff like the Boost MPL can abstract away most of the weirdness. As for constraints, they make sense for the limited scope of C# generics, but would have to balloon into a meta-language proper to handle the increase scope of C++ templates.
Iterators — Seems easier than defining iterators in C++, but again, doesn’t have all the power of C++ iterators. You can define multiple iterator types (constant, reverse, etc), specialize iterator types, extend iterators, etc. I don’t see anyone being able to do a whole lot of that in this iterator model.
Anonymous methods — C++ has almost proper lambdas through the Boost.Lambda and Phoenix libraries. Again, the synatx can be a rather hairy (although the Phoenix library’s syntax isn’t all that bad) but they have most of the features of “real” lambdas like higher-order functions, hardware closures, etc. Unlike the template mechanism, the syntax issues with C++ lambdas is bad enough that people are looking into adding lambdas at the compiler level for the C++ 0x standard. C# wins in this catagory, at least for now. C# anonymous methods seem to be proper language level constructs, although the syntax is a little weird from the looks of the examples. However, it doesn’t look like C# will have any of the advanced lambda features (higher order functions, closures, etc) that C++ does.
Partial Types — Simply a fix for the initial design mistake of not being able to define class methods outside the class declaration. Using it seperate out the actual declarations smacks of people who do this:
class MyClass
{
#include “my_class_functions.h”
#include “my_class_data.h”
#include “my_class_more_data.h”
…
};
That’s just plain weird, error prone, and a sign that you should split up that class into smaller associated classes. I’m not saying that you can’t do it in C++ (I just showed how) but people aren’t exactly billing “goto” as a feature of C++, are they
java ! it’s only surviving on the bookshelf. And now microshaft is trying to copy java c syntax. Pure garbage. Come up with something new. I really python catches on. Personally my fav interpereted language is euprhoria.
http://www.rapideuphoria.com Easy ,Fast programming language. Blows Javacrap and c#shit to hell.
Took a look at the license. No thanks. It has the same hooks that keep me away from Java.
interested in your comment that C# generics are incomplete, and that C++ are complete. this is the first i’ve heard of this.
do you have some examples of what can’t be done w/ C# generics, but can with C++?
I would have to suggest Eiffel.
http://smarteiffel.loria.fr
It’s a pure OO language with excellent handling of MI, constrained genericity, dead simple syntax(for instance, only one looping construct), it’s strongly typed, has proper language level(read “inheritable”) support for pre and post conditions as well as invariants(no assert.h-ish hacks), and can use C, C++, and Java code. Also important to many people, it’s quite fast(natively compiled).
euphoria is free. you pay if you want extras. So what ?
My fave: Fortran 90/95 and soon 200x. No, really. (So I’m a physicist).
Damn, dude. You like to talk alot…
The C++ template mechanism is a Turing complete meta-language. This means that you can do any calculation that you can in any other Turing-complete language (like C). While most of C++ works at runtime, you can think of the template mechanism as a interpreted (by the compiler) that works only at compile time (with constant values). This also means that it is entirely recursive, since there are no mutable iterator variables to change. For example, the following code will calculate the factorial of number.
template <int Num>
struct factorial{static const int value = Num * factorial<n-1>::value;};
template <>
struct factorial<1>{static const int value = 1;};
You can call this “function” by doing:
const int fac5 = factorial<5>::value
The basic mechanism is flexible enough that a lot of focus in modern C++ design is on using the template mechanism as a built-in code generator. For example, Phoenix uses templates to implement a lambda mechanism in C++. Spirit uses templates to impement a parser generator like yacc. Lots of templates in Boost libraries for finding the properties of types, getting the maximum size of types, etc. If you’re curious about this, take a look at the book “Modern C++ Design” by Andrei Alexandrescu. In it, takes a systematic approach to templates as a meta language, and uses them to write classes that manipulate lists of types, implement patterns like Command, Visitor, and Factories, and introduces the idea of using template parameters to encapsulate various compile-time policy choices, like whether smart-pointers check for null before dereferencing. Also check out http://www.boost.org, which at the moment is one of the only collections of STL-style modern C++ libraries.
PS> Yes, like I said, the syntax is a little hairy. But the next version of C++ will have template typedefs, so you can get rid of most of the “::value” stuff, and the Boost MPL provides template versions of “if”, “for”, etc.
Um, you have it the wrong way around. http://www.bagley.org/~doug/shootout/craps.shtml shows that the fast cool language just after C is OCaml. Completely Opensource and Free. Also, the top half of the list is dominated by either completely Opensource languages or at least Opensource compilers for less open languages.
Get Euphoria on the list and we will see if it is fast and cool.
Cheers
I like lots of languages better than C# syntactically. Ruby is my favorite scripting language, for instance. (I’ve abandoned Python for it.)
But that’s not why I use C# extensively. It’s not the language, it’s all that comes with it: the fantastic development tools and debugger, custom attributes, the huge, useful, and mostly consistent framework, the remoting and serialization infrastructure, etc.
(Got to admit I used to really enjoy C++ templates. You could do some great stuff with them.)
If not, it will show that all the Microsoft talk about standard compliance was just talk.
Yes. The specs for these features have all been given to the members of the ECMA standard committee, and a few people not on the committee. However, they haven’t been released to the public yet; that should happen within the year.
For example, Miguel de Icaza is currently implementing Iterator support in MCS, and people on the Mono team have already looked into implementing Generics support.
I’m not sure when these features will be completely standardized, nor do I know when Mono will get a full implementation of these features, but I do know that they’re being worked on in both areas.
“i’m not sure when these features will be completely standardized, nor do I know when Mono will get a full implementation of these features”
oh! probably in next 10 years! –: ))))
Rayiner – Thanks for the reply. From my reading of the Syme & Kennedy paper (ref below), I sense that you are right (sadly). The generics mechanism proposed supports Type parameters but NOT constant parameters & expressions -unless Kennedy and Syme have left something out of their paper.
Is there a reason that the current generics proposal could NOT be extended to support constant parameters as well? What I’m wondering is – there isn’t some horrible problem that prohibits doing this in the JIT, is there?
http://research.microsoft.com/projects/clrgen/generics.pdf
I wonder wether it will be usable in other contexts as well.
Basically it lets you return a value from a method while keeping its state intact. The next time you call the method, it will be in the same state that you left it in. This could give a very efficient way to do lightweight parralelism without threading. It is quite an old concept, but I can’t remember the correct name for it.
Nothing stoping it, other than a desire by the C# and Java guys to avoid complexity. C++ templates result in nothing more, at the binary level, than plain old C functions with really funky names (mangled by the compiler to include the type and the template names). It could easily be implemented in a JIT language, with the only caveat being that because of the requisite compiler trickery (the compiler has to do a *lot* of analysis to support templates likes this), instantiating a complex template could take a bit of time. However, once that initial instantiation is cached, further instantiations can proceed at full speed.
It is all very well that the c++ templates are a turing-complete meta-language, but I think that it is much too complicated for a mainstream programming language. The C# generics are a good compromise between power and simplicissity, and if you really like the c++ syntax you can just use a c++ compiler that targets MSIL.
By the way: Would it be possible to emulate the behavior of the C# (or python) yield statement using C++? I think not. So there are situations where the enhanced C# can do more than C++!
weenies 🙂
After using VS.net 2003 for four months now, and Borland tools before that, and Metroworks before that.. I can easily say that Borland’s IDE’s are simpler and more intuitive than Microsoft’s. VS.net is a headache compared to the borland tools.
I have a real beef with the msdn library… Tons of information that is horribly organized and it is honestly too time consuming to go diving through it repeatedly. Borland’s help files are very intuitive and you can easily navigate it in a logical fashion.
Of course for straight-up coding, I will take anyday.
As for C#.. It’s okay, but still immature. Lots of little things bug me.. Like arrays.. Why do some arrays use [,] for multidimensional stuff, while some use [][] and some use getters and setters. It really feels like bunches of different programming groups threw their stuff together. Little inconsistencies make a huge difference. It’s the main reason I like java better… It’s maturity makes it feel much more coherent then C#, and its docs are great.
About the array syntax: [,] denotes a single multidimensional array. For this kind of array a single region on the heap is used. [][] denotes an array of arrays, which uses many regions on the heap. The first will have much better performance when you need rectangular arrays such as in a matrix class. The second is less efficient, but it makes it possible to have jagged arrays.
C# is much more powerful than java, so it makes sense that they have more language constructs.
I agree about the docs though. I prefer borland docs to MSDN any day. And even the plain html java docs are better organized than MSDN.
I wouldn’t say that templates are complicated. Different, sure, inelegant, maybe. But not complicated. There is rather little to know about the template mechanism. There are only a few types (int, bool, class) and a few rules (partial specialization, template template parameters, recursive application, operator overloading) to memorize. Then, you’ve got a few basic conventions (typedef defines a “type” variable, enum or static const defines a numeric variable, struct defines a function body) and that’s it. Once you understand the whole recursion thing (if you don’t like that, talk with Scheme or Lisp folks sometimes it’s not all that hard. Besides, a lot of the purpose of templates is to make things easy for library writers. You’ll rarely see front-end code that uses templates, but rather templates act as a support mechanism for back end code. You can use a uber-generic, policy parameterized smart pointer just as you use a much simpler C# generic, but the template version can do a lot more cool stuff under the hood.
As for “yield” I would say two things: first, the way to do enumerators in C++ is to write an iterator. Take the second iterator example from the article. in C++, instead of the nested ‘foreach’ and the ‘yield’ statement, you’d simply do:
… snippet from C++ version of article’s “List” class …
typedef std::vector<object>::iterator iterator;
std::vector<object> elements;
iterator begin(){return elements.begin();}
iterator end(){return elements.end();}
… end snippet …
10 lines of code, versus 8 for the C# version. If you’re repeating the same work over and over for a lot of similar, but not quite the same classes, you could write a template (left as an excercise to the reader that would let you declare the delegation in only 5 lines of code. Now what about a “yield” keyword with similar semantics and usage to the C# version? Now I don’t know a whole lot about the ‘yield’ keyword in C++, but off the top of my head, there is an easy, function-object based way to implement the examples in the article with an overhead of only one line-of-code per enumerator function and one line-of-code per class.
“C# is much more powerful than java, so it makes sense that they have more language constructs.”
completely an idiotic sentence. java’s simplicity has a reason. C#’s stupid language constructs confuse people as seen here. there is nothing you can do with C# and can’t do with Java as a result, and infact, they take same time. the only difference is that, you cannot run C# programs on many platforms.
–; )
I completely agree with you regarding Borland’s development tools. They are simply the best.
I also agree with you regarding MSDN. In fact, I would go so far to say that MSDN is a worthless mass of unsearchable drivel peppered with precious few treasured nuggets of almost ledgible, quality information. It would still be okay, I suppose, if their search engine wasn’t derived from the same technology used to power Pong.
Why Microsoft thought it was a good idea to ship the wadded up, unsearchable pile of every white-paper, article, help file, or email ever written by anybody who has ever worked at Microsoft or used a Microsoft product, as their main documentation and help system for their development tools is entirely beyond me.
I would point out that while Python also uses “yield”, I don’t think I’ve seen it better implemented than in Ruby.
You better watch any recommendations of anything related to france! You anti-american! Relax, it’s a joke. Looks like a great idea. I recently read about another similar language, based on all of the objects residing in a database, and only required incremental compiling by it’s very nature. Also, replacing a class name didn’t require search and replace, the database did all the work. Does anyone happen to know what it’s called, (conviently I have forgotten.)
I don’t know about a database, but I’ve thought about(as have many others) taking some general set of features common to many languages and having an IDE internally map your code to both the original source(because it’s handy to have), and an XML file.
So you have something like(I hope this formats properly):
<class name=”Dictionary”>
<generic name=”Key” must-be-instance-of=”Hashable”/>
<generic name=”Value”/>
</class>
Which can be translated into either:
template <class Key, class Value>
class Dictionary;
Or(in Eiffel):
class DICTIONARY[K -> HASHABLE, VALUE]
end
Tuttle:
It is quite an old concept, but I can’t remember the correct name for it.
Generators?
That’s what they were called in Icon 15(?) years ago, although the concept is much more powerful there, where almost all of the control structures are based on them. I’d have to warn you though that my experience with Icon has shown that a concept that powerful can lead to powerful, subtle bugs.
On the other hand, you gotta love being able to write a back-tracking, recursive decent parser in one line of simple, readable code. I haven’t seen anything that powerful in any other language until perhaps the REBOL parse dialect, or maybe Perl 6 regular expressions (but not those of any prior version of Perl).
might be Jade you are thinking of, at http://www.discoverjade.com
Fully OO language, slightly delphi like syntax, everything lives in a database (enterprise class) and you simply create and use persistent objects the same as any other.
Very nice.
Also coding in the database is very good, change an attribute/class/reference and all the references to it can be updated automatically.
Want to see where the attribute is used, its all crossreferenced for quick lookup.
Everything is incremental compiles, just compile the method you are working on, also working in a team is easy as your are sharing a database connection rather than text files.
That’s the one! Time for more research.
Cheers!