-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support two value factory methods:
FromNillable()
and `PtrFromNilla…
…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>
- Loading branch information
Showing
4 changed files
with
112 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters