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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:34:13+00:00 2026-05-31T07:34:13+00:00

Suppose that I want to write a case class Stepper as follows: case class

  • 0

Suppose that I want to write a case class Stepper as follows:

case class Stepper(step: Int) {def apply(x: Int) = x + step}

It comes with a nice toStringimplementation:

scala> Stepper(42).toString
res0: String = Stepper(42)

but it’s not really a function:

scala> Some(2) map Stepper(2)
<console>:10: error: type mismatch;
 found   : Stepper
 required: Int => ?
              Some(2) map Stepper(2)

A workaround is to implement the Function trait…

case class Stepper(step: Int) extends (Int => Int) {def apply(x: Int) = x + step}

But then, I can’t have for free a nice toString implementation anymore:

scala> Stepper(42).toString
res2: java.lang.String = <function1>

Then, the question is: can I have the best of these two worlds? Is there a solution where I have the nice toString implementation for free AND an implementation of trait Function. In other words, is there a way to apply the linearization in such a way that case class syntaxic sugar is applied at last?

  • 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-31T07:34:14+00:00Added an answer on May 31, 2026 at 7:34 am

    The question is not really to do with linearisation. In case-classes toString is a method automatically generated by the compiler if and only if Any.toString is not overridden in the end-type.

    However, the answer is partly to do with linearisation – we need to override Function1.toString with the method that would have been generated by compiler if not for the version introduced by Function1 :

    trait ProperName extends Product {
      override lazy val toString = scala.runtime.ScalaRunTime._toString(this)
    }
    
    // now just mix in ProperName and... magic!
    case class Stepper(step: Int) extends (Int => Int) with ProperName {
      def apply(x:Int) = x+step
    }
    

    Then

    println(Some(2) map Stepper(2))
    println(Stepper(2))
    

    Will produce

    Some(4)
    Stepper(2)
    

    Update

    Here is a version of ProperName trait that doesn’t rely on the undocumented API method:

    trait ProperName extends Product {
      override lazy val toString  = {
        val caseFields = {
           val arity = productArity
           def fields(from: Int): List[Any] =
             if (from == arity) List()
             else productElement(from) :: fields(from + 1)
           fields(0) 
        }
        caseFields.mkString(productPrefix + "(", ",", ")")
      }
    }
    

    Alternative toString implementation is derived from the source code for the original _toString method scala.runtime.ScalaRunTime._toString.

    Please note that this alternative implementation is still based on the assumption that a case class always extends Product trait. Although the latter holds true as of Scala 2.9.0 and is a fact that is known to and relied upon by some members of Scala community it’s not formally documented as part of Scala Language Spec.

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

Sidebar

Related Questions

Suppose I have a Python class that I want to add an extra property
Lets suppose that I have a list of floats and I want to write
Suppose that I have a table my_table(id, x, y) . I want to write
Suppose that you have two huge files (several GB) that you want to concatenate
Suppose that for every Form in a WinForms application, you want to change the
Suppose I want to put objects that identify a server into a stl set
Suppose I want to implement a reasonably efficient 'keyword recognition algorithm', that is first
Suppose I want to create a set of observers based on type. That is
Example: Suppose in the following example I want to match strings that do not
Suppose, I want to have a link or a button that when user click

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.