-
Notifications
You must be signed in to change notification settings - Fork 318
[compiler] Functions, reborn #9060
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
base: main
Are you sure you want to change the base?
[compiler] Functions, reborn #9060
Conversation
|
All changed packages have been documented.
Show changes
|
@typespec/compiler
@typespec/events
@typespec/html-program-viewer
@typespec/http
@typespec/http-client
@typespec/json-schema
@typespec/openapi
@typespec/openapi3
@typespec/protobuf
@typespec/rest
@typespec/spector
@typespec/sse
@typespec/streams
@typespec/tspd
@typespec/versioning
@typespec/xml
commit: |
|
You can try these changes here
|
This PR implements
fnin TypeSpec. Functions are like decorators in that they are callable entities bound to implementations through the TypeSpec JavaScript host function interface.Functions are declared using
extern fnand must bind to an implementation declared through a$functionsexport in a JS source file.Functions accept and produce entities. Value arguments to functions are converted to JS values like decorator arguments are, and the inverse is also true for functions: returned JS values are converted to TSP Value entities through an "unmarshaling" process. This allows the implementation to be natural for JS developers while integrating with the TSP value space.
Functions can have a return type constraint. The default return type constraint of a function, if none is specified, is
unknown. Functions can also returnvoid, in which case JS functions that returnundefinedare specially accepted as if they returnedvoidType. This allows JS void functions to bind naturally to TypeSpec functions that returnvoid.Functions calls are evaluated at check-time. When a
CallExpressionis checked, where the callee is an instance ofFunctionType:CallExpressionchecking result is the unmarshaled entity.Functions support mixed constraints (
Type | valueof Type) in both parameter and return position.Like decorators, functions cannot serve as regular types and are only allowed to appear when resolving the target of a
CallExpression.model Foo { p: f }wherefis aFunctionTypeis not allowed, butmodel Foo { p: f() }is.Unlike decorators, function host bindings MUST use
$functions. Bare exported functions are not bound to JS source files.Functions appear in the type graph as
functionDeclarationson a Namespace. The semantic walker has been updated to visit FunctionType declarations.TSPD is updated to generate extern signatures for functions, like decorators.
This PR also adds an
unknownvalue kind. The unknown value denotes the presence of a value that is not known (e.g. because the service author simply did not wish to specify what the value is, or because the service author cannot specify what it is because it depends on unknown runtime factors such as region, server time, etc.). The unknown value is also used as a moral equivalent oferrorTypein value space. It cannot be passed to decorator or function implementations, and it cannot be serialized to JSON. The OpenAPI3 emitter has been updated to ignore the presence ofUnknownValuein default position.