Why is boolean value f (false) defined as a parsing-word while t (true) is not in Factor?
1 answer
The answer is indicated on the page for f
. Specifically, "[t]he f
object is the singleton false value, the only object that is not true." In contrast, t
is defined simply as SINGLETON: t
where SINGLETON:
defines a class and thus a class word which is the sole instance of that class (i.e. itself).
For the purposes of logical comparisons, there is absolutely nothing special about t
. Literally any object (other than f
) could be used instead. As the docs say, t
is just a "canonical" choice.
f
, on the other hand, is uniquely special. f
couldn't be defined the same way as t
, i.e. as a SINGLETON:
because then f
and its class word would be the same thing, but the class word of f
should be a perfectly normal object, and thus behave as true, and only its instance should behave as false.
Ultimately, the f
object (as opposed to the f
parsing word) is deeply wired into the Factor implementation. t
gets some mild special handling by the implementation because the implementation needs to sometimes return t
from the C++ implementation code. But it is not defined by the C++ code, while the f
object is.
To be clear, f
being a parsing word has nothing to do with the behavior you saw with random
. The reason f random
works is because f
is an instance of an immutable-sequence
and thus a sequence
for which random
is defined. t
is not. Factor, like many languages, especially dynamically typed languages with "generalized" booleans, fails to distinguish between "no result" and "a valid result that happens to be f
". In other words, { } random
and { f } random
both return f
but for different reasons. f random
is behaving more like the former as f
behaves like an empty sequence.
0 comment threads