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:

SLICE
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:

SLICE
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.

SLICE
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:

SLICE
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 MATLAB property, with by default the same name. We often remap the field name with matlab:identifier to convert the name to Pascal case.

The MATLAB class for the property is the mapped type, as presented earlier, except the following properties don’t specify a MATLAB class:

  • properties mapped from optional fields

  • properties mapped from fields of class type or that reference class types.

The size and validation function(s) of each property depends on the field type:

Slice Field Type

MATLAB Size

MATLAB Validation Function

Remarks

bool, numeric type, enum, dictionary

Scalar: (1, 1)

string

(1, :)

An empty array (of char) represents an empty string.

sequence

(1, :)

An empty array or an empty cell array represents an empty sequence.

class, proxy

No size constraint

{mustBeScalarOrEmpty}

An empty array represents a null class instance or null proxy.

struct

No size constraint

{mustBeScalarOrEmpty}

An empty array is a temporary value. Replace this empty array with a scalar.

For example:

CODE
class Address { ... }

struct Person
{
    ["matlab:identifier:Name"]
    string name;

    ["matlab:identifier:Address"]
    Address address;
}

maps to:

MATLAB
classdef Address < Ice.Value
    ...
end

classdef (Sealed) Person
    properties
        Name    (1, :) char
        % empty corresponds to null
        Address {mustBeScalarOrEmpty} = Example.Address.empty
    end
    ...
end

Optional Fields

An optional field maps to a MATLAB property just like a regular field, except you can also set this property to the marker value Ice.Unset. The tag value is not mapped to MATLAB.

A well-behaved program must test a MATLAB property (mapped from an optional field) before using its value:

MATLAB
obj = ...;
if obj.OptionalField ~= Ice.Unset
    fprintf('OptionalField = %s\n', obj.OptionalField);
else
    fprintf('OptionalField is unset\n');
end

Default Values

Slice default values are mapped to default MATLAB property values.

For example:

CODE
struct Location
{
    ["matlab:identifier:Name"]
    string name;

    ["matlab:identifier:Point"]
    Point point;

    ["matlab:identifier:Display"]
    bool display = true;

    ["matlab:identifier:Source"]
    string source = "GPS";
}

maps to:

MATLAB
classdef (Sealed) Location
    properties
        Name    (1, :) char
        Point          Example.Point {mustBeScalarOrEmpty} = Example.Point.empty
        Display (1, 1) logical = true
        Source  (1, :) char = sprintf('GPS')
    end
    methods
        function obj = Location(Name, Point, Display, Source)
            if nargin > 0
                assert(nargin == 4, 'Invalid number of arguments');
                obj.Name = Name;
                obj.Point = Point;
                obj.Display = Display;
                obj.Source = Source;
            end
       end
   ...
   end
end

When you don’t define a default value in Slice, and you initialize a property without providing a value for this property, the generated code uses the following default:

Optional Field?

Slice Field Type

MATLAB Default Value

No

bool, numeric type

0 (implicit default)

string

Empty 1-by-0 array of char (implicit default)

enum

First enumerator (implicit default)

Object*, proxy

MappedPrx.empty(implicit default)

Value, class, struct

MappedType.empty(implicit default when the MATLAB class is specified)

sequence

MappedElementType.empty(implicit default), or {} (implicit default when the MATLAB class is specified).

dictionary

configureDictionary('keyType', 'valueType')

Yes

Any

Ice.Unset

JavaScript errors detected

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

If this problem persists, please contact our support.