Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[request] final variables (const) #6590

Closed
elsassph opened this issue Sep 17, 2017 · 3 comments
Closed

[request] final variables (const) #6590

elsassph opened this issue Sep 17, 2017 · 3 comments

Comments

@elsassph
Copy link
Contributor

elsassph commented Sep 17, 2017

We need a way to declare non mutable variables.

I'm not qualified to describe all the semantics, but the goal is that const variables can not be re-assigned, which (I assume) would correspond to Java final or ECMAScript const. A const Array or object would not be immutable.

Haxe being openly inspired by ECMAScript it wouldn't hurt to follow what people are now used in ES6/TypeScript, that is using const.

class Foo {
    const a:Int; // must be initialised in ctor, then is final
    const b = 'a const'; // variable is final and can be inlined
    static const c = 'also const'; // equivalent to 'static inline var'

    function new() {
        a = 42; 
        a = -1; // compiler error, already assigned
        const d = 123;
        d = 0; // compiler error
        const  e = [];
        e = ['no']; // compiler error
        e.push('yes'); // ok, only the Array reference is final
    }
}
@ibilon
Copy link
Member

ibilon commented Sep 17, 2017

Is that the same idea as #6584?

@Simn
Copy link
Member

Simn commented Sep 17, 2017

Yes

@Simn Simn closed this as completed Sep 17, 2017
@mastef
Copy link
Contributor

mastef commented Dec 2, 2017

Watch out, you can't redeclare a const in js, but you can shadow a final in haxe/java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants