matlab.lang.HandlePlaceholder - Basic subclass of handle - MATLAB (original) (raw)

Main Content

Namespace: matlab.lang

Basic subclass of handle

Since R2024b

Description

This class is a subclass of handle, and it defines no members other than what it inherits from handle. You can usematlab.lang.HandlePlaceholder when you need a basic handle class to test code. You can also use an object of this class as a default value when defining a property with the WeakHandle attribute.

The matlab.lang.HandlePlaceholder class is a handle class.

Creation

The no-argument constructor of matlab.lang.WeakReference returns an instance with its Handle property set to a handle to a deleted matlab.lang.WeakReference object. You can also call thematlab.lang.HandlePlaceholder class constructor directly.

Examples

collapse all

Create a 2-by-3 array of weak reference placeholders by calling createArray with matlab.lang.WeakReference as the class.

weakArray = createArray(2,3,"matlab.lang.WeakReference")

weakArray =

2×3 WeakReference array with properties:

Handle

Verify that the Handle property of the array elements is set to a handle to a deleted matlab.lang.HandlePlaceholder object.

ans =

handle to deleted HandlePlaceholder

When defining a property using the WeakHandle attribute, you must use class validation. You can use handle as a way of allowing all handle class values, but because handle is an abstract class, you must provide a default value as part of the validation. Define a class with aWeakHandle property named Property1, and validate the property value with the handle class and an empty array ofmatlab.lang.HandlePlaceholder as the initial value.

classdef NonspecificWeak properties (WeakHandle) Property1 handle = matlab.lang.HandlePlaceholder.empty end end

Construct an instance of the class.

x =

NonspecificWeak with properties:

Property1: [0×0 matlab.lang.HandlePlaceholder]

If you also need to use size validation, rewrite the class to use the matlab.lang.invalidHandle function to create an array of handles to deleted matlab.lang.HandlePlaceholder objects instead.

classdef SizedNonspecificWeak properties (WeakHandle) Property1 (2,2) handle = matlab.lang.invalidHandle("matlab.lang.HandlePlaceholder",2,2) end end

Construct an instance of the class. Access an element ofProperty1 to verify that the value is a handle to a deletedmatlab.lang.HandlePlaceholder object.

y = SizedNonspecificWeak; y.Property1(1,1)

ans =

handle to deleted HandlePlaceholder

Version History

Introduced in R2024b