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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:00:58+00:00 2026-06-11T18:00:58+00:00

How does one declare in Scala the type signatures needed to implement multiple arithmetic

  • 0

How does one declare in Scala the type signatures needed to implement multiple arithmetic systems that all share a same trait declaring the operations? I thought I had it solved until I tried to add some helper implementations to the base trait/class. In the following code-snippet (which compiles) notice that sq() is defined as this*self not the more obvious this*this. And self() can not be implemented in the trait either (we have to wait until we get to a concrete extending class to implement it).

trait NumberBase [NUMBERTYPE <: NumberBase[NUMBERTYPE]] {
   // type NUMBERTYPE >: this.type
   def *(that: NUMBERTYPE):NUMBERTYPE
   def self:NUMBERTYPE
   def sq:NUMBERTYPE = { this*self }
}

class D(val v:Double) extends NumberBase[D] {
   def self:D = { this }
   def *(that: D):D = { new D(this.v*that.v) }
}

The question/goal is: remove the use of self() or (at least move self()’s implementation into NumberBase) without changing the type signature of * in D. A lot of the fixes in the above link make the derived class unimplementable (things like new D() not being a type returnable by * or * having an unguessable type signature in D). I don’t mind some of the signatures getting uglier- but I would like the code to express what this code expresses: that the derived classes work on their own types only and return instances of their own type (they are not moving up and down in a hierarchy).

I found some discussion here that lists some problems (but don’t see a solution): http://www.scala-lang.org/node/839 . Some issues you don’t run into until you see both the base class and the implementation.

Complete code with the work-around given above (forcing implementing classes to implement self()) is given here: https://github.com/WinVector/AutoDiff where we use the fact different arithmetic systems share the same base-class or trait to write functions that are generic over different arithmetic implementations. This lets us use a wrapping of standard machine arithmetic (like D) or something else (like a number system that computes gradients as a side-effect).

  • 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-11T18:00:59+00:00Added an answer on June 11, 2026 at 6:00 pm

    I think you want to use a self-type, which will ensure that the NumberBase[N] instance will also be an instance of type N:

    trait NumberBase[N <: NumberBase[N]] { this: N =>
      def *(that: N): N
      def sq: N = this * this
    }
    
    class D(val v: Double) extends NumberBase[D] {
      def *(that: D): D = new D(this.v * that.v)
    }
    
    val a = new D(0.5)
    val b = new D(0.25)
    
    a * b  // D(0.125)
    a.sq   // D(0.25)
    

    BUT, if you really want to define a new type of number, you should do what the Scala libraries themselves do and use the Numeric typeclass.

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

Sidebar

Related Questions

In C#, how does one create a delegate type that maps delegate types to
How does one wait until all of the Javascript is loaded before curling a
How does one implement a multithreaded single process model in linux fedora under c
How does one write a (Intel) F90 function that converts a string into lowercase
How does one alternate row colors in a table in django that's generated using
Does all the columns in select statement gets selected one after another as listed?
Normally one would declare/allocate a struct on the stack with: STRUCTTYPE varname; What does
Why does SQL Server insist that the temp table already exists! one or the
[C++11: 12.8/7]: If the class definition does not explicitly declare a copy constructor, one
How does one declare stdin , stout , and stderr (preferably the C versions)

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.