Defining your own types can be useful in complex projects. Types consist of properties, functions, and subroutines. Types can be defined in local Snap code (in a configurator rule or safe function), or can be defined globally, for use throughout your company.
Once a type has been created, you can refer to it when creating new variables of that type. This is called "instantiating" a type as a variable. The new variable can be used to more easily manage your data.
Properties
Properties of types are similar to regular variables, except they are assigned to that type. You can set properties on an instance of that type.
Functions and Subroutines
Like regular functions and subroutines, a type can have its own built-in functions and subroutines, which are called within the context of the instance of that type. Within the function or subroutine you have access to that instance's property values.
Global Types
You can also define global types which can be used across all other Snap rules. These types are useful in cases where you'd like a safe function to return the result of a web service which does not have an implicitly known type, but you wish to define properties that you expect to be present in the result of that web service call to aid in retrieving those properties.
Examples
The "This" Variable
This type provides a way to describe the properties of a wheel, which consists of a radius and width. A helper function can use the radius data to provide the circumference using the well-known formula 2 * PI * r. One can then instantiate the wheel:
You can also use the slot in the declare property block within the type to define default property values. The following code would result in the same value for circumference.
This is a special variable in the scope of a type function or subroutine which gives you access to that type's properties, functions, and subroutines.