function S = buildScalarLike(fcn, prototype)
%BUILDSCALARLIKE common code for building a codistributed (replicated)
%scalar with attributes matching a prototype.

%   Copyright 2021 The MathWorks, Inc.

value = fcn(underlyingType(prototype));

if issparse(prototype) && ~issparse(value)
    value = sparse(value);
end
if isnumeric(prototype) && ~isreal(prototype) && isreal(value)
    value = complex(value);
end

% Result is always replicated
S = codistributed.pConstructFromReplicated(value);