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

Is there a way to assign calculated values in guards to variables?

3 replies
David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Howdy,
In pattern matching, I can assign the matched value to a variable:
{  case f @ Foo(_) => f.toString}
I'd like to be able to assign a computed value from a guard as well.  For example:
{  case Some(id) if (record @ findRecord(id)).isDefined => record.name}
Is there any way to do this?
Thanks,
David

--
Lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Is there a way to assign calculated values in guards to va

On Thu, Jan 15, 2009 at 1:19 AM, David Pollak
wrote:
> Howdy,
> In pattern matching, I can assign the matched value to a variable:
> {
> case f @ Foo(_) => f.toString
> }
> I'd like to be able to assign a computed value from a guard as well. For
> example:
> {
> case Some(id) if (record @ findRecord(id)).isDefined => record.name
> }
> Is there any way to do this?

Just to confirm the silence on the list since then: no, there's no way
to do this.

Cheers

StephaneLD
Joined: 2008-08-28,
User offline. Last seen 4 years 9 weeks ago.
Re: Is there a way to assign calculated values in guards to va
There's a way if you decide to try the Extractor way but you will not have the same syntax and it's less flexible.Good luck!Stephane

On Wed, Jan 21, 2009 at 10:39 PM, martin odersky <martin [dot] odersky [at] epfl [dot] ch> wrote:
On Thu, Jan 15, 2009 at 1:19 AM, David Pollak
<feeder [dot] of [dot] the [dot] bears [at] gmail [dot] com> wrote:
> Howdy,
> In pattern matching, I can assign the matched value to a variable:
> {
>   case f @ Foo(_) => f.toString
> }
> I'd like to be able to assign a computed value from a guard as well.  For
> example:
> {
>   case Some(id) if (record @ findRecord(id)).isDefined => record.name
> }
> Is there any way to do this?

Just to confirm the silence on the list since then: no, there's no way
to do this.

Cheers

 -- Martin

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Is there a way to assign calculated values in guards to va


On Wed, Jan 21, 2009 at 2:21 PM, Stephane Le Dorze <stephane [dot] ledorze [at] gmail [dot] com> wrote:
There's a way if you decide to try the Extractor way but you will not have the same syntax and it's less flexible.Good luck!

Slick... I hadn't thought of this.  It solves the problem.  Thanks!
 
Stephane

On Wed, Jan 21, 2009 at 10:39 PM, martin odersky <martin [dot] odersky [at] epfl [dot] ch> wrote:
On Thu, Jan 15, 2009 at 1:19 AM, David Pollak
<feeder [dot] of [dot] the [dot] bears [at] gmail [dot] com> wrote:
> Howdy,
> In pattern matching, I can assign the matched value to a variable:
> {
>   case f @ Foo(_) => f.toString
> }
> I'd like to be able to assign a computed value from a guard as well.  For
> example:
> {
>   case Some(id) if (record @ findRecord(id)).isDefined => record.name
> }
> Is there any way to do this?

Just to confirm the silence on the list since then: no, there's no way
to do this.

Cheers

 -- Martin




--
Lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

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