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

Re-consider interface-based approach for R[T] #2

Open
kdungs opened this issue Apr 14, 2023 · 2 comments
Open

Re-consider interface-based approach for R[T] #2

kdungs opened this issue Apr 14, 2023 · 2 comments

Comments

@kdungs
Copy link
Owner

kdungs commented Apr 14, 2023

I.e.

type R[T any] interface {
  Unwrap() (T, error)
  Or(T) T
}

with two (unexported) implementations for error / value

type rErr[T any] struct {
  err error
}

type rVal[T any] struct {
  v T
}

The only way for calling code to construct them would be through Of, OfErr, or Wrap. For Wrap we'd need to handle the case where the user provides both an error and a value. Instead of panicking we should probably just return rErr because that's what all the current implementations of the free functions do implicitly, as well. (Since they unwrap and check for err != nil, first).

@kdungs
Copy link
Owner Author

kdungs commented Apr 19, 2023

No shortcuts! If an error happens early on, all computations still happen in the form of forwarding the error. With the current implementation this also means constructing a few default values on the way.

Using the interface-based approach, we'd still not get shortcuts, but at least we wouldn't construct a bunch of useless default values along the way 🤔 .

@kdungs
Copy link
Owner Author

kdungs commented Apr 19, 2023

Using the interface-based approach, we'd still not get shortcuts, but at least we wouldn't construct a bunch of useless default values along the way 🤔 .

This actually wouldn't happen automatically. If we just switched the current implementation of R[T] for one based on an interface, the free functions would still be based on Unwrap and unnecessary default values would be constructed along the way. Instead, we'd probably have to match on type internally...

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