Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Roslyn in F#

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#.

like image 946
Rsh Avatar asked Jul 24 '14 10:07

Rsh


1 Answers

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.

like image 158
pad Avatar answered Oct 07 '22 07:10

pad