This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Scala and the fly likes What's the value of case class X(x:Int)(y:String) vs case class X(x:Int, y:String)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "What Watch "What New topic
Author

What's the value of case class X(x:Int)(y:String) vs case class X(x:Int, y:String)?

Jacek Laskowski
Ranch Hand

Joined: Nov 06, 2005
Posts: 34

Hi,

I've been running into it quite often, but can't find the answer - why would one use two or more constructor's parameter lists over one? When is case class X(x:Int)(y:String) better than case class X(x:Int, y:String)? I'd highly appreciate samples of their use cases.

Jacek


Jacek Laskowski - http://blog.japila.pl
Konrad Malawski
Greenhorn

Joined: Feb 26, 2013
Posts: 1

Hello Jacek, you've made me to register here so I could reply ;-)

To start out, I've never seen anyone using a case class with multiple parameter lists.

Back to what it actually does, first the normal syntax:



And let's investigate what your ()() syntax does to a case class:



Let's take a look at it's apply (B$ is the companion object of B):



So well... I don't see any reason why one would use the second syntax you proposed.

It makes a lot of sense for methods (it's for currying there), but definitely not for case classes - keep 'em simple :-)

Where did you bump into code using ()() in case classes? I can imagine using an implicit parameter list there, but otherwise I see no use case that appeals to me.


GeeCON.org - Let's move the Java world!
Theodore Norvell
Greenhorn

Joined: Aug 09, 2013
Posts: 1
It is so you can write matches without mentioning y. E.g., e match {case X(x) => ... }.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1479
    
  11

Welcome to Javaranch, Konrad and Theodore, and thanks for your thoughtful responses to Jacek's question.


No more Blub for me, thank you, Vicar.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684

Hi,

May be below link help you,

Case Classes


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's the value of case class X(x:Int)(y:String) vs case class X(x:Int, y:String)?
 
Similar Threads
How do i 'curry' in scala
Method Syntax
need to run threads with different parameters
The fizz buzz coding challenge
def vs val