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
    }
}

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 Swift structure when this Slice structure does not have (recursively) any Slice class field. Conversely, a Slice structure maps to a Swift class when this Slice structure has (recursively) one or more Slice class field.

Mapping to Swift Struct

Consider the following Slice structure:

SLICE
struct Point
{
    double x;
    double y;
}

This simple structure does not have any Slice class field so it maps to a public Swift structure:

SWIFT
public struct Point {
    public var x: Double = 0
    public var y: Double = 0

    public init() {}

    public init(x: Double, y: Double) {
        self.x = x
        self.y = y
    }
}

For each field in the Slice definition, the Swift structure contains a corresponding public stored property of the same name.

When all the stored properties of the generated Swift structure are Hashable, the generated structure is itself hashable. For example:

SLICE
struct TimeOfDay
{
    short hour;
    short minute;
    short second;
}

The corresponding Swift structure conforms to Hashable:

SWIFT
public struct TimeOfDay: Hashable {
    public var hour: Int16 = 0
    public var minute: Int16 = 0
    public var second: Int16 = 0

    public init() {}

    public init(hour: Int16, minute: Int16, second: Int16) {
        self.hour = hour
        self.minute = minute
        self.second = second
    }
}

Mapping to Swift Class

A Slice structure with a field of a class type is mapped to a Swift class. Take the Entry structure below:

SLICE
class Data
{
    ...
}

struct Entry
{
    int key;
    Data value;
}

Entry is mapped to a public Swift class:

SWIFT
public class Entry {
    public var key: Int32 = 0
    public var value: Data? = nil

    public init() {}

    public init(key: Int32, value: Data?) {
        self.key = key
        self.value = value
    }
}

For each field in the Slice definition, the Swift structure contains a corresponding public stored property of the same name. Fields with type class or proxy are mapped to Swift optionals: the mapped type for value in the example above is Data?.

A Slice structure is mapped to a Swift class when this Slice structure contains a class field anywhere: it can be a direct field or a nested field such as:

SLICE
class Data
{
    ...
}

struct Entry
{
    int key;
    Data value;
}

sequence<Entry> EntryList;

// Mapped to a Swift class, since entries contains indirectly a class field.
struct Record
{
    string name;
    EntryList entries;
}

Generated Initializers

The mapped Swift struct or class has always two public initializers:

  • a memberwise initializer that initializes all properties explicitly

  • a parameterless initializer that assigns default values to all properties (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.