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

  • Home
  • SEARCH
  • 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 6972727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:00:30+00:00 2026-05-27T17:00:30+00:00

Say I have a simple class AClass with a public member f1 which can

  • 0

Say I have a simple class AClass with a public member f1 which can be overridden. What are the ways to define a new instance of AClass with another member f2, short of duplicating the source code of AClass? Toy code below:

class AClass a where
    f1 :: a -> Int

data Val = I Int

instance AClass Val where
  f1 x = 0

  -- the method below can't be added as it is not public member of AClass
  -- f2:: a -> Float
  -- f2 x = 0.0 

I looked around but I didn’t find any clear examples on how to do it (i.e., examples I could understand well – clarity is relative). What are the possible ways? Closure, newtype declaration or something else? It will be helpful to demonstrate the technique with above toy code – you can change data declaration etc. (e.g., replace it with a newtype wrapper around Int) but the only thing immutable in above code is class declaration of AClass. This is because the assumption is the class has already been written by a library writer, and so, I can’t touch it. The end result should be another toy code which inherits the goodies of AClass, and adds f2 member.

There will be caveats of course, in overriding classes like this. But, it helps to know what is possible, and how.

— Update —

Working code below – Credit to Ben and mergeconflict for coming up with the solution – there were few missing pieces – filled in below:

class AClass a where
    f1 :: a -> Int

class (AClass a) => BClass a where
    f2 :: a -> Float

data Val = I Int

instance AClass Val where
   f1 _ = 0

instance BClass Val where
   f2 _ = 0.0                       
  • 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-27T17:00:30+00:00Added an answer on May 27, 2026 at 5:00 pm

    What are you trying to achieve?

    You have a type Val, which you make an instance of AClass. You can define any number of functions that use Val that have nothing to do with the class. Just stop trying to define them in the instance declaration.

    If what you’re expecting is to be able to have one particular instance of AClass that has an extra f2 function, which you then use in functions that use AClass instances and have them be able to call f2… that’s absurd. By definition, the only things that are known to be common to all AClass instances are the things declared in AClass. If all you know about some value is that it is a member of a type that is an instance of AClass, you can’t do anything with it that you can’t do with all instances of AClass. You can’t call anything extra that’s particular to certain instances.

    If you want to create a new class that supports all of the operations that AClass does as well as f2, and have Val be an instance of that new class… then you just do that.

    class AClass a => AnotherClass a where
        f2 :: a -> Float
    
    instance AnotherClass Val where
        f2 x = 0.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a simple address class like below: public class Address { public
Let's say I have a regular simple Java class, such as: public class Foo
Say I have a simple object such as class Something { public int SomeInt
Simple question on best practice. Say I have: public class Product { public string
Say we have this hyper simple class hierchy: public class SomeMath { public int
Hi Im stuck on a simple problem Say I have a class public Abstract
Lets say I have a class A that is fairly simple like this -
Say you have a simple class with some storage data structure (list, vector, queue,
Let's say I have a very simple PrototypeJS class that looks like this: var
Say I have the following simple java bean: class MyBean { private Date startDate;

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.