Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage Articles What’s New on F#: Q&A With Phillip Carter

What’s New on F#: Q&A With Phillip Carter

Key Takeaways

  • Functional Programming is a different programming paradigm that can work well with Object-Oriented Programming. Choosing when to use one or another is a matter of choosing the right tool to solve the problem at hand.
  • F# is one of the .NET programming languages. It's functional-first, general-purpose, and - similarly to C# - its compiler produces intermediate language (IL) from the source code. Typical F# code runs as fast as C# code.
  • F# 5 embodies a focus adjustment in the language development. With the new release, Microsoft brings better interactive and analytical programming with support for tools like Jupyter and VSCode notebooks and features aimed at the data science and machine learning domains.
  • F# 5 is a high-productivity language, lightweight, and it can be used with Visual Studio. It's being widely used in the industry, with applications ranging from machine learning in financial markets to web applications and productivity and support tools..

Last month, at the 2020 edition of .NET Conf, Microsoft released the latest version of F#,  together with .NET 5. F# is one of the .NET programming languages, along with C# and Visual Basic. It is functional-first, cross-platform, open-source, and it’s developed by Microsoft and several open source partners and contributors.

InfoQ interviewed Phillip Carter, program manager at Microsoft, to talk about functional programming, F#, and the new features of F# 5.

InfoQ: What exactly is functional programming?

Phillip: Functional Programming (FP) has several accepted definitions depending on who you talk to. The way I see it, Functional Programming is just programming with functions and orienting your program around some of the properties of functions. Functions take inputs and produce outputs. Do you have some data? Pass it to a function to get a result. Do you have a result that needs to be used to produce another result? Pass that to a function. Do you have a “chain” of functionality that needs to execute to produce a result? Write several functions and “compose” them such that outputs from one are inputs to another. If you have a program state, you can change it by passing it to a function and observing the result.

FP and Object-Oriented Programming (OOP) aren’t really at odds with each other, at least not if you use each as if they were a tool rather than a lifestyle. In FP, you generally try to cleanly separate your data definitions from functionality that operates on. In OOP, you’re encouraged to combine them and blur the differences between them. Both can be incredibly helpful depending on what you’re doing. For example, in the F# language we encourage the use of objects to encapsulate data and expose functionality conveniently. That’s a far cry from encouraging people to model everything using inheritance hierarchies, and at the end of the day you still tend to work with an object in a functional way, by calling methods or properties that just produce outputs. Both styles can work well together if you don’t “all in” on one approach or the other.

InfoQ: How does F# work?

Phillip: F# works as a somewhat typical compiler in that it reads source code as a string and produces tree-like structures until it finally emits a data format (IL). That process generally looks like this:

  1. Lex and parse source code, as a string, into a syntax tree
  2. Import .NET and F# references (packages, projects, etc.)
  3. Take data from steps 1 and 2 and sequentially typecheck files in the order in which they were declared, which involved inferring types with a Hindley-Milner type inference algorithm
  4. Solve type constraints to resolve things like generics over a closed set (e.g., generic arithmetic) and other phases
  5. Perform some final type checking after inference and constraint solving (some rules can’t be applied until after constraints are solved) to finalize the typed syntax tree
  6. Optimize a typed syntax tree that is produced, doing things like tail call optimization and tuple elimination
  7. Rewrite the typed syntax tree as an abstract IL tree
  8. Perform further optimizations on the abstract IL tree
  9. Emit the IL tree as actual IL and emit either as a binary or via reflection emit
  10. This is also an oversimplification but does map out the general phases of compilation. Typical F# code ends up running just as fast as typical C# code, even though the input is so much more different. This is because by the time we’re emitting IL, the F# compiler has been able to transform data and optimize things enough to the point where it’s very well optimized for the .NET runtime. The only major difference tends to be that F# emits the .tail instruction, and prior to .NET 5, the .NET runtime had not handled it efficiently. Now it does!

However, this isn’t the only way that we look at the F# compiler. The compiler isn’t just a black box that emits IL from source code. It’s also a:

  • Database of syntactic and semantic information
  • High-throughput server process
  • A large API for accessing the data in that database

This is because the F# compiler is also run as a server process in IDE tooling. When you’re writing code in an editor, the kind of features you’re used to need to concurrently access data from different stages of compilation. For example, code folding in an IDE depends on the syntax tree of the source code in your document. Renaming a symbol requires the ability to resolve the symbol your caret is at in a document and distinguish it from others that may have the same name, but don’t refer to the same thing (semantic information). And these kinds of features need to be able to be requested concurrently, be cancellable, etc.

So, F# is both a batch process that does a whole lot, but also serves a very different role than most compilers out there.

