Skip to main content
Skip table of contents

Structures

Struct Syntax

Slice supports structures containing one or more named fields of arbitrary type, including user-defined complex types. For example:

SLICE
module M
{
    struct TimeOfDay
    {
        short hour;         // 0 - 23
        short minute;       // 0 - 59
        short second;       // 0 - 59
    }
}

As in C++, this definition introduces a new type called TimeOfDay. Structure definitions form a scope, so the names of the structure fields need to be unique only within their enclosing structure.

Field definitions using a named type are the only construct that can appear inside a structure. It is impossible to, for example, define a structure inside a structure:

SLICE
struct TwoPoints 
{
    struct Point      // Illegal!
    {            
        short x;
        short y;
    }
    Point coord1;
    Point coord2;
}

This rule applies to Slice in general: type definitions cannot be nested (except for modules, which do support nesting). The reason for this rule is that nested type definitions can be difficult to implement for some target languages and, even if implementable, greatly complicate the scope resolution rules. For a specification language, such as Slice, nested type definitions are unnecessary – you can always write the above definitions as follows (which is stylistically cleaner as well):

Slice
SLICE
struct Point
{ 
    short x;
    short y;
}

struct TwoPoints      // Legal (and cleaner!)
{   
    Point coord1;
    Point coord2;
}

Language Mapping

A Slice structure maps to a Python dataclass with the same name. For each Slice field, the Python dataclass contains a corresponding field. For example, here is our Employee structure once more:

SLICE
struct Employee
{
    long number;
    string firstName;
    string lastName;
}

The Python mapping generates the following definition for this structure:

PY
@dataclass(order=True, unsafe_hash=True)
class Employee:
    number: int = 0
    firstName: str = ""
    lastName: str = ""

All mapped fields have default values, such as 0 and the empty string (see Fields for details).

For structures that are also legal dictionary key types, the mapped dataclass is configured with order=True and unsafe_hash=True, as shown in our example above. The hashing is “unsafe” because the mapped dataclass is not frozen.

See Also
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.