Derives: Copying and Default (reference)¶
This page specifies Clone, Copy and Default, and field defaults at construction. The derive catalog and the automatic derives are in Derives and traits.
Clone¶
- Provides:
.clone(), which returns a deep copy, andT with Clonebounds. - Provided by: every
model,classandenum; anewtypeas listed under Automatic derives;@derive(Clone). - Behavior: every field is cloned.
- Dunder: none.
- Requires: every field type implements
Clone(INCAN-T0113for a field of amodel,classorenum).
Copy¶
- Provides: an assignment or an argument copies the value, and the original stays usable.
- Provided by:
@derive(Copy); anewtypewhose underlying type is a number,bool,None,decimal[p, s],FrozenStrorFrozenBytes, or a tuple,OptionorResultof those. - Behavior: every field is copied.
- Dunder: none.
- Requires: every field type is
Copy: a number,bool, a tuple,OptionorResultofCopytypes, a type that derivesCopy, or a newtype that isCopywithout a derive (see Provided by). A field of any other type is refused (INCAN-T0001).
Default¶
- Provides:
Type.default(), andT with Defaultbounds, under whichT.default()constructs a value. - Provided by:
@derive(Default)on a model, class or newtype. On an enum it is refused (INCAN-T0001). - Behavior: each field takes its declared default, or its type's default when it declares none.
- Dunder: none.
- Requires: each field that declares no default has a type that implements
Default(INCAN-T0001).
| Type | Default |
|---|---|
int and the exact-width integers |
0 |
float, f32, f64 |
0.0 |
bool |
false |
str |
"" |
list[T] |
[] |
dict[K, V] |
{} |
set[T] |
an empty set |
Option[T] |
None |
| A tuple | the tuple of its elements' defaults |
A type that derives Default |
its default() |
Result[T, E] has no default.
@derive(Default)
model Settings:
theme: str = "dark"
font_size: int = 14
retries: int
def make[T with Default]() -> T:
return T.default()
def main() -> None:
a = Settings.default() # accepted: theme is "dark", font_size 14, retries 0
b: Settings = make() # accepted
Field defaults¶
- A field declares a default with
name: Type = value. - A construction may omit a field that declares a default; the field takes that default.
- A construction that omits a field without a default is refused (
INCAN-T0001).
model Settings:
theme: str = "dark"
font_size: int
def main() -> None:
a = Settings(font_size=14) # accepted: theme is "dark"
b = Settings() # refused: font_size declares no default (INCAN-T0001)