Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smalltalk - Compare two strings for equality

I am trying to compare two strings in Smalltalk, but I seem to be doing something wrong.

I keep getting this error:

Unhandled Exception: Non-boolean receiver. Proceed for truth.

stringOne := 'hello'.
stringTwo := 'hello'.
myNumber := 10.

[stringOne = stringTwo ] ifTrue:[
   myNumber := 20].

Any idea what I'm doing wrong?

like image 571
user69514 Avatar asked Oct 29 '09 02:10

user69514


3 Answers

As others have said, it will work the way you want if you get rid of the first set of square brackets.

But to explain the problem you were running into better:

[stringOne = stringTwo ] ifTrue:[myNumber := 20]

is passing the message ifTrue: to a block, and blocks do not understand that method, only boolean objects do.

If you first evaluate the block, it will evaluate to a true object, which will then know how to respond:

[stringOne = stringTwo] value ifTrue:[myNumber := 20]

Or what you should really do, as others have pointed out:

stringOne = stringTwo ifTrue:[myNumber := 20]

both of which evaluates stringOne = stringTwo to true before sending ifTrue:[...] to it.

like image 88
Greg Leaver Avatar answered Nov 01 '22 04:11

Greg Leaver


Try

stringOne = stringTwo 
     ifTrue: [myNumber := 20]`

I don't think you need square brackets in the first line

Found great explanation. Whole thing is here

In Smalltalk, booleans (ie, True or False) are objects: specifically, they're instantiations of the abstract base class Boolean, or rather of its two subclasses True and False. So every boolean has type True or False, and no actual member data. Bool has two virtual functions, ifTrue: and ifFalse:, which take as their argument a block of code. Both True and False override these functions; True's version of ifTrue: calls the code it's passed, and False's version does nothing (and vice-versa for ifFalse:). Here's an example:

a < b
  ifTrue: [^'a is less than b']
  ifFalse: [^'a is greater than or equal to b']

Those things in square brackets are essentially anonymous functions, by the way. Except they're objects, because everything is an object in Smalltalk. Now, what's happening there is that we call a's "<" method, with argument b; this returns a boolean. We call its ifTrue: and ifFalse: methods, passing as arguments the code we want executed in either case. The effect is the same as that of the Ruby code

if a < b then
  puts "a is less than b"
else
  puts "a is greater than or equal to b"
end
like image 22
Bostone Avatar answered Nov 01 '22 05:11

Bostone


[stringOne = stringTwo] is a block, not a boolean. When the block is invoked, perhaps it will result in a boolean. But you are not invoking the block here. Instead, you are merely causing the block to be the receiver of ifTrue.

Instead, try:

(stringOne = stringTwo) ifTrue: [
    myNumber := 20 ].
like image 32
yfeldblum Avatar answered Nov 01 '22 05:11

yfeldblum