Skip to content

Add a Map utility to this package #17

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

Open
blackbird-simon opened this issue Apr 24, 2025 · 0 comments
Open

Add a Map utility to this package #17

blackbird-simon opened this issue Apr 24, 2025 · 0 comments

Comments

@blackbird-simon
Copy link

blackbird-simon commented Apr 24, 2025

When we've been using this library there are often times where it can be useful to apply a function to the interior value of a nullable field before further processing.

While it's a simple utility to write, it feels like it would be an ergonomic to have such a utility exposed as part of this package. Something like

func Map[T1, T2 any](maybeValue Nullable[T1], fn func(value T1) T2) Nullable[T2] {
	if !maybeValue.IsSpecified() {
		return nil
	} else if maybeValue.IsNull() {
		return NewNullNullable[T2]()
	} else {
		// SAFETY: Must is safe to use here as all other states of the nullable.Nullable value
		// are checked in the above if blocks.
		return NewNullableWithValue(fn(maybeValue.MustGet()))
	}
}

With a usage like

result := nullable.Map(maybeValue, func(x int) int { return x + 1 })
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

No branches or pull requests

1 participant