Hive language
  • 06 Feb 2024
  • 4 Minutes to read
  • Contributors
  • Dark
    Light

Hive language

  • Dark
    Light

Article summary

Types

Each value in Hive is of a certain type, which determines what you can do with that value. It is a statically typed language, unlike JavaScript for example. In short, this means that the language is very strict about which expressions you can combine. For a subtraction like 100 - 50, only numbers may be used - if you try to replace an operand with a text, the system shows an error. The aim here is to avoid mistakes and to point out a problem to the user immediately so that it is not discovered during the live operation of the configurator. These are some of the most essential types in Hive: 

  • Number

  • Text

  • Logic

  • List

  • Record

Each of these types has its own article, with use cases and detailed descriptions.

Comments

Comments allow you to write down helpful information directly in your rules. They have no effect whatsoever on a rule itself, but they can help you or others better understand what the rule is all about.

To write a comment, start with two slashes // and then write whatever you want. This kind of comment takes up the remainder of the line.
To write a comment over multiple lines, start with /* and end with */ when you're done

// this is a single-line comment

/*
this comment
spans over
multiple lines
*/

Variables

Use the var keyword to assign a name to an arbitrary value in order to re-use it in your rules:

var myValue = Input or 1;
if myValue> 10 then
  10
else
  myValue
end

At present, the value of a variable cannot be changed after the variable was initialized.

The declaration of a variable must be separated with a semicolon ; from the next expression.

Scope

A variable can only be used in the “scope” it is declared in, or any nested scopes. For instance, a variable declared in one rule cannot be used in another rule.

The then and else branches of an if expression each form their own scopes, which means that a variable declared in one of these scopes is not available outside the if.

A variable declared in an inner scope will have higher precedence than other named entities from outer scopes. What this means is that a variable with the name “x” will hide other variables (or components) that are also called “x”.

var x = 1;
if true then
  var y = 2;
  y
else
  var x = 2; // this is allowed; x now has a different meaning in the else-scope
  x + y // ERROR! y is not available here, it belongs to the then-scope
end;
x  // this is the x defined in the first line, it's still set to 1

Input and empty

Some components allow the use of a special variable in their Hive rules: input. This primarily refers to user input. For example, if you connect a value component with a UI element such as a text box, the text that a user writes in this text box ends up in the input variable. The value of a component cannot be changed directly, only its input can. The input value is also saved automatically, so we don't have to worry about that.

By default, the type of input is identical to that of the component. For example, if a component produces a number, then its input will have the type number too.
The value component allows you to choose a specific type for input.

The simplest possible way to store a value in a configurator is to create a value component with just this code:

input

However, unless you assign an initial value to input, it will be empty.
The empty value is similar to null in other programming languages and represents the absence of a meaningful value. It cannot be processed further in a meaningful way and essentially just exists to signal that a real value is missing.

In almost all cases, you should strive to avoid rules that return empty !

There are multiple ways to check if something is empty, the simplest being the or operator. It checks if the left operand is empty and if it is, it returns the right operand as result:

input or 1

This will return the input value unless it is empty, in which case 1 will be returned. You can use any value as the right operand, as long as it is of the same type as the left operand. For example, if input is a text, the above example would not work – but Input or "hello" would.

You can also check for empty with the if expression:

if input = empty then
  "No value here yet!"
else
  input
end

Optional chaining operator (?.)

The optional chaining operator (also known as conditional access operator) ?. allows you to access a property or function of something that could potentially be empty, without causing an error:

// instead of this:
if MyRecord <> empty then MyRecord.Name else "" end

// you can write:
MyRecord?.Name or ""

The ?. operator checks if the left operand is empty and if so, returns empty itself. Otherwise it returns the field (or calls the function) on its right-hand side.


Was this article helpful?