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

value: add a Maybe[T] type #7

Merged
merged 1 commit into from
Sep 3, 2024
Merged

value: add a Maybe[T] type #7

merged 1 commit into from
Sep 3, 2024

Conversation

creachadair
Copy link
Owner

@creachadair creachadair commented Jun 5, 2024

A Maybe[T] is a container for an optional single value of arbirary type, which
may be either present or absent.

This implementation does not include more general utility functions using the
type (yet). I will add those as experience demands.

@creachadair creachadair force-pushed the mjf/maybe branch 2 times, most recently from f135651 to b4076c2 Compare June 22, 2024 23:45
Copy link
Collaborator

@danderson danderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like ❤️

@creachadair
Copy link
Owner Author

I like ❤️

Aha! Now we can bikeshed about the API. I did this to troll @noncombatant but I haven't convinced myself the API is right yet. Ideas welcome!

value/maybe.go Outdated Show resolved Hide resolved
@creachadair creachadair force-pushed the mjf/maybe branch 2 times, most recently from 53876cc to 07acd3a Compare June 30, 2024 16:01
@creachadair creachadair force-pushed the mjf/maybe branch 3 times, most recently from d507d7e to b9ef178 Compare July 9, 2024 18:39
@noncombatant
Copy link

I was doubting the need for Absent but then I figured it out. :)

@creachadair
Copy link
Owner Author

I was doubting the need for Absent but then I figured it out. :)

Is there an example that would be helpful for clarifying that to future readers? I could add it to the examples file.

@creachadair creachadair force-pushed the mjf/maybe branch 2 times, most recently from a77ab0e to 6a18b0f Compare July 25, 2024 23:15
Copy link
Collaborator

@danderson danderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some bikeshedding for your consideration. To repeat what I say in some of the comments, all these are very weakly held opinions and suggestions, mostly just pointing out where I felt friction reading the API, and throwing out some ideas to see if anything useful sticks. Feel free to ignore all of it, and paint the bikeshed in your preferred hue.

value/example_test.go Outdated Show resolved Hide resolved
value/maybe.go Outdated Show resolved Hide resolved
value/maybe.go Outdated Show resolved Hide resolved
value/maybe.go Outdated Show resolved Hide resolved
value/maybe.go Outdated Show resolved Hide resolved
value/maybe.go Outdated Show resolved Hide resolved
value/maybe.go Outdated Show resolved Hide resolved
@creachadair
Copy link
Owner Author

Some bikeshedding for your consideration. To repeat what I say in some of the comments, all these are very weakly held opinions and suggestions, mostly just pointing out where I felt friction reading the API, and throwing out some ideas to see if anything useful sticks. Feel free to ignore all of it, and paint the bikeshed in your preferred hue.

Thanks! Some very thoughtful suggestions, most of which I took. 🙂

@danderson
Copy link
Collaborator

A few more thoughts, but 👍 generally. I'm still not sure how this would feel to wield in actual code, but short of committing it to mds/exp/maybe or something, I don't know how to resolve that short of just seeing what happens and having relaxed views on compatibility 🤷

value/example_test.go Show resolved Hide resolved
value/maybe.go Show resolved Hide resolved
value/maybe.go Outdated Show resolved Hide resolved
value/maybe.go Outdated Show resolved Hide resolved
@creachadair
Copy link
Owner Author

creachadair commented Jul 26, 2024

Some bikeshedding for your consideration. To repeat what I say in some of the comments, all these are very weakly held opinions and suggestions, mostly just pointing out where I felt friction reading the API, and throwing out some ideas to see if anything useful sticks. Feel free to ignore all of it, and paint the bikeshed in your preferred hue.

I've resisted being too strict about it, though now that there are more dependents outside my own work I might have to revise that a bit. But I chose a module name that is pretty easy to grep for, and I don't mind sending fixes directly, so for now I'm not going to fret too much, especially for a new package.

That said, I would like to play with it some more. I have a few usage patterns in mind:

  1. Optional fields in structures. Especially for encoding, for which use it would harmonize well if the omitzero proposal winds up getting implemented 🤞🏻, but could still be useful even if not.

  2. Documenting harmless errors, where control flow is annoying but you want better documentation of intent, e.g.,

    id := value.Check(strconv.ParseInt(s, 10, 64)).Or(25).Get()
  3. As a nicer way to compose (T, bool) functions, e.g.,

    func MapGet[T comparable, U any](m map[T]U, key T) value.Maybe[U] {
       if v, ok := m[key]; ok {
          return value.Just(v)
       }
       return value.Absent()
    }

But mostly this was an essay in the API, I don't have an in-anger need for it yet.

A Maybe[T] is a container for an optional single value of arbirary type, which
may be either present or absent.

There are several more interesting things we could do with this type, but for
now this is just the basics.
@creachadair
Copy link
Owner Author

I have removed a couple of the top-level helpers that I wasn't convinced are necessary yet, and I'm going to go ahead and merge the basic type plumbing.

@creachadair creachadair merged commit 23409e4 into main Sep 3, 2024
1 check passed
@creachadair creachadair deleted the mjf/maybe branch September 3, 2024 03:14
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.

4 participants