Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9094199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:17:46+00:00 2026-06-16T23:17:46+00:00

I have recently been going through the book Scala by Example in which the

  • 0

I have recently been going through the book “Scala by Example” in which the author creates an abstract class to represent a set of ints “IntSet” with two subclasses (EmptySet and NonEmptySet) as follows:

abstract class Stack[A] { 
  def push(x: A): Stack[A] = new NonEmptyStack[A](x, this)
  def isEmpty: Boolean
  def top: A
  def pop: Stack[A]
}

class EmptyStack[A] extends Stack[A] {
  def isEmpty = true 
  def top = error("EmptyStack.top")
  def pop = error("EmptyStack.pop")
}

class NonEmptyStack[A](elem: A, rest: Stack[A]) extends Stack[A] {
  def isEmpty = false
  def top = elem
  def pop = rest
}

My question is this: How useful is this paradigm of representing an empty container as its own class instead of creating one concrete class to handle both the empty and non-empty cases?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:17:47+00:00Added an answer on June 16, 2026 at 11:17 pm

    Each implementation is simpler and more readable as the is-empty-check does not have to be done in the implementation. This leads to better code metric values (like cyclomatic complexity) too.

    Moreover, It makes the implementations slightly faster in general, since the distinction between empty and non-empty does not have to be done at runtime. As far as I know, Scala’s Set applies this technique and implements different types of sets (used depending on their size) to optimize performance.

    Obviously this works for immutable data structures only.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently, I have been going through search trees and I encountered red-black trees, the
Recently, I was going through an open-source project and although I have been developing
I have recently been going through some of our windows python 2.4 code and
I'm relatively new to cross-platform mobile development, and recently have been going through some
Recently I have been going through a project in an attempt to sanitize several
I have been recently going through my database code trying to improve on my
I have recently been asked to estimate a piece of work which will provide
I have been learning ASP.NET MVC recently and everything is going well apart from
I just started using IBM Worklight 5.0. I have been going through the HTTP
I've been learning more about Python recently, and as I was going through the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.