Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "errors"
- "fmt"
- "golang.org/x/exp/constraints"
- )
- type Maybe[T any] struct {
- Value T
- }
- type None struct {
- }
- func (n None) Unwarp() any {
- return errors.New("empty")
- }
- func (m Maybe[T]) Unwarp() any {
- return m.Value
- }
- type Option interface {
- Unwarp() any
- }
- func division[T constraints.Float](m, n int) Option {
- if n == 0 {
- return None{}
- }
- result := T(m) / T(n)
- return Maybe[T]{result}
- }
- func main() {
- divide := division[float64](10, 20)
- value := divide.Unwarp()
- switch value.(type) {
- case error:
- fmt.Println("Can't divide.")
- break
- default:
- fmt.Println(value)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement