- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
pattern matching code block
Sat, 2011-12-17, 23:07
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
Sun, 2011-12-18, 03:41
#2
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
I believe that this is something to do with how . and () are inferred:
scala> m(x) match { case _ => x }
res2: Int = 1
Thanks,