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

Suggestion: support symmetric handler for FromNillable #25

Closed
gwolves opened this issue May 26, 2023 · 2 comments · Fixed by #28
Closed

Suggestion: support symmetric handler for FromNillable #25

gwolves opened this issue May 26, 2023 · 2 comments · Fixed by #28
Assignees
Labels
enhancement New feature or request

Comments

@gwolves
Copy link

gwolves commented May 26, 2023

I often map some structs with pointers (intended to express optional values) into Option types.

Example:

type A {
    X *int
    Y *string
}

type B {
    X Option[int]
    Y Option[string]
}

I can easily map pointer typed value into Option values:

var a A
...
b := B{
    X: optional.FromNillable(a.X),
    Y: optional.FromNillable(b.Y),
}

However, in the opposite case, I have to add some boilerplate code:

var b B
...
var x *int
B.X.IfSome(func(value int) {
    x = &value
})

var y *int
B.Y.IfSome(func(value string) {
    y = &value
})

a := A{X: x, Y: y}

I believe this process could be simplified with a symmetric handler for FromNillable. (i.e. ToNillable)

func (o Option[T]) ToNillable() *T

var b B
...
a := A{
    X: b.X.ToNillable(),
    Y: b.Y.ToNillable(),
}

Thank you for your hard work on this library!

@moznion
Copy link
Owner

moznion commented Jul 14, 2023

Hi @gwolves, thank you for your suggestion!
I just implemented UnwrapAsPtr() method at #28 so I'll be happy if this would work for you.

@moznion moznion added the enhancement New feature or request label Jul 14, 2023
@moznion moznion self-assigned this Jul 14, 2023
@moznion
Copy link
Owner

moznion commented Jul 22, 2023

I published v0.11.0 which contains this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants