How do i convert from type ?agent to agent... im so confused with optionals

:frowning: why cant this just be cpp

2 Likes

Try the following (where Agent is of type ?agent):

if (CastAgent := agent[Agent?]):
    # ...
1 Like

Like this:

 Foo(MaybeAgent: ?agent): void =
        if (Agent := MaybeAgent?):
            # Do stuff with 'Agent'
4 Likes

Here are some additional examples of how to use the optionals:

OptionalsExample():void=

    # Value is maybe defined
    var MaybeValue: ?int = false

    # Check there is a value and use it
    if. Value := MaybeValue?
    then. Print("Value is defined as {Value}")

    # Check the value is not defined
    if. not MaybeValue?
    then. Print("There is no value defined")
    
    # Value is definitely defined
    DefoValue: int = 0

    # Use `option{}` to assign it to an optional of same type
    set MaybeValue = option{DefoValue}

    if. Value := MaybeValue? then. Print("{Value}") # Prints "0"
3 Likes