Predicate Methods: foo?


#1

Re:

// Method calls that return true/false usually end with '?'
// The fancy name for this is a 'predicate' method.
if test? [do_this]

Question: Does the question mark actually do anything or is it just part of the word?


#2

The question mark ? is part of the predicate name.

It does also indicate to the SkookumScript parser that it will always return a Boolean: true or false.

Predicates are intuitive to read and you don’t have to worry about whether to start a method with is_, has_, are_ - instead you just use obj.alive?, guy.enemy?, effect.active?.

Method calls with a ? must return a Boolean class type. If you put any other result class type the SkookumScript parser will complain. If you omit the result class type for a predicate, the parser will infer (fill in the blank) with Boolean.

Variables may also be predicates and end with a question mark ? such as @@class_test?, @obj_test? and var_test?

Data member predicates must also only be Boolean. If you use any other class type when defining a data member in the !Data.sk file the SkookumScript parser will complain and if the type is omitted then Boolean is inferred.


Great curated posts to learn about SkookumScript