# Union
Union is a data type that combines two or more data types and represents a value of one of those types. Union can combine primitive types, lists, tuples, structures.
Examples:
The type of list items is a union of the types of all items. For example, each element of a
List[Int|String]
is a string or an integer.let aList = [1, 2, "Waves"] # List[Int|String] let bList = [true,false] # List[Boolean] let joined = aList ++ bList # List[Boolean|Int|String]
An argument type of the function wavesBalance(Address|Alias): BalanceDetails is a Union of Address and Alias types. You can call a function with an argument of any of these types.
wavesBalance(Address(base58'3Mz9N7YPfZPWGd4yYaX6H53Gcgrq6ifYiH7')) wavesBalance(Alias("merry")) # The result is the same
A return type of the function getInteger(Address|Alias, String): Int|Unit is a Union of Int and Unit types. The function returns an integer value by key from the data storage of a given account, or
unit
if there is no entry with such a key or the value has a different type.let addr = Address(base58'3N4iKL6ikwxiL7yNvWQmw7rg3wGna8uL6LU') getInteger(addr,"integerVal") # Returns 1 getInteger(addr,"noKey") # Returns unit
# Functions and operators
A value of a Union type cannot be an argument to a function or operator that requires a particular type. For example, the expression
size(getString("key"))
causes a compilation error because the argument type of the function size is String
and the return type of the function getString is String|Unit
. You can get a value of String
type using the getStringValue function.
size(getStringValue("key"))
To get a value of a particular type from a Union, you can use:
Example:
func getAssetName(assetId: ByteVector|Unit) = {
match assetId {
case id: ByteVector => assetInfo(id).value().name
case waves: Unit => "WAVES"
}
}