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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:25:17+00:00 2026-06-14T04:25:17+00:00

Scala-lang reference 5.5.1 and 6.6.1 gave me the impression that a default parameter would

  • 0

Scala-lang reference 5.5.1 and 6.6.1 gave me the impression that a default parameter would be able to refer to a previously evaluated one:

class Test(val first: String, val second: String = first)

but from experimenting it seems the only way to do this is to use the form:

class Test(val first: String)(val second: String = first)

and then define an auxiliary constructor or a creational companion class to avoid specifying the second set of brackets when creating. I don’t really understand how this second constructor works, it looks like a curried function so I might guess that it is necessary to evaluate first independently of second, is this correct? Is this form necessary or is there some syntatic sugar I can use to tweak the first constructor into doing what I want?

  • 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-14T04:25:18+00:00Added an answer on June 14, 2026 at 4:25 am

    As Travis Brown points out, you can indeed only refer to a previous argument in a default expression when it is from a previous argument list (so you do need to currify).

    Now, regarding your particular use case, default arguments and method overloading are sometimes two ways of achieving the same thing.

    I think the simplest solution to your scenario is simply to define Test as follows:

    class Test(val first : String, val second : String) {
      def this(f : String) = this(f, f)
    }
    

    If you want to make it more complicated, an alternative way, using a companion object:

    class Test(val first : String)(val second : String = first)
    object Test {
      def apply(f : String) = new Test(f)
      def apply(f : String, s : String) = new Test(f)(s)
    }
    

    (A small difference is that now you create objects without new.)

    What you cannot do, is define it as:

    class Test(val first : String)(val second : String = first) {
      def this(f : String, s : String) = this(f)(s)
    }
    

    …because the curried version gets translated into (among other things) a method with the same signature as the overloaded contructor.

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

Sidebar

Related Questions

How to do that: http://download.oracle.com/javase/tutorial/uiswing/components/spinner.html using that: http://www.scala-lang.org/api/current/index.html#package ?
Is the current Scala Eclipse plugin ( http://www.scala-lang.org/node/94 ) usable? I keep reading that
In the example on this page: http://www.scala-lang.org/node/125 class Point(xc: Int, yc: Int) { val
I have a scala .class file that I convert to a jar and try
While working with a Java class in Scala, I noticed that Scala can't multiply
Details on the packages/types is in the Scala API documentation on scala-lang.org. But that's
The Scaladoc page for Random ( http://www.scala-lang.org/api/current/scala/util/Random.html ) specifies that nextDouble Returns the next
I'm trying out the code at http://www.scala-lang.org/node/112 and I'm getting a match error for
In Scala, { x: Option[Int] => x } .getClass .getMethod(apply, classOf[Option[_]]) .getGenericParameterTypes returns Array(scala.Option<java.lang.Object>)
scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:

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.