I would like to better document the return type of this naïve memoization method using YARD:
# Ensures that given block is only executed exactly once and on subsequent
# calls returns result from first execution. Useful for memoizing methods.
#
# @param key [Symbol]
# Name or unique identifier of the method that is being memoized
# @yield
# @return [Object] Whatever the block returns
def memoize(key, &_block)
return @memos[key] if @memos.key?(key)
@memos[key] = yield
end
Note: there's an empty hash @memos being initialized on this class' #initialize method.
Now, it would be great to express that this method always returns whatever the given block returns, but am not sure how to best do this. I thought of using @yieldreturn but would then need something like a generic to express something similar to:
# @yieldreturn [<T>]
# @return [<T>] Whatever the block returns
but that's not valid YARD, I believe.
Is there any way of expressing this connection of the return type and the provided block? Any suggestion is appreciated :)