From F# to Scala – type inference

Presently sponsored by Serverless Guru: Your guide to cloud excellence, helping you every step of your serverless journey, including team training, pattern development, mass service migrations, architecting, and developing new solutions. Speak to a Guru today.

Read the whole series:

Part 1 – type inference <- you’re here

Part 2 – traits

Part 3 – case class/object (ADTs)

Part 4 – apply & unapply functions

Part 5 – implicits

It’s a new year, a new job, a new language (Scala) and runtime (JVM) to work with.

After a week of learning about my new surroundings at Space Ape Games and getting up to speed with Scala I have learnt a fair bit. In my own journey (aided by Twitter’s Scala School, Odersky’s Coursera course and Effective Scala) I found the target audience for a lot of online materials is Java developers, or developers who are new to functional programming.

As a proficient F# developer, a lot of the FP concepts translate quite easily to Scala (albeit with a different, more verbose syntax). However, Scala does have a lot of language features that do not exist in F# and many things (such as type inference) work slightly differently and can bite you in subtle ways if caught unaware of these differences.

So, for anyone coming into Scala from F#, I hope I can provide some useful info based on the things that I found interesting in my journey.

One of those things is how type inference differs in the two languages.


F# uses a Hindley-Milner type inference system similar to its ML brethren, but Scala’s type inference works slightly differently.

Take the following example for instance, this bit of code works just fine in F#.

but the same doesn’t work in Scala…

unless you explicitly specify the type parameter for toList.

Interestingly, if you rewrite the function in curried form then the Scala compiler is able to infer the type for toList.

but the order of parameters matters somehow..

which I eventually understood thanks to some helpful folks on Twitter.


Although less restrictive, this limitation of only being able to unify types from left to right also applies to F#.

For example, a simple example using the backward pipe (<|) is enough to cause the compiler to bark.

I hope this has been useful for you, over the next weeks or months I should be adding more posts to this series as I better familiarise myself with Scala, both in terms of language features as well as the toolchain/ecosystem around it.

See here for the rest of the series (as they become available).

6 thoughts on “From F# to Scala – type inference”

  1. Pingback: From F# to Scala – type inference

  2. Hi. Great post.
    A couple of questions: 1. Did you find “call-by-name” feature to be useful in daily work?
    2. Traits and implicit conversions in scala look like an utter mess from a syntax standpoint but the features by themselves are quite useful. I saw recent proposals for F# ( as to introducing “type classes” into the language. What do you reckon chances are of that happening anytime soon?

  3. Hi, I have seen “call-by-name” used in our codebase sporadically but I haven’t worked with Scala for long enough (it’s only been a week) to form an informed opinion about their usefulness yet, let’s see how it goes in the future.

    On the other hand I find traits and implicit both powerful language features, though I can see how they might be abused.. I like type classes, but I’m not convinced we necessarily need it in F# and having now spent some time with Scala (which is more powerful and expansive, and have more advanced language features than F#) it actually makes me appreciate the relative simplicity of F#.

    Don has often talked about the desire to remove features from languages (or at least to add them in a very purposeful and guided way to avoid having multiple ways of achieving the same goal). Go and Elm are also good examples of how less can be more, and that simplicity is worth more than language features. Or, to put it in another way, a language is more than the sum of its parts.

    So to answer your last question, I don’t see type classes as something Don will be desperate to add to F#.

  4. Pingback: F# Weekly #3, 2017 – Sergey Tihon's Blog

Comments are closed.