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

compact notation for mutators

6 replies
Sam Stainsby
Joined: 2009-01-14,
User offline. Last seen 42 years 45 weeks ago.

I have a pair of case class like this:

case class AccessorAction[T,V](val accessor:(T) => V)

case class MutatorAction[T,V](val mutator:(T,V) => _)

I can do this:

val aa = AccessorAction[MyRectangle,Int](_.width)

which is nice and compact, and this:

val ma = MutatorAction((r:MyRectangle, w:Int) => r.width = w)

but for the latter I really want something like this:

val ma = MutatorAction[MyRectangle,Int](_.width = _)

Is something like that possible?

Alex Boisvert
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: compact notation for mutators
On Tue, Jan 13, 2009 at 4:21 PM, Sam Stainsby <sam [at] sustainablesoftware [dot] com [dot] au> wrote:
I have a pair of case class like this:

case class AccessorAction[T,V](val accessor:(T) => V)

case class MutatorAction[T,V](val mutator:(T,V) => _)


I can do this:

 val aa = AccessorAction[MyRectangle,Int](_.width)


which is nice and compact, and this:

 val ma = MutatorAction((r:MyRectangle, w:Int) => r.width = w)

but for the latter I really want something like this:

 val ma = MutatorAction[MyRectangle,Int](_.width = _)

Is something like that possible?

The type inferencer doesn't seem to like it... but this is possible:

scala> val ma = MutatorAction( (_:MyRectangle).width = (_:Int) )                
ma: MutatorAction[MyRectangle,Int] = MutatorAction(<function>)

alex
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: compact notation for mutators

On Wed, Jan 14, 2009 at 12:21:27AM +0000, Sam Stainsby wrote:
> but for the latter I really want something like this:
>
> val ma = MutatorAction[MyRectangle,Int](_.width = _)
>
> Is something like that possible?

scala> class MyRectangle { var width = 0 }
defined class MyRectangle

scala> case class MutatorAction[T,V](mutator: Function2[T,V,Unit])
defined class MutatorAction

scala> val ma = MutatorAction[MyRectangle,Int](_.width = _)
ma: MutatorAction[MyRectangle,Int] = MutatorAction()

scala> val rect = new MyRectangle
rect: MyRectangle = MyRectangle@4a64a

scala> ma.mutator(rect, 15)

scala> rect.width
res14: Int = 15

Sam Stainsby
Joined: 2009-01-14,
User offline. Last seen 42 years 45 weeks ago.
Re: compact notation for mutators

On Tue, 13 Jan 2009 16:30:38 -0800, Alex Boisvert wrote:

> The type inferencer doesn't seem to like it... but this is possible:
>
> scala> val ma = MutatorAction( (_:MyRectangle).width = (_:Int) )
> ma: MutatorAction[MyRectangle,Int] = MutatorAction()

Neat! That works for me. Thanks.

Sam.

Sam Stainsby
Joined: 2009-01-14,
User offline. Last seen 42 years 45 weeks ago.
Re: compact notation for mutators

On Tue, 13 Jan 2009 16:54:48 -0800, Paul Phillips wrote:

> scala> case class MutatorAction[T,V](mutator: Function2[T,V,Unit])

That also works - the difference is that my definition was equivalent to:

case class MutatorAction[T,V](mutator: Function2[T,V,_])

(any return type allowed) which seems to cause the problems. I might be
able to live without it though.

Cheers,
Sam.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Re: compact notation for mutators

On Wed, Jan 14, 2009 at 01:07:34AM +0000, Sam Stainsby wrote:
> > scala> case class MutatorAction[T,V](mutator: Function2[T,V,Unit])
>
> That also works - the difference is that my definition was equivalent to:
>
> case class MutatorAction[T,V](mutator: Function2[T,V,_])

Is there some reason this won't do it for you:

case class MutatorAction[T,V](mutator: Function2[T,V,Any])

Sam Stainsby
Joined: 2009-01-14,
User offline. Last seen 42 years 45 weeks ago.
Re: compact notation for mutators

On Tue, 13 Jan 2009 17:31:14 -0800, Paul Phillips wrote:

>
> Is there some reason this won't do it for you:
>
> case class MutatorAction[T,V](mutator: Function2[T,V,Any])

I can't see one. That works - thanks!

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