I expect that compile-time constants don't transpi...
# javascript
u
I expect that compile-time constants don't transpile to variables, but just substituted in the corresponding places. But all this constants from example, are transpiled to js:
Copy code
const val TEST1 = "test1"

object Test2 {
    const val TEST2 = "test2"
}

class Test {
    object Test3 {
        const val TEST3 = "test3"
    }
}
r
What if your string is 2000 characters long, would you rather have a constant being defined in JS or the string copy-pasted every time?
u
@ribesg This decision is up to the programmer. He must understand what he is writing. Just use
val
that's how
const enum
in TypeScript works. Without problem.
s
Is this a performance or a code size concern? It doesn't seem to affect program behavior. And VMs are good at optimizing this stuff. Compiler could certainly do a lot of cool optimizations with constants, even for `val`s without `const`s. And it can be smart about not duplicating 2000 char strings. I'd love to explore this area someday.
u
@Svyatoslav Kuzmich [JB] It's about both a performance and a code size concern. It also can be used to complicate reverse engineering.
s
It also can be used to complicate reverse engineering.
And complicate debug 🙂
We are working on a new compiler. It would be a better platform for optimization and obfuscation
u
@Svyatoslav Kuzmich [JB] would be great if new compiler will have option to optimize it in such way. Or are you talking that with IR compiler plugins I could easy create plugin which will replace such constants with their values? Perhaps this would be even better, then I could turn on this option only for production.
s
We are considering adding various optimization as part of a production mode of main compiler. Can't promise this particular optimization in early versions, but it looks like a low hanging fruit.
Or are you talking that with IR compiler plugins I could easy create plugin which will replace such constants with their value
Easy.
👍 1