InfoQ: Microsoft just released F# 5 at the latest .NET Conf. How does it compare with the previous F# versions?

Phillip: F# 5 is our first foray into an adjusted focus for F#. For the past few years, F# has been focused on being a great choice for cross-platform development, and we spent most of our time bringing up various aspects of the F# language, core library, and tooling to work cross-platform. F# 5 finishes this by making F# Interactive work great and it also has some awesome new features like package references that allow you to pull in a package from NuGet interactively.

The F# Interactive work is combined with some extensive support for Jupyter and VSCode notebooks to start our rethink of what interactive programming means for F#. Additionally, data science and ML are domains that heavily involve interactive programming, and so we added some features to make F# a great choice for those domains when combined with other libraries. This is our first journey into this space, and it’s something F# has traditionally had a strength it, but hadn’t been explored since about 2010.

Finally, features like String Interpolation, nameof, open type declarations, and some enhancements to Computation Expressions were done to just make general F# programming even better. We have general areas of focus for each release, but we also bring along a bag of goodies each time we ship a new version, because we recognize that sometimes the best things you do are just making things better for everyone.

InfoQ: What are the advantages of using F# today?

Phillip: I’d say that the biggest advantages of using F# today are twofold:

  1. It is a carefully designed language intended to give you tools to get a lot done without writing much code. F# developers, especially new ones, regularly take to Twitter to exclaim how quickly and conveniently they were able to “just get some shit done” and know that their code is correct because the compiler guided them towards correctness.

  2. The F# community, though smaller than others, is highly engaged and produces a lot of high-quality libraries and guidance in the form of docs and blog posts. If you run into a problem, chances are there’s already a post somewhere online explaining how to solve it, or if not, someone will jump to help you out at a moment’s notice. There is a consistent level of excitement in the F# community and it’s addicting to participate in.

What’s interesting is that even though F# runs on .NET, which often has an “enterprisey” kind of reputation, F# itself doesn’t really suffer the negative aspects of that kind of reputation. It can be used for enterprise work, but it’s usually seen as lightweight (as opposed to heavyweight “enterprisey” languages) and its community is engaged and available as opposed to stuck behind a corporate firewall.

InfoQ: Is F# 5 supported by Visual Studio?

Phillip: Yes, F# is supported in Visual Studio. It has been fully supported since 2010 and has undergone an immense number of improvements since then. As of Visual Studio 2019 version 16.8, it includes a swath of productivity features and code fixes such as IntelliSense, semantic colorization, rename refactoring, navigation features, and more. It also performs quite favorably for very large codebases, something we focused on over the past 2 years working with paying customers who had enormous amounts of code that had to work well.

InfoQ: Can I write and deploy a service or API to the cloud using F# today?

Phillip: Of course! By virtue of running on .NET, F# can run in Azure anywhere that .NET can. Azure App Service, Azure Functions, Cosmos DB, etc. all support F# through their supported .NET SDKs. And when you use .NET 5 (or .NET Core 3.1), you can use standard OSS tools like Docker and Kubernetes for your services and deploy them anywhere.

InfoQ: How is F# being used in the industry?

Phillip: F# has wide usage in the industry today. It’s used to power financial engines in banks, F# is used to produce ML models that power insurance models that process billions in claims, F# powers eCommerce infrastructure, and more. But more than anything else, it’s used to power apps and services that do lots of “boring but useful” work that developers need to fulfill business requests all across the world. It’s not just used in Windows apps or a backend service, though. Through tools like WebSharper or Fable, F# is also used to power the frontend web apps and embed directly in the JavaScript ecosystem. This toolchain is even used to power the F# plugin for VSCode, which is written in 100% F# code.

InfoQ: What is the best way to start learning F# today?

Phillip: The easiest way to get started with F# is to follow our tutorial on the F# homepage for the .NET website:

But it honestly depends on what you’re trying to accomplish. Another great resource for trying out F# for full-stack apps is to look at SAFE-stack

F# also has a foundation, the F# Software Foundation, which runs various aspects of the F# community and has its own extremely active slack that I always encourage new people to join.

You can also access the F# Software Foundation forms without a (free) membership.

My advice is to ask the community what resources are best based on the kind of things you’re trying to build. You’ll find great resources online just by doing a quick google search, but the community typically has the best advice to give about which way to start based on what you’re trying to accomplish.

About the Interviewee

Phillip Carter is a Senior Program Manager on the .NET team at Microsoft, focusing on F#, language tooling in Visual Studio, and efforts to expand .NET in non-traditional spaces. He’s been working on .NET and languages for 5 years.


Rate this Article