AI Generate Scala docs instantly

Scala Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

4 snippets

Your first steps with Scala basics

Hello World

@main def hello(): Unit =
  println("Hello, World!")

Comments

// Single line comment

/* Multi-line
   comment */

String Interpolation

val name = "Scala"
println(s"Hello, $name!")  // Hello, Scala!

User Input

import scala.io.StdIn
val input = StdIn.readLine("Enter: ")
println(s"You entered: $input")

Variables & Data Types

7 snippets

Store and manage data in Scala

Val (Immutable)

val name = "John"

Var (Mutable)

var count = 0

Type Annotation

val age: Int = 25

List

val nums = List(1, 2, 3)

Map

val ages = Map("John" -> 30, "Jane" -> 25)

Tuple

val pair = (1, "hello")

Option

val name: Option[String] = Some("John")

Operators

5 snippets

Symbols for calculations and comparisons

Arithmetic

5 + 3    // 8 (addition)
5 - 3    // 2 (subtraction)
5 * 3    // 15 (multiplication)
5 / 3    // 1 (integer division)
5 % 3    // 2 (modulus)

Comparison

5 == 3   // false (equal)
5 != 3   // true (not equal)
5 > 3    // true (greater)
5 < 3    // false (less)
5 >= 3   // true (greater or equal)
5 <= 3   // false (less or equal)

Logical

true && false  // false (AND)
true || false  // true (OR)
!true          // false (NOT)

Compound Assignment

var x = 5
x += 3   // x = 8
x -= 2   // x = 6
x *= 2   // x = 12
x /= 3   // x = 4

String Operators

"Hello" + " World"  // concatenation
"abc" * 3           // "abcabcabc"

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Loops

7 snippets

Repeat code efficiently with iterations

For Range

for (i <- 1 to 5) {
  println(i)  // 1, 2, 3, 4, 5
}

For Until

for (i <- 0 until 5) {
  println(i)  // 0, 1, 2, 3, 4
}

For Collection

val fruits = List("apple", "banana")
for (fruit <- fruits) {
  println(fruit)
}

For Yield

val doubled = for (n <- nums) yield n * 2

While

var i = 0
while (i < 5) {
  println(i)
  i += 1
}

Foreach

nums.foreach(n => println(n))
nums.foreach(println)  // shorthand

For with Guard

for (i <- 1 to 10 if i % 2 == 0) {
  println(i)  // 2, 4, 6, 8, 10
}

Control Flow

2 snippets

Direct program execution with conditions

If Expression

val max = if (a > b) a else b

Match

x match {
  case 1 => "one"
  case 2 | 3 => "two or three"
  case _ => "other"
}

Functions

5 snippets

Reusable blocks of code with lambdas

Def

def greet(name: String): String = {
  s"Hello, $name!"
}

Short Form

def add(a: Int, b: Int) = a + b

Default Param

def greet(name: String = "World") = s"Hello, $name!"

Lambda

val add = (a: Int, b: Int) => a + b

Currying

def add(a: Int)(b: Int) = a + b

Collections

6 snippets

Functional transformations on data

Map

nums.map(_ * 2)

Filter

nums.filter(_ > 0)

FlatMap

nums.flatMap(n => List(n, n * 2))

Reduce

nums.reduce(_ + _)

Fold

nums.foldLeft(0)(_ + _)

GroupBy

people.groupBy(_.city)

Classes & OOP

5 snippets

Define custom types and objects

Class

class Dog(val name: String) {
  def bark(): String = s"$name says woof!"
}

Case Class

case class Person(name: String, age: Int)

Object

object Singleton {
  def doSomething(): Unit = {}
}

Trait

trait Greetable {
  def greet(): String
}

With Trait

class Person extends Animal with Greetable

Pattern Matching

3 snippets

Powerful matching expressions

Case Class Match

person match {
  case Person(name, age) => s"$name is $age"
  case _ => "unknown"
}

Option Match

opt match {
  case Some(value) => value
  case None => "default"
}

Guard

x match {
  case n if n > 0 => "positive"
  case _ => "non-positive"
}

More Cheat Sheets

FAQ

Frequently asked questions

What is a Scala cheat sheet?

A Scala cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Scala. It helps developers quickly look up syntax without searching through documentation.

How do I learn Scala quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Scala concepts?

Key Scala concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Scala code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Scala code using AI.

Code Conversion Tools

Convert Scala to Other Languages

Easily translate your Scala code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Scala Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week