Skip to content
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

Constant / compile-time-only functions #26534

Closed
3 of 4 tasks
thargol1 opened this issue Aug 18, 2018 · 5 comments
Closed
3 of 4 tasks

Constant / compile-time-only functions #26534

thargol1 opened this issue Aug 18, 2018 · 5 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@thargol1
Copy link

thargol1 commented Aug 18, 2018

Search Terms

const compiler keyword functions

Suggestion

Allow for functions to be marked as 'const', a function marked as 'const' can be evaluated at compile time.

Allow for functions to be marked as 'compiler', a function marked with compiler can only be evaluated at compile time and is not emitted in the final code.

Functions marked with 'compiler' van only be called with arguments that are constants, or the result
from other 'compiler' functions.

Use Cases

Calculate and generate 'stuff' at compile time instead of runtime.

Examples

const x = concat("a","b");

const function concat(a: string, b: string) : string
{
	return a + b;
}

Compiles to (ES5):

var x = "ab";
function concat(a, b) {
	return a + b;
}

const x = concat("a","b");

compiler function concat(a: string, b: string) : string
{
	return a + b;
}

Compiles to (ES5):

var x = "ab";

const templateFunction = compileHandleBars("<p>{{a}}<p>");

compiler function compileHandleBars(template: string) : Function
{
	// code for compiling handlebars template ommitted.
}

Compiles to:

var templateFunction = function(object) {
	// generated handlebars code ommited.
}

const myVueTemplate = optimizeHtml(`
<div>
	<p>{{a}}</p>
</div>
`);

compiler function optimizeHtml(html: string) : string
{
	// code for optimizing html ommitted.
}

Compiles to:

var myVueTemplate = "<div><p>{{a}}</p></div>";

compiler const PngDataUriPrefix = "data:image/png;base64,";
const myImage = PngDataUriPrefix + getBinaryFileAsBase64("~/logo.png");

compiler function getBinaryFileAsBase64(path: string) : string
{
	// code for reading file from disk and base64-encoding ommitted
}

Compiles to:

var myImage = "data:image/png;base64,0lGODl ... ommitted... hEAAQAM=";

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)
@saschanaz
Copy link
Contributor

This could be implemented without emitting different JS based on the types of the expressions

This proposal does depend on the type of function (compiler function or normal function), no?

@thargol1
Copy link
Author

I stand corrected. Proposal updated.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints labels Aug 20, 2018
@RyanCavanaugh
Copy link
Member

We don't ever do type directed emit, nor do we want to add additional expression-altering syntax (const enum was a big enough mistake already)

@thargol1
Copy link
Author

The main reason for this proposal was that all other build chain tools like Uglify, WebPack etc. are not TypeScript-aware. They use the output from the TS-compiler and generate less efficient and optimized code than would be possible if they were TypeScript aware.

Looks like I have to build or find a pre-compiler...

Off topic: I use const enum a lot, the generated code is a lot better than the code with a regular enum. If you could somehow make a regular enum behave like a const enum, if typescript detects it's used like a const enum, everyone would be happy.

@Morglod
Copy link

Morglod commented Apr 2, 2021

https://github.com/Morglod/tsts

Currently working on comptime transformer

const numbers = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
const result = comptime(() => {
    return numbers.reduce((total, x) => sum(total, x), 0);
});

to

const result = (() => { return (36); })();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants