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

  • SEARCH
  • Home
  • 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 9192471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:55:56+00:00 2026-06-17T20:55:56+00:00

In a class on programming languages, my professor quotes mixins as one of the

  • 0

In a class on programming languages, my professor quotes mixins as one of the solutions to the fragile base class problem. Wikipedia also used to list (Ruby) mixins as a solution for the fragile base class problem, but some time ago someone removed the reference to mixins. I still suspect that they might somehow have an advantage over inheritance with regards to the fragile base class problem. Otherwise, why would a professor say that they help?

I’ll give an example of a possible problem. This is a simple Scala implementation of the (Java) problem that the professor gave us to illustrate the problem.

Consider the following base class. Assume that this is some very efficient special implementation of a list, and that more operations are defined on it.

class MyList[T] {
    private var list : List[T] = List.empty
    def add(el:T) = {
        list = el::list
    }
    def addAll(toAdd:List[T]) : Unit = {
        if (!toAdd.isEmpty) {
            add(toAdd.head)
            addAll(toAdd.tail)
        }
    }
}

Also consider the following trait, which is supposed to add size to the list above.

trait CountingSet[T] extends MyList[T] {
    private var count : Int = 0;
    override def add(el:T) = {
        count = count + 1
        super.add(el)
    }
    override def addAll(toAdd:List[T]) = {
        count = count + toAdd.size;
        super.addAll(toAdd);
    }
    def size : Int = { count }
}

The problem is that whether or not the trait will work depends on how we implement addAll in the base class, i.e. the functionality provided by the base class is ‘fragile’, just as would be the case with a regular extends in Java or any other programming language.

For example, if we run the following code with MyList and CountingSet as defined above, we get back 5, whereas we would expect to get 2.

object Main {
    def main(args:Array[String]) : Unit = {
        val myCountingSet = new MyList[Int] with CountingSet[Int]
        myCountingSet.addAll(List(1,2))
        println(myCountingSet.size) // Prints 5
    }
}

If we change addAll in the base class (!) as follows, the trait CountingSet works as expected.

class MyList[T] {
    private var list : List[T] = List.empty
    def add(el:T) = {
        list = el::list
    }
    def addAll(toAdd:List[T]) : Unit = {
        var t = toAdd;
        while(!t.isEmpty) {
            list = t.head::list
            t = t.tail
        }
    }
}

Keep in mind that I am anything but a Scala expert!

  • 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-17T20:55:57+00:00Added an answer on June 17, 2026 at 8:55 pm

    Mixins (whether as traits or something else) cannot completely prevent fragile base class syndrome, nor can strictly using interfaces. The reason should be pretty clear: anything you can assume about your base class’s workings you could also assume about an interface. It helps only because it stops and makes you think, and imposes a boilerplate penalty if your interface gets too large, both of which tend to restrict you to needed and valid operations.

    Where traits really get you out of trouble is where you already anticipate that there may be a problem; you can then parameterize your trait to do the appropriate thing, or mix in the trait you need that does the appropriate thing. For example, in the Scala collections library, the trait IndexedSeqOptimized is used to not just indicate but also implement various operations in a way that performs well when indexing is as fast as any other way to access elements of the collection. ArrayBuffer, which wraps an array and therefore has very fast indexed access (indeed, indexing is the only way into an array!) inherits from IndexedSeqOptimized. In contrast, Vector can be indexed reasonably fast, but it’s faster to do a traversal without explicit indexing, so it does not. If IndexedSeqOptimzed was not a trait, you’d be out of luck, because ArrayBuffer is in the mutable hierarchy and Vector is in the immutable hierarchy, so couldn’t make a common abstract superclass (at least not without making a complete mess of other inherited functionality).

    Thus, your fragile base class problems are not solved; if you change, say, Traversable‘s implementation of an algorithm so that it has O(n) performance instead of O(1) (perhaps to save space), you obviously can’t know if some child might use that repeatedly and generate O(n^2) performance which might be catastrophic. But if you do know, it makes the fix much easier: just mix in the right trait that has an O(1) implementation (and the child is free to do that whenever it becomes necessary). And it helps divorce concerns into conceptually coherent units.

    So, in summary, you can make anything fragile. Traits are a tool that, used wisely, can help you be robust, but they will not protect you from any and all follies.

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

Sidebar

Related Questions

Which programming languages other than C++ support the concept of a constant class method?
I have a question from a test in a Programming Languages class that is
I'm reading some slides of a class on object oriented programming languages and stepped
I'm making an Ontology that describes programming languages, but I have the following problem:
I remember touching on this subject during a class on programming languages. I vaguely
For my programming languages class, I'm writing a research paper on some papers by
What is the use of Meta-Class in Groovy and other OO programming languages?
Right. I'm currently in a class that is exploring many different programming languages. Among
I just learned in my programming languages class that contravariant argument types would actually
Modern programming languages provide parallelism and concurrency mechanisms as first class citizens to their

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.