string constants in compiler (and when a 'constant' is considered to be such)

47 views
Skip to first unread message

Peter StJ

unread,
May 15, 2016, 11:39:04 AM5/15/16
to Closure Compiler Discuss
I am wondering: why goog.getCssName('some-string') is considered a constant (and thus work with goog.string.Const.from()) and 

/** @const {string} */
var a = goog.getCssName('some-string')


function getCssClas() { return a; }


goog
.string.Const.from(getCssClass())


is not working... when running via compiler passes both should evaluate to the same constant....

My use case is a bit more complicated than that: ControlRenderer class has 'getCssClass' that I specifically override to return a static property assigned (and documented as const) via goog.getCssNam('somestring')

In theory the compiler should be able to see that 'this.getCssClass()' returns a constant (return MyRenderer.CSS_CLASS <- this is assigned as constant from getCssName), but it does not and makes me repeat the constant.

Maybe the const check pass is earlier, or simply it does not look for the result of the expression in the call and when it sees a property access gives up, even if it always returns a constant?

John Lenz

unread,
May 16, 2016, 7:43:09 PM5/16/16
to closure-compiler

--

---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/closure-compiler-discuss/11d2373e-e022-4dd0-b5eb-b7d8ed105520%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter StJ

unread,
May 17, 2016, 6:34:02 AM5/17/16
to Closure Compiler Discuss
I am not sure: I know that getCssName is replaced everywhere with a string const before everything else, but I am not sure why a call site that will eventually be replaced by inlined const string is not allowed in goog.string.Const - after some more processing / passes it will end up there anyways. 

Never mind, I found a workaround. Thanks


On Tuesday, 17 May 2016 02:43:09 UTC+3, John wrote:
On Sun, May 15, 2016 at 8:39 AM, Peter StJ <regard...@gmail.com> wrote:
I am wondering: why goog.getCssName('some-string') is considered a constant (and thus work with goog.string.Const.from()) and 

/** @const {string} */
var a = goog.getCssName('some-string')


function getCssClas() { return a; }


goog
.string.Const.from(getCssClass())


is not working... when running via compiler passes both should evaluate to the same constant....

My use case is a bit more complicated than that: ControlRenderer class has 'getCssClass' that I specifically override to return a static property assigned (and documented as const) via goog.getCssNam('somestring')

In theory the compiler should be able to see that 'this.getCssClass()' returns a constant (return MyRenderer.CSS_CLASS <- this is assigned as constant from getCssName), but it does not and makes me repeat the constant.

Maybe the const check pass is earlier, or simply it does not look for the result of the expression in the call and when it sees a property access gives up, even if it always returns a constant?

--

---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages