Skip to content

How to Use the Swift REPL

Swift, REPL, Interactive Shell2 min read

The Swift REPL (Read-Eval-Print Loop) is a powerful tool that allows developers to interactively write and execute Swift code snippets. It provides a convenient way to experiment with the language, test ideas, and quickly prototype functionality. In this article, we will explore the basics of using the Swift REPL and demonstrate its usage with some examples.

Starting the Swift REPL

To start the Swift REPL, open a terminal window on your macOS system and type swift repl followed by pressing the Return key. This will launch the Swift interpreter, and you will see a prompt indicating that you are now in the Swift REPL environment:

1$ swift repl
2Welcome to Apple Swift version 5.8 (swiftlang- clang-1403.
3Type :help for assistance.
4 1>

You are now ready to enter Swift code and execute it directly within the REPL.

Executing Code in the Swift REPL

To execute code in the Swift REPL, simply type the desired Swift expression or statement at the prompt and press Return. The REPL will immediately evaluate the code and display the result, if any:

11> let greeting = "Hello, World!"
2greeting: String = "Hello, World!"

In the example above, we defined a constant named greeting and assigned it the value of the string "Hello, World!". The REPL then displayed the result, indicating that the constant is of type String.

You can also define functions, structs, classes, or any other Swift constructs within the Swift REPL. For example:

12> struct Person {
2. var name: String
3. var age: Int
4. }
5 3> let person = Person(name: "John", age: 30)
6person: Person = Person(name: "John", age: 30)

In this example, we defined a struct called Person with properties name and age. We then created an instance of Person named person with the values "John" and 30 for the name and age properties, respectively.

Using the REPL as a Playground

The Swift REPL can be used as an interactive playground for trying out code snippets and experimenting with language features. You can define variables, perform calculations, and observe the results in real-time.

14> let radius = 5.0
2radius: Double = 5.0
3 5> let area = Double.pi * pow(radius, 2)
4area: Double = 78.53981633974483

In this example, we calculated the area of a circle using the formula π * r^2, where radius is 5.0. The REPL immediately evaluated the expression and displayed the result.

Importing Modules and Libraries

The Swift REPL allows you to import external modules and libraries to leverage additional functionality. You can import system modules, such as Foundation, or third-party frameworks that you have installed.

16> import Foundation
2 7> let currentDate = Date()
3currentDate: Date = 2023-07-29 10:15:23 +0000

In this example, we imported the Foundation module, which provides fundamental capabilities for working with dates, strings, collections, and more. We then created an instance of Date named currentDate, representing the current date and time.

Lastly(Important): Exiting the Swift REPL

To exit the Swift REPL and return to the terminal, type :quit or press Ctrl + D:

18> :quit

This will terminate the Swift interpreter and bring you back to the command prompt.