Skip to content

v0.7.0

Compare
Choose a tag to compare
@moznion moznion released this 12 Nov 00:39
· 42 commits to main since this release
610af3c

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