-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
Improve the Memoizable module #1139
Improve the Memoizable module #1139
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dude! 🤩 this is amazing! I had been thinking about how to best tackle this, and I love the solution you came up with. I just left some small comments, but this is good to go!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AMAZING work. Macros are crazy so I appreciate you showing the generated output for ease of reference.
Just a couple small tweaks I suggested, but overall this is awesome. Can't wait to get it in
src/lucky/memoizable.cr
Outdated
# a return type for your method, or if your return type is a `Union`. | ||
# Arguments are not allowed in memoized methods because these can change the return value. | ||
# a return type for your method, or if any arguments are missing a type. | ||
# Arguments are allowed but as soon as one changes, the previous value is no longer held on to. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not totally sure I understand this part. I think it does hold onto the value, but it creates a new value for the new arguments?
I wonder if this might just be expected and so this sentence could be left off. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't tell if we're on the same page or not. Calling the memoized method with new arguments and then with the old arguments will re-run the memoized method. Does the updated description make this more clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohhhh, so if you do my_method(1)
and then my_method(2)
. my_method(2)
will overwrite the cached value. So if you do my_method(1)
again it will re-run the method.
Is that right? If so I think I got it and the doc changes look good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly 👍
Thank you :D 🚀 ❤️ |
Fixes #1119
Purpose
This adds support for memoization of nil responses as well as using the macro with methods with arguments. I've not written any real macros before and this seems to be quite a monster so I have no idea if this is any good or not or if there are any missing edge cases.
It made sense to me to store the cached value in a tuple with all the arguments that are passed. If the method does not take any arguments but returns a string the tuple will just be
Tuple(String)
but if the method takes in a number it would beTuple(String, Int32)
. Then, those tuple values that aren't the return value are checked against all passed in args. I didn't expect iterating over the args with a for-loop to automatically handle named arguments but my test seems to show that it does.Example
becomes...
Checklist
crystal tool format spec src
./script/setup
./script/test