github.com/haraldrudell/parl@v0.4.176/is-nil.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  import "unsafe"
     9  
    10  // IsNil checks whether an interface value is truly nil
    11  //   - In Go, comparison of an interface value that has been assigned
    12  //     a concretely typed nil value yields unexpected results
    13  //   - (any)((*int)(nil)) == nil → false, where true is expected
    14  //   - IsNil((*int)(nil)) → true
    15  //   - as of go1.20.3, an interface value is 2 pointers,
    16  //   - — the first currently assigned type and
    17  //   - —the second currently assigned value
    18  func IsNil(v any) (isNil bool) {
    19  	return (*[2]uintptr)(unsafe.Pointer(&v))[1] == 0
    20  	/*
    21  		if isNil = v == nil; isNil {
    22  			return // untyped nil: true return
    23  		}
    24  		reflectValue := reflect.ValueOf(v)
    25  		isNil = reflectValue.Kind() == reflect.Ptr &&
    26  			reflectValue.IsNil()
    27  		return
    28  	*/
    29  }