[scala] pattern matching & destructive assignment

View: New views
4 Messages — Rating Filter:   Alert me  

[scala] pattern matching & destructive assignment

by daniel mahler :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is it possible to use patternmatching or extracor variable to update
prexisting variables.

something along the lines of

var a=0; var b = 0
(a,b) = (1,2)

which of course is not valid in scala.

thanks
Daniel

Re: [scala] pattern matching & destructive assignment

by Bill Burdick-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well, you can do something like that in a for loop:

for ((a, b) <- Array(1->2, 3->4)) println(a+", "+b)

Can you use a for comprehension to do what you need?


Bill


On Fri, Nov 6, 2009 at 12:31 AM, Daniel Mahler <dmahler@...> wrote:
Is it possible to use patternmatching or extracor variable to update
prexisting variables.

something along the lines of

var a=0; var b = 0
(a,b) = (1,2)

which of course is not valid in scala.

thanks
Daniel


Re: [scala] pattern matching & destructive assignment

by daniel mahler :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

[+scala]

I do not think so, I need to update a bunch of member variable in a method

class A {
 var a =0; var b = 0; var c = 0

 def update(l : Seq[Int] {
 Seq(a, b, c) = l
 }
}

thanks
Daniel

On 11/5/09, Bill Burdick <bill.burdick@...> wrote:

> Well, you can do something like that in a for loop:
>
> for ((a, b) <- Array(1->2, 3->4)) println(a+", "+b)
>
> Can you use a for comprehension to do what you need?
>
>
> Bill
>
>
> On Fri, Nov 6, 2009 at 12:31 AM, Daniel Mahler <dmahler@...> wrote:
>
>> Is it possible to use patternmatching or extracor variable to update
>> prexisting variables.
>>
>> something along the lines of
>>
>> var a=0; var b = 0
>> (a,b) = (1,2)
>>
>> which of course is not valid in scala.
>>
>> thanks
>> Daniel
>>
>

Re: [scala] pattern matching & destructive assignment

by Seth Tisue :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>>>>> "Daniel" == Daniel Mahler <dmahler@...> writes:

 Daniel> Is it possible to use patternmatching or extracor variable to
 Daniel> update prexisting variables.
 Daniel> something along the lines of
 Daniel> var a=0; var b = 0 (a,b) = (1,2)
 Daniel> which of course is not valid in scala.

A ticket on this was closed as wontfix (without a SIP anyway):
http://lampsvn.epfl.ch/trac/scala/ticket/1324

--
Seth Tisue @ Northwestern University / http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/