
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Apply Method
- Scala - Update Methods
- Scala - UnapplySeq Method
- Scala - Inheritance
- Scala - Extending a Class
- Scala - Method Overloading
- Scala - Method Overriding
- Scala - Generic Classes
- Scala - Generic Functions
- Scala - Superclass Construction
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Lazy Val
- Scala - Pure Function
- Scala - Currying Functions
- Scala - Control Abstractions
- Scala - Corecursion
- Scala - Unfold
- Scala - Tail Recursion
- Scala - Infinite Sequences
- Scala - Dynamic Invocation
- Scala - Lambda Expressions
- Scala Collections
- Scala - Collections
- Mutable and Immutable Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - TreeMap
- Scala - SortedMap
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Infinite Streams
- Scala - Parallel Collections
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
Scala - REPL
Introduction to Scala REPL
REPL stands for Read Evaluate Print Loop which is a command line tool. REPL is used for expression, evaluation and execution of small code snippets. Just like Java Shell, Scala REPL is great for beginners and people who want to try out new libraries or language features.
First, REPL reads the input given on the Scala command line. Then evaluates the given input and displays the result on the screen. It is ready to read the next input and process continues in loop. In the scope of current input, previous results are automatically imported as needed. REPL reads input expressions at the prompt during interactive mode. It then transforms these expressions into executable templates, compiles and executes result.
Implementation of REPL
- You can use switch -Yrepl-class-based to wrap either object and class in user code.
- Every input line is compiled individually.
- Automatically generated imports include dependencies from previous lines.
- You can control implicit import of scala.Predef by providing explicit import.
REPL Installation
Scala REPL is included with Scala when you install it. So, you can use it whenever you have Scala on your computer. You need to run "scala" command in your terminal then this is what will appear in our terminal:
Welcome to Scala 3.3.1 (21.0.1, Java Java HotSpot(TM) 64-Bit Server VM). Type in expressions for evaluation. Or try :help. scala>
Useful Commands and Features of REPL
Running Scala in a terminal shell can be useful. REPL includes many useful commands and features. We have discussed some of them.
1. help Command
You need to hit `:help` command which will prints these list of available commands:
scala> :help The REPL has several commands available: :help print this summary :load <path> interpret lines in a file :quit exit the interpreter :type <expression> evaluate the type of the given expression :doc <expression> print the documentation for the given expression :imports show import history :reset [options] reset the repl to its initial state, forgetting all session entries :settings <options> update compiler options, if possible scala>
2. load Commad
The `:load` command loads and runs code from a file in the REPL.
For example, if you have file names `script.scala` with following content:
case class Person(name: String, Roll:Int) val me = Person("Mithlesh", 101)
You can run this file using `:load` command:
scala> :load script.scala // defined case class Person val me: Person = Person(Mithlesh,101) scala> me val res0: Person = Person(Mithlesh,101) scala>
3. paste Command
One of the first obstacles we encounter when using a REPL is the fact that, by default, code is evaluated line by line. So, multi-line code like functions can not be executed without use of `paste` command. This command is used to paste and load code in the REPL. First type :paste in REPL then paste your block of code.
4. lastException Binding
IastException is bound for the last thrown exception. For example,
scala> throw new NullPointerException("Oops, a null reference!") java.lang.NullPointerException: Oops, a null reference! ... 32 elided scala> lastException res4: Throwable = java.lang.NullPointerException: Oops, a null reference!
5. reset Command
If you want to clear variables or functions from our REPL session. You can use the ':reset' command. When you reset a session, you can not use the definitions from before anymore.
For example,
scala> val x = 42 x: Int = 42 scala> def bar(): String = "hello" bar: ()String scala> :reset Resetting interpreter state. Forgetting this session history: val x = 42 def bar(): String = "hello" Forgetting all expression results and named terms: x, bar scala> x <console>:12: error: not found: value x x ^
6. silent Command
System shows the definition when you create variable or function. You can disable it by using `silent` command.
For example,
scala> val x = 42 x: Int = 42 scala> :silent scala> val y = "Hello, Scala!" scala>
7. quit Command
To end a REPL session, you can use the ':quit' command or press ctrl-D.
Working with Dependencies of REPL
While Scala REPL is not designed for large projects, it can be useful by adding extra jars to the classpath.
1. require Commad
This command adds jars to classpath. For example,
scala> :require my-library-1.0.jar Added '/path/to/my-library-1.0.jar' to classpath. scala>
2. scala -cp
You can include jars when starting the REPL by using the '-cp' argument. For example,
scala -cp my-library-1.0.jar myScript.scala
3. sbt Console
An alternative method to bring in more dependencies in a REPL session is to utilize the 'sbt console' within an sbt project. When you use the 'sbt console' command, it starts a REPL with all the sbt dependencies included in the classpath.
Key Features of REPL
- IMain Binding − The IMain of REPL is linked to $intp.
- Tab Key for Completion − Use the tab key for auto-completion.
- lastException Binding − lastException is tied to the last exception in the REPL.
- :load Command − Use :load to load a file containing REPL input.
- :javap Command − Utilize :javap to examine class artifacts.
- -Yrepl-outdir Switch − Use -Yrepl-outdir to inspect class artifacts using external tools.
- :power Command − Imports compiler components after entering compiler mode.
- :help Command − Get a list of commands for user assistance.