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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:22:26+00:00 2026-06-02T07:22:26+00:00

so say I have two dependencies in my app, a connection to some pub

  • 0

so say I have two dependencies in my app, a connection to some pub sub system, and a connection to a database. I can do something like

trait DB {
    def lookup(query:String):String
}

trait PubSub {
    def subscribe(key:String, callback:String => Any)
}

then I can write my logic like

trait Functionality { this:DB with PubSub => 
    def doSomething() {
        val key = lookup("get key")
        subscribe(key, data => println(data))
    }
}

and then my app can be like

object Awesome extends App {

    object repository extends Functionality with DB with PubSub {
        def lookup(query:String) = "some key"
        def subscribe(key:String, callback:String => Any) {
            scala.concurrent.ops.spawn { while(true) { callback(key) ; Thread.Sleep(1000) } } 
        }
    }
    repository.doSomething()
}

and all is well and good in the world.

But what if I want connections to two pub sub systems that share the same database implementation in the same app?

I want to do something like

object Awesome2 extends App {
    object repository extends DB {
        def lookup(query: String): String = "some other key"

        object connection1 extends Functionality with PubSub with DB {
            def subscribe(key: String, callback: (String) => Any) {
                scala.concurrent.ops.spawn { while(true) { callback(key.toUpperCase) ; Thread.sleep(1000) } }
            }
        }

        object connection2 extends Functionality with PubSub with DB {
            def subscribe(key: String, callback: (String) => Any) {
                scala.concurrent.ops.spawn { while(true) { callback(key.toLowerCase) ; Thread.sleep(1000) } }
            }
        }
    }
}

where the objects in the second layer of cake (implicitly?) slurp in the DB implementation from the parent level.

But the scala compiler tells me

error: object creation impossible, since method lookup in trait DB of type (query:String) String is not defined
object connection2 extends Functionality with PubSub with DB {

if I do the following then it does what I want

object Awesome3 extends App {
    object repository extends DB {
        override def lookup(query: String): String = "some other key"

        object connection1 extends Functionality with PubSub with DB {
            def subscribe(key: String, callback: (String) => Any) {
                scala.concurrent.ops.spawn { while(true) { callback(key.toUpperCase) ; Thread.sleep(1000) } }
            }

            def lookup(query: String): String = repository.lookup(query)
        }

        object connection2 extends Functionality with PubSub with DB {
            def subscribe(key: String, callback: (String) => Any) {
                scala.concurrent.ops.spawn { while(true) { callback(key.toLowerCase) ; Thread.sleep(1000) } }
            }

            def lookup(query: String): String = repository.lookup(query)
        }
    }
    repository.connection1.doSomething()
    repository.connection2.doSomething()
}

but this is kind of messy

I can add this trait

trait DB_Base extends DB {

    private val db:DB = this

    trait DB_Layer extends DB {
        def lookup(query:String):String = db.lookup(query)
    }
}

and then the following works

object Awesome4 extends App {
    object repository extends DB_Base {
        override def lookup(query: String): String = "some other key"

        object connection1 extends Functionality with PubSub with DB_Layer {
            def subscribe(key: String, callback: (String) => Any) {
                scala.concurrent.ops.spawn { while(true) { callback(key.toUpperCase) ; Thread.sleep(1000) } }
            }
        }

        object connection2 extends Functionality with PubSub with DB_Layer {
            def subscribe(key: String, callback: (String) => Any) {
                scala.concurrent.ops.spawn { while(true) { callback(key.toLowerCase) ; Thread.sleep(1000) } }
            }
        }
    }    
    repository.connection1.doSomething()
    repository.connection2.doSomething()
}

so now I have two layers. How do I get three? I feel like I’m losing the plot.

  • 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-02T07:22:28+00:00Added an answer on June 2, 2026 at 7:22 am

    A comment isn’t large enough to explain it, so here is an answer that basically says, “Don’t do that!” and suggests an alternative.

    The key problem you’re running into is that you want to have multiple copies of some functionality, but you don’t have any way to refer to it by name (only by type). The solution is: give it a name.

    Let’s take your double-cake pattern.

    trait Foo { def foo(s: String): String }
    trait Bar { def bar(s: String, f: String => Any): Any }
    trait Bippy { this: Foo with Bar =>
      def bip(s: String) = bar(foo(s),println)
    }
    

    Okay, great, we can mix in Bippy to anything that implements Foo with Bar and we’ll be able to bip. But what if Foo and Bar are implemented at different levels? If we instead

    trait Bippy {
      def myFoo: Foo
      def myBar: Bar
      def bip(s: String) = myBar.bar(myFoo.foo(s), println)
    }
    

    this initially looks more awkward. (It is.) But it now lets you mix and match instead of being forced to cake in increasingly awkward ways. For example:

    object Foozle extends Foo { theFoo =>
      def foo(s: String) = s.toUpperCase
      trait BippyImpl extends Bippy { this: Bar =>
        def myFoo = theFoo
        def myBar = this
      }
      object Woozle1 extends BippyImpl with Bar {
        def bar(s: String, f: String => Any) = f(s)
      }
      object Woozle2 extends BippyImpl with Bar {
        def bar(s: String, f: String => Any) = f(s.reverse)
      }
    }
    

    Now you can mix and match any functionality from anywhere; the only downside is you have to name it. (Here we’ve created a nested trait BippyImpl in order to split out the common parts for the Woozles, but we could just do it directly.)

    Also, you don’t get the original method names mixed in; you’ll have to write proxies or refer to a member variable.

    It misses out on some of the nice aspects of the cake pattern, but in my experience it ends up being a lot clearer than a massive mess of cake layers. And now you can see that you can nest it as deeply as you like and fill in the details you want wherever you need to.

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

Sidebar

Related Questions

Say I have two NSTimers in my iPhone app: timer1 and timer2 . timer1
Say I have two applications running; App A and App B. What would be
Say I have two classes like this: class A{ private static Random random =
Say I have two complex nested arrays in PHP, like these: $a = array(
Say I have two pages, A and B. The user can modify things on
Say I have two lists: >>> l1=[1,2,3,4] >>> l2=[11,12,13,14] I can put those lists
Say I have two apps, www.test.com and sub.test.com , now in sub.test.com , I
Say you have two integer vectors: I would like to define a function that
Say we have two activities, Activity1 and Activity2. In Activity1's onClick() method, we have
Say I have two Java apps that I wrote: Ping.jar and Pong.jar and they

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.