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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:39:47+00:00 2026-05-26T16:39:47+00:00

Suppose that we define an interface like this: interface Hashable { int hash(); }

  • 0

Suppose that we define an interface like this:

interface Hashable {
   int hash();
}

Now we can make classes that implement this interface. For example a List class:

class List<T> : Hashable {
   int hash(){
      int h = 0;
      foreach(x in this){
         h = combineHash(h,x.hash());
      }
      return h;
   }
}

The problem is that we call hash on elements inside the List<T>, so T has to be Hashable. So we have to do:

class List<T : Hashable> : Hashable {
   ... same here ...
}

On the other hand, we also want to be able to construct lists with elements that are not hashable. In that case we simply don’t want List<T> to implement Hashable. So:

List<AHashableType>      is itself Hashable
List<NotAHashableType>   is not Hashable

Furthermore, we want the hash method to be polymorphic (the OO kind of polymorphic, not parametrically polymorphic). So if we construct a List<Foo> with Bar‘s and Baz‘s inside, where Foo is Hashable and Bar and Baz are subtypes of Foo with different hash() methods, then that List<Foo>.hash() should call the right hash method at runtime.

This seems like a basic thing that languages should be able to express. The same pattern comes up in different cases, for example with ToString() and with IComparable. So far I haven’t found a language and a solution in that language that lets me express this in a type safe way. Do you know of such a language and a solution in that langauge? Haskell comes quite close with its type classes, but unfortunately these are dispatched at compile time.

  • 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-26T16:39:47+00:00Added an answer on May 26, 2026 at 4:39 pm

    I think it’s useful to separate the two issues you’ve described. The first is conditionally implementing an interface. Like you said, Haskell type classes do this. It would look something like:

    class Hashable t where
      hash :: t -> Int
    
    instance Hashable Int where
      hash i = i
    
    -- Reads as "(List t) is Hashable if t is Hashable"
    instance (Hashable t) => Hashable (List t) where
      hash l = ...
    

    The second issue is OO polymorphism, which you can mimic in Haskell with a function-pointer-like mechanism.

    -- Pretending we have some kind of OO method dispatch here
    instance Hashable Foo where
        hash foo = foo.computeHash()
    
    class Foo
       computeHash()
    

    I’ve read a couple papers that add this sort of “conditional interface implementation” to a Java-like language. One of them is JavaGI (http://homepages.cwi.nl/~ralf/JavaGI/), which also adds the ability to implement an interface for a data type that you didn’t originally define (like Haskell type classes). I can’t remember what the other paper was called.

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

Sidebar

Related Questions

Suppose that I define some class: class Pixel { public: Pixel(){ x=0; y=0;}; int
I'm writing an interface that requires classes to implement a clone() method. My naive
Let's suppose I have a struct like this: struct my_struct { int a; int
I understand that we need to create MXML file to define a view. Suppose
Suppose that I have a Java program within an IDE (Eclipse in this case).
Suppose that I have a Java class with a static method, like so: class
Why would I want to define a C++ interface that contains private methods? Even
There is a design problem like this. Suppose you have a set of class
I have a Data Access Layer library that I would like to make portable.
Probably a simple question. I have an interface (MyInterface) that defines a property like

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.