Tweets by @Actipro
Please take some time to learn more about us and our product offerings.
We’ve been adding some more updates to the SyntaxEditor .NET Languages Add-on for WPF/Silverlight in preparation for what might be the final 2011.1 maintenance release, and some of these are really exciting.
Today I’d like to show a couple highly requested features for the add-on: automated IntelliPrompt for lambda expressions and generic method type parameter inference. That’s a mouthful, but it’s really helpful for advanced coders. Let’s see some examples of how it works.
Generic methods are C#/VB methods that have type parameters. Generic methods can be invoked in one of two ways. In the first way, you explicitly indicate the type arguments. In the second way, you don’t indicate type arguments. Instead, the compiler figures them out based on the parameters that are passed to the method.
In the screenshot above we have defined an Echo generic method that takes a T type parameter and returns a T value. Our sample implementation just returns the same value that is passed in.
Back on line 9, we declare a variable called echo that gets implicitly typed from the result of the Echo method invocation. We could have explicitly called the Echo method like: Echo<int>(1). However in the sample we chose to implicitly call it instead. Our resolver engine knows that we passed an integer literal to the method. It figures out that Echo’s T type argument is an int and thus an int is the return value. Then echo gets implicitly typed as an int, which we can see when we hover over it on the next line.
That is a simple case of how generic method type parameter inference works. Let’s get more complex:
Here we have a generic method GetFirstOrDefault which is simply executing the LINQ FirstOrDefault method on the collection and returning the result. But look at the code above it. We implicitly declare an integer list. We then call our generic method, passing in the list, and the first variable is assigned the result. Finally, we call Console.WriteLine and pass in first.
You can see this scenario is very complex and the resolver handles it perfectly. It figures out that first is an int and shows the proper Console.WriteLine overload that takes a single int.
Let’s check out new lambda expression features. Lambda expressions are anonymous functions that can contain expressions and statements. Here’s a quick sample:
In the screenshot above, we’ve declared a dbl variable that is a System.Func<int, int> delegate type. We’re going to assign a lambda expression that doubles a number to the dbl variable. Our resolver is able to see that num is being assigned to a delegate whose single parameter is an int parameter, and thus knows that num is an int. The completion list we see is for the int type.
Now let’s see a sample of combining all the concepts discussed above… LINQ extension methods!
Here we declare a variable list that is a list of strings. Then we call the Enumerable.Where LINQ extension method on it and pass a lambda expression predicate in. As we hover over the i lambda expression parameter, you can see the resolver properly identified it as a string.
The item variable is implicitly declared as the FirstOrDefault result of the Where method call. Now let’s hover over Console.WriteLine and we see that it correctly picks the overload that takes a string parameter.
You can see that these new resolver features are pretty incredible and make the editing experience (especially with LINQ extension methods) so much nicer. All of these features are the building blocks for further query expression enhancements.
The new features described above are ready to go for the next WPF and Silverlight maintenance releases.
July 1, 2011 at 00:22
Actipro Blog 2011 Q2 Posting Summary
Actipro Blog 2011 Q2 Posting Summary
The Actipro Blog - WPF, Silverlight, and WinForms Development