function D = createArrayLike(T, dims, F)
%createArrayLike  create a codistributed array
%   
%   See also createArray, CODISTRIBUTED.


%   Copyright 2023 The MathWorks, Inc.

% We need to combine the fill value and the "like" template then call
% createArray with the merged value.
proto = distributedutil.Allocator.extractTemplate(T);
if nargin<3
    % If no value was provided use in-memory createArray to get one from T.
    F = createArray(1,"like",proto);
else
    % Make sure the fill value is scalar and replicated.
    if ~isscalar(F)
        error(message("MATLAB:createArray:invalidFillValue"));
    end
    if iscodistributed(F)
        F = gather(F);
    end
    % Make F "like" T by assignment as cast only works for numeric types.
    proto(1) = F;
    F = proto;
end
% The fill value must be codistributed to get a codistributed result.
F = codistributed(F);
% Now just let createArray do the work.
D = createArray(dims,FillValue=F);

end
