Search code examples
indexingrebolrebol2

Handling 'none' when trying to find the index? of a value that doesn't exist in Rebol?


I have a block! x: ["one" "two" "three"]

index? find x "two" returns... 2

index? find x "four" returns...

** Script Error: index? expected series argument of type: series port ** Near: index? find x "twos"

What's the best way to have index? return none rather than the error?


Solution

  • >> help index?
    USAGE:
        INDEX? series /xy
    
    DESCRIPTION:
         Returns the index number of the current position in the series.
         INDEX? is an action value.
    
    ARGUMENTS:
         series -- (Type: series port)
    
    REFINEMENTS:
         /xy -- Returns index as an XY pair offset.
    

    index? expects a series. If the argument is none, it will raise an error.

    >>  find x "four"
    == none
    

    You can either check if the argument exists or is a series or guard against the error e.g.

    >> if  series? i: find x "four" [index? i]
    == none
    

    or

    >> attempt [index? find x "four" none]
    == none