Oren Eini

CEO of RavenDB

a NoSQL Open Source Document Database

Get in touch with me:

oren@ravendb.net +972 52-548-6969

Posts: 7,565
|
Comments: 51,184
Privacy Policy · Terms
filter by tags archive
time to read 1 min | 185 words

This is purely hypotetical, but imagine the following way to show the user's details:

User user = ... ;//Get user something

var view = new View( user, 
 new Text(u => u.Name,  s => user.Name = s),
 new Password( u => u.Password,  s => user.Password = s ),
 new List(from g in user.Groups select g,  g => user.Groups.Add(g), g => user.Groups.Remove(g)) );
view.Show();

Where View is a class which display view items in a TableLayout. And each item knows how to handle changes. This could be a very rapid way to create UI.

What do you think?

time to read 1 min | 117 words

Just thinking, I think that Microsoft was the first* that made deep integration between the language constructs mix with the framework it live on. I'm talking about such things as using and IDisposable, where you had a language construct that you can control and add to.

This theme continue with the Linq infra structure, it's not ADO.Net build into the language, it's simply macro transformation that is open for the developer to extend with his own code. I'm familiar with the concept from Boo, and it's a very powerful one.

* I'm not a language researcher, but I would love to know about similar schemes.

time to read 5 min | 878 words

After getting so excited about the things that are going to be in C# 3, let's see the things that I would like to see coming:

Extentions method for properties and operators - Operators are super important because this would be the only way to add operators to interfaces. Extentions properties are a must have if we have extentions methods. The spec mentions that they are considered, I certainly think that they should go in.

Compiler time reflection information - By that I mean a way to explicitly get reflection information where it's known. For instance, I may want to pass a property info to a method, right now I've to use strings to do it. The things that I would like to see are: methodInfo(), propertyInfo(), eventInfo(), fieldInfo(), parameterInfo(). All of them as language constructs exactly like typeof(), I can see that this would be a problem with overloaded methods, though.

The anonymous type is depedant on the properties orderring - Does this makes sense?

(new { Str = "a", Int = 1}).GetType() != (new { Int = 1, Str = "a"}).GetType();

This is just silly. What is the reason you would want  two types with the same number / type of properties to be different? This is inconsistent with the rest of the language.

Type inferencing beyond the local scope - I want to be to do the following:

public var Method() { return "something"; } 

This have a lot of implications, but it's a great productivity saver and it allows a lot of nice tricks. Some of the problems I can think of with this is what happens when you've anonymous type as a return type:

public var Method() { return new { Str = "Something" }; } 
  • What is the return type of the method?
  • How would I know from outside the method?
  • How would I know what the type is outside of the assembly?

I can see two possible way to do that:

  • Allow only static types in non-internal API, this is the best choice, I think.
  • Only allow var return types for internal methods, this is not a favoriate of mine.

In both cases, I think that it's important to allow anonymous types as a return type from a method inside a single assembly. It's a very good way to move a lot of information from one method to the other in a type safe way.

Another problem is accidently changing the return type of a method with var return type. I think that this would be cought very quickly, as it would cause immediate breakage in other places, so it's not such a big problem.

One thing to consider is situation like this:

public var Method(int i) 

      if (i%2==0) 
            return new object(); 
      else 
            return "something"

What would be the return type of the method?

  • error if they are not exactly the same type.
  • revert to the most basic type.

This leads me to another question, why are all the type inferencing insisting on keeping the same type?

var array = new []{1,"something"null}; 

The array should be object[] , and the above should not cause an error. The idea is to go to the common base class (or object, of course), and it's been successfully implemented in other languages.

Innovation

time to read 2 min | 277 words

I'm still digesting all the new things that we're going to get in C# 3. I've already expressed my joy about the things that this would allow me to do.

I think that those changes should shut anyone who claims that Microsoft cannot innovate. Yes, some of the new features have been there in other langauges before, but I've never seen a main stream language that make such a move. The interesting thing is that while Linq will probably get all the attention, it's the foundations that makes it so interesting, since you can use it outside of Linq.

I'm simply amazed that the next next version has such good features even before the next version has been released. It makes me wonder what goodies we can expect toward beta 1.

P.S,

I'm currently seeing the Office 12 movie from channel 9, and it's just an amazing UI.
I now understand why we've Office 2003 UI in the .Net framework, because Office 12 UI is so different.
But the amount of mail that she is getting in the movie is simply staggering.

C# 3, so cool!

time to read 12 min | 2273 words

Just look at C# 3 future, it's so pretty I want it now. Check out the videos that they have there. There is so much good stuff there, and it's only the start.  First of all, read the C# 3.0 spec [doc file], or just read the rest of the post to see the highlights.

Type inferencing and implicitly typed arrays- I got addicted to that when using Boo, it's something so simple, but it saves so much. In most cases, it means no more casting hell. Andres let it slip in a presentation about a year ago, in essence, it means:

var sum = 0;
var intArray = new [] { 1,2,3,4};
foreach ( var i in intArray )
{
    sum += i;
}

This is a very cool idea, especially since you can use it for the foreach construct, which should make for far better code. Did you notice how I declared the array? "new []", without a type, the compiler just fix it up automatically.

Extensions methods - This is a feature that lets you add new instance methods to a class, without touching the class code and even if the class is already compiled. The idea opens up so many possibilities that it's just staggering. Fowler talked about it in Refactoring. Other languages, such as SmallTalk and Ruby already has it (SmallTalk has it for 30 years, I think), and it has prove to be a useful feature. You could add methods to System.Object, which would then be "inherited" by any object. Consider adding ToXmlString() for all objects, for instance. Or persistence to database. And no, it's not just syntactic sugar, those things are important. This mean that you're no longer at the mercy of the class designer. It's a truly powerful tool. The nicest thing about it, I think, is that it'll work with generics, so you get a host of goodness for very little effort. Here is the example from the spec:

public static class Sequence{
            public static IEnumerable<S> Select<T,S>(
                        this IEnumerable<T> source, Func<T,S> selector)
            {
                        foreach (T element in source) yield return selector(element);
            }

            public static IEnumerable Select<T,S>(
                        this IEnumerable source, Func<T,S> selector)
            {
                        foreach (T element in source) yield return selector(element);
            }
}

What does the above say? Well, it means that you can now use Select() on any object that implements IEnumerable. You could even use them on your 1.0 and 1.1 collections! Can you say goodness? This is cool on so many levels that it's not even funny.

Here is another nice thing that you can do:

public static class ExtermalObjectMethods
{
    public static bool IsNull(this object obj)
    {
         return obj == null;
    }
}
object nullObj = null;
if (nullObj.IsNull() )
   Console.WriteLine("Object is null");

Just how cool is that?

Extension properties, events and operators are currently considered, but not yet supported. I certainly hope that Microsoft will implement those, as they would allow a much nicer syntax for a lot of things (an IsNull property, for start). Adding operators to interfaces is another big problem that this can solve.

Lambda Expressions - Like anonymous methods, only with an even easier syntax. This is something that I don't think most .Net developers will appreciate now, but they certainly would two years from now, when the functional goodness will be fully entranced. Here is how you would extract all the names from a list of customers:

var customers = GetListOfCustomers();
string[] names = customers.Select( c => c.Name );

Can you see the beauty of it? The "c => c.Name" is the lambda expression, which is just a way to tell the compiler to do the dirty work, instead of having to do it ourselves using anonymous delegates. If it was all that they were good for, they wouldn't amount to much, but they have quite a few other usages, as you'll see shortly.

Object and collection initializers - This is something that we had for quite some time in attributes, which is now coming to both object initialization and collections initialization. The idea is actually very simple, consider the class Person:

var ayende = new Person{Name = "Ayende", Surname = "Rahien"};
var persons = new List<Person> { 
    new Person{Name = "Ayende", Surname = "Rahien"}, 
    new Person{Name = "Foo", Surname = "Bar"}
}

