Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional represents an optional value.
In other programming languages this is know as: an option type, or a maybe type.
For example:
var op opt.Optional[string] = opt.Something("once twice thrice fource")
// ...
value, found := op.Get()
if found {
fmt.Println("value:", value)
} else{
fmt.Println("nothing")
}
Also, for example:
var op opt.Optional[uint8] = opt.Something[uint8](101)
// ...
value, found := op.Get()
if found {
fmt.Println("value:", value)
} else{
fmt.Println("nothing")
}
func Map ¶ added in v1.2.0
Map applies the function ‘fn’ to the value inside of the optional-type ‘op’, if the optional-type ‘op’ is holding something, and returns it as a new optional-type. If the optional-type ‘op’ is holding nothing, then Map also returns nothing.
For example:
var op opt.Optional[string] = opt.Something("Hello world!")
var result opt.Optional[string] = opt.Map(op, strings.ToUpper)
// result == opt.Something[string]("HELLO WORLD!")
// ...
var op2 opt.Optional[string] = opt.Nothing[string]()
var result2 opt.Optional[string] = opt.Map(op, strings.ToUpper)
// result2 == opt.Nothing[string]()
Or also, for example:
fn := func(s string) int {
return len(s)
}
var op opt.Optional[string] = opt.Something("Hello world!")
var result opt.Optional[int] = opt.Map(op, fn)
// result == opt.Something[int](12)
// ...
var op2 opt.Optional[string] = opt.Nothing[string]()
var result2 opt.Optional[int] = opt.Map(op, fn)
// result2 == opt.Nothing[int]()
func Nothing ¶
Nothing returns an optional-type with nothing in it.
For example, here is an optional-type that can hold a string with nothing in it:
var op opt.Optional[string] = opt.Nothing[string]()
Note that the default value for any optional-type is nothing. So the following code it equivalent to it:
var op opt.Optional[string]
Also, for example, here is an optional-type that can hold a uint8 with nothing in it:
var op opt.Optional[uint8] = opt.Nothing[uint8]()
Again, note that the default value for any optional-type is nothing. So the following code it equivalent to it:
var op opt.Optional[uint8]
func Something ¶
Something returns a optional-type with something in it.
For example, here is an optional-type with the string "once twice thrice fource" in it:
var op opt.Optional[string] = opt.Something("once twice thrice fource")
And, for example, here is an optional-type with the uint8 101 in it:
var op opt.Optional[uint8] = opt.Something(uint8(101))
func Then ¶ added in v1.3.0
Then applies the function ‘fn’ to the value inside of the optional-type ‘op’, if the optional-type ‘op’ is holding something, and returns the resulting optional-type. If the optional-type ‘op’ is holding nothing, then Then also returns nothing.
For example:
fn := func(s string) opt.Optional[byte] {
if len(s) < 2 {
return opt.Nothing[byte]()
}
return opt.Something[byte](s[1])
}
var op opt.Optional[string] = opt.Something("Hello world!"")
var result opt.Optional[byte] = opt.Then(op, fn)
// result == opt.Something[byte]('e')
// ...
var op2 opt.Optional[string] = opt.Something[string]("X")
var result2 opt.Optional[byte] = opt.Then(op, fn)
// result2 == opt.Nothing[byte]()
// ...
var op2 opt.Optional[string] = opt.Nothing[string]()
var result2 opt.Optional[byte] = opt.Then(op, fn)
// result2 == opt.Nothing[byte]()
func (Optional[T]) Filter ¶ added in v1.3.0
Filter returns itself if it is holding something and ‘fn’ applied to its value returns true. Else it return nothing.
For example:
fn := func(value int) bool {
return 0 == (value % 2)
}
// ...
var op1 opt.Optional[int] = opt.Something[int](10)
op1 = op1.Filter(fn)
// ...
var op2 opt.Optional[int] = opt.Something[int](11)
op2 = op2.Filter(fn)
// ...
var op3 opt.Optional[int] = opt.Nothing[int]()
op3 = op3.Filter(fn)
func (Optional[T]) Get ¶
Get returns the value inside of the optional-type if it is holding something.
Example usage:
var op opt.Optional[string]
// ...
value, found := op.Get()
if found {
fmt.Println("VALUE:", value)
} else {
fmt.Println("nothing")
}
func (Optional[T]) GoString ¶
GoString makes it that when the fmt.Fprintf(), fmt.Printf(), and fmt.Sprintf() family of functions renders this type with the %#v verb, that it will be easier to understand.
For example:
var op opt.Optional[string] = opt.Something("once twice thrice fource")
// ...
fmt.Printf("op = %#v", op)
// Output:
// op = opt.Something[string]("once twice thrice fource")
Also, for example:
var op opt.Optional[uint8] = opt.Nothing[uint8]()
// ...
fmt.Printf("op = %#v", op)
// Output:
// op = opt.Nothing[uint8]()
func (Optional[T]) WhenNothing ¶
func (receiver Optional[T]) WhenNothing(fn func())
WhenNothing will call ‘fn’ when ‘receiver’ is holding nothing.
It will not call ‘fn’ when ‘receier’ is hold something.
For example:
var op opt.Optional = opt.Nothing[string]
// ...
op.WhenNothing(func(){
//@TODO
})
func (Optional[T]) WhenSomething ¶
func (receiver Optional[T]) WhenSomething(fn func(T))
WhenSomething will ‘fn’ when ‘receiver’ is holding something. The value that ‘receiver’ is holding will be passed as a parameter to the function ‘fn’.
It will not call ‘fn’ when ‘receiver’ is hold nothing.
For example:
var op opt.Optional = opt.Something("once twice thrice fource")
// ...
op.WhenSomething(func(value string){
//@TODO
})