function obj = reshape(obj,varargin)
%RESHAPE Reshape array.
%   B = RESHAPE(A, M, N)
%   B = RESHAPE(A, M, N, P, ...)
%   B = RESHAPE(A, [M, N, P, ...])
%
%   The output B has no dimension labels, even if the input A has them.
%   
%   See also RESHAPE, DLARRAY.

%   Copyright 2019-2020 The MathWorks, Inc.

isObjDlarray = isa(obj,'dlarray');
anyDlarrayInVarargin = false;
if ~isObjDlarray
    for ii=1:numel(varargin)
        arg = varargin{ii};
        if isobject(arg) && isa(arg, 'dlarray')
            varargin{ii} = gather(extractdata(arg));
            anyDlarrayInVarargin = true;
        end
    end
else
    for ii=1:numel(varargin)
        arg = varargin{ii};
        if isobject(arg) && isa(arg, 'dlarray')
            [~, fd] = extractInternalData(arg);
            varargin{ii} = fd.Data;
            anyDlarrayInVarargin = true;
        end
    end
end

if anyDlarrayInVarargin
    % Rethrow this call, let another object handle it unless obj is a
    % dlarray.
    obj = reshape(obj, varargin{:});
    return
end

obj = permuteExplicitly(obj);
[fdobj, objdata] = extractData(obj.FormattedData);

objdata = reshape(objdata, varargin{:});

fdobj.DimensionMetadata = '';
obj.FormattedData = insertData(fdobj, objdata);
end
