dart-lang / dart-lang/language

[extension-types] Boxing?

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

Description

The proposal in #1452 introduces the notion of boxing for an explicit extension type. A boxed entity obtained from an object whose static type is an extension type is an actual wrapper object where all the extension methods are available as regular instance members. This means that member invocation is subject to object-oriented dispatch (and even dynamic invocations are possible).

```dart

class Zeroing {
Object get zeroed => "Zero!";
}

extension type E on int implements Zeroing {
Object get zeroed => 0;
}

void main() {
E e = 1;
Zeroed z = e.box; // OK.
z.zeroed; // `0`.
(z as dynamic).zeroed; // `0`.
}
```

The purpose of boxing is that it enables a choice: We may wish to use an extension type `E` to work on a large number of objects using a specific interface, without paying for the abstraction in terms of allocating and initializing wrapper objects. However, if a few of the objects must be used in some other context where the static type cannot be `E` then we'd need a wrapper object. This is safe and easy when boxing is a built-in mechanism.

Contributor guide

Open the contributing guide

Research direction

Start by reading proposal #1452 and the boxing example in this issue. Determine whether boxing for explicit extension types should be specified and what semantics the proposal must define; the work is done when the language-design question has a settled specification direction.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.