[squeak-dev] Protocol extension proposal: Boolean>>asBit ?
bert at freudenbergs.de
Sat Dec 5 13:16:48 UTC 2009
On 05.12.2009, at 13:38, Igor Stasenko wrote:
> 2009/12/5 Bert Freudenberg <bert at freudenbergs.de>:
>> On 05.12.2009, at 11:44, Igor Stasenko wrote:
>>> I currently writing a code which looks like:
>>> a := some numeric expression + (someBoolean ifTrue:  ifFalse: ).
>>> And i get somewhat tired repeating the same pattern in multiple places.
>>> So, i thinking , maybe #asBit convenience method could be useful?
>>> ^ 1
>>> ^ 0
>>> so, then, in case of need like above, we could write much shorter expressions:
>>> a := some numeric expression + someBoolean asBit.
>>> And its much less error prone comparing to #ifTrue:ifFalse:, since i
>>> found its relatively easy to make a mess (swapping 1 with 0),
>>> especially, when you having negations in expression, because once you see:
>>> (someBoolean not ifTrue:  ifFalse: )
>>> you always tempted to write it as:
>>> (someBoolean ifTrue:  ifFalse: ).
>>> or as:
>>> (someBoolean ifFalse:  ifTrue: ).
>>> but once you start playing with it, its very easy to shoot into own
>>> feet if you are not careful.
>>> But if we could add #asBit, then we could write:
>>> someBoolean not asBit
>>> which is much more:
>>> a) concise & cleaner
>>> b) error proof
>>> Best regards,
>>> Igor Stasenko AKA sig.
>> Wouldn't #asInteger be more appropriate?
> Probably. But i choose a 'bit', because booleans is an enumeration
> type, not a number ,
> and its hard to expect that something which is not a number can be
> converted to number.
> Another reason why i cautious about #asInteger, that it is already
> exists & used widely and probably
> introducing it into boolean classes can be fatal to some applications.
> Maybe i'm overly cautious about that, if so, then #asInteger is just fine.
I don't think it would break any sane application. #asInteger is descriptive, and "feels right" to me. Reading "asBit" I'd expect it to return an instance of Bit.
I have not really felt the need for such a method, but I can see how it's tempting to have, in particular when porting code.
Does any other Smalltalk have such a conversion method? If so, we should consider using that.
- Bert -
More information about the Squeak-dev