Record class - dart:core library (original) (raw)
A record value.
The Record
class is a supertype of all record types, but is not itself the runtime type of any object instances_(it's an abstract class)_. All objects that implement Record
has a record type as their runtime type.
A record value, described by a record type, consists of a number of fields, which are each either positional or named.
Record values and record types are written similarly to argument lists and simplified function type parameter lists (no required
modifier allowed, or needed, since record fields are never optional). Example:
(int, String, {bool isValid}) triple = (1, "one", isValid: true);
is syntactically similar to
typedef F = void Function(int, String, {bool isValid});
void callIt(F f) => f(1, "one", isValid: true);
Every record and record type has a shape, given by the number of positional fields and the names of named fields. For example:
(double value, String name, {String isValid}) another = (
3.14, "Pi", isValid: "real");
is another record declaration with the same shape (two positional fields, one named field named isValid
), but with a different type. The names written on the positional fields are entirely for documentation purposes, they have no effect on the program (same as names on positional parameters in function types, like typedef F = int Function(int value);
, where the identifier value
has no effect).
Record values are mainly destructured using patterns, like:
switch (triple) {
case (int value, String name, isValid: bool ok): // ....
}
The individual fields can also be accessed using named getters, using $1
, $2
, etc. for positional fields, and the names themselves for named fields.
int value = triple.$1;
String name = triple.$2;
bool ok = triple.isValid;
Because of that, some identifiers cannot be used as names of named fields:
- The names of
Object
members:hashCode
,runtimeType
,toString
andnoSuchMethod
. - The name of a positional getter in the same record, so
(0, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>1</mn><mo>:</mo><mn>0</mn><mo stretchy="false">)</mo><mi mathvariant="normal">‘</mi><mi>i</mi><mi>s</mi><mi>i</mi><mi>n</mi><mi>v</mi><mi>a</mi><mi>l</mi><mi>i</mi><mi>d</mi><mo separator="true">,</mo><mi>b</mi><mi>u</mi><mi>t</mi><mi mathvariant="normal">‘</mi><mo stretchy="false">(</mo><mn>0</mn><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">1: 0)
is invalid, but(0, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">1</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">0</span><span class="mclose">)</span><span class="mord">‘</span><span class="mord mathnormal">i</span><span class="mord mathnormal">s</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">i</span><span class="mord mathnormal">d</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">b</span><span class="mord mathnormal">u</span><span class="mord mathnormal">t</span><span class="mord">‘</span><span class="mopen">(</span><span class="mord">0</span><span class="mpunct">,</span></span></span></span>2: 0)
is valid, since there is no positional field with getter$2
in that record shape. (It'll still be confusing, and should be avoided in practice.) - Also, no name starting with an underscore,
_
, is allowed. Field names cannot be library private.
The run-time type of a record object is a record type, and as such, a subtype of Record, and transitively of Object and its supertypes.
Record values do not have a persistent identical behavior. A reference to a record object can change at any time to a reference to another record object with the same shape and field values.
Other than that, a record type can only be a subtype of another record type with the same shape, and only if the former record type's field types are subtypes of the other record type's corresponding field types. That is, (int, String, {bool isValid})
is a subtype of(num, String, {Object isValid})
, because they have the same shape, and the field types are pointwise subtypes. Record types with different shapes are unrelated to each other.
Properties
A hash-code compatible with ==
.
no setteroverride
A Type
object representing the runtime type of a record.
no setteroverride
Methods
noSuchMethod(Invocation invocation)→ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
Creates a string-representation of the record.
override
Operators
operator ==(Object other)→ bool
Checks whether other
has the same shape and equal fields to this record.
override