Anders and the Future of C#
This is a REALLY long post. Was a great session and will be worth watching the video when it’s available.
December marks the 10th anniversary of the inception of C#.
Today looking at C# 4.0 and a little bit @ C# 5.0
3 Key Trends Shaping the Industry
The Uber trend is language paradigms borrowing from each other.
E.g. C# 3.0 borrowed a lot from functional langauges
The whole What vs How
Impertivie is VERy explicit which means that it is much harder to optimize.
Contrast this with the more declarative model like LINQ- easier to optimize. Easier to parallelize.
Dynamic vs Static
Lots of religious debates about each.
Both have good qualities. Want attributes of both.
Going to see lots of languages borrowing from each other
The elephant in the room!
Hard to do. Moores law has allowed us to stick our head in the sand for decades.
Moores law, in terms of clock speed, has stopped. We are still getting higher transistor densities, instead we get more CPUs. The onus now falls on us as to what to do with them.
No Silver bullet… no /parallel switch on the compiler
Current models of concurrency typically focus on coarse grained concurrency- multiple logical things on fewer CPUs than you have things to do. We are not getting to the flip side- taking single logical things and running them, splitting them, onto multiple CPUs. Language designers need to find ways for users to be able to do this.
There are interim solutions such as the ParralellFor statement that takes upper and lower bound and a lambda in the Parallel Toolkit for .NET
C# vs VB
Blah blah.. usual tree huggy waffle.
Says co-evolution will continue to be the model.
Goal remains to be to maintain feature parity… The goal is such that if one language has a feature it should not be impossible to do something similar in the other language.
E.g. Given is XML Literals in VB vs LINQ to XML in C#
C# 4, Key theme Dynamic Programming
In the broadest sense of the word. i.e. talking to anything that is not a statically typed .NET class.
- Dynamically types objects
- Optional and names parameters
- Co-Contra variance
- Better COM interop
Dynamically typed objects
Talking a it about IronPython and the DLR.
DLR designed to unify Dynamic languages in the same way CLR does for static languages
In 4.0 C# and VB will support the DLR explicitly.
Under the DLR there are binders for other things.
Calculator calc = GetCalculator();
int sum = cal.Add(10,5);
object calc = GetCalculator();
Type calcTye = GetType(calc);
//Reflection blah blah
C# 4 introduces a new static type… of dynamic
int sum = calc.Add(20,10);
In above, .Add returns dynamic result. = sign performs a dynamic conversion.
This all comes down to should it be painful to talk to dynamic ‘stuff’ outside of C#?
Or should it be easy?
When operands are dynamic we defer member selection to run time- e.g. can do dynamic overloads
2nd demo showed C# calling out to Python. Showed a calculator example and the ability to basically pass ANYTHING into the calc.Add method. Remembering that Python being dynamic can work on any variable type that has an operator ‘+’ defined for it.
Back to Slides
All the above is based around the idea of IDynamicObject.
Out of the box support for COM and .NET. Dynamic languages can implement the IDynamicObject interface themselves too or indeed you can implement it yourself in your own .NET code. E.g. you want to implement your own property bag with dynamic properties.
dynamic propBag = new MyPropBag();
propBag.whatever = 12345;
dynamic x = propBag.whatever;
So DLR can dispatch to a statically typed object to… e.g could pas in an anonymous type to a dynamic method.
Optional and named parameters
The usual pattern in .NET is to =declare a primary overload that has all parameters.
Then declare a BUNCH of overloads that call with default.
Can now just define defaults on optional parameters.
Supported in .NET since the beginning and of course supported in VB. Anders says ‘he relented’.
Can now also pass in names arguments (after positional arguments). No compiler overhead.
Can do funky things such as putting named arguments out of order…. which in urn can be a bit intersting if you hav side effects.
Anders mea culpa.
Basically trying to combat the HUGe issue with Office still being COM. and as such interop being poor.
No more file.SaveAs(fileName, ref missing, ref missing…….) crap.
Key new features
Option and named parameters and dynamic types lets us write code as it’s meant to look.
Can now omit ref in many cases
Supports an automatic mapping of type object to type dynamic when you import COM objects.
Compiler able to embed interoperability types- no need for PIA any more.
Did a demo showing all the fun ‘oddities’ of COM interop.. i.e. some fugly code and how it now becomes nice looking code.
Co- and Contra-Variance
This is a bit geeky for me :-( Me non Grok it.
Something to do with the ability to pass less derived array types.
Says co-variant arrays in .NET are not safe.
Supports safe co and contra variance.
Ability to safely support less derived objects as more derived when using read only interfaces.
C# 5.0 Initial Thoughts
Talking a it about meta-programming.
Lots of use of code gen. Reflection.emit.
Need a modern approach to meta programming.
E.g. what is driving the success of Ruby on Rails is that it’s a good meta programming language.
A focus for beyond 4.0 is to actually write the C# compiler in managed code.
Initially compiler written in C++. It’s a classical compiler.
Want to provide ability to allow people to hook into the compiler. Will end up with a true object model for C# source code. Compiler will no longer just be a black box.
So with compiler in managed code we can now call it as an API. Pass in a string, return some cobiled binaries, walk these with reflection.
Anders wrote a simple C# evaluator.
First Evals a simple for loop.
Next Evals to create a delegate, Evals a refernece to that delegate then statically calls through the delegate from the main block of code. You kinda gotta watch the video
Well worth scolling to this bit of the video to bend your brain!
he uses dynamically evaluation to new up a windows form and then adds buttons and event handlers and things… Incrementally bui8lding the application… this is gonna be such cool tech to work with…. Like just VERY cool.
We *may* get this out of band. i.e. before .NET 5.0
Anders on stage.
An Anders session is always a popular session. No one wants to be presenting against Anders (same time slot). Like my cheap arse panorama
Goodies Gone Burger
PDC08|Monday, October 27, 2008 10:36:36 PM UTC||