In business apps, the need for default values happen all the time, so the idiom has value (not sure for the message name though).
Totally. In real apps, having to compare against uninitialized variable or nil as response or empty string happens so often that having this method makes it quite convenient (AKA lots of code becomes one-liners).
We could use
x := [ self thing ] ifError: [ someDefault ]
I understand you���re setting a similar, quite not like it example but in any case this one raises and catches an exception and that sounds quite less efficient if compared to return self (when object is not nil and is not an empty collection/string)
for these purposes. Triggering errors is not too nice still.
Now, what if self itself is nil or empty?
BTW, isEmptyOrNil exists in the image for Collections and UndefinedObject. Empty has no meaning for Object, so why test against empty in the name?
Note that is not a testing method, it���s a conditional executor of the closure.
The reason why was already mentioned, is to allow you to write this one-liner convenience:
someVar := self thing ifNilOrEmpty: [blah]
`self thing` could be an expensive process that returns something or nil or an empty collection. If you get nil or empty as result then you would get the block values resulting in having blah at someVar
In the image, I see that we do have #default: anObject in several places. It seems to serve the same intent.
What is the idiom for such things in Pharo? Importing idioms from other languages works but if we do have one already, we will introduce confusion.
how can you do that one-liner without introducing ifNilOrEmpty: ?