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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:20:16+00:00 2026-05-26T19:20:16+00:00

Consider this code: val foo = if(true) new java.lang.Double(4) else new java.lang.Integer(4) The inferred

  • 0

Consider this code:

val foo = if(true) 
            new java.lang.Double(4) 
          else
            new java.lang.Integer(4)

The inferred type for foo is:

Number with Comparable[_ >: Double with Integer <: Number with 
  Comparable[_ >: Double with Integer <: Number]]

So basically the compiler loops on the bounds and aborts after the third recursion.

Why isn’t the following enough?

Number with Comparable[_ >: Double with Integer <: Number]
  • 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-26T19:20:17+00:00Added an answer on May 26, 2026 at 7:20 pm

    Not an answer but some clues using implicitly in the REPL. The compiler doesn’t think the types are the same. The inferred type is more specific:

    // some type aliases to make reading easier
    type Dx = java.lang.Double
    type Ix = java.lang.Integer
    
    // the type the compiler came up with:
    type Inferred = Number with Comparable[
      _ >: Dx with Ix <: Number with Comparable[_ >: Dx with Ix <: Number]]
    
    // your type:
    type Soc = Number with Comparable[_ >: Dx with Ix <: Number]
    

    Checking I did the type aliases right:

    val d = new java.lang.Double(4)
    val i = new java.lang.Integer(4)
    val foo: Soc = if (true) d else i
    // foo: Soc = 4.0
    val foo: Inferred = if (true) d else i
    // foo: Inferred = 4.0
    

    Types are not the same:

    implicitly[Soc =:= Inferred] // error
    

    Your type is a super type of the inferred type:

    implicitly[Inferred <:< Soc] // ok
    implicitly[Soc <:< Inferred] // error
    

    So according to the compiler, it came up with a more specific type – which would be the right thing to do. Note that the use case can be re-created like this:

    class N                     // like java.lang.Number
    
    trait C[T]                  // like Comparable
    
    class I extends N with C[I] // like java.lang.Integer
    class D extends N with C[D] // like java.lang.Double
    
    type DI = N with C[_ >: D with I <: N with C[_ >: D with I <: N]]
    // DI is like the type inferred
    
    type DI_SOC = N with C[_ >: D with I <: N] // your type
    
    val foo: DI = if (true) new D else new I     // ok
    val foo: DI_SOC = if (true) new D else new I // ok
    
    implicitly[DI =:= DI_SOC] // error
    implicitly[DI <:< DI_SOC] // DI_SOC super type of DI
    implicitly[DI_SOC <:< DI] // error
    

    So I wonder if we can craft a class that is a DI_SOC but not a DI, which would illustrate DI and DI_SOC are not the same types and your type is not the least upper bound.

    Ok, after stepping out for the computer for a bit and then trying again. Here is a class that is a DI_SOC but not a DI:

    class A extends N with C[N]
    implicitly[A <:< DI_SOC] // ok
    implicitly[A <:< DI]     // error
    

    Applied to the original use case:

    class Ax extends Number with Comparable[Number] {
      def doubleValue() = 0d
      def floatValue() = 0f
      def intValue() = 0
      def longValue() = 0L
      def compareTo(n: Number) = 0
    }
    
    implicitly[Ax <:< Soc]      // ok
    implicitly[Ax <:< Inferred] // error
    

    Therefore, the types Soc and Inferred are not the same and Ax proves that Number with Comparable[_ >: Double with Integer <: Number] is not the least upper bound…

    In other words, there is some room between Double with Integer <: ? <: Number but not much between Double with Integer <: ? <: Number with Comparable[_ >: Double with Integer <: Number]

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

Sidebar

Related Questions

Consider this code: public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return new
Consider this code: import scala.xml.{Node,HasKeyValue} def domatch(x:Node): Node = { val hasBar = new
Consider the following code: object U { def foo(s:String) = true } val boolType
Consider this code: class Foo[T : Manifest](val id: String = manifest[T].erasure.getName) I basically want
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
consider this code block public void ManageInstalledComponentsUpdate() { IUpdateView view = new UpdaterForm(); BackgroundWorker
Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }
Consider this code: new Ajax.Request('?service=example', { parameters : {tags : 'exceptions'}, onSuccess : this.dataReceived.bind(this)
Consider this code: import socket store = [] scount = 0 while True: scount+=1
Consider this code: int main() { int e; prn(e); return 0; } void prn(double

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.