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

How FutureOr<T> works? #43160

Closed
guangmingzizai opened this issue Aug 24, 2020 · 2 comments
Closed

How FutureOr<T> works? #43160

guangmingzizai opened this issue Aug 24, 2020 · 2 comments

Comments

@guangmingzizai
Copy link

I want to define a type like FutureOr, for example:

@pragma("vm:entry-point")
abstract class PromiseOr<T> {
  // Private generative constructor, so that it is not subclassable, mixable, or
  // instantiable.
  PromiseOr._() {
    throw new UnsupportedError("PromiseOr can't be instantiated");
  }
}

The code is identical to FutureOr, but it doesn't work.

@eernstg
Copy link
Member

eernstg commented Aug 24, 2020

FutureOr is a built-in type which is basically a union type for the special case where the union has the form Future<T> | T for some T. You can't get the same special treatment of any type that a non-system library can define.

You might have some plans to create your own fork of the Dart system and SDK (I can see that you're using @pragma("vm:entry-point")), but you would then need to introduce a treatment of types like PromiseOr which is similar to the many special exceptions with FutureOr in the common front end and the analyzer, and you'd probably need to make substantial changes to additional tools such as dart2js and DDC. So you can't do this in Dart, and even if you plan to create your own "MyDart" variant of the language it would be a highly non-trivial exercise.

We may get union types in general in the future, but for that it's better to keep it in one issue: dart-lang/language#145.

Because this is a repository where issues with the current implementation are handled (not proposals for new language design), and because of the shared goal, I'll close this issue. Please follow up as needed on dart-lang/language#145.

@eernstg eernstg closed this as completed Aug 24, 2020
@AsteriskZuo
Copy link

sdk/runtime/vm/class_id.h
sdk/runtime/vm/object.h
FutureOr implement in C++ native code.
FutureOr define in object.h
I hope this is helpful.
I am also a curious person.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants