Powered by SAFIRE

office +49 711 1398 130
fax +49 711 1866 1240
Sales@SAFIRE-World.com


FAQ

How to work with OPTIONAL fields?

Does Y support pointers?


 

How to work with OPTIONAL fields

OPTIONAL fields are initialized to NULL, not optional to respective default value: STRING - "", NUMBER - 0, etc. So, if you want to check if the optional field is present you just have to compare it with NULL as in the example below:

typedef SEQUENCE {
    STRING field_1;
    STRING field_2 OPTIONAL;
} My_Seq_T;

My_Seq_T myVar;

Print(myVar.field_1);
IF (myVar.field_2 NEQ NULL) {
    Print("field_2 is present");
    Print(myVar.field_2);
}
ELSE{
    Print("field_2 is not present");
}

The output will be:

Print: ""  
     Print: "field_2 is not present"  
     Print: "NULL"


Does Y support pointers?
Yes ... here is an example:

Declaring a pointer:

    NUMBER Count;
    (NUMBER *) Count_p;

    Count_p= &Count;
    *Count_p= 101;

If a pointer has not been intialised, it's value is NULL, so this is a difference to C, ie the following checks if Count_p has been initialised.

    IF (Count_p EQ NULL)
    {
            ...
    }

To check if the target of the pointer is NULL, dereference it:

    IF (*Optional_p EQ NULL)
    {
            ...
    }

The reason for this is, structured types in SAFIRE do not use pointers, instead each field is a value.

In C:         p= &MyStruct.FieldA;
In SAFIRE:    p= &MySequence.FieldA;

In SAFIRE FieldA may be declared as OPTIONAL, which means if it's not present, the value of the field is NULL. This has no matching equivalent in C, as C has no support for OPTIONAL fields, requiring instead these fields to be explicitly handled using pointers.So to check if an OPTIONAL field is present in SAFIRE:

    IF (MySequence.FieldA EQ NULL)
    {
            ...
    }

So if p points at this field, there are two cases to consider:

  • Has p been initialised? (p EQ NULL)
  • Is the value it points at present? (*p EQ NULL).

A pointer variable has a value of pointer type, which means:

  • a pointer can be assigned to another pointer, i.e. 'p2= p1;'
  • pointers can be compared, i.e. 'IF (P1 EQ P2) {...}'

But to use what pointer points at, it has to be dereference, ie '*p1= 101'.

    MySeq_p= &MySequence;
            ...
    x= (*MySeq_p).FieldA;

Or using the C notation:

    x= MySeq_p->FieldA;

Parameters can be defined as by-reference, i.e.

    VOID
    Fn102( NUMBER &Para1)
    {
            Para1= 102;
    }

    Main()
    {
            NUMBER i;
            i= 55;
            Fn102( i);

            IF (i EQ 102)
            {
                    Print( "i has changed to " + $i);
            }
    }

    NOTE: Para1 is the SAME VARIABLE as i, it is not a pointer to it.

Q: [FAQ Question]
A: [FAQ Answer]