WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Scala · Intermediate · question 37 of 100

Discuss the use of path-dependent types in Scala and provide an example.?

📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

Path-dependent types are an advanced feature in the Scala type system that allows types to depend on a value (path) rather than just types themselves. The term "path-dependent" refers to the fact that the type depends on a specific value, which can create a unique type for each instance of a class or object.

Path-dependent types enable you to create a more controlled and expressive type system, helping you to model scenarios where types are closely bound to the values they depend on. This enables strong static checks in the code and reduces potential bugs.

Let’s explore path-dependent types with an example.

Consider a scenario where we want to model a ‘Bank‘, which has multiple ‘Account‘s associated with it, and each account has some balance.

class Bank {
  class Account(val id: Int, var balance: Double)

  def createAccount(id: Int, initialBalance: Double): Account = new Account(id, initialBalance)
}

val bank1 = new Bank
val account1Bank1 = bank1.createAccount(1, 1000.0)

val bank2 = new Bank
val account2Bank2 = bank2.createAccount(1, 500.0)

In this example, we have two banks, each with an ‘Account‘. Notice that the ‘Account‘ class is defined inside the ‘Bank‘ class.

Here, each account created will have a different type, as the type of the account depends on the bank it is associated with. In other words, the type of ‘account1Bank1‘ and ‘account2Bank2‘ are path-dependent on ‘bank1‘ and ‘bank2‘.

The type of ‘account1Bank1‘ will be ‘bank1.Account‘, and for ‘account2Bank2‘, it’ll be ‘bank2.Account‘.

You can’t accidentally interchange accounts from different banks. The below code will give a compile-time error:

val wrongAccount: bank1.Account = account2Bank2 // Error

This is because the type ‘bank2.Account‘ is not a subtype of ‘bank1.Account‘, because the type of each account depends on the path (bank object) it is associated with.

Now let’s create a method that transfers an amount from one account to another account within the same bank.

class Bank {
  class Account(val id: Int, var balance: Double) {
    def transfer(dest: Account, amount: Double): Unit = {
      if (amount <= balance) {
        balance -= amount
        dest.balance += amount
      } else {
        throw new Exception("Insufficient funds.")
      }
    }
  }

  def createAccount(id: Int, initialBalance: Double): Account = new Account(id, initialBalance)
}

Using path-dependent types, the ‘transfer‘ method is now guaranteed to work for only those accounts that belong to the same bank, providing better type safety at compile time.

Path-dependent types offer a powerful feature in the Scala type system, allowing you to express relationships between instances of your classes more precisely and enforce type safety at compile-time. This feature can significantly reduce the potential for bugs in your code by ensuring that incompatible types simply won’t compile.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Scala interview — then scores it.
📞 Practice Scala — free 15 min
📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

All 100 Scala questions · All topics