Skip to content

Commit

Permalink
Change typedesc:constructFrom to value:cloneWithType
Browse files Browse the repository at this point in the history
Fixes #471.
Changing return type will be done in #426 and #386.
  • Loading branch information
jclark committed Apr 28, 2020
1 parent ceac9f6 commit c2f497c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
34 changes: 0 additions & 34 deletions lang/lib/typedesc.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnydataType> t, anydata v) returns AnydataType|error = external;
28 changes: 28 additions & 0 deletions lang/lib/value.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnydataType> t) returns AnydataType|error = external;

# Tests whether `v` is read-only, i.e. immutable
# Returns true if read-only, false otherwise.
#
Expand Down

0 comments on commit c2f497c

Please sign in to comment.