From c2f497c3272bd15f07a780717985553d50ca85d9 Mon Sep 17 00:00:00 2001 From: James Clark Date: Tue, 28 Apr 2020 11:05:43 +0700 Subject: [PATCH] Change typedesc:constructFrom to value:cloneWithType Fixes #471. Changing return type will be done in #426 and #386. --- lang/lib/typedesc.bal | 34 ---------------------------------- lang/lib/value.bal | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/lang/lib/typedesc.bal b/lang/lib/typedesc.bal index 45c07b24..8f975207 100644 --- a/lang/lib/typedesc.bal +++ b/lang/lib/typedesc.bal @@ -13,37 +13,3 @@ // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. - -# A type parameter that is a subtype of `anydata`. -# Has the special semantic that when used in a declaration -# all uses in the declaration must refer to same type. -@typeParam -type AnydataType anydata; - -# Constructs a value with a specified type by copying another value. -# + t - the type for the copy to be constructed -# + v - the value to be copied -# + return - a new value that belongs to type `t`, or an error if this cannot be done -# -# When `v` is a structural value, the inherent type of the value to be constructed -# comes from `t`. When `t` is a union, it must be possible to determine which -# member of the union to use for the inherent type by following the same rules -# that are used by list constructor expressions and mapping constructor expressions -# with the contextually expected type. If not, then an error is returned. -# The `constructFrom` operation is recursively applied to each member of `v` using -# the type descriptor that the inherent type requires for that member. -# -# Like the Clone abstract operation, this does a deep copy, but differs in -# the following respects: -# - the inherent type of any structural values constructed comes from the specified -# type descriptor rather than the value being constructed -# - the read-only bit of values and fields comes from the specified type descriptor -# - the graph structure of `v` is not preserved; the result will always be a tree; -# an error will be returned if `v` has cycles -# - immutable structural values are copied rather being returned as is; all -# structural values in the result will be mutable, except for error values -# (which are always immutable) -# - numeric values can be converted using the NumericConvert abstract operation -# - if a record type descriptor specifies default values, these will be used -# to supply any missing members -public function constructFrom(typedesc t, anydata v) returns AnydataType|error = external; diff --git a/lang/lib/value.bal b/lang/lib/value.bal index 7093839f..67b6de44 100644 --- a/lang/lib/value.bal +++ b/lang/lib/value.bal @@ -45,6 +45,34 @@ public function clone(CloneableType v) returns CloneableType = external; # + return - immutable clone of `v` public function cloneReadOnly(CloneableType v) returns CloneableType = external; +# Constructs a value with a specified type by cloning another value. +# + v - the value to be cloned +# + t - the type for the cloned to be constructed +# + return - a new value that belongs to type `t`, or an error if this cannot be done +# +# When `v` is a structural value, the inherent type of the value to be constructed +# comes from `t`. When `t` is a union, it must be possible to determine which +# member of the union to use for the inherent type by following the same rules +# that are used by list constructor expressions and mapping constructor expressions +# with the contextually expected type. If not, then an error is returned. +# The `constructFrom` operation is recursively applied to each member of `v` using +# the type descriptor that the inherent type requires for that member. +# +# Like the Clone abstract operation, this does a deep copy, but differs in +# the following respects: +# - the inherent type of any structural values constructed comes from the specified +# type descriptor rather than the value being constructed +# - the read-only bit of values and fields comes from the specified type descriptor +# - the graph structure of `v` is not preserved; the result will always be a tree; +# an error will be returned if `v` has cycles +# - immutable structural values are copied rather being returned as is; all +# structural values in the result will be mutable, except for error values +# (which are always immutable) +# - numeric values can be converted using the NumericConvert abstract operation +# - if a record type descriptor specifies default values, these will be used +# to supply any missing members +public function cloneWithType(anydata v, typedesc t) returns AnydataType|error = external; + # Tests whether `v` is read-only, i.e. immutable # Returns true if read-only, false otherwise. #