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

pattern matching code block

2 replies
null
Joined: 2011-10-25,
User offline. Last seen 51 weeks 1 day ago.

Hello,

I was wondering if someone could explain why passing a pattern
matching code block to getOrElse in the REPL session below works,
while passing the same to my contrived method m does not.

scala> def m(block: => Any) = block
m: (block: => Any)Any

scala> val x = 1
x: Int = 1

scala> None getOrElse x match { case _ => x }
res0: Int = 1

scala> m x match { case _ => x }
:10: error: missing arguments for method m in object $iw;
follow this method with `_' if you want to treat it as a partially
applied funct
ion
m x match { case _ => x }
^

I can of course do:

m { x match { case _ => x } }

And that'd work but my question is about passing a pattern matching
block directly.

Thanks,
Keyur

fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Re: pattern matching code block
On 17/12/2011 23:07, Keyur Shah wrote:
8924ea6b-c337-49ca-ba95-2cc62741e0cb [at] k5g2000pra [dot] googlegroups [dot] com" type="cite">
Hello,
[...]
scala> m x match { case _ => x }
<console>:10: error: missing arguments for method m in object $iw;
follow this method with `_' if you want to treat it as a partially
applied funct
ion
              m x match { case _ => x }
              ^
</REPL>
[..]


I believe that this is something to do with how . and () are inferred:

scala> m(x) match { case _ => x }
res2: Int = 1



Thanks,
-- 
Francois ARMAND
http://fanf42.blogspot.com
http://www.normation.com
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: pattern matching code block

Whenever Scala sees "x y z", it will parse that as "x.y(z)". So "m x
match" became "m.x(match)", which doesn't make sense.

On Sat, Dec 17, 2011 at 20:07, Keyur Shah wrote:
> Hello,
>
> I was wondering if someone could explain why passing a pattern
> matching code block to getOrElse in the REPL session below works,
> while passing the same to my contrived method m does not.
>
>
> scala> def m(block: => Any) = block
> m: (block: => Any)Any
>
> scala> val x = 1
> x: Int = 1
>
> scala> None getOrElse x match { case _ => x }
> res0: Int = 1
>
> scala> m x match { case _ => x }
> :10: error: missing arguments for method m in object $iw;
> follow this method with `_' if you want to treat it as a partially
> applied funct
> ion
>              m x match { case _ => x }
>              ^
>
>
> I can of course do:
>
> m { x match { case _ => x } }
>
> And that'd work but my question is about passing a pattern matching
> block directly.
>
>
> Thanks,
> Keyur

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