Skip to content

[WIP] compiler: extern fn #8119

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

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

witemple-msft
Copy link
Member

@witemple-msft witemple-msft commented Aug 6, 2025

This is a proof-of-concept for extern functions, (related: #7296)

  • Re-enables fn declarations, with a required extern keyword, which bind a type FunctionType with kind: "Function".
  • Adds FunctionType to the Type union.
  • Like decorators, fn declarations must have an associated JS implementation (for now -- we may eventually allow implementations to be defined in TypeSpec itself).
  • Changed parsing of fn declarations to allow mixed constraints in return type position.
  • Implemented tspd extern signature generation for functions alongside decorators.
  • Partially implements checking for CallExpression where target: FunctionType

This strategy replaces the extern alias proof of concept, allowing aliases to simply invoke functions:

extern fn transformVisibility(t: Reflection.Type, filter: valueof VisibilityFilter, nameTemplate: valueof string): Reflection.Type;

alias Read<
  T,
  NameTemplate extends valueof string = "Read{name}"
> = transformVisibility(t, #{ all: #[Lifecycle.Read] }, NameTemplate);

@microsoft-github-policy-service microsoft-github-policy-service bot added the compiler:core Issues for @typespec/compiler label Aug 6, 2025
Copy link

pkg-pr-new bot commented Aug 6, 2025

Open in StackBlitz

npm i https://pkg.pr.new/microsoft/typespec/@typespec/compiler@8119

commit: ed664a3

Copy link
Contributor

github-actions bot commented Aug 6, 2025

❌ There is undocummented changes. Run chronus add to add a changeset or click here.

The following packages have changes but are not documented.

  • @typespec/compiler
  • @typespec/events
  • @typespec/html-program-viewer
  • @typespec/http
  • @typespec/json-schema
  • @typespec/openapi
  • @typespec/openapi3
  • @typespec/protobuf
  • @typespec/rest
  • @typespec/spector
  • @typespec/sse
  • @typespec/streams
  • @typespec/tspd
  • @typespec/versioning
  • @typespec/xml
Show changes

@azure-sdk
Copy link
Collaborator

azure-sdk commented Aug 6, 2025

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@witemple-msft
Copy link
Member Author

Some basic examples: https://cadlplayground.z22.web.core.windows.net/prs/8119/?e=%40typespec%2Fopenapi3&options=%7B%22options%22%3A%7B%22%40typespec%2Fopenapi3%22%3A%7B%22openapi-versions%22%3A%5B%223.1.0%22%5D%7D%7D%7D&c=bW9kZWwgRm9vIHsKICBiYXI6IEV4YW1wbGU8c3RyaW5nPjvFGHrKGGludDMyxRdxdXjKFyJhc2RmIscYzBnJSMVJc2U6IE15QWxpYXPKGn0KCmHEEskaVD4gPSBUW107Cg%3D%3D&vs=%7B%7D

You can see that the two properties where the type is Example<string> have the same first field (you need to use OpenAPI3 to render tuples, since the implementation in the Example template returns a tuple) because the instantiation is cached.

@witemple-msft witemple-msft changed the title [WIP] compiler: extern alias [WIP] compiler: extern fn Aug 8, 2025
Copy link
Contributor

@markcowl markcowl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will review in detail in bit. Thinking about rolling this out:

  • We should validate that there are no compile breaks in typespec-azure or typespec-azure-pr
  • We should talk to TCGC folks about whether they want to support this for some scenarios
  • We should think about preventing usage in Azure specs until TCGC is ready to support (linting rules)
  • We should think about some http-specs scenarios for this
  • We should add some website conceptual documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:core Issues for @typespec/compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants