Feature #9037
closedMemoization of "anonymous constants"
Description
=begin
I was thinking about the frozen literal argument in https://bugs.ruby-lang.org/issues/8992. I believe that the original intent for that proposal was to avoid reevaluation of specially marked string literals. I think that that idea would be best realized by memoization of a literal rather than freezing. Suppose we introduce a method memoize
like this:
memoize{"foo"}
whose block would be evaluated only the first this expression it is parsed, and always returns the return value of the block, then that would suffice for the purpose. In fact, this proposal in an extension/generalization of the arguments regarding frozen array/hash literals. The proposed syntax allows objects of an arbitrary class to be memoized, and is regardless of whether it is a literal expression. It is also independent of and is free from the difficulties regarding what to do with frozen arrays/hashes.
At the Ruby level, I believe there is a way to implement this by keeping a hash whose key would be something that uniquely identifies the source location of this expression. But I also believe there must be a better way at the C level.
If it is possible to add a keyword to the syntax, then we can have memoize
as a keyword (of the kind similar to defined?
) instead of a method, used like:
memoize("foo")
or
memoize "foo"
which might look more pleasant.
In either case, for Ruby 2.0 compatibility, a method memoize
can be defined to evaluate the block or return the argument.
=end
Updated by sawa (Tsuyoshi Sawada) almost 5 years ago
This proposal is indeed a duplicate of #8804. Please close this.
Updated by Eregon (Benoit Daloze) almost 5 years ago
- Status changed from Open to Closed