OrderedCollection detect: <- exception if not found?
i had a problem earlier, thinking that if i did a detect: on an ordered collection, and the item was not found, i would get back nil. it turns out that it throws an error.. to get this to work, you use: detect:ifNone: my only question is... why is this so? what is the motivation behind throwing an error rather than returning nil? i guess i would like to understand the 'smalltalkiness' about this, just in case i run into something like this in the future.. thanks! -- ---- peace, sergio photographer, journalist, visionary http://www.ThoseOptimizeGuys.com http://www.CodingForHire.com http://www.coffee-black.com http://www.painlessfrugality.com http://www.twitter.com/sergio_101 http://www.facebook.com/sergio101
On Tue, Oct 16, 2012 at 7:46 PM, sergio_101 <sergio.rrd@gmail.com> wrote:
i had a problem earlier, thinking that if i did a detect: on an ordered collection, and the item was not found, i would get back nil. it turns out that it throws an error..
to get this to work, you use:
detect:ifNone:
my only question is... why is this so?
what is the motivation behind throwing an error rather than returning nil?
i guess i would like to understand the 'smalltalkiness' about this, just in case i run into something like this in the future..
Smalltalk tries to be explicit and not to put things under the carpet. So in this case, it will throw an exception instead of returning nil. If that is what you want, then you can always do detect: [ ... ] ifNone: [ nil ] and you will get the behavior you want. So, forget for a moment all you background of any programming language you have, why would you expect to return nil? ;) Anyway, we have to admit, there still places in Smalltalk where we return nil instead of throwing an exception.
thanks!
-- ---- peace, sergio photographer, journalist, visionary
http://www.ThoseOptimizeGuys.com http://www.CodingForHire.com http://www.coffee-black.com http://www.painlessfrugality.com http://www.twitter.com/sergio_101 http://www.facebook.com/sergio101
-- Mariano http://marianopeck.wordpress.com
Another reason is that there could be a nil in your collection, by design or not, so that returning nil on not found would result in both of these (OrderedCollection with: nil) detect: [ :each | each isNil ] ifNone: [ nil ]. (OrderedCollection with: #somethingElse) detect: [ :each | each isNil ] ifNone: [ nil ]. being nil, so that you don't know the answer to what you want to know. On 16 Oct 2012, at 20:40, Mariano Martinez Peck <marianopeck@gmail.com> wrote:
On Tue, Oct 16, 2012 at 7:46 PM, sergio_101 <sergio.rrd@gmail.com> wrote: i had a problem earlier, thinking that if i did a detect: on an ordered collection, and the item was not found, i would get back nil. it turns out that it throws an error..
to get this to work, you use:
detect:ifNone:
my only question is... why is this so?
what is the motivation behind throwing an error rather than returning nil?
i guess i would like to understand the 'smalltalkiness' about this, just in case i run into something like this in the future..
Smalltalk tries to be explicit and not to put things under the carpet. So in this case, it will throw an exception instead of returning nil. If that is what you want, then you can always do detect: [ ... ] ifNone: [ nil ] and you will get the behavior you want. So, forget for a moment all you background of any programming language you have, why would you expect to return nil? ;) Anyway, we have to admit, there still places in Smalltalk where we return nil instead of throwing an exception.
thanks!
---- peace, sergio photographer, journalist, visionary
http://www.ThoseOptimizeGuys.com http://www.CodingForHire.com http://www.coffee-black.com http://www.painlessfrugality.com http://www.twitter.com/sergio_101 http://www.facebook.com/sergio101
-- Mariano http://marianopeck.wordpress.com
-- Sven Van Caekenberghe http://stfx.eu Smalltalk is the Red Pill
participants (3)
-
Mariano Martinez Peck -
sergio_101 -
Sven Van Caekenberghe