#after:ifAbsent: bug?

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

#after:ifAbsent: bug?

Bill Schwab-2
Blair,

( Array with:1 ) after:1 ifAbsent:[].

raises a walkback.  A proposed fix appears below.  You might also want to
tweak #after: to send #errorLastObject:???

Have a good one,

Bill

------------------------

!SequenceableCollection methodsFor!

after: target ifAbsent: exceptionHandler
 "Answer the element after target, or if not present the result of
evaluating the
 niladic valuable, exceptionHandler."

 | index |
 index := self indexOf: target.
 ^index == 0
  ifTrue: [exceptionHandler value]
  ifFalse: [
   index = self size
    "ifTrue: [self errorLastObject: target]"
    ifTrue:[ exceptionHandler value ]
    ifFalse: [self at: index + 1]]! !
!SequenceableCollection categoriesFor: #after:ifAbsent:!public!searching! !


--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: #after:ifAbsent: bug?

Blair McGlashan
"Bill Schwab" <[hidden email]> wrote in message
news:atb0a3$12fi7e$[hidden email]...
> Blair,
>
> ( Array with:1 ) after:1 ifAbsent:[].
>
> raises a walkback.  A proposed fix appears below.  You might also want to
> tweak #after: to send #errorLastObject:???
>...

Hmmm, that is by design. The ifAbsent: block is only evaluated if the
element searched for is not actually in the collection. If the block were
also evaluated in the case where the located element happens to be the last
in the collection, then it would not be possible to tell between the case of
searching for something which isn't there, and the case of attempting to
read off the end. On the other hand this would appear to make it rather less
than useful.

Actually I'm not convinced that #after:ifAbsent: or #before:ifAbsent: are
actually that useful at all, so perhaps they should be removed altogether?

Regards

Blair


Reply | Threaded
Open this post in threaded view
|

Re: #after:ifAbsent: bug?

Bill Schwab-2
Blair,

> Hmmm, that is by design. The ifAbsent: block is only evaluated if the
> element searched for is not actually in the collection. If the block were
> also evaluated in the case where the located element happens to be the
last
> in the collection, then it would not be possible to tell between the case
of
> searching for something which isn't there, and the case of attempting to
> read off the end. On the other hand this would appear to make it rather
less
> than useful.

Agreed.  I'd rather have the handler for the last element, and have it
signal a non found error if I'm stupid enough<g> to give a starting element
that's not in the collection.

> Actually I'm not convinced that #after:ifAbsent: or #before:ifAbsent: are
> actually that useful at all, so perhaps they should be removed altogether?

Subject to a change as suggested above, they seem useful enough.  At least I
recently encountered a situation in which they were helpful.

Have a good one,

Bill

--
Wilhelm K. Schwab, Ph.D.
[hidden email]