v0.7.0
New Features
Support two value factory methods: FromNillable()
and PtrFromNillable()
#18
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