Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DerefWithDefault to ref package #17

Merged
merged 1 commit into from
May 14, 2024
Merged

Add DerefWithDefault to ref package #17

merged 1 commit into from
May 14, 2024

Conversation

sevein
Copy link
Member

@sevein sevein commented May 14, 2024

This PR adds func DerefDefault[T any](x *T, d T) T which is just like ref.DerefZero but it returns the default value specified by d, instead of the zero value, when the pointer x is nil, e.g.:

x := (*string)(nil)
fmt.Println(ref.DerefDefault(x, "default")) // "default"

y := "data"
fmt.Println(ref.DerefDefault(&y, "default")) // "data"

@sevein sevein merged commit 79f19ff into main May 14, 2024
4 checks passed
@sevein sevein deleted the dev/deref-with-default branch May 14, 2024 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant