- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
pattern matching: case classes
Fri, 2009-01-23, 15:28
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
Fri, 2009-01-23, 16:07
#2
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 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
---------- 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
Fri, 2009-01-23, 16:17
#3
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
>
>
Fri, 2009-01-23, 17:27
#4
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
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
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:
--
Viktor Klang
Senior Systems Analyst