-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Proposal: Comptime. Optimization hints for Preprocessors #49052
Comments
You should take a critical look at the viability checklist, because this is definitely not in line with TypeScripts goals. I also fail to see why you would need a new keyword for this, when a comment would suffice. The "TypeScript aware preprocessor" could just check for the presence of the comment. |
Hey Martin! Typescript does this so often. specifically like adding Also:
|
Iβm really confused. You say:
But then later you go on to say this:
Which one is it? Because I canβt tell what the difference is between this and #26534. |
I updated the description. π |
But
Shows that the concept works just fine. :-) IMO adding a new keyword to the TypeScript language that third-party tools may use is the wrong approach and will just lead to confusion when users expect it to do something. |
This doesn't really fit into our design parameters. There are a huge number of questions that would need to be answered here to make this work in the context of a type system, like what happens when you indirect one of these expressions, what the lexical environment is when these functions are evaluated, and so on. If I had a program that really needed this, I'd write a pass-through // Write once
type Precompiled<T> = { value_do_not_access: T };
function precompile<T>(value: Precompiled<T>): T { return value.value_do_not_access; };
function makePrecompiled<T>(value: T): Precompiled<T> {
return { value_do_not_access: value };
}
// Use elsewhere
function getVersion(): Precompiled<string> {
return makePrecompiled("3.1.4.1");
}
const s: string = precompile(getVersion()); This makes the semantics of this extremely clear, which I would think is very important to avoid surprises since writing JS that looks like JS but does something non-JSy is bad. |
This issue has been marked as "Out of Scope" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
π Search Terms
compiler preprocessor inline comptime
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Add syntax hints that preprocessor can use.
Typescript should add this so developers would have a common interface to tell there toolchains (in this context: typescript aware preprocessors) which functions or expression can be evaluate at compile time.
Preprocessor could then evaluate the runtime code and make it run at compiletime and serve its response static.
This Suggestion is not the first idea proposed here, but it tries to fix the problem the other ones had.
π« Non Goals
Typescript should not evaluate the code itself. (Which #26534 suffered from)
Typescript should not throw any comptime errors
π Motivating Example
This Idea is nothing new. Zig is one of the biggest languages which have implemented this feature.
And comptime is a great feature for the web, as code size matters (like for loading speeds). Yet we want readable code, which often suffers from more code. more readable code.
https://kristoff.it/blog/what-is-zig-comptime/
https://ziglang.org/documentation/master/#comptime
π» Use Cases
1. comptime for setting a variable
New Valid Syntax:
Compiles directly to:
A Typescript aware preprocessor would turn this into:
2. comptime getting used in a function
New Valid Syntax:
Compiles directly to:
A Typescript aware preprocessor would turn this into:
3. comptime validation depending on function paramets
New Valid Syntax:
Compiles directly to:
A Typescript aware preprocessor would turn this into:
The text was updated successfully, but these errors were encountered: