Closed GoogleCodeExporter closed 9 years ago
If you want to make something read-only in code that you're writing, you'd just
add a getter with no setter. So this is really just for externs. Maybe as an
alternative, we could just establish a rule that when the compiler sees @const
in externs, it won't treat it as truly constant (it will assume that the value
could change over time) but it will still prevent the user from explicitly
changing the value.
Original comment by tbreisac...@google.com
on 7 Mar 2014 at 5:21
I think the convention Tyler mentioned sounds good. Not a pressing need for yet
another annotation.
Original comment by dim...@google.com
on 7 Mar 2014 at 6:15
Tyler's suggestion sounds good to me as well. I don't know how the compiler
treats externs exactly but I guess this new convention wouldn't kill any
existing optimizations, eg. the inlining of "real" constant values.
Original comment by RetroModular
on 7 Mar 2014 at 6:43
I don't think this is a good idea at all. @const has a very specific meaning
and is very important for type checking namespaces and similar. My thought
was that properties that were "readonly" would be defined as a getter use
either ES5 get syntax (externs are always parsed as ES5 or greater now) or
using Object.defineProperty.
This doesn't require a new annotation is should be clear as to the meaning.
Original comment by concavel...@gmail.com
on 13 Mar 2014 at 12:24
There currently isn't a way to tell the compiler what is and isn't a getter in
externs, and there needs to be a way to do that. Consider the following:
/** @type {number} */
Foo.prototype.counter;
That property isn't a constant because it doesn't have the @const keyword
attached to it, so is it read-only or read-write? The compiler *will* be able
to work out the access rules for that property *if* the compiler encounters the
property definition in source code but the source code won't always be
available to the compiler, externs for W3C APIs are a very good example and
they are what sparked my request for a @readonly annotation to be added.
Original comment by RetroModular
on 13 Mar 2014 at 1:34
Issue tracking has been migrated to github. Please make any further comments on
this issue through https://github.com/google/closure-compiler/issues
Original comment by blic...@google.com
on 1 May 2014 at 6:31
Original comment by blic...@google.com
on 1 May 2014 at 6:34
Original issue reported on code.google.com by
RetroModular
on 7 Mar 2014 at 10:09