Skip to content

Commit

Permalink
add idempotent one-arg constructor forms for string, datetime, etc (b…
Browse files Browse the repository at this point in the history
…ut not table)

The table class does not get one, because that's not how Matlab's table(x) constructor works, and would be a type discontinuity. Instead, it creates a new one-variable table whose variable contains the table passed to the constructor. (That is, it creates a table containing a nested table.)
  • Loading branch information
apjanke committed Feb 4, 2024
1 parent 2e46b2f commit bf1db6c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
5 changes: 5 additions & 0 deletions inst/calendarDuration.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
# this = calendarDuration (..., 'Format',displayFormat)

args = varargin;
if (nargin == 1 && isa (args{1}, 'calendarDuration'))
this = args{1};
return
endif

# Peel off options
knownOptions = {'Format'};
opts = struct;
Expand Down
4 changes: 4 additions & 0 deletions inst/categorical.m
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@
if (nargin == 0)
return
endif
if (nargin == 1 && isa (x, 'categorical'))
this = x;
return
end

validOptions = {'Ordinal', 'Protected'};
[opts, args] = peelOffNameValueOptions (varargin, validOptions);
Expand Down
9 changes: 6 additions & 3 deletions inst/datetime.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@

# Peel off options
args = varargin;
knownOptions = {'Format','InputFormat','Locale','PivotYear','TimeZone'};
knownOptions = {"Format", "InputFormat", "Locale", "PivotYear", "TimeZone"};
opts = struct;
while (numel (args) >= 3 && isa (args{end-1}, 'char') ...
while (numel (args) >= 3 && isa (args{end-1}, "char") ...
&& ismember (args{end-1}, knownOptions))
opts.(args{end-1}) = args{end};
args(end-1:end) = [];
Expand All @@ -147,7 +147,10 @@
dnums = now;
case 1
x = args{1};
if (isnumeric (x))
if (isa (x, 'datetime'))
this = x;
return
elseif (isnumeric (x))
# Convert date vectors
dnums = datenum (x);
elseif (ischar (x) || iscellstr (x) || isa (x, 'string'))
Expand Down
7 changes: 5 additions & 2 deletions inst/duration.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
#DURATION Construct a new duration array
args = varargin;
# Peel off options
knownOptions = {'InputFormat','Format'};
knownOptions = {'InputFormat', 'Format'};
opts = struct;
while (numel (args) >= 3 && isa (args{end-1}, 'char') ...
&& ismember (args{end-1}, knownOptions))
Expand All @@ -109,7 +109,10 @@
return
case 1
in = args{1};
if (isnumeric (in))
if (isa (in, 'duration'))
this = in;
return
elseif (isnumeric (in))
switch (size (in, 2))
case 3
[H, MI, S] = deal (in(:,1), in(:,2), in(:,3));
Expand Down
5 changes: 4 additions & 1 deletion inst/localdate.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@
dnums = floor (now);
case 1
x = args{1};
if (isnumeric (x))
if (isa (x, 'localdate'))
this = x;
return
elseif (isnumeric (x))
# Convert datenums
mustBeIntOrNanOrInf (x, 'input');
dnums = double (x);
Expand Down
14 changes: 3 additions & 11 deletions inst/string.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,15 @@
## @end itemize
##
## @end deftypefn
function this = string(in, varargin)
# TODO: Support a 'MapMissing' option to map "standard missing values"
# (empty strings, NaN, NaT) to string missings.
#
function this = string(in)
# TODO: Maybe fall back to calling cellstr() on arbitrary input objects.
if (nargin == 0)
return
endif
if (isa (in, "string"))
# Copy properties, because I don't know if a full-array pass-through
# works in Octave. -apj
# this = in; % That is, don't do this.
this.strs = in.strs;
this.tfMissing = in.tfMissing;
this = in;
return
endif
if (ischar (in))
elseif (ischar (in))
this.strs = cellstr (in);
this.tfMissing = false (size (this.strs));
elseif (iscell (in))
Expand Down

0 comments on commit bf1db6c

Please sign in to comment.