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).
I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong