Why comparing optional with non-optional compiles well and always fails?

X :?int = option{1}
Y :int = 1
X = Y // This compiles and fails in the context

To use ‘=’ operator, both sides should have same type.
Optional type and non-optional type is not the same type. However it compiles.

Also in enum

X :?my_enum = option{my_enum.A}
Y :my_enum = my_enum.A
X = Y //  This comiples and fails in the context

I used to create state as optional enum but sometimes I make a mistake comparing optional enum with non optional enum.
Comparing non-optional and optional always fails. Does anyone know why this compiles?

I’ll share code so you can easily test

# Code for test
using { /Fortnite.com/Devices }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Verse.org/Colors }
using { /Verse.org/Simulation }


my_enum := enum:
    A
    B

hello_world_device := class(creative_device):
    OnBegin<override>()<suspends>:void=
        loop:
            Print("Test Start", ?Color:=NamedColors.Blue)

            Tests := array:
                Compare0,
                Compare1,
                Compare2,
                Compare3,
                Compare4,
                Compare5

            for (Index -> Test :Tests):
                if (Test[]):
                    Print("Test {Index} is true", ?Color:=NamedColors.Green)
                else:
                    Print("Test {Index} is false", ?Color:=NamedColors.Red)

            Sleep(10.0)

    Compare0()<transacts><decides>:void= # Expect to be true
        X :my_enum= my_enum.A
        Y :my_enum= my_enum.A
        X = Y

    Compare1()<transacts><decides>:void= # Expect to be true
        X :?my_enum= option{my_enum.A}
        Y :my_enum= my_enum.A
        X = Y 

    Compare2()<transacts><decides>:void= # Expect to be false
        X :?my_enum= option{my_enum.A}
        Y :my_enum= my_enum.B
        X = Y 

    Compare3()<transacts><decides>:void= # Expect to be true
        X :?my_enum= option{my_enum.A}
        Y :?my_enum= option{my_enum.A}
        X = Y

    Compare4()<transacts><decides>:void= # Expect to be false
        X :?my_enum= option{my_enum.A}
        Y :?my_enum= option{my_enum.B}
        X = Y

    Compare5()<transacts><decides>:void= # Expect to be true 
        X :?int= option{1}
        Y :int= 1
        X = Y
        

That’s because they are different, a ?int and int are different.

Same reason while this will compile it will never be true
image

1 Like

Thanks, I understand it better after seeing the example. The comparison operator works even if the types are not exactly the same.
I think it would be better to add some custom methods to compare explicit types.

1 Like