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

'or' in pattern matching?

9 replies
Robert Nikander
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.

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

Colin Bullock
Joined: 2009-01-23,
User offline. Last seen 42 years 45 weeks ago.
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.)
Jesse Eichar
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
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.)

Colin Bullock
Joined: 2009-01-23,
User offline. Last seen 42 years 45 weeks ago.
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
Walter Chang
Joined: 2008-08-21,
User offline. Last seen 3 years 26 weeks ago.
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
.......\<,
....( )/ ( )...
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
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
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
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
Walter Chang
Joined: 2008-08-21,
User offline. Last seen 3 years 26 weeks ago.
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
.......\<,
....( )/ ( )...
nilskp
Joined: 2009-01-30,
User offline. Last seen 1 year 27 weeks ago.
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?
Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
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?
>

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