Last active
November 3, 2023 07:27
-
-
Save Stasenko-Konstantin/b45b05a033fd2385a0bbc25686cc7b78 to your computer and use it in GitHub Desktop.
call/cc in golang
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
package main | |
func callCC[A, B, R any](f func(func(A) B) B, res *R) { | |
defer func() { | |
if err := recover(); err != nil { | |
*res = err.(R) | |
} | |
}() | |
f(func(a A) B { | |
panic(a) | |
}) | |
} | |
func main() { | |
var r = new(int) | |
f := func(escape func(int) int) int { | |
return 2 + escape(3) | |
} | |
println(f(func(x int) int { | |
return x | |
})) | |
callCC[int, int, int](f, r) | |
println(*r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment