The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Boolean Objects

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Boolean Objects Posted: Mar 4, 2008 5:34 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Boolean Objects
Feed Title: Michael Lucas-Smith
Feed URL: http://www.michaellucassmith.com/site.atom
Feed Description: Smalltalk and my misinterpretations of life
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Michael Lucas-Smith

Advertisement

My colleague Andre's just posted an interesting experiment where DNU returns ^self and you attempt to do a boolean test on it. #mustBeBoolean runs through the DNU and returns self - yet the boolean test actually goes down the ifTrue: path instead of simply exploding.

He discovered the reason for this is that the bytecode is a false test, so since the resulting object isn't false it runs the true side of the equation. This is an interesting demonstration of "catching the VM out" .. instead of running the true side, a VM that isn't cheating and optimizing boolean tests should go in to an infinite loop.

Pondering this behavior, I suddenly wondered if I could simply implement mustBeBoolean on Object and UndefinedObject to return ^true and ^false and if that would immediately mean we could do boolean tests like they do in Ruby, Python, Perl.

Object>>mustBeBoolean 
	^true
UndefinedObject>>mustBeBoolean 
	^false
Number>>mustBeBoolean 
	^self isZero not
Collection>>mustBeBoolean
	^self notEmpty

The result is that you can send ifTrue:, ifFalse:, ifTrue:ifFalse: to any object and if it is nil, 0 or empty it will act as 'false' otherwise it will act as 'true'.

Eg: 
nil ifTrue: [self halt] ifFalse: []. 
#() ifTrue: [self halt] ifFalse: [].
Object new ifTrue: [] ifFalse: [self halt]. 
0 ifTrue: [self halt] ifFalse: [].
12 ifTrue: [] ifFalse: [self halt]. 

I was surprised this worked, but sort of delighted too because it may actually prove useful and is reasonably backward compatible with the existing environment - in otherwords, it's generally considered a bug to do a boolean test on a non-boolean so people don't have their code do that.

I've published this hack in to public store as Oooleans - Booleans and Objects combined. I've also published Oooleans-Tests to make sure it behaves as expected. This was an interesting experiment and I'm pondering the implications. Naturally, having code run this way is a little inefficient.. but less failure is a good thing right?

Read: Boolean Objects

Topic: ruby files as libraries or scripts Previous Topic   Next Topic Topic: Twitter Irony

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use