Skip to main content
Skip table of contents

Fields

Syntax

A field is defined as Type name, where Type is the field's type and name is the field's name. For example:

CODE
string name;
ByteSeq image;
Fruit fruit;

Each field definition must be followed by a semicolon. You can define several fields on the same line if you wish, as in:

CODE
struct Point { int x; int y; }

The type of a field can be a basic type, a user-defined type, Object* (an untyped proxy), or Value(an untyped class instance).

Nullable and Non-Nullable Fields

Fields with proxy and class types are nullable. Optional fields (presented below) are also nullable. All other fields are non-nullable, meaning they must have a value when transmitted through Ice.

CODE
struct Example
{
    int count; // must be set
    Greeter* greeter; // proxy type, so it can be null
    Node node; // class type, so it can be null
}

Optional Fields

A field of a Slice class or exception may be declared as optional to indicate that a program can leave its value unset. Fields not declared as optional are known as required fields; a program must supply legal values for all required fields.

Each optional field must be assigned a unique, non-negative integer tag:

SLICE
module M
{
    class C
    {
        string name;
        bool active;
        optional(2) string alternateName;
        optional(5) int overrideCode;
    }
}

It is legal for a base type's tag to be reused by a derived type:

SLICE
exception BaseException
{
    optional(1) int systemCode;
}
 
exception DerivedException extends BaseException
{
    optional(1) string diagnostic; // OK
}

The scope of a tag is limited to its enclosing type and has no effect on base or derived types.

You can mark any field as optional, except if the field’s type is a class type or a constructed type that holds a class type. At the field level, optional and class are incompatible.

Optional fields and required fields can appear in any order in your class definition. You can also assign tags in any order. For example:

CODE
class UnusualButValidPerson
{
    optional(5) string nickname;
    string name;
    optional(0) Date dateOfBirth; // Date is a struct
    string currentAddress;
    optional(123) Fruit favoriteFruit; // Fruit is an enum
}

Default Values

You can specify a default value for a field that has one of the following types:

For example:

SLICE
struct Location
{
    string name;
    Point pt;
    bool display = true;
    string source = "GPS";
}

The legal syntax for literal values is the same as for Slice constants, and you may also use a constant as a default value. The language mapping guarantees that fields are initialized to their declared default values using a language-specific mechanism.

You can declare a default value for optional fields just as you can for required fields:

SLICE
class C
{
    string name;
    bool active = true;
    optional(2) string alternateName;
    optional(5) int overrideCode = -1;
}

An optional field with a default value is considered to be set by default.

Language Mapping

A Slice field maps to a C++ data member with the same name. The type of the C++ data member is the default, memory-owning, mapping of the Slice type.

For example:

CODE
struct Person
{
    string name;
}

maps to:

CPP
struct Person
{
    std::string name; // not std::string_view
    ...
};

Field with a Class Type

A Slice field with a class type maps to a C++ data member with a shared pointer type. For example:

CODE
class Address { ... }

struct Person
{
    string name;
    Address address;
}

maps to:

CPP
class Address { ... };
using AddressPtr = std::shared_ptr<Address>;

struct Person
{
    std::string name;
    AddressPtr address; // can be null
    ...
};

Field with a Proxy Type

A Slice field with a proxy type maps to a C++ data member with a std::optional<T> type. For example:

CODE
interface Widget { ... }

struct Person
{
    Widget* favoriteWidgetProxy;
}

maps to:

CPP
class WidgetPrx { ... };

struct Person
{
    std::optional<WidgetPrx> favoriteWidgetProxy; // can be nullopt
    ...
};

Optional Fields

An optional field maps to a C++ data member with the same name. The data member's type is the mapped type, wrapped in a std::optional. The tag value is not mapped to C++.

For example:

CODE
class C
{
    optional(2) string alternateName;
    optional(5) int overrideCode;
    optional(1) Widget* favoriteWidgetProxy;
}

maps to:

CPP
class C
{
    std::optional<std::string> alternateName;
    std::optional<std::int32_t> overrideCode;
    std::optional<WidgetPrx> favoriteWidgetProxy; // single optional
    ...
};

Proxies are not wrapped twice in std::optional, as illustrated above. As a result, you cannot distinguish between an optional proxy field that is not set and an optional proxy field set to null.

Default Values

Slice default values map to default member initializers in C++.

For example:

CODE
struct Location
{
    string name;
    Point point;
    bool display = true;
    string source = "GPS";
}

maps to:

CPP
struct Location
{
    std::string name;
    Point point;
    bool display{true};
    std::string source{"GPS"};
    ...
};

See Also
JavaScript errors detected

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

If this problem persists, please contact our support.