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

The Archive Base Latest Questions

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

Refer to the following code snippet: trait Fruit { val color:String def == (fruit:Fruit)

  • 0

Refer to the following code snippet:

 trait Fruit {
   val color:String
   def == (fruit:Fruit) = this.color == fruit.color
 }

 case class Orange(color:String) extends Fruit 

 case class Apple(color:String) extends Fruit

As expected, Orange("red") == Orange("red") is true. However, I would like to enforce that only the same type of fruits can be compared, so for instance Orange("red") == Apple("red") should give an error. Can we enforce this in the signature of == in trait Fruit in an elegant way?

EDIT: I want the error to be caught at compile time, not at runtime.

  • 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-20T13:57:36+00:00Added an answer on May 20, 2026 at 1:57 pm

    Scalaz has an Equal “type class” that solves this problem, albeit with a different operator.

    https://github.com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/Equal.scala

    The heart of it is basically this (although I use === where they use some unicode)

    /** Defines a type safe === operator */
    trait Equals[A] {
     def ===(y : A) : Boolean
    }
    
    /** A conventient way to define Equals traits based on the == operator */
    def equalA[A](x : A) = new Equals[A] {
      def ===(y : A) = x == y
    }
    

    And is used like so

    // one for oranges
    implicit val EqualsOrange = equalA[Orange] _
    
    // one for apples
    implicit val EqualsApple = equalA[Apple] _
    
    
    Orange("red") === Orange("red") // true
    
    Orange("red") === Orange("green") // false
    
    Orange("red") === Apple("red") // Compile error
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code public class Test { public static void main(String[] args){ int
I have the following code: $('<div id=' + id + '><a href=# class=edit>Edit</a> |
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
Given an aggregation of class instances which refer to each other in a complex,
In JavaScript, the this operator can refer to different things under different scenarios. Typically
Consider the following code: // t included so block1 is a stack block. See
I have the following code to format a URL: <cfset textToFormat = ReReplaceNoCase( textToFormat,
I'm getting stackoverflow exceptions on the following code... IQueryable<MilestoneList> ml = from MilestoneList d
I'm trying to refer to a drive whose letter may change. I'd like to
The test driven development guys refer to a quick, exploratory, investigation that involves coding

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.