'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:
Hi,

Is it possible to do something like this

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?

I have a related question.

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:

On Thu, Aug 20, 2009 at 7:57 AM, Robert Nikander <rob [dot] nikander [at] gmail [dot] com> wrote:
Hi,

Is it possible to do something like this

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?



On Thu, Aug 20, 2009 at 4:14 PM, Jesse Eichar <jeichar [dot] w [at] gmail [dot] com> wrote:
I have a related question.

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?

Something like this ugly duckling?

def foo(x : Any) = x match { case z if z.isInstanceOf[Int] || z.isInstanceOf[String] => println(z) }

 


Jesse

On Thu, Aug 20, 2009 at 3:15 PM, Colin Bullock <cmbullock [at] gmail [dot] com> wrote:

On Thu, Aug 20, 2009 at 7:57 AM, Robert Nikander <rob [dot] nikander [at] gmail [dot] com> wrote:
Hi,

Is it possible to do something like this

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.)




--
Viktor Klang

Rogue Scala-head

Blog: klangism.blogspot.com
Twttr: viktorklang

Re: 'or' in pattern matching?

o match { case x if x.isInstanceOf[String] || x.isInstanceOf[Int] => println(x) }


On Thu, Aug 20, 2009 at 10:14 PM, Jesse Eichar <jeichar [dot] w [at] gmail [dot] com> wrote:
I have a related question.

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:

On Thu, Aug 20, 2009 at 7:57 AM, Robert Nikander <rob [dot] nikander [at] gmail [dot] com> wrote:
Hi,

Is it possible to do something like this

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.)




--
.......__o
.......\<,
....( )/ ( )...

Re: 'or' in pattern matching?



Is there a way to do this or do you have to make 2 separate case statements?

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?

On Thu, Aug 20, 2009 at 9:22 AM, Colin Bullock <cmbullock [at] gmail [dot] com> wrote:
case x @ (_: String | _: Int) => 

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?

+1  that's cool

On Thu, Aug 20, 2009 at 10:22 PM, Colin Bullock <cmbullock [at] gmail [dot] com> wrote:


Is there a way to do this or do you have to make 2 separate case statements?

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



--
.......__o
.......\<,
....( )/ ( )...

Re: 'or' in pattern matching?



On Thu, Aug 20, 2009 at 4:22 PM, Colin Bullock <cmbullock [at] gmail [dot] com> wrote:


Is there a way to do this or do you have to make 2 separate case statements?

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

Much cleaner than what I suggested, good find!


--
Viktor Klang

Rogue Scala-head

Blog: klangism.blogspot.com
Twttr: viktorklang

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