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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:29:13+00:00 2026-05-27T13:29:13+00:00

My meta-question would be How do I ask this question intelligibly? but perhaps an

  • 0

My meta-question would be “How do I ask this question intelligibly?” but perhaps an example would make it clearer:

trait Toolbox {
  trait Bolt
  trait Wrench {
    def tighten(b : Bolt) 
  }

  def getWrench : Wrench
  def getBolt : Bolt
}


object MetricToolbox extends Toolbox {
...
}

object EnglishToolbox extends Toolbox {
...
}

Of course, a metric wrench can only tighten a metric bolt; ditto for tools from the English toolbox. My question is, how do I express that in a type-safe way so the following won’t compile:

MetricToolbox.getWrench tighten EnglishToolbox.getBolt

but the following will:

def doTighten(box : Toolbox) = box.getWrench tighten box.getBolt

I know for a fact that this is possible because I heard Martin Odersky say it with his own mouth at the Scala meetup last night but at that moment, my wife called my cell and I had to scurry out of the room before he explained how.

EDIT didierd pointed out that my code worked as written. He’s right — I’m a little unclear on the details but it does. The natural reverse questions is, how do I not do that? The answer was pretty simple: put those classes at the same level as the main trait.

trait Nail
trait Hammer {
  def pound(n : Nail)
}

trait Toolbox {
  trait Bolt
  trait Wrench {
    def tighten(b : Bolt) 
  }

  def getWrench : Wrench
  def getBolt : Bolt
  def getNail : Nail
  def getHammer : Hammer
}

 MetricToolbox.getHammer pound EnglishToolbox.getNail // this DOES compile
  • 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-05-27T13:29:13+00:00Added an answer on May 27, 2026 at 1:29 pm

    As written, it already works. As Bolt and Wrench are nested inside Toolbox, the Bolt that types the argument of tighten means a Bolt of this exact toolbox, not a Bolt of any toolbox. They are written respectively someToolbox.Bolt, which means a Bolt of the exact instance someToolBox and Toolbox#Bolt, which means the Bolt defined in the type Toolbox, whatever the insatance toolbox. So Toolbox#Bolt is a common supertype of all x.Bolt.

    Here Bolt without qualification means this.Bolt, this being the enclosing Toolbox. So you cannot mix.

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

Sidebar

Related Questions

(I am sorry, I wanted to ask this question on meta first, but it
This question is based on this thread in Meta . I would like to
This isn't really a coding question, but I wanted to ask about what is
This sounds a bit like a trivia question, but it would help me figure
Okay, so this is a straight math question and I read up on meta
I ask this question as a followup of this question . A solution that
This question is all about performance and I would appreciate if the answers are
In this answer to a Meta question , Jeff states that he has fixed
My question is similar to this , but creating junction table is not good
First a caveat: I posted this question here on SuperUser, but it is clearly

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.