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

Support two value factory methods: FromNillable() and PtrFromNillable() #18

Merged
merged 1 commit into from
Nov 12, 2022

Conversation

moznion
Copy link
Owner

@moznion moznion commented Nov 12, 2022

These methods accept the nillable pointer value as an argument and make the Optional[T] type value.

FromNillable()

If the given value is not nil, this returns Some[T] value with doing value-dereference.
On the other hand, if the value is nil, this returns None[T].

example:

num := 123

some := FromNillable[int](&num)
fmt.Printf("%v\n", some.IsSome()) // => true
fmt.Printf("%v\n", some.Unwrap()) // => 123

none := FromNillable[int](nil)
fmt.Printf("%v\n", none.IsSome()) // => false
fmt.Printf("%v\n", none.Unwrap()) // => 0 (the default value of int)

PtrFromNillable()

If the given value is not nil, this returns Some[*T] value without doing value-dereference.
On the other hand, if the value is nil, this returns None[*T].

example:

num := 123

some := PtrFromNillable[int](&num)
fmt.Printf("%v\n", some.IsSome())  // => true
fmt.Printf("%v\n", *some.Unwrap()) // => 123 (NOTE: it needs doing dereference)

none := PtrFromNillable[int](nil)
fmt.Printf("%v\n", none.IsSome()) // => false
fmt.Printf("%v\n", none.Unwrap()) // => nil

…ble()`

These methods accept the nillable pointer value as an argument and make
the `Optional[T]` type value.

`FromNillable()`
----

If the given value is not nil, this returns Some[T] value with doing
value-dereference.
On the other hand, if the value is nil, this returns None[T].

example:

```go
num := 123

some := FromNillable[int](&num)
fmt.Printf("%v\n", some.IsSome()) // => true
fmt.Printf("%v\n", some.Unwrap()) // => 123

none := FromNillable[int](nil)
fmt.Printf("%v\n", none.IsSome()) // => false
fmt.Printf("%v\n", none.Unwrap()) // => 0 (the default value of int)
```

`PtrFromNillable()`
----

If the given value is not nil, this returns Some[*T] value **without**
doing value-dereference.
On the other hand, if the value is nil, this returns None[*T].

example:

```go
num := 123

some := PtrFromNillable[int](&num)
fmt.Printf("%v\n", some.IsSome())  // => true
fmt.Printf("%v\n", *some.Unwrap()) // => 123 (NOTE: it needs doing dereference)

none := PtrFromNillable[int](nil)
fmt.Printf("%v\n", none.IsSome()) // => false
fmt.Printf("%v\n", none.Unwrap()) // => nil
```

Signed-off-by: moznion <moznion@mail.moznion.net>
@moznion moznion added the enhancement New feature or request label Nov 12, 2022
@moznion moznion self-assigned this Nov 12, 2022
@moznion moznion merged commit 610af3c into main Nov 12, 2022
@moznion moznion deleted the from_nillable branch November 12, 2022 00:34
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 this pull request may close these issues.

1 participant