Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
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) } }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)