January, 2013

  • Kirill Osenkov

    Naming Roslyn concepts

    • 28 Comments

    We have a rather central component in Roslyn and we’re looking how to name it best. I’d like to gather some advice and opinions that could potentially help us find a good name.

    I am intentionally not mentioning what is it called right now. Instead, I’m going to describe it. If you recognize this component from the Roslyn CTPs, please don’t name it in the comments so as not to accidentally drive others towards its current name, which we believe is overused in our domain already.

    Roslyn analyzes code. Code is most often organized into solutions, projects and documents. Primarily we use three types to model source code: Solution, Project and Document. A Solution has a set of Projects, a Project has a set of Documents. These form an immutable tree that captures a snapshot of a source code tree at a moment in time. This data structure only captures the information interesting to the compiler, it’s not an MSBuild or a project system concept. When the user edits the text of a document in the editor, adds a new project in Visual Studio or otherwise modifies code, a new immutable snapshot of the entire Solution is created. This is implemented using efficient immutable data structures that reuse most of the data between the snapshots. This way if an analysis was in progress while the user changed code, the analysis can continue on it’s own snapshot safely knowing that the snapshot will never change. Also we can utilize multiple cores for fast, safe, lock-free analysis.

    Now, the concept we’re looking to name is something that has a CurrentSolution property that points to the latest snapshot of the solution data structure. It can listen to changes in user’s source code, build a new Solution snapshot to reflect those changes, and finally update the CurrentSolution property to point to the latest snapshot. This concept also has an event that our features can listen to to update themselves when the current solution has changed.

    An environment that is hosting Roslyn (say the Visual Studio project system) is responsible for forwarding changes into this component such that it can recalculate the most up-to-date Solution and notify all observers.

    If a Roslyn feature, such as a refactoring, wants to modify the users code (e.g. generate a method), it doesn’t talk directly to the environment. Instead, it describes the changes it wants to apply to the current solution and forms a new, delta (diff) Solution, and hands it off to our component. Our component then takes this delta (changes) and sends it back to the host, asking the host to do whatever is necessary to actually apply these changes (edit a file, checkout a file from TFS, add a reference, etc.) The host then, after applying these changes will pipe the notifications back into our component to complete the circle.

    How do we name this component that is responsible for keeping the CurrentSolution up-to-date and ensures bidirectional communication of changes with the host environment?

    Thanks!

  • Kirill Osenkov

    Collection initializers and Add method with more than one parameter

    • 7 Comments

    I didn’t know this about C# until today. This code compiles:

    using System;
    using System.Collections.Generic;
    
     
    class List : List<Tuple<string, int, bool>>
    {
        public void Add(string s, int i, bool b)
        {
            Add(Tuple.Create(s, i, b));
        }
    
     
        static void Main(string[] args)
        {
            var list = new List
            {
                { "zero", 0, true },
                { "one", 1, false }
            };
     
            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }
        }
    }

    and outputs:

    (zero, 0, True)
    (one, 1, False)

    It turns out, the way collection initializers work they pattern match to expect the Add method on the list object, and it can have more than one or two parameters. This is extremely handy for writing declarative, data-driven unit-tests where you only specify the data for your scenario and the imperative control-flow of the test is abstracted away into a helper method. This allows you to shape the relevant data in a way convenient to be able to clearly read the test scenario.

    P.S. For a second, I admit to having a thought “I bet even Jon Skeet didn’t know about this one”, but then of course I opened C# in Depth 2nd Edition, page 216 and was once again in awe of Jon’s superpowers. Sorry for ever having the slightest of doubt, Jon, won’t happen again!

Page 1 of 1 (2 items)