Pattern to increment an instance variable

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

Pattern to increment an instance variable

Costas Menico
I have an Integer instance variable accessed thru accessors. I was
wondering if there is a pattern to increment/decrement these
variables.

I got into trouble doing the following:

nextCount:= (self totalCount: self totalCount + 1.)

MyClass>>totalCount
        ^totalCount.

MyClass>>totalCount: anObject
        totalCount := anObject.


My nextCount was being initialized to self instead of the totalCount.
I fixed #totalCount: but I thought there must be a better pattern for
this?

Costas


Reply | Threaded
Open this post in threaded view
|

Re: Pattern to increment an instance variable

Peter van Rooijen
Costas,

Not sure if this is what you're looking for, but the value of an assignment
is the value of its right-hand side, so you could write:

self totalCount: (nextCount := self totalCount + 1)

I suggest you leave the setter to return self.

Regards,

Peter van Rooijen


"Costas Menico" <[hidden email]> wrote in message
news:[hidden email]...

> I have an Integer instance variable accessed thru accessors. I was
> wondering if there is a pattern to increment/decrement these
> variables.
>
> I got into trouble doing the following:
>
> nextCount:= (self totalCount: self totalCount + 1.)
>
> MyClass>>totalCount
> ^totalCount.
>
> MyClass>>totalCount: anObject
> totalCount := anObject.
>
>
> My nextCount was being initialized to self instead of the totalCount.
> I fixed #totalCount: but I thought there must be a better pattern for
> this?
>
> Costas
>