- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
'or' in pattern matching?
Hi,
Is it possible to do something like this
cmdLineArg match {
case Or("-h", "--help") => ...
I just read about the `unapply' method, but I'm not sure how I could
use it in this situation.
thanks,
Rob










Re: 'or' in pattern matching?
On Thu, Aug 20, 2009 at 7:57 AM, Robert Nikander <rob [dot] nikander [at] gmail [dot] com> wrote:
Sure is:
scala> "-h" match { case "-h" | "--help" => println("got help") }
got help
scala> "--help" match { case "-h" | "--help" => println("got help") }
got help
- Colin
(Sorry for the dupe Rob, missed reply-all.)
Re: 'or' in pattern matching?
in normal matching you can do:
val o:AnyRef = "hi"
o match { case s:String => println(s) }
but you cannot do this:
scala> o match { case s:String | s:Int => println(s)}
Is there a way to do this or do you have to make 2 separate case statements?
Jesse
On Thu, Aug 20, 2009 at 3:15 PM, Colin Bullock <cmbullock [at] gmail [dot] com> wrote:
Re: 'or' in pattern matching?
On Thu, Aug 20, 2009 at 4:14 PM, Jesse Eichar <jeichar [dot] w [at] gmail [dot] com> wrote:
Something like this ugly duckling?
def foo(x : Any) = x match { case z if z.isInstanceOf[Int] || z.isInstanceOf[String] => println(z) }
--
Viktor Klang
Rogue Scala-head
Blog: klangism.blogspot.com
Twttr: viktorklang
Re: 'or' in pattern matching?
On Thu, Aug 20, 2009 at 10:14 PM, Jesse Eichar <jeichar [dot] w [at] gmail [dot] com> wrote:
--
.......__o
.......\<,
....( )/ ( )...
Re: 'or' in pattern matching?
Yes indeed:
scala> ("foo": Any) match { case x @ (_: String | _: Int) => "stringorint" }
res5: java.lang.String = stringorint
scala> (1: Any) match { case x @ (_: String | _: Int) => "stringorint" }
res6: java.lang.String = stringorint
- Colin
Re: 'or' in pattern matching?
Could someone explain this syntax in detail?
Re: 'or' in pattern matching?
case x if x.instanceOf[String] || x.instanceOf[Int] =>
is equivalent, if that helps. Other than that, search your favourite
Scala reference for "@ ".
2009/8/20 Nils Kilden-Pedersen :
> On Thu, Aug 20, 2009 at 9:22 AM, Colin Bullock wrote:
>>
>> case x @ (_: String | _: Int) =>
>
> Could someone explain this syntax in detail?
>
Re: 'or' in pattern matching?
On Thu, Aug 20, 2009 at 10:22 PM, Colin Bullock <cmbullock [at] gmail [dot] com> wrote:
--
.......__o
.......\<,
....( )/ ( )...
Re: 'or' in pattern matching?
On Thu, Aug 20, 2009 at 4:22 PM, Colin Bullock <cmbullock [at] gmail [dot] com> wrote:
Much cleaner than what I suggested, good find!
--
Viktor Klang
Rogue Scala-head
Blog: klangism.blogspot.com
Twttr: viktorklang