|
@@ -339,28 +339,27 @@ for separate compilation.
|
|
|
|
|
|
|
|
#### Exporting entities from an API file
|
|
#### Exporting entities from an API file
|
|
|
|
|
|
|
|
-In order to actually be part of a library's API, entities must both be in the
|
|
|
|
|
-API file and explicitly marked as an API. This is done using the `api` keyword,
|
|
|
|
|
-which is only allowed in the API file. For example:
|
|
|
|
|
|
|
+Entities in the API file are part of the library's public API by default. They
|
|
|
|
|
+may be marked as `private` to indicate they should only be visible to other
|
|
|
|
|
+parts of the library.
|
|
|
|
|
|
|
|
```carbon
|
|
```carbon
|
|
|
package Geometry library "Shapes" api;
|
|
package Geometry library "Shapes" api;
|
|
|
|
|
|
|
|
-// Circle is marked as an API, and will be available to other libraries as
|
|
|
|
|
-// Geometry.Circle.
|
|
|
|
|
-api struct Circle { ... }
|
|
|
|
|
|
|
+// Circle is an API, and will be available to other libraries as
|
|
|
|
|
+ Geometry.Circle.
|
|
|
|
|
+struct Circle { ... }
|
|
|
|
|
|
|
|
-// CircleHelper is not marked as an API, and so will not be available to other
|
|
|
|
|
-// libraries.
|
|
|
|
|
-fn CircleHelper(Circle circle) { ... }
|
|
|
|
|
|
|
+// CircleHelper is private, and so will not be available to other libraries.
|
|
|
|
|
+private fn CircleHelper(Circle circle) { ... }
|
|
|
|
|
|
|
|
// Only entities in namespaces should be marked as an API, not the namespace
|
|
// Only entities in namespaces should be marked as an API, not the namespace
|
|
|
// itself.
|
|
// itself.
|
|
|
namespace Operations;
|
|
namespace Operations;
|
|
|
|
|
|
|
|
-// Operations.GetCircumference is marked as an API, and will be available to
|
|
|
|
|
|
|
+// Operations.GetCircumference is an API, and will be available to
|
|
|
// other libraries as Geometry.Operations.GetCircumference.
|
|
// other libraries as Geometry.Operations.GetCircumference.
|
|
|
-api fn Operations.GetCircumference(Circle circle) { ... }
|
|
|
|
|
|
|
+fn Operations.GetCircumference(Circle circle) { ... }
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
This means that an API file can contain all implementation code for a library.
|
|
This means that an API file can contain all implementation code for a library.
|
|
@@ -374,7 +373,9 @@ However, separate implementation files are still desirable for a few reasons:
|
|
|
- From a code maintenance perspective, having smaller files can make a library
|
|
- From a code maintenance perspective, having smaller files can make a library
|
|
|
more maintainable.
|
|
more maintainable.
|
|
|
|
|
|
|
|
-Use of the `api` keyword is not allowed within files marked as `impl`.
|
|
|
|
|
|
|
+Entities in the `impl` file should never have visibility keywords. If they are
|
|
|
|
|
+forward declared in the `api` file, they use the declaration's visibility; if
|
|
|
|
|
+they are only present in the `impl` file, they are implicitly `private`.
|
|
|
|
|
|
|
|
#### Granularity of libraries
|
|
#### Granularity of libraries
|
|
|
|
|
|
|
@@ -404,7 +405,7 @@ package Checksums library "Sha" api;
|
|
|
|
|
|
|
|
namespaces Sha256;
|
|
namespaces Sha256;
|
|
|
|
|
|
|
|
-api fn Sha256.HexDigest(Bytes data) -> String { ... }
|
|
|
|
|
|
|
+fn Sha256.HexDigest(Bytes data) -> String { ... }
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
Calling code may look like:
|
|
Calling code may look like:
|
|
@@ -434,7 +435,7 @@ import IDENTIFIER (library NAME_PATH)?;
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
An import declares a package entity named after the imported package, and makes
|
|
An import declares a package entity named after the imported package, and makes
|
|
|
-`api`-tagged entities from the imported library through it. The full name path
|
|
|
|
|
|
|
+API entities from the imported library available through it. The full name path
|
|
|
is a concatenation of the names of the package entity, any namespace entities
|
|
is a concatenation of the names of the package entity, any namespace entities
|
|
|
applied, and the final entity addressed. Child namespaces or entities may be
|
|
applied, and the final entity addressed. Child namespaces or entities may be
|
|
|
[aliased](/docs/design/aliases.md) if desired.
|
|
[aliased](/docs/design/aliases.md) if desired.
|
|
@@ -444,7 +445,7 @@ For example, given a library:
|
|
|
```carbon
|
|
```carbon
|
|
|
package Math api;
|
|
package Math api;
|
|
|
namespace Trigonometry;
|
|
namespace Trigonometry;
|
|
|
-api fn Trigonometry.Sin(...);
|
|
|
|
|
|
|
+fn Trigonometry.Sin(...);
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
Calling code would import it and use it like:
|
|
Calling code would import it and use it like:
|
|
@@ -574,12 +575,12 @@ server for open source packages. Conflicts can also be addressed by renaming one
|
|
|
of the packages, either at the source, or as a local modification.
|
|
of the packages, either at the source, or as a local modification.
|
|
|
|
|
|
|
|
We do need to address the case of package names conflicting with other entity
|
|
We do need to address the case of package names conflicting with other entity
|
|
|
-names. It's possible that a pre-existing `api` entity will conflict with a new
|
|
|
|
|
-import, and that the `api` is infeasible to rename due to existing callers.
|
|
|
|
|
-Alternately, the `api` entity may be using an idiomatic name that it would
|
|
|
|
|
-contradict naming conventions to rename. In either case, this conflict may exist
|
|
|
|
|
-in a single file without otherwise affecting users of the API. This will be
|
|
|
|
|
-addressed by [name lookup](/docs/design/name_lookup.md).
|
|
|
|
|
|
|
+names. It's possible that a pre-existing entity will conflict with a new import,
|
|
|
|
|
+and that renaming the entity is infeasible to rename due to existing callers.
|
|
|
|
|
+Alternately, the entity may be using an idiomatic name that it would contradict
|
|
|
|
|
+naming conventions to rename. In either case, this conflict may exist in a
|
|
|
|
|
+single file without otherwise affecting users of the API. This will be addressed
|
|
|
|
|
+by [name lookup](/docs/design/name_lookup.md).
|
|
|
|
|
|
|
|
### Potential refactorings
|
|
### Potential refactorings
|
|
|
|
|
|
|
@@ -816,10 +817,13 @@ should be part of a larger testing plan.
|
|
|
- [Collapse file and library concepts](/proposals/p0107.md#collapse-file-and-library-concepts)
|
|
- [Collapse file and library concepts](/proposals/p0107.md#collapse-file-and-library-concepts)
|
|
|
- [Collapse the library concept into packages](/proposals/p0107.md#collapse-the-library-concept-into-packages)
|
|
- [Collapse the library concept into packages](/proposals/p0107.md#collapse-the-library-concept-into-packages)
|
|
|
- [Collapse the package concept into libraries](/proposals/p0107.md#collapse-the-package-concept-into-libraries)
|
|
- [Collapse the package concept into libraries](/proposals/p0107.md#collapse-the-package-concept-into-libraries)
|
|
|
|
|
+ - [Default `api` to private](/proposals/p0752.md#default-api-to-private)
|
|
|
|
|
+ - [Default `impl` to public](/proposals/p0752.md#default-impl-to-public)
|
|
|
- [Different file type labels](/proposals/p0107.md#different-file-type-labels)
|
|
- [Different file type labels](/proposals/p0107.md#different-file-type-labels)
|
|
|
- [Function-like syntax](/proposals/p0107.md#function-like-syntax)
|
|
- [Function-like syntax](/proposals/p0107.md#function-like-syntax)
|
|
|
- [Inlining from implementation files](/proposals/p0107.md#inlining-from-implementation-files)
|
|
- [Inlining from implementation files](/proposals/p0107.md#inlining-from-implementation-files)
|
|
|
- [Library-private access controls](/proposals/p0107.md#library-private-access-controls)
|
|
- [Library-private access controls](/proposals/p0107.md#library-private-access-controls)
|
|
|
|
|
+ - [Make keywords either optional or required in separate definitions](/proposals/p0752.md#make-keywords-either-optional-or-required-in-separate-definitions)
|
|
|
- [Managing API versus implementation in libraries](/proposals/p0107.md#managing-api-versus-implementation-in-libraries)
|
|
- [Managing API versus implementation in libraries](/proposals/p0107.md#managing-api-versus-implementation-in-libraries)
|
|
|
- [Multiple API files](/proposals/p0107.md#multiple-api-files)
|
|
- [Multiple API files](/proposals/p0107.md#multiple-api-files)
|
|
|
- [Name paths as library names](/proposals/p0107.md#name-paths-as-library-names)
|
|
- [Name paths as library names](/proposals/p0107.md#name-paths-as-library-names)
|