lo icon indicating copy to clipboard operation
lo copied to clipboard

add FromPtrOrF

Open lesomnus opened this issue 1 year ago • 0 comments

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.

lesomnus avatar Nov 26 '23 16:11 lesomnus