github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/expr/get/api.go (about)

     1  // Package get provides conditional expression builders
     2  package get
     3  
     4  import "github.com/m4gshm/gollections/expr/use"
     5  
     6  // If builds a head of getter function call by condition.
     7  // Looks like val := get.If(condition, getterFuncOnTrue).Else(defaltVal) tha can be rewrited by:
     8  //
     9  //	var val type
    10  //	if condtion {
    11  //		val = getterFuncOnTrue()
    12  //	} else {
    13  //		val = defaltVal
    14  //	}
    15  func If[T any](condition bool, then func() T) use.When[T] {
    16  	return use.IfGet(condition, then)
    17  }
    18  
    19  // IfErr is like If but aimed to use an error return function
    20  func IfErr[T any](condition bool, then func() (T, error)) use.WhenErr[T] {
    21  	return use.IfGetErr(condition, then)
    22  }
    23  
    24  // If_ is alias of IfErr
    25  func If_[T any](condition bool, tru func() (T, error)) use.WhenErr[T] {
    26  	return IfErr(condition, tru)
    27  }