- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
case class pattern match - non-positional?
Thu, 2011-12-15, 20:36
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.
Fri, 2011-12-16, 02:11
#2
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.
>
>
Fri, 2011-12-16, 08:31
#3
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@hotmail.com> wrote:
case RectangleOfWidth(w) =>
On Thu, Dec 15, 2011 at 2:36 PM, Sophie <itsme213@hotmail.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.
Fri, 2011-12-16, 18:31
#4
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.
Fri, 2011-12-16, 20:11
#5
Re: Re: case class pattern match - non-positional?
On Fri, Dec 16, 2011 at 9:18 AM, Sophie <itsme213@hotmail.com> wrote:
FWIW I've wished for the exact same thing in the past... Although it might have been only on IRC.
-0xe1a
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
Fri, 2011-12-16, 20:21
#6
Re: Re: case class pattern match - non-positional?
On Fri, Dec 16, 2011 at 10:56 AM, Alex Cruise <alex@cluonflux.com> wrote:
Just to make it official... https://issues.scala-lang.org/browse/SI-5323
-0xe1a
On Fri, Dec 16, 2011 at 9:18 AM, Sophie <itsme213@hotmail.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
Fri, 2011-12-16, 20:51
#7
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@cluonflux.com> wrote:
object Foo {
def unapply(x: ?): Option[(?,?,?)] // Where do I put the names?
}
On Fri, Dec 16, 2011 at 2:14 PM, Alex Cruise <alex@cluonflux.com> wrote:
On Fri, Dec 16, 2011 at 10:56 AM, Alex Cruise <alex@cluonflux.com> wrote:On Fri, Dec 16, 2011 at 9:18 AM, Sophie <itsme213@hotmail.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
Fri, 2011-12-16, 22:41
#8
Re: Re: case class pattern match - non-positional?
On Fri, Dec 16, 2011 at 11:31 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
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
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
Sat, 2011-12-17, 00:51
#9
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@cluonflux.com> wrote:On Fri, Dec 16, 2011 at 11:31 AM, Josh Suereth <joshua.suereth@gmail.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
Sat, 2011-12-17, 01:31
#10
Re: Re: case class pattern match - non-positional?
On Fri, Dec 16, 2011 at 3:44 PM, Josh Suereth <joshua.suereth@gmail.com> wrote:
case class Foo(i: Int, s: String)object Foo { def unapply(foo: Foo): Option[Product2[Int,String]] = foo}
-0xe1a
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 classes are conceptual syntactic sugar around unapply returning a tuple though, so it's a very important question.
case class Foo(i: Int, s: String)object Foo { def unapply(foo: Foo): Option[Product2[Int,String]] = foo}
-0xe1a
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.