This API documentation is for Ballerina 1.2.0. View API documentation for the latest release.

Functions - lang.typedesc

constructFrom

Constructs a value with a specified type by copying another value.

constructFrom

(typedesc t, anydata v)

returns null | error

Constructs a value with a specified type by copying another value.

Parameters

  • t typedesc
  • the type for the copy to be constructed

  • v anydata
  • the value to be copied

  • Return Type

    (null | error)
  • a new value that belongs to the type of 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 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