I’ve thought about this for awhile and had on my to do list a catalog
of how container objects respond to various methods that are meant to
change the contents of the container.
The inspiration for these thoughts is the implementation in the set
class, where the default is to return the object whether or not the
method modified the contents of the container. If one wants to know
whether the modification occurred, one calls the method with a question
mark appended.
For example Set#add(object) will add the object if it does not exist in
the set and return the modified set. If the object is in the set, it is
not added and the set is returned. Set#add?(object) will add the object
if not already present and return ‘nil’ if the object is already
present.
The array class provides similar functionality for the nil case for at
least one method (Array#delete), but uses a block rather than a
question mark. Array#delete(object) returns nil if the object is not in
the array and returns the deleted object if it is in the array.
However, if a block is used, the block is executed if the object is not
in the array. For example,
a = Array.new(1, 2, 3, 4, 5)
a.delete(6) { a }
returns the array [1, 2, 3, 4, 5].
I don’t know whether or how these method returns should be changed, but
I do think the topic is worth further thought and discussion.
The inspiration for these thoughts is the implementation in the set
class, where the default is to return the object whether or not the
method modified the contents of the container. If one wants to know
whether the modification occurred, one calls the method with a question
mark appended.
For example Set#add(object) will add the object if it does not exist in
the set and return the modified set. If the object is in the set, it is
not added and the set is returned. Set#add?(object) will add the object
if not already present and return ‘nil’ if the object is already present.
[snippage]
I don’t know whether or how these method returns should be changed, but
I do think the topic is worth further thought and discussion.
I’ve thought about these issues, too… I usd to be bitten frequently
by the nil-return bug^H^H^H feature. But then I just fell out of the
habit of chaining certain methods.
I never use the fact that they return nil.
On the other hand, the add? method seems a little unintuitive to me…
in principle I like it OK, but it is a little unusual that a ?-method
changes its receiver. I don’t know anywhere else that happens.
I once suggested as a joke four other suffixes: !!, ??, ?!, and !?.
But the last of these is looking slightly more serious and less of a
joke now. But no, I’m not seriously proposing it. Who would use a
language feature like !? on a method name?
I’ve thought about these issues, too… I usd to be bitten frequently
by the nil-return bug^H^H^H feature. But then I just fell out of the
habit of chaining certain methods.