dart-lang / dart-lang/language

Questions about "extension types"

Open
#3,532 3 comments 2 reactions 0 assignees View on GitHub
extension-types-later feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Hi folks! Really excited, but I admit I have a hard time reading the [specification](https://github.com/dart-lang/language/blob/main/accepted/future-releases/extension-types/feature-specification.md) and couldn't find good examples to refer to.

Feel free to close if these are all handled, or shouldn't be handled (though I suspect they should). Most of these questions come from wanting to avoid so-called "primitive obsession" in our code, i.e. over-using primitives such as `int` for dubious reasons (convenience, terseness, performance):

1. Can the "constructor" have logic similar to below?

I understand it will be possible to do so-called "unsafe" casts (possibly catchable with a lint), but for the "normal" use case, can I require validation before (explicit) conversion? For example, can I limit the `Level` type to integer values of 1 through 20?

```dart
/// A level of a character in D&D 5e. 1-20 inclusive.
final class Level {
final int value;
Level(this.value) {
RangeError.checkValueInInternval(value, 1, 20);
}
}

extension type Level(int i) {
/* what do I write here? */
}
```

1. Is `==` and `hashCode` forwarded for me? If so, how about these cases?

_In short, if `IdNumber(1) == 1`, I'm going to be sad._

```dart
extension type Strength(i) {}
extension type Dexterity(i) {}

void main() {
final strength = Strength(3);
final dexterity = Dexterity(3);
final constitution = 3;

print(strength == strength); // true (I hope)
print(strength == dexterity); // false (I hope), if it's true I ... understand, but hope a lint will catch this.
print(strength == constitution); // false (I hope), if it's true I ... understand, but hope a lint will catch this.
}
```

1. Is my/can my extension type be, say, `Comparable`?

```dart
extension type Level(int i) implements Comparable {
/* what do I write here? */
}
```

Is it comparable by default because `int` is?

What if I'm wrapping a type that isn't comparable, such as `Uri`?

---

I'm worried that without support for these 3 scenarios, it's still going to be attractive to either continue using primitives, or extension types _will_ be used, but they'll lack a lot of the flair of the pattern in say, Rust's [newtype](https://doc.rust-lang.org/rust-by-example/generics/new_types.html) idiom.

Thanks for listening!

Contributor guide

Open the contributing guide

Research direction

Start with accepted/future-releases/extension-types/feature-specification.md and compare its documented behavior with the three questions about validation, equality, and Comparable. Determine whether each point is already specified or requires a language change; done means the questions have clear answers or a decided specification update.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.