Skip to content

Commit

Permalink
add deref.Or
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhalytch committed May 29, 2024
1 parent 5c38877 commit cdd6589
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 1 addition & 9 deletions deref/of.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ package deref

// OrDefault is a 'dereference or default':
// it will dereference the pointer passed, or return default value for type T if p == nil.
func OrDefault[T any](ptr *T) T {
if ptr == nil {
ptr := new(T)

return *ptr
}

return *ptr
}
func OrDefault[T any](ptr *T) T { return Or(ptr, *new(T)) }

Check failure on line 5 in deref/of.go

View workflow job for this annotation

GitHub Actions / Build

newDeref: replace `*new(T)` with `T(nil)` (gocritic)

// Of is an alias to OrDefault.
func Of[T any](p *T) T { return OrDefault(p) }
9 changes: 9 additions & 0 deletions deref/or.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package deref

func Or[T any](ptr *T, or T) T {
if ptr == nil {
return or
}

return *ptr
}

0 comments on commit cdd6589

Please sign in to comment.