named parameters in case matching

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

named parameters in case matching

Matthew Pocock-3
Hi,

Consider:

case class Name(fst: String, family: String)

val me = Name("Matthew", "Pocock")

me match { case Name(a, b) => (a, b) }
me match { case Name(a, _) => a }
me match { case Name(family = b) => b}

The third of those uses a named variable. It isn't currently legal scala. Would it be worth at some point in the future extending pattern matching to allow named parameters like this?

The rules would be that the name must match a constructor argument. If you include a named match, then everything else in that list must be named. If a named match is used, all constructor args not referred to are treated as if they were matching `_`.

What do you think?

Matthew

--
Dr Matthew Pocock
Turing ate my hamster LTD

Integrative Bioinformatics Group, School of Computing Science, Newcastle University

skype: matthew.pocock
tel: (0191) 2566550
mob: +447535664143

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: named parameters in case matching

som-snytt

There is

https://issues.scala-lang.org/browse/SI-6524

https://issues.scala-lang.org/browse/SI-5323

My PR for warning about unused pattern variables lets the idiom `x @ _` stand in for `C(x = _)`.

Now you make me wonder if it should warn if `x` doesn't correspond to the parameter name. Otherwise the name `x` is "documentary" but arbitrary.

On Tuesday, December 27, 2016 at 4:32:46 PM UTC-8, Matthew Pocock wrote:
Hi,

Consider:

case class Name(fst: String, family: String)

val me = Name("Matthew", "Pocock")

me match { case Name(a, b) => (a, b) }
me match { case Name(a, _) => a }
me match { case Name(family = b) => b}

The third of those uses a named variable. It isn't currently legal scala. Would it be worth at some point in the future extending pattern matching to allow named parameters like this?

The rules would be that the name must match a constructor argument. If you include a named match, then everything else in that list must be named. If a named match is used, all constructor args not referred to are treated as if they were matching `_`.

What do you think?

Matthew

--
Dr Matthew Pocock
Turing ate my hamster LTD
mailto: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="JHuM2X_KCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">turingate...@gmail.com

Integrative Bioinformatics Group, School of Computing Science, Newcastle University
mailto: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="JHuM2X_KCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">matthew...@....uk

gchat: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="JHuM2X_KCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">turingate...@gmail.com
msn: <a href="javascript:" target="_blank" gdf-obfuscated-mailto="JHuM2X_KCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">matthew...@....uk
<a href="http://irc.freenode.net/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Firc.freenode.net%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFhNyzFF1ZqsAYo2JfkuSWSNMRKUw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Firc.freenode.net%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFhNyzFF1ZqsAYo2JfkuSWSNMRKUw&#39;;return true;">irc.freenode.net: drdozer
skype: matthew.pocock
tel: (0191) 2566550
mob: +447535664143

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.