A couple of weeks ago on "This Week On Channel 9" series there was a reference to Mads Torgensen's presentation in London regarding "The Future of C#", announcing new features that could probably get implemented in C# 6.0.
So, in this post, let me explain some of the features that I hope they implement in C# in the short/middlle run:
1. LLVM-Like Compiler For Native Code
I talked about this many times, but I think it's a perfect time to mention it again.
So far, if you want to compile MSIL to native code at once, you can use a tool called NGen, which creates a native image of the code for the machine where compilation is being done. The problem with this tool is that its main purpose is to reduce startup times. Meaning? You won't get optimized bits for the whole code; just for the blocks first executed when the program starts.
Imho, we need more ... in this relatively new world of app marketplaces it'd be really handy to count on a model where you can deliver optimized native bits to the device/console/machine where the app would be downloaded and installed, don't you think?
Picture it like this: say you create an app/game with C# for the XBox One (using portable assemblies or not) and compile your source code to MSIL. Since the hardware of the console is the same in terms of main components (processor, memory, etc.) then why not compiling the whole MSIL code to native bits optimized for the Xbone console at once? (either on your side or on MSFT servers' one)
With a LLVM-Like compiler this could be achieved and extended to other platforms. But wait a minute! Isn't it what MSFT is doing for WP8? It sounds like it. But wait a minute, again! Isn't it something like the AOT compilation that can be found in the Mono Framework? If the latter gives optimized bits for whole assemblies per platform then it is!
In fact, many sources mentioned the so colled "Project N", which would be used to speed up Windows 8.1 apps. What is more, a few sources also mention that MSFT is working in a common compiler for C++/C#. I hope it also brings a more performing way to do interop stuff with C++.
True or not, this is a "must have" in order to end the C++/C# perf discussion!
2. "Single Instruction, Multiple Data" (SIMD)
In modern HW architecture, SIMD has become a standard when you want to boost performance in specific operations, in particular, ("vectorized") mathematic ones.
As a mather of fact, C++ counts with DirectXMath libraries (based on the formerly called XnaMath ones) which do implement SIMD, but unfortunately do not support C#.
Again, SIMD is already present in the Mono Framework for Math operations, so why not adding it to the .NET Framework once and for all?
I hope MSFT listen to us ...
3. Extension Properties
We have extension methods, so this is a corolary of it. Today, you can only implement getters (and setters) like this:
public static string NameToDisplayGetter (this IPerson person)
{
...
}
Then, why not having something like this?
public static string NameToDisplay: IPerson person
{
get { ... } // You could also add a setter, if needed.
}
Of course that the syntax in the example above may vary, so I guess you get the idea here. There are several use cases where a feature like this could come handy, including MVVM or plain INotifyPropertyChanged ones.
4. Generic Enum Contraints
Generics is one of my favorite .NET features. There's lot of things that can be achieved through it but it has still room for improvement.
One of the things to improve are constraints. So far, when dealing with types of software elements, we have only two: class and struct. So, what about enums?
Currently, if you want to mimic an enum constraint you will have to write something like ...
public void OperationX
where TEnum: struct, IComparable, IFormattable, IConvertible ... and so on so forth.
{
… usual stuff …
}
... and also, given that you are dealing with a subset of elements that approximate to an enum, you need to check whether and enum has been passed, generally throwing an exception if not:
if (!typeof(TEnum).IsEnum)
{
throw new ArgumentException("The passed type is not an enum");
}
Why not simplify it to something like this?
public void OperationX
where TEnum: enum
{
… usual stuff …
}
Not only it makes sense, but also would simplify things a lot as well as open a wide range of handy operations and extension methods.
5. NumericValueType Base Class
.NET's CLR treats structs in a special way, even though they have a base class: ValueType.
I'll not be explaining here the characteristics of built-in primitives and structs; instead, I'll ask the following question: in the current version of C# can we achieve something like this ...?
TNumeric Calculate(TNumeric number1, TNumeric number2, TNumeric number3)
where TNumeric: struct
{
return number1 + number2 * number3;
}
The answer: not without proper casts. So, a life changer for this type of situations would be to add a specification of ValueType that enjoys the benefits of structs and also supports basic math operations without any kind of wizardry: NumericValueType.
With that class and a new reserved word like, say, "numeric", "number" or "primitive", we could write generic operations and extension methods with a syntax as simplier as:
TNumeric Calculate(TNumeric number1, TNumeric number2, TNumeric number3)
where TNumeric: numeric
{
return number1 + number2 * number3;
}
How about declaring new type of numbers? Easy ...
public numeric Percentage
{
… usual stuff …
}
... or ...
public numeric Half
{
… usual stuff …
}
No need to specify "struct" since "numeric" would be a value type that supports basic math operations (that we would need to implement when we declare the type, maybe, by overriding some base operations), and so in common scenarios there would be no need to cast values to do math.
6. Declaration of Static Operations On Interfaces
Put simply: having the possibility of declaring static operations when writing interfaces; like this:
public interface IMyInterface
{
static void DoStaticOp();
static bool IsThisTrue { get; }
... instance properties and operations ...
}
This presents a challenge to both, polymorphic rules and abstract declarations, that is, at a "static" level. But as usual, with the proper rationale and care when modifying the language specs, it could be achieved. Maybe, many of you would be asking: "why bother?" but believe me when I say that I happened to meet situations where static operations on intefaces would have come really handy.
Well, this is it for today. What would you guys like to see implemented in C# 6 and beyond? Comments are welcome.
See ya when I get back,
~Pete
Seems like better generic constraints would fix your NumberBaseType problem too.
ReplyDeleteTNumeric Calculate(TNumeric number1, TNumeric number2, TNumeric number3)
where TNumeric: op+
{
return number1 + number2 * number3;
}
Basically, declare that TNumeric implements the + operator. I think generic constraints should be broadened aggressively. It would be amazing to be able to constrain a type to one that has a ctor with 2 arguments of a specified type, and so on and so forth. All of this is statically available, but goes a long way to making C# a more dynamic-feeling language without losing any type safety.