val (Immutable)
Browse / Scala Cheatsheet
Scala Cheatsheet
A concise reference for Scala syntax, features, and common use cases, designed to boost productivity and aid quick recall.
Basics & Syntax
      
        
            Variables & Data Types
        
      
    
  |  | Declares an immutable variable. Its value cannot be changed after assignment. Example:  | 
| 
 | Declares a mutable variable. Its value can be changed after assignment. Example:  | 
| Basic Data Types | 
 | 
| Type Inference | Scala can often infer the type, so explicit type declarations are optional. Example:  | 
| String Interpolation | Embed variables directly in strings. Example:  | 
| Multiline Strings | Use triple quotes to define multiline strings. Example:  | 
      
        
            Operators
        
      
    
  | Scala uses similar operators to Java: arithmetic ( | 
| Note that  | 
      
        
            Control Structures
        
      
    
  | 
 |  | 
| 
 |  | 
| 
 |  | 
| 
 | Powerful pattern matching.  | 
Functions & Classes
      
        
            Functions
        
      
    
  | Function Definition |  | 
| Anonymous Functions (Lambdas) |  | 
| Currying | Transforming a function that takes multiple arguments into a function that takes a single argument and returns another function that accepts the remaining arguments.  | 
| Default Arguments |  | 
| Higher-Order Functions | Functions that take other functions as arguments or return them as results.  | 
      
        
            Classes
        
      
    
  | Class Definition |  | 
| Auxiliary Constructor |  | 
| Case Classes | Automatically provides   | 
| Traits | Similar to interfaces in Java, but can also contain implemented methods and fields.  | 
Collections
      
        
            Common Collections
        
      
    
  | 
 | An ordered, immutable sequence of elements.  | 
| 
 | A collection of unique elements.  | 
| 
 | A collection of key-value pairs.  | 
| 
 | A mutable, fixed-size sequence of elements. More like Java arrays.  | 
| 
 | Indexed, immutable sequence. Provides fast random access and updates (amortized).  | 
      
        
            Collection Operations
        
      
    
  | Scala collections provide a rich set of operations using higher-order functions. These methods generally return a new collection (immutability). | 
| 
  | 
| 
  | 
| 
  | 
| 
  | 
| 
  | 
| 
  | 
Advanced Features
      
        
            Pattern Matching
        
      
    
  | Matching Literal Values |  | 
| Matching on Types |  | 
| Matching Case Classes |  | 
| Guards | Adding conditions to case statements.  | 
      
        
            Implicits
        
      
    
  | Implicit parameters, conversions, and classes allow for powerful type-safe abstractions and DSL creation. Use with caution, as they can make code harder to understand. | 
| Implicit Parameter: A parameter that the compiler can automatically provide if it’s not explicitly passed.  | 
| Implicit Conversion: Automatically converts one type to another.  | 
| Implicit Class: Adds methods to an existing class.  | 
