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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:36:35+00:00 2026-05-23T18:36:35+00:00

What is the best way to tackle this problem? Here is a simple class

  • 0

What is the best way to tackle this problem?
Here is a simple class hierarchy in F#:

[<AbstractClass>]
type BaseClass (name: string) = 
  member this.Name with get() = name

type FooClass (n, i: int) =
  inherit BaseClass (n)
  member this.Num with get() = i

type BarClass (n) = inherit BaseClass (n)

I will need to add to this hierarchy quite a lot as the project advances.
I need to equate instances of these classes by checking if they are the same type and have the same value for Name (and also the same value for Num if its a FooClass), so ReferenceEquals won’t cut it. What is the best way to do this? Should I make BaseClass inherit IComparable and if so, how should I then deal with the types that inherit from BaseClass and have extra fields to check? Another way of doing it would be to make an IComparer class that checks for every different class but I really want the code to be in the appropriate class rather than in a comparer

  • 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-23T18:36:36+00:00Added an answer on May 23, 2026 at 6:36 pm

    I believe overriding equals on the base class, and then overriding equals on each sub class by leveraging the base class implementation is the standard approach in .NET OO:

    [<AbstractClass>]
    type BaseClass (name: string) = 
      member this.Name with get() = name
      override this.Equals(other:obj) =
        match other with
        | :? BaseClass as other ->
            other.Name = this.Name
        | _ -> false
    
    
    type FooClass (n, i: int) =
      inherit BaseClass (n)
      member this.Num with get() = i
      override this.Equals(other:obj) =
        match other with
        | :? FooClass as other when base.Equals(other) -> //check type equality with this, and base equality
            other.Num = this.Num //check the additional properties for this supertype
        | _ -> false
    
    type BarClass (n, i: bool) =
      inherit BaseClass (n)
      member this.Truth with get() = i
        override this.Equals(other:obj) =
        match other with
        | :? BarClass as other when base.Equals(other) ->
            other.Truth = this.Truth
        | _ -> false
    
    let f1 = FooClass("Foo", 1)
    let f2 = FooClass("Foo", 1)
    f1 = f2 //true
    let f3 = FooClass("Foo", 2)
    f1 = f3 //false
    
    //polymorphism in action (bc1 BaseClass will use sub class override)
    let checkBaseClassesForEquality (bc1:BaseClass) (bc2:BaseClass) =
        bc1 = bc2
    
    checkBaseClassesForEquality f1 f2 //true
    checkBaseClassesForEquality f1 f3 //false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been wondering what is the best way to tackle this situation. Is
What's the best way to approach this problem: I have a list of navigation
I'm looking for sort of a 'best practice' way to tackle this common scenario.
i'm looking for the best way to tackle this issue and in what order
the best way to explain is with example so: this is the model public
The best way I can think of to ask this is by example... In
The best way of describing this is I have a table of people with
What is the best way to verify/test that a text string is serialized to
How would you tackle this problem: I have data in my data store. Each
This seems like an incredibly simple problem however it isn't working out as trivially

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.