PreviousUpNext

5.3.2  NULL Values and THE

In C it is common for functions to return NULL when a valid pointer value is not possible. Failure to check for NULL pointers is a major source of program crashes.

Mythryl has a similar convention, except that the compiler implements compiletime checks sufficient to guarantee that you will never have a runtime crash due to lack of NULL pointer checking.

Function results are wrapped using THE if they are not NULL. The two possibilities are then distinguished using a case. When you are sure a value is non-NULL, you may use the function the to strip off the THE:

    #!/usr/bin/mythryl

    fun safe_divide i j:  Null_Or( Int )
        =
        if (j != 0)   THE (i / j);
        else          NULL;
        fi; 

    fun test i j
        =
        case (safe_divide i j)
            THE k => printf "%d / %d == %d\n" i j k; 
            NULL  => printf "You can't divide %d by %d!\n" i j;
        esac; 

    test 4 2;
    test 4 0;

    printf "6 / 3 == %d\n" (the (safe_divide 6 3));
    printf "6 / 0 == %d\n" (the (safe_divide 6 0));

When run this will yield:

    linux$ ./my-script
    4 / 2 == 2
    You can't divide 4 by 0!
    6 / 3 == 2

    Uncaught exception NULL_OR

Comments and suggestions to: bugs@mythryl.org

PreviousUpNext