question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature request] Compile time expressions in template

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

As i can see angular template syntax is not providing a way to declare a constant compile time expressions in template. Any {{ expression }} should be invoked in runtime in order to be tracked for changing. But it might be useful to declare an expression which won’t be changed, but still referenc it from component’s class.

What is the motivation / use case for changing the behavior?

Let me show you example of use case.

export { globalConstMessages } from './utils/messages'

@Component({
    selector: 'app',
    template: '<p>{{ text }}</p>' // or may be some other syntax something like <p>@text</p>
})
export class MyComponent {
    public readonly text: string = globalConstMessages.someText; // <-- 'Hello world' for example
}

There is no way to transform this template at compile-time into

@Component({
    selector: 'app',
    template: '<p>Hello world</p>'
})
export class MyComponent {
    // empty here, no variable at all
}

In this case we will be able to avoid unnessesary change detections in run-time. It will be usefull in cases where we need to reuse some text messages across application, and it will be much easier to maintain this messages in one file and then only reference them as constant compile-time expression in other templates.

Also it will be a nice feature if we will be able to declare an @Input’s variable of other component’s as a constant compile-time expressions, but i think it will be much harder to implement.

For example

@Component({
    selector: 'tooltip',
    template: '<p>{{ text}}</p>'
})
export class TooltipComponent {
    @Input('text')
    public text: string;
}
@Component({
    selector: 'app',
    template: '<tooltip [text]="constantText"></tooltip>'
})
export class MyComponent {
    public readonly constantText: string = 'Hello world!';
}

and it should be transformed at compile-time into

@Component({
    selector: 'app',
    template: '<p>Hello world</p>'
})
export class MyComponent {}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:37 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
bvdmitricommented, Apr 15, 2018

@trotyl Yea, and that’s it my feature request was about. Let angular template compiler be more intelligent, provide constant folding, let it to optimize constant expression and inline them (of course with some help from user, such as decorators etc).

0reactions
angular-automatic-lock-bot[bot]commented, Sep 13, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

C++ Expression Templates for Specifying Compile-time DSP ...
Slides: https://data.audio.dev/talks/2021/cpp- expression - templates -for-specifying- compile - time -dsp-structures/slides.pdfhttps://audio.dev/ ...
Read more >
Programming at Compile Time with constexpr - Modernes C++
The value of a constexpr function is requested during compile time with constexpr: constexpr auto res = func(5);. Here is a small example...
Read more >
Constant expressions - cppreference.com
Defines an expression that can be evaluated at compile time. Such expressions can be used as non-type template arguments, array sizes, ...
Read more >
Feature request: Compile time code generator annation
I think we need a compile time code generator annotation. Something like that: @CompileTimeGenerator fun compileRegex(str:String):Regex{ val a=template.
Read more >
Simplify Code with if constexpr and Concepts in C++17/C++20
Compile -time if in the form of if constexpr is a fantastic feature that went into C++17. With this functionality, we can improve...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found