Win a copy of Functional Reactive Programming this week in the Other Languages 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
  • 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
  • Likes 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
  • Likes 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
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

May be below link help you,

Case Classes

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic