Today I have finished the port of the sample Ook language extension from C# to F#. It was interesting, as I was learning both the Visual Studio extension mechanisms, and also F#. The only problem remaining, that the Visual Studio crashes when I try to edit (add a module to the vsixmanifest file) with the VSIX editor.
It was not too easy task, some of the problems:
- Implementing an interface with events:
let tagsChangedHandlers = new ResizeArray<EventHandler<SnapshotSpanEventArgs>>()
interface ITagger<ooktokentag> with
member this.add_TagsChanged v = tagsChangedHandlers.Add v |> ignore
member this.remove_TagsChanged v = tagsChangedHandlers.Remove v |> ignore
- Properly setting up the project file was not so easy either (at least I now know what are the ProjectTypeGuids for C#, F# and the VS SDK)
- I had to move the TemplateQuickInfoControllerProvider and TemplateQuickInfoController classes to the same file (I am not sure how to do this if both classes have attributes)
- There are no implicit conversions
- One of the most annoying thing was when there was an error in my F# code and when I tried to correct it and saved the file the Visual Studio just crashed
- You have to use refs instead of mutable variables if you would like to get the result from a method (not so surprising, maybe I should read fully the Expert F# 2.0 before further coding)
- I have not found a simple way to emulate early return from methods
- It was pretty annoying to find that the unbox gives an int by default, which is not so much fun if you want a uint16, this solved the problem:
member private this.GetTypeChar(pvaIn : IntPtr) : char =
let x : uint16 = unbox(Marshal.GetObjectForNativeVariant(pvaIn))
char(x)
In general it was not so bad. I really liked the way it pushed me to make things more independent and be aware of the dependencies. I think because of the legacy API it is not a perfect fit for F# (and to be honest I have not transformed to a really functional solution), but everything can be worked around.
Although the F# Visual Studio plugin is terribly slow... I was typing and typing when finally I got back the cursor and the code completion and other things. I hope this was just because of an extension for Visual Studio, although except the F# refactor those useful, so those will stay.