---
title: "Fields"
url: https://docs.zeroc.com/ice/3.8/slice/fields
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Fields

Parts of this page are for some programming languages only, and name them. For one language's text alone, see [C++](https://docs.zeroc.com/ice/3.8/slice/fields/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/fields/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/fields/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/fields/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/fields/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/fields/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/fields/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/fields/ruby.md), and [Swift](https://docs.zeroc.com/ice/3.8/slice/fields/swift.md).

## 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;
```

The type of a field can be a [basic type](https://docs.zeroc.com/ice/3.8/slice/basic-types.md), a [user-defined type](https://docs.zeroc.com/ice/3.8/slice/user-defined-types.md), `Object*` (an untyped proxy), or `Value` (an untyped class instance).

## Nullable and Non-Nullable Fields

Fields with proxy and class types are nullable: they can hold null. Fields of other types always hold a value, unless they are [optional fields](#optional-fields) that are not set.

```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](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes.md) or [exception](https://docs.zeroc.com/ice/3.8/slice/exceptions.md) 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.

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

The scope of a tag is the class or exception that defines the field, and this class or exception only: a derived class or exception can reuse a tag of its base type. For example:

```slice
exception BaseException
{
    optional(1) int systemCode;
}

exception DerivedException extends BaseException
{
    optional(1) string diagnostic; // OK: a different scope
}
```

Optional and required fields can be interleaved, and their tags can be in any order. The tag identifies the field in the [encoding](https://docs.zeroc.com/ice/3.8/encoding/data-encoding-for-optional-values.md); the language mappings do not expose it.

An optional field cannot have a class type, or a constructed type that holds a class type.

## Default Values

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

- An [integral](https://docs.zeroc.com/ice/3.8/slice/basic-types.md) type (`byte`, `short`, `int`, `long`)
- A [floating point](https://docs.zeroc.com/ice/3.8/slice/basic-types.md) type (`float` or `double`)
- [string](https://docs.zeroc.com/ice/3.8/slice/basic-types.md)
- [bool](https://docs.zeroc.com/ice/3.8/slice/basic-types.md)
- [enum](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations.md)

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](https://docs.zeroc.com/ice/3.8/slice/constants-and-literals.md), 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

> **C++**
>
> 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:
>
> ```slice
> 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:
>
> ```slice
> 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:
>
> ```slice
> interface Widget { ... }
>
> struct Person
> {
>     Widget* favoriteWidgetProxy;
> }
> ```
>
> maps to:
>
> ```cpp
> class WidgetPrx { ... };
>
> struct Person
> {
>     std::optional<WidgetPrx> favoriteWidgetProxy; // can be nullopt
>     ...
> };
> ```
>
> ### Optional Fields {#language-mapping-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`.
>
> For example:
>
> ```slice
> 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 {#language-mapping-default-values}
>
> Slice default values map to default member initializers in C++.
>
> For example:
>
> ```slice
> 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"};
>     ...
> };
> ```

> **C#**
>
> A Slice field maps to a C# field with the same name. The type of the C# field is the mapped Slice type.
>
> In C#, we often remap the field name with `cs:identifier` to convert the name for Pascal case. For example:
>
> ```slice
> class Address { ... }
>
> struct Person
> {
>     ["cs:identifier:Name"]
>     string name;
>
>     ["cs:identifier:Address"]
>     Address address;
> }
> ```
>
> maps to:
>
> ```csharp
> public sealed partial record class Person
> {
>     public string Name = ""; // Slice string maps to C# string
>     public Address? Address; // Slice Address maps the nullable C# Address
>     ...
> }
> ```
>
> ### Optional Fields {#language-mapping-optional-fields}
>
> An optional field maps to a C# field with the same name. The mapped field’s type is nullable.
>
> For example:
>
> ```slice
> class C
> {
>     ["cs:identifier:AlternateName"]
>     optional(2) string alternateName;
>
>     ["cs:identifier:OverrideCode"]
>     optional(5) int overrideCode;
>
>     ["cs:identifier:FavoriteWidgetProxy"]
>     optional(1) Widget* favoriteWidgetProxy;
> }
> ```
>
> maps to:
>
> ```csharp
> public partial class C : Ice.Value
> {
>     public string? AlternateName;
>     public int? OverrideCode;
>     public WidgetPrx? FavoriteWidgetProxy;
>     ...
> }
> ```
>
> Optional and non-optional proxies are mapped the same way, 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 {#language-mapping-default-values}
>
> Slice default values map to default values in C#.
>
> For example:
>
> ```slice
> struct Location
> {
>     ["cs:identifier:Name"]
>     string name;
>
>     ["cs:identifier:Point"]
>     Point point;
>
>     ["cs:identifier:Display"]
>     bool display = true;
>
>     ["cs:identifier:Source"]
>     string source = "GPS";
> }
> ```
>
> maps to:
>
> ```csharp
> public sealed partial record class Location
> {
>     public string Name = "";
>     public Point Point;
>     public bool Display = true;
>     public string Source = "GPS";
>     ...
> }
> ```
>
> When you don’t define a default value in Slice, and you initialize a field without providing a value for this field, the generated code uses the following default:
>
> | **Optional Field?** | **Slice Field Type** | **C# Default Value** |
> | --- | --- | --- |
> | No | `string` | Empty string |
> |  | `enum` | `default` |
> |  | `struct` | `default` (when the struct is mapped to a C# struct), `null!` (when the struct is mapped to a C# class) |
> |  | Numeric | `0` |
> |  | `bool` | `false` |
> |  | `sequence`, `dictionary` | `null!` |
> |  | `class`, proxy | `null` |
> | Yes | Any | `null` |
>
> > **Note**
> >
> > The generated constructor for a struct does not initialize any field to `null!`: you always have to provide values for these fields.

> **Java**
>
> A Slice field maps to a Java field with the same name. The type of the Java field is the mapped Slice type. This is the default mapping.
>
> For example:
>
> ```slice
> class Address { ... }
>
> struct Person
> {
>     string name;
>     Address address;
> }
> ```
>
> maps to:
>
> ```java
> public final class Person implements java.lang.Cloneable, java.io.Serializable {
>     public String name; // Slice string maps to Java String
>     public Address address; // Slice Address maps the Java Address
>     ...
> }
> ```
>
> ### JavaBean Mapping
>
> Use the metadata directive `java:getset` to map a Slice field to two or more JavaBean-style methods instead of a public field.
>
> For each field `val` of type `T`, the mapping generates the following methods:
>
> ```java
> public T getVal();
> public void setVal(T v);
> ```
>
> The mapping generates an additional method if `T` is the `bool` type:
>
> ```java
> public boolean isVal();
> ```
>
> Finally, if `T` is a sequence type with an element type `E`, two methods are generated to provide direct access to elements:
>
> ```java
> public E getVal(int index);
> public void setVal(int index, E v);
> ```
>
> Note that these element methods are only generated for sequence types that use the default mapping.
>
> You can apply the `java:getset` directive to an individual field, or to enclosing construct, as illustrated by the following example:
>
> ```slice
> sequence<int> IntSeq;
> class C
> {
>     ["java:getset"] int i;
>     double d;
> }
>
> ["java:getset"]
> struct S
> {
>     bool b;
>     string str;
> }
>
> ["java:getset"]
> exception E
> {
>     IntSeq seq;
> }
> ```
>
> JavaBean get-set methods are generated for all fields of struct `S` and exception `E`, but for only one field of class `C`. Relevant portions of the generated code are shown below:
>
> ```java
> public class C extends com.zeroc.Ice.Value {
>     private int i;
>     public double d;
>
>     public int getI() {
>         return i;
>     }
>
>     public void setI(int i) {
>         this.i = i;
>     }
>     ...
> }
>
> public final class S implements java.lang.Cloneable, java.io.Serializable {
>     private boolean b;
>     private java.lang.String str;
>
>     public boolean getB() {
>         return b;
>     }
>
>     public void setB(boolean b) {
>         this.b = b;
>     }
>
>     public boolean isB() {
>         return b;
>     }
>
>     public java.lang.String getStr() {
>         return str;
>     }
>
>     public void setStr(java.lang.String str) {
>         this.str = str;
>     }
>     ...
> }
>
> public class E extends com.zeroc.Ice.UserException {
>     private int[] seq;
>
>     public int[] getSeq() {
>         return seq;
>     }
>
>     public void setSeq(int[] seq) {
>         this.seq = seq;
>     }
>
>     public int getSeq(int index) {
>         return this.seq[index];
>     }
>
>     public void setSeq(int index, int val) {
>         this.seq[index] = val;
>     }
>     ...
> }
> ```
>
> ### Optional Fields {#language-mapping-optional-fields}
>
> The mapping for optional fields in Slice classes and exceptions uses a JavaBean-style API that provides methods to get, set, and clear a field’s value, and test whether a value is set. Consider the following Slice definition:
>
> ```slice
> class C
> {
>     string name;
>     optional(2) string alternateName;
>     optional(5) bool active;
> }
> ```
>
> The generated Java code provides the following API:
>
> ```java
> public class C extends com.zeroc.Ice.Value {
>     public String getAlternateName()...
>     public void setAlternateName(String alternateName)...
>     public boolean hasAlternateName()...
>     public void clearAlternateName()...
>     public void optionalAlternateName(java.util.Optional<String> v)...
>     public java.util.Optional<String> optionalAlternateName()...
>
>     public boolean isActive()...
>     public boolean getActive()...
>     public void setActive(boolean v)...
>     public boolean hasActive()...
>     public void clearActive()...
>     public void optionalActive(java.util.Optional<Boolean> v)...
>     public java.util.Optional<Boolean> optionalActive()...
>
>     ...
> }
> ```
>
> The `has` method allows you to test whether a field’s value has been set, and the `clear` method removes any existing value for a field.
>
> > **Note**
> >
> > Calling a `get` method when the field’s value has not been set throws `java.util.NoSuchElementException`.
>
> The `optional` methods provide an alternate API that uses standard Java types to encapsulate the value:
>
> - `java.util.OptionalDouble` Encapsulates a value of type `double`
> - `java.util.OptionalInt` Encapsulates a value of type `int`
> - `java.util.OptionalLong` Encapsulates a value of type `long`
> - `java.util.Optional<T>` Encapsulates all other Slice types
>
> ### Default Values {#language-mapping-default-values}
>
> Slice default values change the implementation of the parameterless constructor of the enclosing type.
>
> For example:
>
> ```slice
> struct Location
> {
>     string name;
>     Point point;
>     bool display = true;
>     string source = "GPS";
> }
> ```
>
> maps to:
>
> ```java
> public final class Location implements java.lang.Cloneable, java.io.Serializable {
>     public String name;
>     public Point point;
>     public boolean display;
>     public String source;
>
>     public Location() {
>         this.name = "";
>         this.point = new Point();
>         this.display = true;
>         this.source = "GPS";
>     }
>
>     ...
> }
> ```
>
> When you don’t define a default value in Slice, and you initialize a field without providing a value for this field, the generated code uses the following default:
>
> | **Optional Field?** | **Slice Field Type** | **Default Java Value** |
> | --- | --- | --- |
> | No | `string` | Empty string |
> |  | `enum` | First enumerator in enumeration |
> |  | `struct` | New instance created with no argument |
> |  | Numeric | `0` |
> |  | `bool` | `false` |
> |  | `class`, proxy, `sequence`, `dictionary` | `null` |
> | Yes | Any | Not set |

> **JavaScript**
>
> A Slice field maps to a JavaScript property with the same name. The type of the property is the mapped Slice type.
>
> For example:
>
> ```slice
> class Address { string city; }
>
> struct Person {
>     string name;
>     Address address;
> }
> ```
>
> Generates the following JavaScript code:
>
> ```js
> M.Person = class {
>     constructor(name = "", address = null) {
>         this.name = name;
>         this.address = address;
>     }
>     // ...
> };
> ```
>
> And the corresponding TypeScript declarations:
>
> ```typescript
> export class Address extends Ice.Value {
>     constructor(city?: string);
>     // ...
>     city: string;
> }
>
> export class Person {
>     constructor(name?: string, address?: Address);
>     // ...
>     name: string;
>     address: Address | null;
> }
> ```
>
> ### Optional Fields {#language-mapping-optional-fields}
>
> An optional field maps to a JavaScript property with the same name. The property's type is optional: an unset field is `undefined`, and Ice also marshals an optional proxy field set to `null` as unset.
>
> For example:
>
> ```slice
> class C
> {
>     optional(2) string alternateName;
>     optional(5) int overrideCode;
>     optional(1) Widget* favoriteWidgetProxy;
> }
> ```
>
> Generates the following JavaScript code:
>
> ```js
> M.C = class extends Ice.Value {
>     constructor(
>         alternateName = undefined,
>         overrideCode = undefined,
>         favoriteWidgetProxy = undefined) {
>         super();
>         this.alternateName = alternateName;
>         this.overrideCode = overrideCode;
>         this.favoriteWidgetProxy = favoriteWidgetProxy;
>     }
>     // ...
> }
> ```
>
> And the corresponding TypeScript declarations:
>
> ```typescript
> export class C extends Ice.Value {
>     constructor(
>         alternateName?: string,
>         overrideCode?: number,
>         favoriteWidgetProxy?: WidgetPrx);
>
>     alternateName?: string;
>     overrideCode?: number;
>     favoriteWidgetProxy?: WidgetPrx | null;
>     // ...
> }
> ```
>
> ### Default Values {#language-mapping-default-values}
>
> Slice default values map to default values in JavaScript.
>
> For example:
>
> ```slice
> class Point { int x; int y; }
>
> struct Location
> {
>     string name;
>     Point point;
>     bool display = true;
>     string source = "GPS";
> }
> ```
>
> Generates the following JavaScript code:
>
> ```js
> M.Location = class {
>     constructor(name = "", point = null, display = true, source = "GPS") {
>         this.name = name;
>         this.point = point;
>         this.display = display;
>         this.source = source;
>     }
>     // ...
> }
> ```
>
> And the corresponding TypeScript declarations:
>
> ```typescript
> export class Location {
>     constructor(name?: string, point?: Point, display?: boolean, source?: string);
>
>     name: string;
>     point: Point | null;
>     display: boolean;
>     source: string;
> }
> ```
>
> When you don’t define a default value in Slice, and you initialize a field without providing a value for this field, the generated code uses the following default:
>
> | **Optional Field?** | **Slice Field Type** | **Default JavaScript Value** |
> | --- | --- | --- |
> | No | `string` | Empty string |
> |  | `enum` | First enumerator in enumeration |
> |  | `struct` | New instance created with no argument |
> |  | `byte`, `short`, `int`, `float`, `double` | `0` |
> |  | `long` | `0n` |
> |  | `bool` | `false` |
> |  | `sequence`, `dictionary`, `class`, proxy | `null` |
> | Yes | Any | undefined |

> **MATLAB**
>
> 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:
>
> ```slice
> 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 {#language-mapping-optional-fields}
>
> An optional field maps to a MATLAB property just like a regular field, and accepts the marker value `Ice.Unset` to mark the field as unset. `Ice.Unset` applies to any field type except proxies: use an empty proxy array, such as `GreeterPrx.empty`, to mark a proxy field as unset.
>
> 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
>     disp(obj.OptionalField);
> else
>     disp('OptionalField is unset');
> end
> ```
>
> ### Default Values {#language-mapping-default-values}
>
> Slice default values are mapped to default MATLAB property values.
>
> For example:
>
> ```slice
> 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 | `Object*`, proxy | `MappedPrx.empty` |
> |  | Other types | `Ice.Unset` |

> **PHP**
>
> A Slice field maps to a PHP public property with the same name.
>
> For example:
>
> ```slice
> class Address { ... }
>
> struct Person
> {
>     string name;
>     Address address;
> }
> ```
>
> maps to:
>
> ```php
> class Address extends \Ice\Value
> {
>     ...
> }
>
> class Person
> {
>     public $name;
>     public $address;
>
>     public function __construct($name='', $address=null)
>     {
>         $this->name = $name;
>         $this->address = $address;
>     }
>     ...
> }
> ```
>
> ### Optional Fields {#language-mapping-optional-fields}
>
> An optional field maps to a PHP public property with the same name.
>
> For example:
>
> ```slice
> class C
> {
>     optional(2) string alternateName;
>     optional(5) int overrideCode;
>     optional(1) Widget* favoriteWidgetProxy;
> }
> ```
>
> maps to:
>
> ```php
> class C extends \Ice\Value
> {
>     public $alternateName;
>     public $overrideCode;
>     public $favoriteWidgetProxy;
>
>     public function __construct($alternateName=\Ice\None, $overrideCode=\Ice\None, $favoriteWidgetProxy=\Ice\None)
>     {
>         $this->alternateName = $alternateName;
>         $this->overrideCode = $overrideCode;
>         $this->favoriteWidgetProxy = $favoriteWidgetProxy;
>     }
>     ...
> }
> ```
>
> The default value for optional fields is `\Ice\None`; it represents the “not set” value.
>
> ### Default Values {#language-mapping-default-values}
>
> Slice default values map to default values in the constructor of the mapped class.
>
> For example:
>
> ```slice
> struct Location
> {
>     string name;
>     Point point;
>     bool display = true;
>     string source = "GPS";
> }
> ```
>
> maps to:
>
> ```php
> class Location
> {
>     public $name;
>     public $point;
>     public $display;
>     public $source;
>
>     public function __construct($name='', $point=null, $display=true, $source="GPS")
>     {
>         $this->name = $name;
>         $this->point = is_null($point) ? new \Example\Point : $point;
>         $this->display = $display;
>         $this->source = $source;
>     }
>     ...
> }
> ```
>
> When you don’t define a default value in Slice, and you initialize a field without providing a value for this field, the generated code uses the following default:
>
> | **Optional Field?** | **Slice Field Type** | **Default PHP Value** |
> | --- | --- | --- |
> | No | `string` | Empty string |
> |  | `enum` | First enumerator in enumeration |
> |  | `struct` | New instance created with no argument |
> |  | Numeric | `0` |
> |  | `bool` | `false` |
> |  | `sequence`, `dictionary`, `class`, proxy | `null` |
> | Yes | Any | `\Ice\None` |

> **Python**
>
> A Slice field maps to a Python dataclass field with the same name. The type of the Python field is the mapped Slice type.
>
> For example:
>
> ```slice
> class Address { ... }
>
> struct Person
> {
>     string name;
>     Address address;
> }
> ```
>
> maps to:
>
> ```py
> @dataclass
> class Person:
>     name: str = ""
>     address: Address | None = None
> ```
>
> ### Optional Fields {#language-mapping-optional-fields}
>
> An optional field maps to a Python field with the same name. The mapped field’s type is the mapped type or `None`.
>
> For example:
>
> ```slice
> class C
> {
>     optional(2) string alternateName;
>     optional(5) int overrideCode;
>     optional(1) Widget* favoriteWidgetProxy;
> }
> ```
>
> maps to:
>
> ```py
> @dataclass(eq=False)
> class C(Value):
>     alternateName: str | None = None
>     overrideCode: int | None = None
>     favoriteWidgetProxy: WidgetPrx | None = None
> ```
>
> Optional and non-optional proxies are mapped the same way, 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 (None).
>
> ### Default Values {#language-mapping-default-values}
>
> Slice default values map to default values in Python.
>
> For example:
>
> ```slice
> struct Location
> {
>     string name;
>     Point point;
>     bool display = true;
>     string source = "GPS";
> }
> ```
>
> maps to:
>
> ```py
> @dataclass(order=True, unsafe_hash=True)
> class Location:
>     name: str = ""
>     point: Point = field(default_factory=Point)
>     display: bool = True
>     source: str = "GPS"
> ```
>
> When you don’t define a default value in Slice, and you initialize a field without providing a value for this field, the generated code uses the following default:
>
> | **Optional Field?** | **Slice Field Type** | **Default Python Value** |
> | --- | --- | --- |
> | No | `string` | Empty string |
> |  | `enum` | First enumerator in enumeration |
> |  | `struct` | New instance created with no argument |
> |  | `byte`, `short`, `int`, `long` | `0` |
> |  | `float`, `double` | `0.0` |
> |  | `bool` | `False` |
> |  | `sequence<byte>` | Empty `bytes` |
> |  | Other `sequence` | Empty list |
> |  | `dictionary` | Empty dictionary |
> |  | `class`, proxy | `None` |
> | Yes | Any | `None` |
>
> A sequence field whose type or declaration carries a [sequence metadata directive](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/sequences/python.md#customizing-the-sequence-mapping) defaults to an empty instance of the type the directive selects, such as an empty tuple for `python:tuple`. With `python:memoryview`, the default value is the object the factory function returns for a `None` buffer.

> **Ruby**
>
> A Slice field maps to a Ruby instance variable with the same name, plus accessors to read and write this instance variable.
>
> For example:
>
> ```slice
> class Address { ... }
>
> struct Person
> {
>     string name;
>     Address address;
> }
> ```
>
> maps to:
>
> ```ruby
> class Address < Ice::Value
>     ...
> end
>
> class Person
>     attr_accessor :name, :address
>
>     def initialize(name='', address=nil)
>         @name = name
>         @address = address
>     end
>     ...
> end
> ```
>
> ### Optional Fields {#language-mapping-optional-fields}
>
> An optional field maps to a Ruby instance variable and accessors, just like a non-optional field.
>
> For example:
>
> ```slice
> class C
> {
>     optional(2) string alternateName;
>     optional(5) int overrideCode;
>     optional(1) Widget* favoriteWidgetProxy;
> }
> ```
>
> maps to:
>
> ```ruby
> class C < Ice::Value
>     attr_accessor :alternateName, :overrideCode, :favoriteWidgetProxy
>
>     def initialize(alternateName=Ice::Unset, overrideCode=Ice::Unset, favoriteWidgetProxy=Ice::Unset)
>         @alternateName = alternateName
>         @overrideCode = overrideCode
>         @favoriteWidgetProxy = favoriteWidgetProxy
>     end
> end
> ```
>
> The default value for optional fields is `Ice::Unset`; it represents the “not set” value.
>
> ### Default Values {#language-mapping-default-values}
>
> Slice default values map to default values in the mapped `initialize` method.
>
> For example:
>
> ```slice
> struct Location
> {
>     string name;
>     Point point;
>     bool display = true;
>     string source = "GPS";
> }
> ```
>
> maps to:
>
> ```ruby
> class Location
>     attr_accessor :name, :point, :display, :source
>
>     def initialize(name='', point=::Example::Point.new, display=true, source="GPS")
>         @name = name
>         @point = point
>         @display = display
>         @source = source
>     end
>
>     # ...
> end
> ```
>
> When you don’t define a default value in Slice, and you initialize a field without providing a value for this field, the generated code uses the following default:
>
> | **Optional Field?** | **Slice Field Type** | **Default Ruby Value** |
> | --- | --- | --- |
> | No | `string` | Empty string |
> |  | `enum` | First enumerator in enumeration |
> |  | `struct` | New instance created with no argument |
> |  | Numeric | `0` |
> |  | `bool` | `false` |
> |  | `sequence`, `dictionary`, `class`, proxy | `nil` |
> | Yes | Any | `Ice::Unset` |

> **Swift**
>
> A Slice field maps to a Swift property with the same name. The type of the property is the mapped Slice type. When the Slice field is non-optional, the property type is non-optional as well, except for class and proxy fields.
>
> For example:
>
> ```slice
> class Address { ... }
>
> struct Person
> {
>     string name;
>     Address address;
> }
> ```
>
> maps to:
>
> ```swift
> public final class Person {
>     public var name: String = ""       // non-optional String
>     public var address: Address? = nil // optional Address
>     ...
> }
> ```
>
> ### Optional Fields {#language-mapping-optional-fields}
>
> An optional field maps to a Swift stored property with the same name. The mapped property’s type is optional.
>
> For example:
>
> ```slice
> class C
> {
>     optional(2) string alternateName;
>     optional(5) int overrideCode;
>     optional(1) Widget* favoriteWidgetProxy;
> }
> ```
>
> maps to:
>
> ```swift
> open class C: Ice.Value {
>     public var alternateName: String? = nil
>     public var overrideCode: Int32? = nil
>     public var favoriteWidgetProxy: WidgetPrx? = nil
>     ...
> }
> ```
>
> Optional and non-optional proxies are mapped the same way, as illustrated above. As a result, you cannot distinguish between an optional proxy property that is not set and an optional proxy property set to nil.
>
> ### Default Values {#language-mapping-default-values}
>
> Slice default values map to default property values in Swift.
>
> For example:
>
> ```slice
> struct Location
> {
>     string name;
>     Point point;
>     bool display = true;
>     string source = "GPS";
> }
> ```
>
> maps to:
>
> ```swift
> public struct Location: Hashable, Sendable {
>     public var name: String = ""
>     public var point: Point = Point()
>     public var display: Bool = true
>     public var source: String = "GPS"
>     ...
> }
> ```
>
> 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** | **Default Swift Value** |
> | --- | --- | --- |
> | No | `string` | Empty string |
> |  | `enum` | First enumerator in enumeration |
> |  | `struct` | New instance created with no argument |
> |  | Numeric | `0` |
> |  | `bool` | `false` |
> |  | `sequence` | Empty array |
> |  | `dictionary` | Empty dictionary |
> |  | `class`, proxy | `nil` |
> | Yes | Any | `nil` |
