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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:02:58+00:00 2026-06-18T10:02:58+00:00

Consider an abstract class defining two properties abstract class A { def a: Int

  • 0

Consider an abstract class defining two properties

abstract class A {
  def a: Int
  def b: Int
  // real A has additional members
}

which is the base class for various case classes such as

case class Foo(a: Int, b: Int) extends A
case class Bar(a: Int, b: Int) extends A
// and many more

Goal: I would finally like to be able to create instances of the aforementioned case classes in two ways, namely

val b1 = Bar(1, 2)
val b2 = Bar(1) has 2
assert(b1 == b2) // must hold

Approach: It therefore seems reasonable to define a helper class that defines has and that allows me to partially construct As

case class PartialA(f: Int => A) {
  def has(b: Int) = f(b)
}

Problem: The current machinery doesn’t allow for calls like Bar(1) because this is actually an invocation of Bar.apply(1), that is, of the method apply as defined by the compiler-generated object Bar.

It would be great if I could force the compiler to generate the Bar object as object Bar extends PartialAConstructor, where

abstract class PartialAConstructor{
  def apply(a: Int, b: Int): A // abstract, created when the compiler creates
                               // object Bar
  def apply(a: Int) = PartialA((b: Int) => apply(a, b))
}

However, it doesn’t seem to be possible to influence the generation of companion objects of case classes.

Desired properties:

  • Case classes: Foo, Bar etc. should remain case classes because I would like to use the compiler-generated goodies such as structural equality, copy and automatically generated extractors.

  • “Full” structural equality: Defining the case classes as

    case class Bar(a: Int)(val b: Int)
    

    is not an option, because the compiler-generated equals method only considers the first list of arguments, and thus the following would hold erroneously:

    assert(Foo(1)(0) == Foo(1)(10))
    
  • As little code repetition as possible: For example, it is of course possible to define a

    def Bar(a: Int) = PartialA((b: Int) => Bar(a, b))
    

    but that would have to be done for every case class extending A, that, is Foo, Bar etc.

  • 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-06-18T10:02:59+00:00Added an answer on June 18, 2026 at 10:02 am

    You could heavily rely on currrying (and on the fact that Foo.apply, as any method, will automatically get promoted to a function) and on a little helper to enhance syntax:

    object partially {
      def apply[A1,A2,R]( f: (A1, A2) => R ) = f.curried
      def apply[A1,A2,R]( f: (A1, A2) => R, a1: A1 ) = f.curried( a1 )
    
      def apply[A1,A2,A3,R]( f: (A1, A2, A3) => R ) = f.curried
      def apply[A1,A2,A3,R]( f: (A1, A2, A3) => R, a1: A1 ) = f.curried( a1 )
      def apply[A1,A2,A3,R]( f: (A1, A2, A3) => R, a1: A1, a2: A2 ) = f.curried( a1 )( a2 )
    
    
      def apply[A1,A2,A3,A4,R]( f: (A1, A2, A3, A4) => R ) = f.curried
      def apply[A1,A2,A3,A4,R]( f: (A1, A2, A3, A4) => R, a1: A1 ) = f.curried( a1 )
      def apply[A1,A2,A3,A4,R]( f: (A1, A2, A3, A4) => R, a1: A1, a2: A2 ) = f.curried( a1 )( a2 )
      def apply[A1,A2,A3,A4,R]( f: (A1, A2, A3, A4) => R, a1: A1, a2: A2, a3: A3 ) = f.curried( a1 )( a2 )( a3 )
      // ... and so on, potentially up to 22 args
    }
    

    Then you can do:

    scala> val x = partially(Foo)(1)
    x: Int => Foo = <function1>
    scala> x(2)
    res37: Foo = Foo(1,2)
    

    If you really want to use your has method (instead of just directly applying the function), throw in an implicit classes on top of that:

    implicit class Func1Ops[-A,+R]( val f: A => R ) extends AnyVal { 
      def has( arg: A ): R = f( arg ) 
    }
    

    and now you can do:

    scala> val x = partially(Foo)(1)
    x: Int => Foo = <function1>
    
    scala> x has 2
    res38: Foo = Foo(1,2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider a webmethod which exposes an abstract class: [WebMethod] public void Save(AbstractEntity obj) {
Please consider two restrictions - Can't move MyProperty to interface or abstract class. FooEventHandler
Consider the following code: abstract class X { def a:Unit a } class Y
Consider I have some abstract Vehicle class and car, truck, motorcycle abstract classes which
Consider the following code: public abstract class Base { public void getAnswer(); } public
Consider the following class hierarchy: public abstract class Entity { public virtual int Id
I have an abstract base class from which many classes are derived. I want
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
I'm defining an abstract base class as a part of an API that will
Consider this code sample: public abstract class Parent { public int val; public Parent()

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.