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 Java class with the same name. For each Slice field, the Java class contains a corresponding public field. For example, here is our Employee structure once more:

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

The Slice-to-Java compiler generates the following definition for this structure:

JAVA
public final class Employee implements java.lang.Cloneable, java.io.Serializable {
    public long number;
    public String firstName;
    public String lastName;

    public Employee() {
        this.firstName = "";
        this.lastName = "";
    }

    public Employee(long number, String firstName, String lastName) {
        this.number = number;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public boolean equals(java.lang.Object rhs) ...
    
    @Override
    public int hashCode() ...
    
    @Override
    public Employee clone() ...
}

You can optionally customize the mapping for fields to use getters and setters instead.

The equals method compares two structures for equality. Note that the generated class also provides the usual hashCode and clone methods. (clone has the default behavior of making a shallow copy.)

Generated Constructors

The mapped Java class provides two constructors:

  • canonical constructor with parameters for all the fields

  • a parameterless constructor that initializes all fields to default values (see Fields)

See Also
JavaScript errors detected

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

If this problem persists, please contact our support.