Skip to content

Instantly share code, notes, and snippets.

@nikolaydubina
Last active November 19, 2022 15:11
Show Gist options
  • Save nikolaydubina/89b6ac90bced3b4c229ae2782ddf9d79 to your computer and use it in GitHub Desktop.
Save nikolaydubina/89b6ac90bced3b4c229ae2782ddf9d79 to your computer and use it in GitHub Desktop.
// https://go.dev/play/p/mcWx20GtkaO
// 🌊
package main
import "fmt"
type SGD int64
func (as SGD) Add(bs SGD) SGD { return as + bs }
type KRW int64
func (ak KRW) Add(bk KRW) KRW { return ak + bk }
type Money interface {
SGD | KRW
}
func logic[T Money](a T, b T) T {
return a + b
}
func nestedLogic[T Money](a T, b T) T {
return logic[T](a, b)
}
func main() {
//compile error
//x := (SGD)(10) + (KRW)(1000)
//fmt.Println(x)
y := (SGD)(10) + (SGD)(11)
fmt.Println(y)
z := logic[SGD](10, 11)
fmt.Println(z)
// not compiles
//fmt.Println(logic[SGD](10, (KRW)(1)))
//fmt.Println(nestedLogic[SGD](10, (KRW)(1)))
}
@nikolaydubina
Copy link
Author

since not struct, big issue with mixing with untyped constants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment