This page is no longer maintained — Please continue to the home page at www.scala-lang.org

pattern matching: case classes

4 replies
beloved
Joined: 2009-01-08,
User offline. Last seen 42 years 45 weeks ago.

hello,

I'm working on code to parse text file with rows consisting of colums of
different values. According to those values I need to create objects for
each row. I would like to use case classes feature of scala for this task.

I've finally gone to following class hierarchy:

case class WfRow(st: Integer, skutFazaNext: String, skutFazaSprac: String,
stav: String, dateIn: Date, dateOut: Date) {
def this(skutFazaNext: String) = this(null, skutFazaNext, null, null,
null, null)
def this(skutFazaNext: String, skutFazaSprac: String) = this(null,
skutFazaNext, skutFazaSprac, null, null, null)
}

case class SendToLiquid(skutFazaNext: String) extends WfRow(skutFazaNext)
case class ElfSendToConfirm(skutFazaNext: String, skutFazaSprac: String)
extends WfRow(skutFazaNext, skutFazaSprac)

but I've got compilation error:
error overriding value skutFazaNext in class WfRow of type String;
value skutFazaNext needs `override' modifier

what would by correct way to define SendToLiquid method?
Thanks.

peter lopen

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: pattern matching: case classes
Signature for WfRow:

case class WfRow(st: Integer, skutFazaNext: String, skutFazaSprac: String, stav: String, dateIn: Date, dateOut: Date)

and when you extend it, you only specify

case class SendToLiquid(skutFazaNext: String) extends WfRow(skutFazaNext)
case class ElfSendToConfirm(skutFazaNext: String, skutFazaSprac: String) extends WfRow(skutFazaNext, skutFazaSprac)

You have to set ALL parameters for the superclass' constructor args:

WfRow(Integer, String, String, String, Date, Date)

Cheers,
Viktor

On Fri, Jan 23, 2009 at 3:26 PM, beloved <peter.lopen@gmail.com> wrote:

hello,

I'm working on code to parse text file with rows consisting of colums of
different values. According to those values I need to create objects for
each row. I would like to use case classes feature of scala for this task.

I've finally gone to following class hierarchy:

case class WfRow(st: Integer, skutFazaNext: String, skutFazaSprac: String,
stav: String, dateIn: Date, dateOut: Date) {
 def this(skutFazaNext: String) = this(null, skutFazaNext, null, null,
null, null)
 def this(skutFazaNext: String, skutFazaSprac: String) = this(null,
skutFazaNext, skutFazaSprac, null, null, null)
}

case class SendToLiquid(skutFazaNext: String) extends WfRow(skutFazaNext)
case class ElfSendToConfirm(skutFazaNext: String, skutFazaSprac: String)
extends WfRow(skutFazaNext, skutFazaSprac)

but I've got compilation error:
error overriding value skutFazaNext in class WfRow of type String;
 value skutFazaNext needs `override' modifier

what would by correct way to define SendToLiquid method?
Thanks.

peter lopen




--
View this message in context: http://www.nabble.com/pattern-matching%3A-case-classes-tp21625329p21625329.html
Sent from the Scala mailing list archive at Nabble.com.




--
Viktor Klang
Senior Systems Analyst
Colin Bullock
Joined: 2009-01-23,
User offline. Last seen 42 years 45 weeks ago.
Fwd: pattern matching: case classes
My inability to remember to reply-all strikes again...


---------- Forwarded message ----------
From: Colin Bullock <cmbullock@gmail.com>
Date: Fri, Jan 23, 2009 at 8:54 AM
Subject: Re: [scala] pattern matching: case classes
To: beloved <peter.lopen@gmail.com>




case class WfRow(st: Integer, skutFazaNext: String, skutFazaSprac: String,
stav: String, dateIn: Date, dateOut: Date) {
 def this(skutFazaNext: String) = this(null, skutFazaNext, null, null,
null, null)
 def this(skutFazaNext: String, skutFazaSprac: String) = this(null,
skutFazaNext, skutFazaSprac, null, null, null)
}

case class SendToLiquid(override val skutFazaNext: String) extends WfRow(skutFazaNext)
Constructor parameters are translated to public vals in case classes, hence you need to explicitly override it in your subclass. As an alternative, making WfRow just an abstract class (as opposed to a case class) solves your problem as well.

- Colin

beloved
Joined: 2009-01-08,
User offline. Last seen 42 years 45 weeks ago.
Re: Fwd: pattern matching: case classes

ok, thanks.

but I still have a problem with matching code:
I'm tryng to match instance of WfRow by this code (in application instances
of WfRow are created by parsing file rows)

new WfRow("Vratene FA") match {
case SendToLiquid("Vratene FA") => "SendToLiquid"
case ElfSendToConfirm("Schvalenie FA", "Danova kontrola FA") =>
"ElfSendToConfirm"
case _ => "no match"
}

but there is always "no match" value returned, but I would like match it to
"SendToLiquid".
And this code:
new WfRow("Vratene FA") match {
case WfRow("Vratene FA") => "SendToLiquid"
case ElfSendToConfirm("Schvalenie FA", "Danova kontrola FA") =>
"ElfSendToConfirm"
case _ => "no match"
}
is not compilable:
wrong number of arguments for :
(java.lang.Integer,String,String,String,java.util.Date,java.util.Date)sk.asseco.sse.migration.cm.WfRow
although constructor with only one argumet is defined.

Am I using case classes correctly? Is it even usefull for this kind of task?

Thanks.
Peter Lopen

Colin Bullock wrote:
>
> My inability to remember to reply-all strikes again...
>
>
> ---------- Forwarded message ----------
> From: Colin Bullock
> Date: Fri, Jan 23, 2009 at 8:54 AM
> Subject: Re: [scala] pattern matching: case classes
> To: beloved
>
>
>
>
> case class WfRow(st: Integer, skutFazaNext: String, skutFazaSprac: String,
>> stav: String, dateIn: Date, dateOut: Date) {
>> def this(skutFazaNext: String) = this(null, skutFazaNext, null, null,
>> null, null)
>> def this(skutFazaNext: String, skutFazaSprac: String) = this(null,
>> skutFazaNext, skutFazaSprac, null, null, null)
>> }
>>
>
> case class SendToLiquid(*override val *skutFazaNext: String) extends
> WfRow(skutFazaNext)
>
> Constructor parameters are translated to public vals in case classes,
> hence
> you need to explicitly override it in your subclass. As an alternative,
> making WfRow just an abstract class (as opposed to a case class) solves
> your
> problem as well.
>
> - Colin
>
>

Colin Bullock
Joined: 2009-01-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Fwd: pattern matching: case classes
The compiler creates the case class "magic" only for the primary constructor. If you want additional constructors, you would need to implement the corresponding apply()/unapply() methods in the companion object (at which point, case classes aren't bringing much to the party). Look up "extractor objects" for more info on this (chapter 24 in Programming in Scala).

However, depending on your use case, it may be simpler to just split WfRow into multiple case classes, such as:

case class WfRowA(st: Integer, skutFazaNext: String, skutFazaSprac: String, stav: String, dateIn: Date, dateOut: Date)
case class WfRowB(skutFazaNext: String)
case class WfRowC(skutFazaNext: String, skutFazaSprac: String)


- Colin

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland