Skip to content

Add syntactic sugar for package imports #3929

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

Open
MichaelFenwick opened this issue Jun 22, 2024 · 2 comments
Open

Add syntactic sugar for package imports #3929

MichaelFenwick opened this issue Jun 22, 2024 · 2 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@MichaelFenwick
Copy link

In Dart, it's extremely common to see imports that look like this

import 'package:epubx/epubx.dart';
import 'package:path/path.dart' as path;
import 'package:quiver/collection.dart';
import 'package:web/web.dart';
import 'package:xml/xml.dart';

The repetition in there should be plain to see. As it is the case that it's standard for libraries to put the primarily intended import in a file with the same name as the package, I suggest adding syntactic sugar where import 'package:foo' would be treated the same as import 'package:foo/foo.dart'. Under this proposed change, the current syntax would remain valid.

With this feature, the example above would instead look like

import 'package:epubx';
import 'package:path' as path;
import 'package:quiver/collection.dart';
import 'package:web';
import 'package:xml';

Beyond being less typing and less reading, and generally looking cleaner, I believe this sugar would also make it easier to see at a glance which packages are being imported partially. It would also allow these "standard" package imports to better mirror the format of Dart SDK imports (import 'dart:async;').

@MichaelFenwick MichaelFenwick added the feature Proposed language feature that solves one or more problems label Jun 22, 2024
@Wdestroier
Copy link

You may want to upvote issue #649.

@lrhn
Copy link
Member

lrhn commented Jun 23, 2024

This is effectively asking for less than #649, that the URI package:foo is accepted and treated as equivalent to (or normalized to) package:foo/foo.dart.

It's not impossible. It requires adding a normalization step to package URI parsing, which adds the /foo.dart, but it should be well defined how to do that.

As in #649, a package:foo.bar.baz would normalize to package:foo.bar.baz/baz.dart, to support existing, and hypothetical future, uses of dotted package names.

I want to normalize the URI rather than having two different URIs denote the same file, because Dart library identity is defined by URI equality.
Alternatively we change the rules for library identity to allow equivalences on URIs, so the compiler gets to decide when two URIs are equivalent (within reason).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

3 participants