close #17403; improve docs for tuple (#18312)

This commit is contained in:
Timothee Cour 2021-06-21 00:45:26 -07:00 • committed by GitHub
commit abd21ef213
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1616,7 +1616,7 @@ heterogeneous storage types with few abstractions. The `()` syntax
can be used to construct tuples. The order of the fields in the constructor can be used to construct tuples. The order of the fields in the constructor
must match the order of the tuple's definition. Different tuple-types are must match the order of the tuple's definition. Different tuple-types are
*equivalent* if they specify the same fields of the same type in the same *equivalent* if they specify the same fields of the same type in the same
order. The *names* of the fields also have to be identical. order. The *names* of the fields also have to be the same.
The assignment operator for tuples copies each component. The assignment operator for tuples copies each component.
The default assignment operator for objects copies each component. Overloading The default assignment operator for objects copies each component. Overloading
@ -1627,15 +1627,16 @@ of the assignment operator is described `here
type type
Person = tuple[name: string, age: int] # type representing a person: Person = tuple[name: string, age: int] # type representing a person:
# a person consists of a name # it consists of a name and an age.
# and an age var person: Person
var
person: Person
person = (name: "Peter", age: 30) person = (name: "Peter", age: 30)
echo person.name assert person.name == "Peter"
# the same, but less readable: # the same, but less readable:
person = ("Peter", 30) person = ("Peter", 30)
echo person[0] assert person[0] == "Peter"
assert Person is (string, int)
assert (string, int) is Person
assert Person isnot tuple[other: string, age: int] # `other` is a different identifier
A tuple with one unnamed field can be constructed with the parentheses and a A tuple with one unnamed field can be constructed with the parentheses and a
trailing comma: trailing comma: