Phil

Phil floodphil

Niner since 2011

Comments

  • TWC9: C9 goes global, Fast App Switching, Geo AR Toolkit, MEF 2.0

    re: F# eye for the C# guy slide deck

    The slides were presented at DDD Belfast early in October. The inspiration for the title came from a talk by Leon Bambrick. The slide that got the focus in the Channel 9 video above was titled Light Syntax and was part of the Demos section, added as a reference for attendees.

    The demo was primarily intended to help C# developers understand how F# maps to C#.

    That said the comparison of number of lines of code is interesting. As discussed in the Channel 9 video above if we don't need the class to be immutable then C# Auto-Implemented Properties could be employed, however you'd still probably want a constructor and comments in your C# code, i.e.:

    public class Person
    {
        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }
    
        /// <summary>
        /// Full name
        /// </summary>
        public string Name { get; private set; }
        
        /// <summary>
        /// Age in years
        /// </summary>
        public int Age { get; private set; }
    }

    F# class types let you define a primary constructor in the parameter list that follows the type name, and by default F# triple-slash comments define summary blocks without the need for XML tags

    type Person (name:string, age:int) =
        
        /// Full name
        member person.Name = name
    
        /// Age in years
        member person.Age = age
    .

    If we forgo the comments, this can be brought down to 1 line with an F# record:

    type Person = { Name:string; Age:int }

    The F# record above will automatically generate a constructor and properties for use from other .Net languages including C#; it is also immutable.

    Further reading:

    A look at F# from C#'s corner: http://www.servicestack.net/mythz_blog/?p=765

    From C# to F#: a developer's perspective: http://www.developerfusion.com/article/122233/from-f-to-c-a-developers-perspective/

    C# Light Syntax: http://trelford.com/blog/post/LighterCSharp.aspx

    More discussion on Reddit: http://www.reddit.com/r/programming/comments/l294s/f_for_c_developers/