lo
lo copied to clipboard
add FromPtrOrF
Motivation
There is a pitfall in existing FromPtrOr
that fallback value must be provided even if it is expensive to create.
For example, assume SecureRandStr
that generates secure random string:
func foo(token *string){
v := lo.FromPtrOr(token, SecureRandStr())
..
}
Even if token
is not nil
, SecureRandStr
is always called.
Proposal
Proposed FromPtrOrF
accepts function that returns fallback value, rather than accept a fallback value itself:
func foo(token *string){
v := lo.FromPtrOrF(token, SecureRandStr)
..
}
Now SecureRandStr
is called only if token
is nil, saving cost to generate secure random token.