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

     1  // Package use provides conditional expression builders
     2  package use
     3  
     4  // If builds use.If(condition, tru).Else(fals) expression builder.
     5  // Looks like val := use.If(condition, valOnTrue).Else(defaltVal) tha can be rewrited by:
     6  //
     7  //	var val type
     8  //	if condtion {
     9  //		val = valOnTrue
    10  //	} else {
    11  //		val = defaltVal
    12  //	}
    13  func If[T any](condition bool, tru T) When[T] {
    14  	return newWhen(condition, tru)
    15  }
    16  
    17  // IfGet is like If but aimed to use an getter function
    18  func IfGet[T any](condition bool, then func() T) When[T] {
    19  	return If(condition, evaluate(condition, then))
    20  }
    21  
    22  // IfGetErr is like If but aimed to use an error return function
    23  func IfGetErr[T any](condition bool, tru func() (T, error)) WhenErr[T] {
    24  	then, err := evaluateErr(condition, tru)
    25  	return newWhenErr(condition, then, err)
    26  }
    27  
    28  // If_ is alias of IfErr
    29  func If_[T any](condition bool, tru func() (T, error)) WhenErr[T] {
    30  	return IfGetErr(condition, tru)
    31  }