-
Notifications
You must be signed in to change notification settings - Fork 213
Optionally imported libraries. #4317
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
Comments
I really doubt something like this would happen, but it would be probably good to add that any top level declaration called Something else, I'd say that extensions from that library could be used if they have an explicit extension override, WDYT? |
I like this. I always felt like the way conditional imports in Dart works is kinda off. This would be a much more reasonable approach IMO. |
I was wondering if we could have something different so we don't "pollute" the namespace with a virtual getter |
The only thing I could think of that would not use any new keyword would be for us to add an exception to conditions by making the library prefix to be possibly tested like: import if (dart.library.whatnot) "library.uri" as whatnot;
void f() {
if (whatnot) {
whatnot.something;
}
} Similar to TS (I think) with nullable things but I'm not sure it would be the best approach here. |
Yes, this would be a special case, because |
Can this apply to optional dependencies? |
Optionally import or Conditionally import, that's great. This maybe not as good as optionally dependencies, but it's definitely solve my flavor problem. |
See dart-lang/sdk#60466 for context.
Sometimes you want platform specific code in the same library as general code.
You can move all platform specific code into a conditionally imported library, with a mock library with the same interface as a default, so that the code will compile on other platforms, even if the mock library code will never be run (because it's guarded by a check to ensure it'll work).
Rather than this cumbersome workaround, what if one could conditionally import a library, without a default to fall back on, so that whether that library is imported or not can be checked in the code, and so that if not, code guarded by that check won't be considered an error if it refers to names that don't exist.
Proposal:
import if (dart.library.whatnot) "library.uri" as whatnot;
- an import with no default URI, which must have a prefix name, and which must not share that prefix with any other imports (just like an deferred import).if
s for other cases, but if none of them are true, the import will be empty.isAvailable
getter (like a deferred import'sloadLibrary
) which is a constant that istrue
if something was imported andfalse
if not. Any exported declaration of the library with that name is hidden.prefix.isAvailable
enables the prefix on thetrue
branch. This is tracked the same ways as type promotion or definite-assignment, so only locally inside a function body.prefix.name
is a compile-time error. Also, extensions from the library are not available.Never
, except that it can also be used as a type or class reference, and a constant, so you can writeprefix.Foo<int>.new(42)
and not get an error. Basically, you can't get an error from a member access on the prefix, or downstream from that. It's dead code anyway. Also any instance member access is allowed. If it would be invalid, it's assumed to be an undefined extension.The "this code can't fail" thing is the biggest challenge. It basically introduces a result of an operation which doesn't have to be a value, but can be a type, so
prefix.foo<prefix.Bar>.new()
is not an error becauseprefix.foo
is "this unspecified thing".The text was updated successfully, but these errors were encountered: