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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:40:36+00:00 2026-05-15T13:40:36+00:00

I’d like to implement a class C to store values of various numeric types,

  • 0

I’d like to implement a class C to store values of various numeric types, as well as boolean. Furthermore, I’d like to be able to operate on instances of this class, between types, converting where necessary Int --> Double and Boolean -> Int, i.e., to be able to add Boolean + Boolean, Int + Boolean, Boolean + Int, Int + Double, Double + Double etc., returning the smallest possible type (Int or Double) whenever possible.

So far I came up with this:

abstract class SemiGroup[A] { def add(x:A, y:A):A }

class C[A] (val n:A) (implicit val s:SemiGroup[A]) {
  def +[T <% A](that:C[T]) = s.add(this.n, that.n)
}

object Test extends Application {
  implicit object IntSemiGroup extends SemiGroup[Int] { 
    def add(x: Int, y: Int):Int = x + y 
  }

  implicit object DoubleSemiGroup extends SemiGroup[Double] { 
    def add(x: Double, y: Double):Double = x + y 
  }

  implicit object BooleanSemiGroup extends SemiGroup[Boolean] { 
    def add(x: Boolean, y: Boolean):Boolean = true;
  }

  implicit def bool2int(b:Boolean):Int = if(b) 1 else 0

  val n = new C[Int](10)
  val d = new C[Double](10.5)
  val b = new C[Boolean](true)

  println(d + n)    // [1]
  println(n + n)    // [2]
  println(n + b)    // [3]
  // println(n + d)    [4] XXX - no implicit conversion of Double to Int exists
  // println(b + n)    [5] XXX - no implicit conversion of Int to Boolean exists
}

This works for some cases (1, 2, 3) but doesn’t for (4, 5). The reason is that there is implicit widening of type from lower to higher, but not the other way. In a way, the method

def +[T <% A](that:C[T]) = s.add(this.n, that.n)

somehow needs to have a partner method that would look something like:

def +[T, A <% T](that:C[T]):T = that.s.add(this.n, that.n)

but that does not compile for two reasons, firstly that the compiler cannot convert this.n to type T (even though we specify view bound A <% T), and, secondly, that even if it were able to convert this.n, after type erasure the two + methods become ambiguous.

Sorry this is so long. Any help would be much appreciated! Otherwise it seems I have to write out all the operations between all the types explicitly. And it would get hairy if I had to add extra types (Complex is next on the menu…).

Maybe someone has another way to achieve all this altogether? Feels like there’s something simple I’m overlooking.

Thanks in advance!

  • 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-15T13:40:37+00:00Added an answer on May 15, 2026 at 1:40 pm

    Okay then, Daniel!

    I’ve restricted the solution to ignore Boolean, and only work with AnyVals that have a weak Least Upper Bound that has an instance of Numeric. These restrictions are arbitrary, you could remove them and encode your own weak conformance relationship between types — the implementation of a2b and a2c could perform some conversion.

    Its interesting to consider how implicit parameters can simulate inheritance (passing implicit parameters of type (Derived => Base) or Weak Conformance. They are really powerful, especially when the type inferencer helps you out.

    First, we need a type class to represent the Weak Least Upper Bound of all pairs of types A and B that we are interested in.

    sealed trait WeakConformance[A <: AnyVal, B <: AnyVal, C] {
      implicit def aToC(a: A): C
    
      implicit def bToC(b: B): C
    }
    
    object WeakConformance {
      implicit def SameSame[T <: AnyVal]: WeakConformance[T, T, T] = new WeakConformance[T, T, T] {
        implicit def aToC(a: T): T = a
    
        implicit def bToC(b: T): T = b
      }
    
      implicit def IntDouble: WeakConformance[Int, Double, Double] = new WeakConformance[Int, Double, Double] {
        implicit def aToC(a: Int) = a
    
        implicit def bToC(b: Double) = b
      }
    
      implicit def DoubleInt: WeakConformance[Double, Int, Double] = new WeakConformance[Double, Int, Double] {
        implicit def aToC(a: Double) = a
    
        implicit def bToC(b: Int) = b
      }
    
      // More instances go here!
    
    
      def unify[A <: AnyVal, B <: AnyVal, C](a: A, b: B)(implicit ev: WeakConformance[A, B, C]): (C, C) = {
        import ev._
        (a: C, b: C)
      }
    }
    

    The method unify returns type C, which is figured out by the type inferencer based on availability of implicit values to provide as the implicit argument ev.

    We can plug this into your wrapper class C as follows, also requiring a Numeric[WeakLub] so we can add the values.

    case class C[A <: AnyVal](val value:A) {
      import WeakConformance.unify
      def +[B <: AnyVal, WeakLub <: AnyVal](that:C[B])(implicit wc: WeakConformance[A, B, WeakLub], num: Numeric[WeakLub]): C[WeakLub] = { 
        val w = unify(value, that.value) match { case (x, y) => num.plus(x, y)}; 
        new C[WeakLub](w)
      }
    }
    

    And finally, putting it all together:

    object Test extends Application {
      val n = new C[Int](10)
      val d = new C[Double](10.5)
    
      // The type ascriptions aren't necessary, they are just here to 
      // prove the static type is the Weak LUB of the two sides.
      println(d + n: C[Double]) // C(20.5)
      println(n + n: C[Int])    // C(20)
      println(n + d: C[Double]) // C(20.5)
    }
    
    Test
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 528k
  • Answers 528k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have probably heard this before, but as you get… May 16, 2026 at 11:02 pm
  • Editorial Team
    Editorial Team added an answer Simply do a check if ftp_nlist() is an array. Like:… May 16, 2026 at 11:02 pm
  • Editorial Team
    Editorial Team added an answer I've solved this, in order to get the WM_SYSCOMMAND message… May 16, 2026 at 11:02 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have text I am displaying in SIlverlight that is coming from a CMS
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.