Member-only story

Awesome tricks every Scala coder should know

Manpreet Singh
3 min readJul 25, 2021

--

Welcome back! Scala is an awesome programming language that seems to be growing steadily, so let’s take a look at some awesome tricks that every Scala developer needs to know. Now, although these do range in use, you could probably utilize most of these in any project out there, with that long introduction out of the way, let’s get started!

Using Futures

Starting off, one of the best things you could do when using a bunch of variables in Scala is Future, this essentially allows you to initialize a variable that may or may not be currently available. To use this, we simply declare a variable as a future:

https://jaxenter.com/cheat-sheet-complete-guide-scala-136558.html

Either

Next up we have the either function, this allows us to create a variable as either a string or int, making it much easier to handle variables with less errors. Here is an example of how to do this (courtesy of the scala-lang.org website):

import scala.io.StdIn._
val in = readLine("Type Either a string or an Int: ")
val result: Either[String,Int] =
try Right(in.toInt)
catch {
case e: NumberFormatException => Left(in)
}
result match {
case

--

--

Manpreet Singh
Manpreet Singh

No responses yet