This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jacek Laskowski
Ranch Hand
Posts: 42
IntelliJ IDE Mac OS X Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Konrad Malawski
Greenhorn
Posts: 1
Java Linux Scala
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Theodore Norvell
Greenhorn
Posts: 6
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is so you can write matches without mentioning y. E.g., e match {case X(x) => ... }.
 
chris webster
Bartender
Pie
Posts: 2366
31
Linux Oracle Postgres Database Python Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Javaranch, Konrad and Theodore, and thanks for your thoughtful responses to Jacek's question.
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

May be below link help you,

Case Classes

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic