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

case class pattern match - non-positional?

10 replies
Sophie
Joined: 2011-11-10,
User offline. Last seen 42 years 45 weeks ago.

case class Rectangle(height: Int, width: Int)

Instead of pattern matching by position:
{
case Rectangle(_, w) => use(w)
}

Can I use the field label, something like:

{
case Rectangle(width = w) => use(w)
}

Thanks.

d_m
Joined: 2010-11-11,
User offline. Last seen 35 weeks 2 days ago.
Re: case class pattern match - non-positional?

On Thu, Dec 15, 2011 at 01:36:05PM -0600, Sophie wrote:
> Can I use the field label, something like:
>
> {
> case Rectangle(width = w) => use(w)
> }

I don't think that is possible. If you are just trying to avoid
remembering the order of the params you can also say:

thing match {
case r:Rectangle => use(r.width)
}

Obviously that won't give you the full flexibility of what you want,
but it's something.

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: case class pattern match - non-positional?

certainly an interesting proposal!!

On 15 Dec 2011, at 19:36, Sophie wrote:

> case class Rectangle(height: Int, width: Int)
>
> Instead of pattern matching by position:
> {
> case Rectangle(_, w) => use(w)
> }
>
> Can I use the field label, something like:
>
> {
> case Rectangle(width = w) => use(w)
> }
>
> Thanks.
>
>

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: case class pattern match - non-positional?
Not out of the box, but you can doobject RectangleOfWidth {  def unapply(r: Rectangle) = Some(r.width)}
case RectangleOfWidth(w) =>

On Thu, Dec 15, 2011 at 2:36 PM, Sophie <itsme213 [at] hotmail [dot] com> wrote:
case class Rectangle(height: Int, width: Int)

Instead of pattern matching by position:
{
       case Rectangle(_, w) => use(w)
}

Can I use the field label, something like:

{
       case Rectangle(width = w) => use(w)
}

Thanks.



Sophie
Joined: 2011-11-10,
User offline. Last seen 42 years 45 weeks ago.
Re: case class pattern match - non-positional?

I was expecting (hoping?) it was simpler. Anyway, more than one option,
some nice underlying capabilities (e.g. unapply()), and perhaps a bit
more built-in leaning towards positional in general, as opposed to
named / labeled.

Thanks!

On 2011-12-15 13:36:05 -0600, Sophie said:

> case class Rectangle(height: Int, width: Int)
>
> Instead of pattern matching by position:
> {
> case Rectangle(_, w) => use(w)
> }
>
> Can I use the field label, something like:
>
> {
> case Rectangle(width = w) => use(w)
> }
>
> Thanks.

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: case class pattern match - non-positional?
On Fri, Dec 16, 2011 at 9:18 AM, Sophie <itsme213 [at] hotmail [dot] com> wrote:
I was expecting (hoping?) it was simpler. Anyway, more than one option, some nice underlying capabilities (e.g. unapply()), and perhaps a bit more built-in leaning towards positional in general, as opposed to named / labeled.

FWIW I've wished for the exact same thing in the past... Although it might have been only on IRC.
-0xe1a
Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: case class pattern match - non-positional?
On Fri, Dec 16, 2011 at 10:56 AM, Alex Cruise <alex [at] cluonflux [dot] com> wrote:
On Fri, Dec 16, 2011 at 9:18 AM, Sophie <itsme213 [at] hotmail [dot] com> wrote:
I was expecting (hoping?) it was simpler. Anyway, more than one option, some nice underlying capabilities (e.g. unapply()), and perhaps a bit more built-in leaning towards positional in general, as opposed to named / labeled.

FWIW I've wished for the exact same thing in the past... Although it might have been only on IRC.

Just to make it official... https://issues.scala-lang.org/browse/SI-5323
-0xe1a
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Re: case class pattern match - non-positional?
named pattern matching would be awesome, although I'm not sure how to generalize it to unapply functions....
object Foo {
  def unapply(x: ?): Option[(?,?,?)] // Where do I put the names?
}

On Fri, Dec 16, 2011 at 2:14 PM, Alex Cruise <alex [at] cluonflux [dot] com> wrote:
On Fri, Dec 16, 2011 at 10:56 AM, Alex Cruise <alex [at] cluonflux [dot] com> wrote:
On Fri, Dec 16, 2011 at 9:18 AM, Sophie <itsme213 [at] hotmail [dot] com> wrote:
I was expecting (hoping?) it was simpler. Anyway, more than one option, some nice underlying capabilities (e.g. unapply()), and perhaps a bit more built-in leaning towards positional in general, as opposed to named / labeled.

FWIW I've wished for the exact same thing in the past... Although it might have been only on IRC.

Just to make it official... https://issues.scala-lang.org/browse/SI-5323
-0xe1a

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: case class pattern match - non-positional?
On Fri, Dec 16, 2011 at 11:31 AM, Josh Suereth <joshua [dot] suereth [at] gmail [dot] com> wrote:
named pattern matching would be awesome, although I'm not sure how to generalize it to unapply functions....
object Foo {  def unapply(x: ?): Option[(?,?,?)] // Where do I put the names? }

Well, "obviously", if you return a Product of some sort from your extractor, we can use its element names. ;)
Tuples would be a bit annoying, but the existing syntax is terse enough that I don't think there's much lost.
-0xe1a
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Re: case class pattern match - non-positional?

Case classes are conceptual syntactic sugar around unapply returning a tuple though, so it's a very important question.

On Dec 16, 2011 4:28 PM, "Alex Cruise" <alex [at] cluonflux [dot] com> wrote:
On Fri, Dec 16, 2011 at 11:31 AM, Josh Suereth <joshua [dot] suereth [at] gmail [dot] com> wrote:
named pattern matching would be awesome, although I'm not sure how to generalize it to unapply functions....
object Foo {  def unapply(x: ?): Option[(?,?,?)] // Where do I put the names? }

Well, "obviously", if you return a Product of some sort from your extractor, we can use its element names. ;)
Tuples would be a bit annoying, but the existing syntax is terse enough that I don't think there's much lost.
-0xe1a
Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: case class pattern match - non-positional?
On Fri, Dec 16, 2011 at 3:44 PM, Josh Suereth <joshua [dot] suereth [at] gmail [dot] com> wrote:

Case classes are conceptual syntactic sugar around unapply returning a tuple though, so it's a very important question.

Ah, I see, yes.  Would it make sense to broaden the definition of n-ary unapply to produce Products instead?  That way a case class's unapply method could be:
case class Foo(i: Int, s: String)object Foo {  def unapply(foo: Foo): Option[Product2[Int,String]] = foo}
-0xe1a

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