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

Implement the 'once' block macro #327

Open
masak opened this issue Jul 1, 2018 · 1 comment
Open

Implement the 'once' block macro #327

masak opened this issue Jul 1, 2018 · 1 comment

Comments

@masak
Copy link
Owner

masak commented Jul 1, 2018

From #313 (comment):

Guess we need to write once as a macro. I'm not currently clever enough to realize whether we have the primitives available to express once as a macro in 007, or whether once itself needs to be such a primitive.

I think it can be done in pure 007. Funnily enough, we'll need another hidden COMPILING:: variable to express it:

@parsed(/ "once" >> <G.block> /)
macro term:<once>(block) {
    my initialized = new Symbol("initialized");
    return quasi {
        my COMPILING::{{{initialized}}};
        if !COMPILING::{{{initialized}}} {
            {{{Q::Block @ block}}}
            COMPILING::{{{initialized}}} = True;
        }
    };
}
@masak
Copy link
Owner Author

masak commented Mar 30, 2024

Rust has a "once cell" crate. Food for inspiration.

All of a sudden I'm also reminded of Josh Bloch's insistance that the singleton pattern should be implemented using enums. This has something to do with the built-in guarantees provided by the Java Virtual Machine itself during initialization.

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

No branches or pull requests

1 participant