A literal is a value that may have multiple representations or formats, similar to an enum or a union type. A literal can be composed of strings, numbers, objects, arrays, or durations.
Examples
A literal can be as simple as a declaration that a parameter must be a certain value.
LET$nine: 9 = 9; LET$nine: 9 = 10;
Response
-------- Query --------
NONE
-------- Query --------
"Tried to set `$nine`, but couldn't coerce value: Expected `9` but found `10`"
Using | allows a literal to be a number of possible options.
LET$nine: 9 | "9" | "nine" = "Nein";
Response
"Tried to set `$nine`, but couldn't coerce value: Expected `9 | '9' | 'nine'` but found `'Nein'`"
A literal can contain possible types in addition to possible values.
CREATEinformationSET error_info={error: "Deprecated", message: "You shouldn't use this anymore"}; -- Doesn't conform to definition, will not work CREATEinformationSET error_info="You shouldn't use this anymore";
Response
-------- Query --------
[ { error_info: { error: 'Deprecated', message: "You shouldn't use this anymore" }, id: info:pkckjrri8q1pg12unyuo } ]
-------- Query --------
"Couldn't coerce value for field `error_info` of `information:qbohn4wu4l2t81wj2fb3`: Expected `{ error: 'Continue' } | { error: 'RetryWithId', id: string } | { error: 'Deprecated', message: string }` but found `\"You shouldn't use this anymore\"`"
Matching on literals
While SurrealQL does not have a match or switch operator, IF ELSE statements can be used to match on a literal, particularly if each possible type is an object. The following shows a similar example to the above except that each object begins with a field containing the name of the type of error.
With the function set up, the info records can be inserted and run one at a time through the function.
LET$info = INSERTINTOinformation[ {error_info: {Continue: {message: ""}}}, {error_info: {Retry: {error: "Retrying", after: 1s}}}, {error_info: {Deprecated: {message: "Thought I said you shouldn't use this anymore"}}} ];