reschu
reschu

Reputation: 1105

Why are non-boolean values not implicitly converted in boolean expressions?

Some programming languages evaluate

5 == true

to true, or allow

if 5 then expr

by converting 5 to a bool.

Julia does not. Why?

Upvotes: 3

Views: 566

Answers (2)

Simon Byrne
Simon Byrne

Reputation: 7874

Because == is an equivalence relation.

In Julia, true, when converted to an integer, becomes 1, and so 1 == true. If true == 5, then in order for == to preserve transitivity, that would imply that 1 == 5

Upvotes: 6

user3710044
user3710044

Reputation: 2344

Because they're not the same.

More verbosely.
The concepts of a number and a truth value (boolean) are distinct. There is no conceptual mapping between these two things. When a computer is creating a mapping it's giving each of the boolean states an arbitrary numeric symbol. There is nothing in mathematics that states what number the token FALSE should map to. A quite reasonable mapping would be:

  1. FALSE
  2. TRUE
  3. FileNotFound

There is a common computing convention that FALSE should map to zero and TRUE should map to one ... or minus one ... or something else ... or everything else. But this isn't a hard rule, and has no basis in mathematics.

This isn't limited to Julia.

Upvotes: 0

Related Questions