AS3 – feature request filed – const initialization in constructor
Juten Tach,
i already wrote about this a while ago, but now i have filed a feature request in the bug base of Adobe. I want const fields allow for being initialized in the constructor, too, not only inline. Here’s, what i wrote in the ticket:
hello,
in the AS3 specification it says:
“A variable declared with the const rather than the var keyword, is read-only outside of the variable’s intializer if it is not an instance variable and outside of the instance constructor if it is an instance variable. It is a verifier error to assign to a const variable outside of its writable region.”This implies, that a const can be initialized in the constructor, but in fact, it cannot, when in strict mode.
In the past, there have been at least two bug reports around this, with slightly different outcome. An early report (ASC-351) seemed to state, that it should be possible to initialize a const in the constructor. Unfortunately, from the report it is unclear, if strict mode was involved or not.
Another report later (ASC-3562) stated, that it would only be possible without strict mode.
I do think, that const initialization should be possible in the constructor, like the AS3 specification suggests, even in strict mode. It would make the whole const concept more useful. For example, declaring a const in a superclass and giving it a value inline currently means, that in subclasses you cannot give it a different value, because you cannot re-declare the field and you cannot give it a different value in the constructor.
Also, you might want to do some checks to decide which value to assign to the const, which you could do in the constructor based on parameters or other factors.
From my point of view, adding the ability to initialize a const in the constructor in addition to do it inline as currently possible, will not break any existing code, since it just adds new functionality, but does not change or take away existing functionality (besides not throwing a compile time error anymore).
So, what do you think? Do you agree? If yes, please vote for the feature request in the bug base. Otherwise, i still would be interested to read your opinion, of course

hmm just doesn’t sound like a constant to me. First its null then it gets assigned a value, therefore it changes and it no longer a constant.
You could implement the functionality your after with a setter that throws a runtime error if invoked more than once?
Hi,
well, inline assignments do take place during construction time. So, the compiler would simply have to distinct between making that assignment inline or in the constructor. Means, if i put an assignment in the constructor for a constant, the compiler should not initialize the constant with null, but with the value i assign it to. I agree, that my use case with super- and subclasses doesn’t make sense, as this would indeed mean assigning a value twice. But still, being able to assign a value based on some kind of pre-check in the constructor would be very useful.