Cannot compare nullable (option) variable with true.

Summary

Take for instance this variable: Counter ?int.

If I wanted to check that this variable is null, I would do:
if (Counter = false)
So logically, if I wanted to check that this variable isn’t null, I should be able to do:
if (Counter = true)
However, it does not seem to work. It always returns false.

Is it intended behavior? Or am I missing something?

Please select what you are reporting on:

Verse

What Type of Bug are you experiencing?

Verse

Steps to Reproduce

  1. Have a nullable (option) variable, like such: ?type,
  2. Compare it with true in an if statement.

Expected Result

If the variable has a value, it should return true.

Observed Result

Even when the variable has a value, it returns false.

Platform(s)

Desktop (I expect all platforms to behave the same).

Island Code

0000-0000-0000

Bump.

try doing

if(Counter?):
     Print("its not false")


if (C := Counter?)
    then:
        Print("Counter? is {C}")
    else:
        Print("Counter is FALSE")

1 Like

Sure, that works. Alongside if (Counter <> false) and if (not(Counter = false)).
But it still wouldn’t make sense for if (Counter = true) to not work.

I would only do that if I actually needed the value, not if I just needed to check that there is a value.

I guess a way to think about it is that false is essentially a value in a way?
eg a float can be any number and a ?float can be any number + false. but yea to be fair it also took me quite a while to understand the logic behind that but it’s something Im used to now.

Here’s a documentation post regarding it:

I prefer to think of ?int more as being like C++'s int*, and false as the equivalent to nullptr.
So if (Counter = false) would be the same as if (Counter == nullptr),
and if (Counter = true) the same as if (Counter != nullptr).

I suppose that’s a better way to think about it, if(Counter?) is similar in thought with if(Counter != nullptr)

  1. This is for you to print the value so you can see it’s type is ?int (not boolean). Because this type can hold an optional integer you almost always want to store something you may need to use later. If you do not need to know specific integer, simply use the logic type instead (boolean).

  2. If you can’t get the value then it is false.

It still doesn’t explain why if (Counter = true) shouldn’t work.

vBool : ?logic = false
if (vBool = false): # Returns true if there is no value.
    Print("Boolean has no value.")
# ----------------------------------------------
vBool : ?logic = option{false}
if (vBool? = false): # Retrieves value (if there is one) then compares.
    Print("Boolean has a value and is false.")
# ----------------------------------------------
vBool : ?logic = option{false} # OR option{true}.
if (vBool = true): # Doesn't work. Should return true if there is a value.
    Print("Boolean has a value.")
# ----------------------------------------------
vBool : ?logic = option{true}
if (vBool? = true): # Retrieves value (if there is one) then compares.
    Print("Boolean has a value and is true.")

Above is what should logically work, but like I said, = true to check that there is a value does not work.

Because it is never true it is maybe3 or maybe5 or false

1 Like

Yes, it is clearly a bug or an oversight.
It never being true is clearly not something that should be happening when there the variable has an optionable value.
On top of that, there is no ambiguity when comparing an optionable variable to true like such since you aren’t fetching its value. It’s not like true could be confused for option{true}.

Let’s hope a dev will show up here. I’ve come across a fair amount of inconsistent things while using Verse and fixing issues such as this one would be a first step to making writing code in Verse a much more enjoyable experience for me (and others) who are used to some things that don’t work in Verse working in other more popular languages.