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 8452607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:32:39+00:00 2026-06-10T11:32:39+00:00

I have a class and its companion object which together have some reusable functionality.

  • 0

I have a class and its companion object which together have some reusable functionality. I have encapsulated the functionality of the companion object into a trait, so now the situation is like

class Foo {
  import Foo._

  def foo: Quux = bar(this)
}

trait Bar {
  def bar(f: Foo): Quux = frobnicate(f)
}

object Foo extends Bar

Since Foo.foo is a reusable method, I would like to put it into its trait.

But I have to find a way to tell the type checker that, although bar is not a method on class Foo, it will be in scope because imported from the companion object. I think I need something like being able to type over the companion object of a class.

Is there something like that?

  • 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-10T11:32:41+00:00Added an answer on June 10, 2026 at 11:32 am

    There are multiple ways to model the abstraction you need in Scala. I will first describe the most simple pattern and analyze your problem, and then I will describe the most complex pattern, which is used in Scala collections.

    The first thing to notice is that companion objects are the right place to put code you will need to call without having an instance of your class, while the place to factor out helpers which you use in instance methods are traits. Furthermore, the smart scala compiler will generate a single static method for your trait, and link all the classes which will use it to it.

    From your code point of view, it is very easy to see how it can be factored out into a trait, and then by using the self-type notation, one can enforce that the trait FooTrait can be mixed only if the trait Bar is mixed as well.

    class Foo extends FooTrait with Bar 
    
      trait FooTrait {
        self:Bar =>
        def foo: Quux = bar(this)
      }
    
      trait Bar {
        def bar(f: Foo): Quux = frobnicate(f)
      }
    

    Please also note, if you do not want to expose the Bar interface through your Foo class, an alternative approach would be the following

      class Foo extends FooTrait {
        protected val barrer = Foo
      }
    
      trait FooTrait {
        protected val barrer:Bar
        def foo: Quux = barrer.bar(this)
      }
    
      trait Bar {
        def bar(f: Foo): Quux = frobnicate(f)
      }
    
      object Foo extends Bar
    

    This second approach works fine when you take a single class and a single companion objects, but does not scale well when you want to develop a hierarchy of classes where you now there is a companion object available for each of these classes, and you also want to enforce the companion object has certain characteristics with respect to the “companed class”.

    There is a more complex approach, which is used in Scala collections and which I warmly recommend you not to use unless strictly necessary.

    Let’s start from GenTraversable:

    trait GenTraversable[+A]
    extends GenTraversableLike[A, GenTraversable[A]]
       with GenTraversableOnce[A]
       with GenericTraversableTemplate[A, GenTraversable]
    {
      def seq: Traversable[A]
      def companion: GenericCompanion[GenTraversable] = GenTraversable
    }
    
    
    object GenTraversable extends GenTraversableFactory[GenTraversable] {
      implicit def canBuildFrom[A] = new GenericCanBuildFrom[A]
      def newBuilder[A] = Traversable.newBuilder
    }
    

    As you see, the trait defines a companion object, which provides some basic infrastructure for building new collections of the same type (typically for filtering, mapping, and so on).

    By going up in the hierarchy, you can see that the def companion is refined:

    trait GenIterable[+A]
    extends GenIterableLike[A, GenIterable[A]]
       with GenTraversable[A]
       with GenericTraversableTemplate[A, GenIterable]
    {
      def seq: Iterable[A]
      override def companion: GenericCompanion[GenIterable] = GenIterable
    }
    
    
    object GenIterable extends GenTraversableFactory[GenIterable] {
      implicit def canBuildFrom[A] = new GenericCanBuildFrom[A]
      def newBuilder[A] = Iterable.newBuilder
    }
    

    If you surf among the classes, you will understand that this mechanism is used to guarantee that for each concrete collection implementation, there is a companion in scope with some kind of properties with respect to the class itself. This is possible because it is legal to refine the return type of a method in a child class.

    Nevertheless, in order to work correctly this mechanism requires some manual casts and a lot of generic parameters, as well as generic signatures.

    trait GenericTraversableTemplate[+A, +CC[X] <: GenTraversable[X]] extends HasNewBuilder[A, CC[A] @uncheckedVariance] {
     protected[this] def newBuilder: Builder[A, CC[A]] = companion.newBuilder[A]
    
      /** The generic builder that builds instances of $Coll
       *  at arbitrary element types.
       */
      def genericBuilder[B]: Builder[B, CC[B]] = companion.newBuilder[B]
    
      private def sequential: TraversableOnce[A] =this.asInstanceOf[GenTraversableOnce[A]].seq
    // other code
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Flamethrower which naturally has its own ammunition that is
I have a class A which takes a reference of B in its constructor.
So I have a class.. In its constructor I include the code which connects
I have a situation where I would like to compare an object encapsulated by
I have a class that retrieves its memeber (more or less 10 members) from
I have a class that defines its own enum like this: public class Test
I have a class that is its own activity that basically i use to
Suppose I have a Base class and its derived class Derived as follows: class
I have a class CollectionOfThings . As its name suggests its a simple collection
I have a class SimpleCircle. Its declaration is as follows: class SimpleCircle { public:

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.