Since Roslyn's syntax trees are immutable, and working with compilers is essentially all about transformations ( which is exactly what functional programming are good at ), I thought to myself using Roslyn in F# might be a great idea.
So, Is it feasible to combine Roslyn and F# ? Has anyone tried it before ? Does it have any advantages over using C# ? And if its possible, does it worth I start learning F# ?
PS : I have some experience in functional programming with Scheme and I'm currently learning Haskell, but I haven't yet tasted F#.
I thought to myself using Roslyn in F# might be a great idea.
It's definitely a great idea. Some people have wondered why Roslyn hasn't been written in F# since the language is very good for writing compilers (see http://neildanson.wordpress.com/2012/12/24/the-roslyn-incident/ for reference).
It's very easy to pattern match on Roslyn ASTs once you define a set of composable active patterns. See https://gist.github.com/jbevain/01a083c07010bc7b7cd0 as a convincing example. Since Roslyn ASTs are immutable, you can also create combinators in order to transform them based on your purpose. Discriminated unions could be used as immediate datatypes if the goal is to spit out C# ASTs again.
The downside of using F# is that you don't find many complete examples using Roslyn API in F#. We have a rough plan to integrate Roslyn into Visual F# Power Tools. Let's say you have a mixed C#/F# solution; if you would like to go to definitions to a C# symbol from an F# project, Roslyn is needed to process C# code for that purpose. However, this requires a large amount of work; I don't know if anyone has been working on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With