github.com/haraldrudell/parl@v0.4.176/invoke-if.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package parl
     7  
     8  // InvokeIf is a deferrable function invoking its function argument when:
     9  //   - the pointer tp is non-nil and the function fn is non-nil
    10  //   - what tp points to is not a T zero-value
    11  //
    12  // Usage:
    13  //
    14  //	someFlag := false
    15  //	defer InvokeIf(&someFlag, someFunction)
    16  //	…
    17  //	someFlag = someValue
    18  func InvokeIf[T comparable](tp *T, fn func()) {
    19  	var zeroValueT T
    20  	if tp == nil || fn == nil || *tp == zeroValueT {
    21  		return
    22  	}
    23  	fn()
    24  }