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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:37:30+00:00 2026-05-29T12:37:30+00:00

Given the code below the method foo should compare operator-wise a given parameter bar

  • 0

Given the code below the method foo should compare operator-wise a given parameter bar with the lowerBound and upperBound all being of the same abstract type Bar.

trait Foo {
  type Bar <: Ordered[Bar]
  val lowerBound: Bar
  val upperBound: Bar
  def foo(bar: Bar) = bar >= lowerBound && bar <= upperBound
}

This way the trait Foo can be defined. The problems start with the below concrete class FooImpl.

class FooImpl extends Foo {
  type Bar = Int
  val lowerBound = 0
  val upperBound = 5
}

I understand that scala.Int isn’t implementing what scala.runtime.RichInt does, effectively scala.math.Ordered[Int]. Defining type Bar as RichInt instead neither works as it does not conform to scala.math.Ordered[RichInt]. My third attempt to define type Bar as Ordered[Ord] where Ord is declared as type Ord and defining it in FooImpl as Int also did not work.

How would a possibly close solution look like?

  • 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-29T12:37:31+00:00Added an answer on May 29, 2026 at 12:37 pm

    There may be a more elegant solution, but you can achieve this by moving the restriction on the type to the method, rather than the type declaration:

    trait Foo {
      type Bar
      val lowerBound: Bar
      val upperBound: Bar
      def foo(bar: Bar)(implicit ev: Bar => Ordered[Bar]) = {
        bar >= lowerBound && bar <= upperBound
      }
    }
    

    Then your FooImpl works as you have it:

    class FooImpl extends Foo {
      type Bar = Int
      val lowerBound = 0
      val upperBound = 5
    }
    

    From the REPL:

    scala> new FooImpl()
    res0: FooImpl = FooImpl@2dbbec72
    
    scala> res0.foo(3)
    res1: Boolean = true
    
    scala> res0.foo(7)
    res2: Boolean = false
    

    The disadvantage here is that the trait can be extended with unordered types (although foo can’t be called in that case):

    class A // not Ordered
    
    class BrokenFoo extends Foo {
      type Bar = A
      val lowerBound = new A
      val upperBound = new A
    } // compiles
    
    new BrokenFoo().foo(new A) // doesn't compile
    

    Alternatively, you can keep the requirement at the class level (and therefore stop anyone creating a BrokenFoo) as follows, but FooImpl has to change slightly:

    trait Foo {
      type Bar
      implicit val baz: Bar => Ordered[Bar]
      val lowerBound: Bar
      val upperBound: Bar
      def foo(bar: Bar) = { bar >= lowerBound && bar <= upperBound }
    }
    
    class FooImpl extends Foo {
      type Bar = Int
      val baz = implicitly[Bar => Ordered[Bar]]
      val lowerBound = 0
      val upperBound = 5
    }
    

    This problem feels like view or context bounds should be applicable, but unfortunately it doesn’t seem like you can use them either in type declarations or in generic type parameters on traits.

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

Sidebar

Related Questions

Given the below code, will the method parameter y in Bar(int y) be assigned
Given the code below, how do I compare a List of objects's values with
Any ideas given the code below why the highlight is being triggered to run
The code below prints out all comments for a given submissionid in chronological order.
Given the code below, how would you create/implement SR.h so that it produces the
Given the code below ... Net::HTTP.start('localhost', 4000) do |http| # # usual stuff omitted
I was given the code below to disable a button on an ASP.Net page
Given the Java code below, what's the closest you could represent these two static
Hello friends i am running code given below which contains the setLogTimeEntery function and
Technology Used:- Asp.Net 2.0 Code:- See Below Description:- hello code given below is working

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.