Hello FSharp from macOS
Previously, we got two old friends talking nice to each other, this time we’ll introduce macOS to the hipper, cooler sister.
MacOS…say hello to FSharp!
What we’ll need
- Visual Studio Code
Let’s get installed
Refer back to the previous article, if you haven’t installed Visual Studio Code and dotnetCore yet, here, we’ll be focusing on setting up fsharp. And as with the csharp example, this means, wait for it, cracking open the command line interface!?!
I know the command line has remained a long neglected distant cousin to the dotnet developer, but getting comfortable with the dotnet cli really is the first step in embracing dotnetCore.
Let’s make sure we’ve got everything setup correctly by checking the version of donetCore we have installed.
$ dotnet --version
Creating our first fsharp project
Generating an fsharp project is the same as csharp, except we’ll include a language option to let dotnetCore know we’re dating the cooler sister.
$ mkdir HelloMacOS
$ cd HelloMacOS
$ dotnet new --lang f#
Yep, that’s it, and now back to the world of the gui that the dotnet rabble is more comortable with.
$ code .
$ dotnet restore
I’m a dotnet developer and I demand assistance
VS Code launches and everything runs, but we don’t get a whole lot of assistance from VS Code when we edit our fsharp files. For instance, click on Program.fs and hover over main, nothing, zilch, zippo.
Let’s rectify that and enahance our fsharp developer experience. Hold down ⌘+ Shift + P to bring up the command palette, type ext, and choose Extensions: Install Extensions from the drop down. We’ll search for fsharp and the first hit should be the Ionide-fsharp plugin.
Installing and enabling the plugin will result in a restart of VSCode, only we’ll be greeted with the following error in VS Code.
The Ionide-fsharp plugin runs on Mono, the original cross platform version of dotnet, and VS Code is letting us know that the plugin can’t find Mono to run the language service against.
We’ll head over to the mono project and download the macOS package here. Once the package installs we’ll open back up VSCode and now when we hover over main we see the following.
Hovering over the function main let’s us know that it’s a function that takes an array of string and returns an integer. The plugin also does auto completions, error highlighting, syntax highlighting, and a slew of other useful development features.
Why would i use fsharp?
We’ve gone to the trouble of setting up fsharp, but why would we use it when we have a perfectly usable language in csharp? I’m sure you will hear plenty of high falutin reasons, but fsharp really does get your brain solving problems in a different manner.
Here is some perfectly good csharp procedural code that sets up a path based on the name of a folder.
The above code is statement based
- First we get our root directory
- Next we get the name of the directory
- Then we write a statement to check the name against “bin”
- Then we overwrite rootDirectory with the parent if our check was true.
- Finally we write out the root directory.
Our chsarp code ends up being a series of statements with various side effects and mutations of state along the way. Compare this to the following fsharp solution.
The thinking in fsharp is almost inside out of how you think with csharp. Instead of doing a bunch of statements to end up with what you want, in fsharp I usually start with with the answer and start asking the question i need to ask in order to get my desired answer.
- Start with the answer i want
- Get the current directory
- Store the current directory and the name of the current directory
- Match the name of the current directory against “bin”
- Return the parent if we match otherwise return the current directory.
Matching the case of string with an active pattern
If you pay close attention to the code above, the fsharp doesn’t quite do what the fsharp code does. I won’t go into the details of active patterns but the following code shows how easy it is to wrap code in a family friendly wrapper with fsharp.
We got our fsharp environment setup on macOS and compared some fsharp code with the corresponding csharp code.
Hopefully, this quick little example got some people thinking that maybe it’s time to get to know the hipper, cooler sister of dotnetCore.
FSharp really is a great language and will change the way you solve problems, now on macOS!