---
title: "Slice Loaders"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders
language: matlab
docs_index: https://docs.zeroc.com/llms.txt
---

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

# Slice Loaders

Programming language: MATLAB. Other languages: [C++](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/js.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slice-loaders/swift.md).

When Ice unmarshals a Slice-defined class or exception, it starts by locating the mapped class and creating an instance of this class.

The abstraction that drives this locate and create process is called the Slice loader. Each communicator has a default Slice loader, implemented by Ice, that locates the generated class for a Slice type ID.

The default Slice loader of Ice for MATLAB converts the Slice type ID into a MATLAB class name, for example `::M::Node` into `M.Node`. It cannot locate the class for a Slice class or exception when:

- you remap either the class name or an enclosing module using the `matlab:identifier` metadata; or
- you assign a compact ID to the class.

For these classes, create an `Ice.ClassSliceLoader` from the meta classes of the generated classes and install it as your custom Slice loader (see below); the communicator keeps using its default Slice loader for all other classes:

```matlab
communicator = Ice.Communicator(args, SliceLoader = Ice.ClassSliceLoader(?Compact, ?CompactExt));
```

## Custom Slice Loaders

You can install your own custom Slice loader on a communicator to create instances of your own classes, typically classes derived from the generated classes, during unmarshaling. The communicator calls your Slice loader first, and falls back on its default Slice loader when your Slice loader doesn't create an instance.

The communicator calls your Slice loader for both Slice classes and Slice exceptions.

A custom Slice loader derives from `Ice.SliceLoader` and implements its `newInstance` method, which takes a type ID and returns a new instance:

```matlab
classdef NodeLoader < Ice.SliceLoader
    methods
        function r = newInstance(~, typeId)
            if strcmp(typeId, '::Demo::Node')
                r = MyNode();
            else
                r = [];
            end
        end
    end
end
```

You install it with the `SliceLoader` argument of the `Ice.Communicator` constructor:

```matlab
communicator = Ice.Communicator(args, SliceLoader = NodeLoader());
```

## See Also

- [Type IDs](https://docs.zeroc.com/ice/3.8/slice/type-ids.md)
