Last active
November 19, 2022 15:11
-
-
Save nikolaydubina/89b6ac90bced3b4c229ae2782ddf9d79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
since not struct, big issue with mixing with untyped constants