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

[extension-types] Boxing? #1491

Open
eernstg opened this issue Mar 4, 2021 · 0 comments
Open

[extension-types] Boxing? #1491

eernstg opened this issue Mar 4, 2021 · 0 comments
Labels
extension-types-later Issues about extension types for later consideration inline-classes Cf. language/accepted/future-releases/inline-classes/feature-specification.md question Further information is requested

Comments

@eernstg
Copy link
Member

eernstg commented Mar 4, 2021

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).

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.

@eernstg eernstg added question Further information is requested extension-types labels Mar 4, 2021
@eernstg eernstg added the inline-classes Cf. language/accepted/future-releases/inline-classes/feature-specification.md label Feb 24, 2023
@eernstg eernstg added extension-types-later Issues about extension types for later consideration and removed extension-types labels Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-types-later Issues about extension types for later consideration inline-classes Cf. language/accepted/future-releases/inline-classes/feature-specification.md question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant