TechDays Paris Roslyn compiler demo

Two days ago, I spoke at Microsoft TechDays in Paris, France on Roslyn.

I made several demos including one where I build my own C# compiler that allows me to add features on C#.

 

This compiler supports AOP in order to implement INotifyPropertyChanged.

So with this code:

[NotifyPropertyChanged]

public class TestNotifyPropertyChanged

{

    public int P1 { get; set; }

 

    private string _p2;

    public string P2

    {

        get { return _p2; }

        set 

        {

            if (_p2 != null)

                _p2 = value;

        }

    }

}

the compilation works like if you wrote this one:

public class TestNotifyPropertyChanged : System.ComponentModel.INotifyPropertyChanged
{
    public int P1
    {
        get
        {
            return _p1;
        }
 
        set
        {
            _p1 = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("P1"));
        }
    }
 
    private string _p2;
    public string P2
    {
        get
        {
            return _p2;
        }
 
        set
        {
            if (_p2 != null)
                _p2 = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("P2"));
        }
    }
 
    private int _p1;
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
}

 

My second C# improvement was to be able to combine return and yield return on the same method and to introduce yield return many keyword.

In this case, this code:

public IEnumerable<int> GetInts(bool b)
{
    if (b)
        return Enumerable.Range(1, 2);
    yield return many Enumerable.Range(1, 2);
    yield return 4;
}

will be translated (still on compilation process) by the following:

public IEnumerable<int> GetInts(bool b)
{
    if (b)
    {
        foreach (int yieldReturnVarLoop in Enumerable.Range(1, 2))
            yield return yieldReturnVarLoop;
        yield break;
    }
 
    foreach (int yieldReturnVarLoop in Enumerable.Range(1, 2))
        yield return yieldReturnVarLoop;
    yield return 4;
}

 

The last one was to support generic constraint constructor with parameters.

With my own compiler, I can use the following code:

public class C<T>
    where T : new(string, bool)
{
    public T CreateT(string s, bool b)
    {
        return new T(s, b);
    }
}
 
public class Test
{
    public static string Foo()
    {
        return new C<C2>().CreateT("A", true).S; 
    }
}

which will be translated by this one:

public class Test
{
    public static string Foo()
    {
        return new C<C2>() { FactoryT = (aT0, aT1) => new ConsoleApplication1.C2(aT0, aT1) }.CreateT("A", true).S;
    }
}
 
public class C<T>
{
    public T CreateT(string s, bool b)
    {
        return FactoryT(s, b);
    }
 
    public System.Func<string, bool, T> FactoryT
    {
        get;
        set;
    }
}

If you want to see how to do it, you can download the source code here.

This entry was posted in 16402, 7671, 7672. Bookmark the permalink.

4 Responses to TechDays Paris Roslyn compiler demo

  1. Rui says:

    Sounds great Matthieu!

    I particularly like the new(params) constraint ;-)

    stupid question, do you have integrated it in vs with intellisense?

    cheers!

  2. Matthieu MEZIL says:

    @Rui: Unfortunately not

  3. Mitsu says:

    Interesting !
    Though you should have use “many yield return” instead of “yield return many” to keep C# grammar consistent.

  4. Matthieu MEZIL says:

    @Mitsu: you’re right. You know how bad my English is…
    BTW, you should look at my POC https://improvecscompiler.codeplex.com/documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>