Optional Type

Sometimes you have data where a value can be blank or missing. For example, you may have a column for when a product stopped being sold, but many of the products are still on sale. If you make the column have type Date, you will be required to give a value. But if you make the column Optional(Date) then you can have blank values.

A value for an optional type is always either:

The present items have to be wrapped with Is(...), even though this may seem cumbersome at times.

How to extract values from Optional

The most common case is to supply a default value for a missing item. This can be done with the "get optional or" function, for example get optional or(Your optional column, 0) will get the number out of a column named "Your optional column" or zero if the column has a blank value.

You can also use pattern matches. The most common form is: @if Optional Column =~ Is(x) @then x @else default value @endif The =~ operator means that the right hand side is a pattern, so x takes on the value if present, and the then-part is evaluated. Otherwise the else-part is evaluated and you must supply the missing value.

If you are certain the value will not be missing then you can write get optional(Your optional column) without specifying a default; an error will occur if the value is blank.

How to convert values to Optional

If you want to convert a value to Optional, you write Is() around it. For example, Is(Price) takes the value of a number column Price and converts it to Optional(Number).

It is quite common to want to convert values to optional where missing has been encoded as a dummy value. For example, you may have a Height column where -1 has been used to indicate missing. In that case, the expression @if Height = -1 @then None @else Is(Height) @endif will convert -1 to blank, but all other values to present.