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

Document @pureOrBreakMyCode #4190

Open
ribrdb opened this issue Sep 3, 2024 · 1 comment
Open

Document @pureOrBreakMyCode #4190

ribrdb opened this issue Sep 3, 2024 · 1 comment

Comments

@ribrdb
Copy link

ribrdb commented Sep 3, 2024

In PR 4180 @shicks said "goog.reflect.cache is also obsoleted by @pureOrBreakMyCode, which can now be used instead of special-purpose caching primitive."
I can't find documentation for this annotation anywhere. How are you supposed to use it to replace calls to goog.reflect.cache? Would it be sufficient to add this into the body of goog.reflect.cache or a similar method, or would the callers need the annotation?

@KimlikDAO-bot
Copy link
Contributor

I second this. A well documented and thought-out @pureOrBreakMyCode would be an awesome addition to GCC.

By looking at the java code, it appears that currently @pureOrBreakMyCode is used for removing function calls whose return value is not used (even though i could not trigger this with js examples).

However it would be great if GCC can also constant propagate through @pureOrBreakMyCode annotated functions. This would give a users a tool maybe as powerful as zigs comptime.

Just a simple example: build time dictionary trimming

/** @const {!Object<string, string>} */
const LibraryDict = { "key": value, ... }
/**
 * @pureOrBreakMyCode
 * @param {!Object<string, string>} dict
 * @param {!Array<string>} keys
 * @return {!Object<string, string>}
 */
const trimDict = (dict, keys) => Object.fromEntries(Object.entries(dict).filter(([dict]) => keys.includes(key));

/** @define {string} */
const UsedKeys = "key1,key2"

/** @const {!Object<string, string>} */
const MyAppDict = trimKeys(LibraryDict, UsedKeys.split(","));

You could get the exact object you want for your app at compile time.

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

2 participants