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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:11:31+00:00 2026-05-25T11:11:31+00:00

I’d like to define a trait with some properties which have a well defined

  • 0

I’d like to define a trait with some properties which have a well defined relationship – for example’s sake, let’s say that a * b = c. The idea is that implementations of this trait can provide two of these and have an accessor for the the third property derived automatically.

(This is the same feature as Haskell’s type classes, if I remember those correctly, where if you defined < from Ord, >= would be implemented as ! . < – though you could define any subset of functions so long as the remainder could be inferred.) (I don’t remember Haskell’s type classes correctly.)

The naïve approach actually works fairly well:

trait Foo {
  // a * b = c
  def a: Double = c / b
  def b: Double = c / a
  def c: Double = a * b
}

class FooOne(override val a: Double, override val b: Double) extends Foo
class FooTwo(override val a: Double, override val c: Double) extends Foo

Here, implementations FooOne and FooTwo are complete implementations of Foo, and behave as expected. So far, so good; this approach does allow classes to define two of the properties and get the third one “for free”.

However, things start to look less rosy if one defines a third class:

class FooFail(override val a: Double) extends Foo

This compiles just fine – however, it will cause a stack overflow if its b or c methods are ever evaluated.


So the naïve approach gives the inference aspect of Haskell’s type class approach, but we don’t have compile-time safety. What I’d like is for the compiler to complain if less than two of the methods are defined by implementing classes. Clearly the current syntax isn’t sufficient here; we need the methods to be considered abstract, albeit with a default implementation which can be used if and only if the dependent methods are non-abstract.

Does Scala expose appropriate semantics to define this? (I don’t have a problem if there’s a somewhat roundabout way of defining it, similar to union types, since I’m not aware of any first-class support for this in the language).

If not, I’ll make do with the naïve approach and just define and test my classes carefully. But I really think this is something that the type system should be able to catch (after all – it’s not Ruby. :)).

  • 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-25T11:11:31+00:00Added an answer on May 25, 2026 at 11:11 am

    Use implicits:

    object test {
      case class A(v : Double)
      case class B(v : Double)
      case class C(v : Double)
    
      implicit def a(implicit b : B, c : C) = A(c.v / b.v)
      implicit def b(implicit a : A, c : C) = B(c.v / a.v)
      implicit def c(implicit a : A, b : B) = C(a.v * b.v)
    
      def foo(implicit a : A, b : B, c : C) = 
        a + ", " + b + ", " + c
    
      // Remove either of these and it won't compile
      implicit val aa = A(3)
      implicit val bb = B(4)
    
      def main(args : Array[String]) {
        println(foo)
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I've got a string that has curly quotes in it. I'd like to replace
I have text I am displaying in SIlverlight that is coming from a CMS
I used javascript for loading a picture on my website depending on which small
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.