You've almost invented lenses. It is possible to emulate imperative programming, without the nasty bits, using lenses; I think it is called := in scalaz.
--
Tony Morris
http://tmorris.net/
Don't worry. The amount of Scalaz needed for Lenses is quite small and
you can safely ignore the other bits. The "Lensed" plugin should do
everything for you. Just have a look at the examples there. I think the
most difficult part is to figure out how to factor out your case classes
into a different sub-project ;)
On Sep 11, 2011, at 10:29 , Russ P. wrote:
> I have immutable classes for aircraft position, velocity, and state.
> Here is my position class (most of it):
>
> case class Position(x: Scalar=0, y: Scalar=0, alt: Scalar=0) { // 3D
> position
>
> override def clone = Position(x, y, alt)
>
Since you override clone() you probably have a use case for it. I wonder why you don’t just return “this”, though, given that your class is immutable. If you do need a new object, you may save some characters (and be unaffected by refactorings) by just using copy().
> def apply(x: Scalar=x, y: Scalar=y, alt: Scalar=alt) = Position(x,
> y, alt)
>
> def to2D = Position2D(x, y)
>
> def + (p: Position) = Position(x + p.x, y + p.y, alt + p.alt)
> def - (p: Position) = Position(x - p.x, y - p.y, alt - p.alt)
> }
>
> As you can see, this class is very simple, and I use apply with
> default values to return a new object with any or all fields modified.
> Here is an example usage:
>
> val pos1 = Position(100 * nmi, 110 * nmi, 35 * kft)
> val pos2 = pos1(alt = 33 * kft)
>
> This seems to work reasonably well for me, but is there a better or
> simpler way? Comments welcome.
>
It looks like the obvious solution. Whether to use apply() instead of copy() for the mutators is a matter of taste, I guess (I have a slight tendency to spend extra characters for indicating copying at the use site).
Roland Kuhn
Typesafe – Enterprise-Grade Scala from the Experts
twitter: @rolandkuhn
Am 11.09.2011 20:13, schrieb Russ P.:
> Thanks for the feedback, Roland. Yes, now that you mention it, I guess
> having clone make a copy is pointless for an immutable class. I might
> as well just return "this". Better yet, I could just get rid of clone
> altogether and use assignment at the call site. That would be
> equivalent for an immutable class, wouldn't it?
>
> By the way, for mutable classes, I've never really understood the
> difference between copy and clone. Can someone clarify that?
>
> --Russ P.
>
>
> On Sep 11, 1:48 am, Roland Kuhn <goo...@rkuhn.info> wrote:
>> Hi Russ,
>>
>> On Sep 11, 2011, at 10:29 , Russ P. wrote:
>>
>>> I have immutable classes for aircraft position, velocity, and state.
>>> Here is my position class (most of it):
>>> case class Position(x: Scalar=0, y: Scalar=0, alt: Scalar=0) { // 3D
>>> position
>>> override def clone = Position(x, y, alt)
>> Since you override clone() you probably have a use case for it. I wonder why you don�t just return �this�, though, given that your class is immutable. If you do need a new object, you may save some characters (and be unaffected by refactorings) by just using copy().
>>
>>
>>
>>> def apply(x: Scalar=x, y: Scalar=y, alt: Scalar=alt) = Position(x,
>>> y, alt)
>>> def to2D = Position2D(x, y)
>>> def + (p: Position) = Position(x + p.x, y + p.y, alt + p.alt)
>>> def - (p: Position) = Position(x - p.x, y - p.y, alt - p.alt)
>>> }
>>> As you can see, this class is very simple, and I use apply with
>>> default values to return a new object with any or all fields modified.
>>> Here is an example usage:
>>> val pos1 = Position(100 * nmi, 110 * nmi, 35 * kft)
>>> val pos2 = pos1(alt = 33 * kft)
>>> This seems to work reasonably well for me, but is there a better or
>>> simpler way? Comments welcome.
>> It looks like the obvious solution. Whether to use apply() instead of copy() for the mutators is a matter of taste, I guess (I have a slight tendency to spend extra characters for indicating copying at the use site).
>>
>> Roland Kuhn
>> Typesafe � Enterprise-Grade Scala from the Experts
>> twitter: @rolandkuhn