Just consider the shear amount of code that you would've to write in order to do the same today (or even in 2.0).

Anonymous types - No, it's not Java's anonymous classes, but it's a nice enough feature. It means that you can declare a type by just using it.

var ayende = new {Name = "ayende", WebSite = http://www.ayende.com/};
Console.WriteLine(ayende.WebSite);

This would create a new type with Name and WebSite properties. While this may save some typing in certain cases, I'm not certain that this is a useful feature. You cannot use it outside of a method's boundary, since it has no type that you can access.

This is nice, but I don't like the way it's implemented now. Boo has something similar, but since Boo has type inferencing throughout the language there it actually makes sense, since it allows to return a strongly typed object from a  method, instead of an object array. Here is the Boo example:

def MethodThatReturnSeveralArgs():
   return tuple { Int: 1, Name: "Ayende", Null: null, TimeStamp: DateTime.Now}

print MethodThatReturnSeveralArgs().Name

Since declaring variables using the "var" syntax is limited to local variables, I think this is unnecessarily limited.

Query Expressions - I expect this to be the big thing for C# 3, just as generics are the big thing for C# 2. The idea is to allow an SQL like syntax for selecting objects from a database/memory/xml directly into the language. The idea is quite involved, and I'm sure to generate a lot of discussion. The idea is to be able to do the following:

var payingCustomers = from c in db.Customers where c.Orders.Count > 0 
     select new { Email = c.Email, Name = c.Name };
foreach (var c in payingCustomers)
{
   SendDiscountOffer(c.Email, c.Name);
}

There are all sorts of ways where you can plug into this statement and do all sorts of cool things with it. db.Customers may be an in memory class, or it can redirect the query to database, to remote machine, etc. This is also apparently the use case for anonymous types, as return types from lambda expressions. It's nice, but it's not enough. It should be expanded to regular methods as well, in my opinion. I suggest checking the spec anyway, I'm just touching the edge here, and I'm sure that there are a lot of cool implementations that I'm missing here.

Expression Trees - are a way to turn lambda expressions into data. The idea is to be able to do this:

var valuedCustomers = session.Select(x => x.IsValued==true);

What is this? Well, what happened here is that we saved the lambda expression "x => x.IsValued==true" and passed it to a persistence layer, which would parse the expression, and load from the database all the rows for which the expression is true.

The important thing here is that the Select method is not using a opaque string, but code that can be type checked and verified by the compiler. Presumably refactoring tools for C# 3 will also support this, so it will make life even easier.

For the author of the Select() method, life would be much easier as well. He won't have to parse strings, but rather deal with an AST tree. From experience, I know that dealing with ASTs is not the easiest thing in the world, but it's certainly much better than dealing with strings.

Summary:

All in all, I really like the direction that C# 3 is heading for. Functional programming is not appropriate all the time, but it can save quite a bit of typing and a lot of awkward syntax. I'm very excited about the new abilities, and I think that they will present a quantum leap much bigger than 1.1 -> 2.0. Considering that those are just the language enhancements, I can hardly grasp the amount of goodness we will get in the class library and the associated tools.

I expect quite a lot of noise about query expressions (yes, they are cool). And a lot of activity in the area of lambda expressions as data, since it's a cool way to express much more than just ORM syntax.

The future is bright indeed.

P.S: It's 2:04 AM and I'm reading language specs, yes, I'm a geek.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. Production Postmortem (52):
    07 Apr 2025 - The race condition in the interlock
  2. RavenDB (13):
    02 Apr 2025 - .NET Aspire integration
  3. RavenDB 7.1 (6):
    18 Mar 2025 - One IO Ring to rule them all
  4. RavenDB 7.0 Released (4):
    07 Mar 2025 - Moving to NLog
  5. Challenge (77):
    03 Feb 2025 - Giving file system developer ulcer
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats
}