Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document the sealed feature #194

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
BenAuerDev marked this conversation as resolved.
Show resolved Hide resolved

### 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.
:::
Loading