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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:00:46+00:00 2026-06-13T13:00:46+00:00

I have the following design issue: /** * Those 2 traits are the public

  • 0

I have the following design issue:

/**
 * Those 2 traits are the public API
 */
trait Box {
  def include(t: Token): Box
}
trait Token

/**
 * Implementation classes
 */
case class BoxImpl(id: Int) extends Box {
  /**
   * the implementation of this method depends on the implementation
   * of the Token trait
   * TODO: REMOVE asInstanceOf
   */
  def include(t: Token) = BoxImpl(t.asInstanceOf[TokenImpl].id + id)
}
case class TokenImpl(id: Int) extends Token

// use case
val b: Box = new BoxImpl(3)
val o: Token = new TokenImpl(4)

// == BoxImpl(7)
b.include(o)

In the code above, I don’t want to expose id in the public API (not even set it as private[myproject] because that would still include circular dependencies in my project).

What would be the way to leave the public API intact while having the implementation classes having some visibility to each other (without the ugly cast)?

  • 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-13T13:00:47+00:00Added an answer on June 13, 2026 at 1:00 pm

    Type param or member:

    trait Box {
      type T <: Token
      def include(t: T): Box
      //def include(t: Token): Box
    }
    trait Token
    
    case class BoxImpl(id: Int) extends Box {
      type T = TokenImpl
      def include(t: T) = BoxImpl(t.id + id)
    
      /*
      def include(t: Token) = t match {
        case ti: TokenImpl => BoxImpl(ti.id + id)
        case _ => throw new UnsupportedOperationException
      }
      */
    
      //def include(t: Token) = BoxImpl(t.asInstanceOf[TokenImpl].id + id)
    }
    case class TokenImpl(id: Int) extends Token
    

    There’s lots of ways to slice and dice it, hence the cake pattern, the proverbial piece of cake:

    trait Boxer {
      type Token <: Tokenable
      trait Box {
        def include(t: Token): Box
      }
      trait Tokenable
    }
    
    trait Boxed extends Boxer {
      type Token = TokenImpl
      case class BoxImpl(id: Int) extends Box {
        override def include(t: Token) = BoxImpl(t.id + id)
      }
      case class TokenImpl(id: Int) extends Tokenable
    }
    
    trait User { this: Boxer =>
      def use(b: Box, t: Token): Box = b.include(t)
    }
    
    object Test extends App with Boxed with User {
      val b: Box = new BoxImpl(3)
      val o: Token = new TokenImpl(4)
      println(use(b, o))
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL design issue. The code below might look a little
I have a following design function crudDataTable() { this.addEditFormContainerSelector = ''; this.paginationType = full_numbers;
I have the following database design: Employee Table: Username, Name, DivisionCode Division Table: SapCode,
I have the following database design: Employee Table: Username, Name Quiz Table: QuizID, Title,
I am creating an App with following design: I have a UITableView with custom
I am in need of some guidance for the following design. I have a
I have troubles with the following database design (simplified for the example). It allows
Is there any name for the following DB table design: Basically we have generic
The following is using EF 5.0.0-rc and Code First. In my design, I have
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>

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.