diff --git a/docs/06-concepts/19-experimental.md b/docs/06-concepts/19-experimental.md index 8fb3355..1175ab3 100644 --- a/docs/06-concepts/19-experimental.md +++ b/docs/06-concepts/19-experimental.md @@ -22,8 +22,16 @@ The current options you can pass are: ## Inheritance +:::warning +Adding a new subtype to a class hierarchy may introduce breaking changes for older clients. Ensure client compatibility when expanding class hierarchies to avoid deserialization issues. +::: + Inheritance allows you to define class hierarchies in your model files by sharing fields between parent and child classes, simplifying class structures and promoting consistency by avoiding duplicate field definitions. +### Extending a Class + +To inherit from a class, use the `extends` keyword in your model files, as shown below: + ```yaml class: ParentClass fields: @@ -46,6 +54,37 @@ class ChildClass extends ParentClass { } ``` +### Sealed Classes + +In addition to the `extends` keyword, you can also use the `sealed` keyword to create sealed class hierarchies, enabling exhaustive type checking. With sealed classes, the compiler knows all subclasses, ensuring that every possible case is handled when working with the model. + +```yaml +class: ParentClass +sealed: true +fields: + name: String +``` + +```yaml +class: ChildClass +extends: ParentClass +fields: + age: int +``` + +This will generate the following classes: + +```dart +sealed class ParentClass { + String name; +} + +class ChildClass extends ParentClass { + String name; + int age; +} +``` + :::info -The `extends` keyword does not work for models with a `table` field. +All files in a sealed hierarchy need to be located in the same directory. :::