zserio icon indicating copy to clipboard operation
zserio copied to clipboard

Allow checking if (auto) optional field is set or not.

Open MisterGC opened this issue 3 years ago • 1 comments

Currently there seems to be no way to check if an (auto) optional is set or not, have a look a the following simple example:

struct Test                                         
{                                                   
   int32 value1;                                    
   optional int32 value2;                           
                                                    
   function int32 sum()                             
   {
      // There is no isused operator for optional yet, but this may be an option for implementation                                                
      return value1 + (isused(value2) ? value2 : 0);
   }                                                
};

MisterGC avatar Oct 14 '22 15:10 MisterGC

Yes, operator isused would be very helpful for this particular use case.

In the meantime, it is possible to use workaround using optional field with if clause:

struct Test
{
    int32 value1;
    bool hasValue2;
    int32 value2 if hasValue2;

    function int32 sum()
    {
        // There is no isused operator for optional yet, but this may be an option for implementation                                                
        return value1 + (hasValue2) ? value2 : 0);
    }
};

mikir avatar Oct 19 '22 06:10 mikir