Xamarin is well known as a platform for building cross-platform mobile apps using C#, but did you know it also supports F#? This leads to a conundrum - C# development is very object-oriented so how can you use F# to build mobile apps but retain the function programming paradigms that make F# such a powerful language? Recently a new language and architecture called Elm appeared in the javascript community with a structure based on immutable models and functions to create and update the UI. This maps really well to functional languages and similar architectures are popping up in the F# community under the banner of ‘Elmish’ - first for web apps using Fable and more recently for Xamarin.Forms apps using Elmish.XamarinForms.
Watch the presentation here:
https://www.youtube.com/watch?v=si9YdWhbwSI
Build cross-platform apps with immutable models and UI with a lot less code using F#, Xamarin.Forms and Elmish
This presentation looks at building Xamarin apps in F#
In particular, using Xamarin.Forms with Elmish.XamarinForms to provide immutable models and UI
F# foundation - body that manages F#
It is a mature language – came out of Microsoft Research.
First stable release was 2005
Open source – managed by the F# foundation with oversight/primary development from Microsoft
Can’t be truly independent because of links to .NET
Because it is a .NET language it has full interop with everything .NET, so it supports OO code
Functional programming is not scary
Seems so due to lots of academia with long words like lambda calculus, monads (which as everyone knows are monoids in the category of endofunctors)
It’s actually an easy to use language, partly because you can get away with writing very little code
As F# is a .NET language it runs wherever C# runs
VS2017 and Mac have full support
So does VSCode thanks to the Ionide extension
Can use it for anything C# can do
Azure Functions support F#
For more functional style, tools like Fable for Web
F# works – just drop in a replacement for C#
Demo
Based on the MVU pattern – model, view, update
Came from Elm in the Javascript world
Each app has a model type and some defined messages.
A init method creates a model
An update method is called with messages – from UI or other services
Update returns a new model, copying the old one and adjusting
New model is passed to view which returns a representation of the UI
Framework updates the real UI based off the representation
Simple example – incrementing a model
The elm pattern always has an immutable model and synchronised update, so no threading issues
One model is the single source of truth, so no managing state in different models and view models
UI representation isn’t a real UI, so can be fully unit tested without running the app
The view function doesn’t interact with the UI, so can be run off the UI thread to reduce UI load
F# has Elmish – like Elm
Originally used with Fable for web
Elmish.XamarinForms brings this to Xamarin Forms
2 variants – half and full
Half elmish – ideal for Xamarin developers who are used to MVVM/binding and want to try out F#
Model and update are in code
View returns bindings
UI is XAML, bound to the results of the view function
Can’t unit test the UI, just the bindings
Full elmish – all in, F# only, no XAML
View function returns ‘shadow’ UI
Framework looks for differences between shadow UI and real UI and updates UI to match
F# works – just drop in a replacement for C#
Demo
This framework is reasonably stable, but still in beta
Third party controls are not supported in full elmish
The XAML namespace will change – not sure what to yet
More needs to be done on the navigation stacks, documentation…
Please get involved
To get started
Docs
Samples,
Awesome elmish
Dotnet new
Get in touch