Skip to content

Commit

Permalink
fix: Document the sealed feature (#194)
Browse files Browse the repository at this point in the history
* document the sealed feature

* remove unnecessary white space

* refactor: improve sealed documentation

- warning about new subtype introducing breaking changes was added
- sealed description explains about exhaustive type checking
- text in info box was updated
  • Loading branch information
BenAuerDev authored Oct 28, 2024
1 parent e6f4f8c commit bd2e4ba
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion docs/06-concepts/19-experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
:::

0 comments on commit bd2e4ba

Please sign in to comment.