Quantcast

[scala] pattern matching proposal

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[scala] pattern matching proposal

Vladimir Kirichenko-2
This proposal is inspired by Prolog:

abstract class Tree
case object Leaf extends Tree
case class Node(x: Int, left: Tree, right: Tree)


def m (t: Tree, v:Int) = t match {
   case Node(_, x, x) => // left and right are equal and bound to x
   case Node(v, _, _) => // Node.x should be equal to function argument
                        // or any visible object
}

The same thing with regular pattern matching:

def t2(t: (Int, Int), v: Int) = t match {
       case (x, x) => "same"
       case (_, v) => "second equal to parameter"
}
       
this kind of matches should reduce the need of guard clauses.

--
Best Regards,
Vladimir Kirichenko


signature.asc (262 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [scala] pattern matching proposal

Erkki Lindpere-2
Part of this is already possible, you just need to wrap visible values
in backticks: `v`. I would also like it if you were able to do it
without the backticks (especially as they are not easy to type on all
keyboards), but it's probably not going to happen.
But you can't do the case (x, x) => // same

So your example would be
def m(t: Tree, v: Int) = t match {
  case Node(_, x, y) if x == y => ...
  case Node(`v`, _, _) => ...
}

Erkki

Vladimir Kirichenko wrote:

> This proposal is inspired by Prolog:
>
> abstract class Tree
> case object Leaf extends Tree
> case class Node(x: Int, left: Tree, right: Tree)
>
>
> def m (t: Tree, v:Int) = t match {
>   case Node(_, x, x) => // left and right are equal and bound to x
>   case Node(v, _, _) => // Node.x should be equal to function argument
>             // or any visible object
> }
>
> The same thing with regular pattern matching:
>
> def t2(t: (Int, Int), v: Int) = t match {
>       case (x, x) => "same"
>       case (_, v) => "second equal to parameter"
> }
>    
> this kind of matches should reduce the need of guard clauses.
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [scala] pattern matching proposal

Jon Pretty
In reply to this post by Vladimir Kirichenko-2
Hi Vladimir,

Vladimir Kirichenko wrote:
> def t2(t: (Int, Int), v: Int) = t match {
>       case (x, x) => "same"
>       case (_, v) => "second equal to parameter"
> }

Scala currently makes the distinction between free and bound identifiers
based on whether the first letter of the character is lower- or
upper-case, so you could rewrite t2 as:

   def t2(t : (Int, Int), V : Int) = t match {
      case (_, V) => "second equal to parameter"
      ...

Aside from that, your first case looks similar to reusing the same
identifier in alternating patterns, which also isn't currently
supported, e.g.:

   trait Baz
   case class Foo(x : Int) extends Baz
   case class Bar(y : Int) extends Baz

   def x(b : Baz) = b match {
     case Foo(p) | Bar(p) => p
     case _ => -1
   }

Cheers,
Jon
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [scala] pattern matching proposal

David MacIver
On 11/26/08, Jon Pretty <[hidden email]> wrote:
Hi Vladimir,


Vladimir Kirichenko wrote:
> def t2(t: (Int, Int), v: Int) = t match {
>       case (x, x) => "same"
>       case (_, v) => "second equal to parameter"
> }


Scala currently makes the distinction between free and bound identifiers
based on whether the first letter of the character is lower- or
upper-case, so you could rewrite t2 as:

   def t2(t : (Int, Int), V : Int) = t match {
      case (_, V) => "second equal to parameter"
      ...

Aside from that, your first case looks similar to reusing the same
identifier in alternating patterns, which also isn't currently
supported, e.g.:

   trait Baz
   case class Foo(x : Int) extends Baz
   case class Bar(y : Int) extends Baz

   def x(b : Baz) = b match {
     case Foo(p) | Bar(p) => p
     case _ => -1
   }

I agree that both of these things would be very nice to support. Particularly the identifiers in alternating patterns (I'm not massively enthused by reusing the same identifier in a pattern. It might be worth supporting but doesn't seem a marked improvement over case (x, y) if x == y => x, and interacts with the type system a bit better).

I'm planning to eventually start a SIP suggesting enhancements to Scala's pattern matching, but am waiting until I've got the pattern matcher in a state which I'm happier with (which includes my understanding it better). If anyone else wants to file SIPs about it in the meantime, please be my guest. :-)
Loading...