Checking types

Writing code that terminates with a runtime error is rather unprofessional, so whenever possible, use code that does not create an error. In our game, you could test the type of guessed-number with the following:

if integer? guessed-number 

If guessed-number was not an integer, use continue to ask for a next guess:

;-- see Chapter04/guess-number2.red:if not integer? guessed-number [    print ["This is no integer! Try again."]    continue]

You could also use the more complicated if (type? guessed-number) <> integer! here, but we strive to make things simpler in Red, remember?

To check whether a value is a number (integer or float), use number? value.

We'll come back to this subject in Chapter 6, Using Functions and Objects, to discuss ...

Get Learn Red - Fundamentals of Red now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.