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

Added @:const property, short for public static inline #16

Closed
wants to merge 1 commit into from
Closed

Added @:const property, short for public static inline #16

wants to merge 1 commit into from

Conversation

boblucas
Copy link

Currently I'm working on a project that uses a lot of event identification constants and business logic contants and I figured that this might be a small improvement:

class SomeConstants
{
    public static inline var GRAVITY = 9.81;
    public static inline var EARTH_CIRUM = 40000;
    public static inline var PI = 3.141592;
}
@:tink class SomeConstants
{
    @:const var GRAVITY = 9.81;
    @:const var EARTH_CIRUM = 40000;
    @:const var PI = 3.141592;
}

It's currently implemented much like @:calc, but maybe generating the getter method is not needed.

@back2dos
Copy link
Member

Hmmm. As it is, I am a bit hesitant to add this for a couple of reasons:

  1. As you said yourself, the getter things is a little weird, although depending on your choices for my second point, it might even become necessary.

  2. You should give some thought as to what should happen with complex values, e.g. @:const var NORTH = new Vec2(1, 0).

  3. Making @:const imply static might close a few other doors. I'd very much like to one day see constants on instances. It just so happens that I suggested semantics here: Syntax suggestion: a real 'const' w/out historical bagage of 'static inline' HaxeFoundation/haxe#4441 (comment)

  4. I wonder whether for what you are doing, the something like following might not be better:

    @:constants class SomeConstants {
       var GRAVITY = 9.81;
       var EARTH_CIRCUM = 40000;
       var PI = 3.141592;
    }

    That could certainly be implemented on top of tink_syntaxhub with very little effort.

Anyway, let me know what you think or if you need any help.

@boblucas
Copy link
Author

1,2: Right, the getter has the problem that it facilitates changing constants due to expressions refererencing something writable, which is bad. but on the other hand, no getter means that I can change the members of some complex type of a constant, which is equally bad.

3: I would love a real c++ like const in haxe, it would solve a lot, I've caught so many mistakes with it. nadako already created my first instinct wich is to create a new identical type where every field is read only. It's hard for me to reason about it what will go wrong with it though, I've only been doing haxe for a week or so.
4: Doesn't quite fit my use case but thanks for the suggestion

So it seems like a simple general solution is somewhat out of reach, I'll just keep writing public inline static for now, thanks anyway :).

@boblucas boblucas closed this Jul 25, 2015
@back2dos
Copy link
Member

The possibilities are near infinite, so don't give up so quickly! If the above doesn't work, then maybe this will?

@:constants(
   GRAVITY = 9.81,
   EARTH_CIRCUM = 40000,
   PI = 3.141592
) 
class SomeConstants {
}

Or this:

@:tink class SomeConstants {
  @:constants var _ = {
    GRAVITY: 9.81,
    EARTH_CIRCUM: 40000,
    PI: 3.141592
  };

}

Or whatever. As long as it passes the parser, we can can give it useful semantics. But given you raised the issue, I'll let you be the the judge of usefulness ;)

@boblucas
Copy link
Author

Haha thanks for the motivation, I think the first variant was quite useful to me, but not generally useful enough, the best solution for the original idea is probably to drop the getter field, it will then work fine for any simple values, and then if the result of the expression is a complex type create an abstract over that which access' all the fields in a read-only way.
But that's quite intrusive for something as simple as replacing public static inline, I'm not sure it's worth it. I probably won't attempt it till I've actually written more than 1k lines :P.

I actually quite like the first variant you made up, I'll see if I can get that working, it seems doable.

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

Successfully merging this pull request may close these issues.

2 participants