Created
December 8, 2021 07:20
-
-
Save kLabz/0e682f750fa36df17ad26eacaa3fb5fe to your computer and use it in GitHub Desktop.
Haxe GADTs for API endpoints
This file contains hidden or 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
| class Test { | |
| static function main() { | |
| var req = makeRequest(Foo, r -> trace(r.foo)); | |
| req.send(); | |
| var req2 = makeRequest(Bar(42), r -> trace(r.answer)); | |
| req2.send(); | |
| } | |
| static function makeRequest<T>(endpoint:Endpoint<T>, handler:T->Void) { | |
| return { | |
| // Replace this with a proper server call | |
| send: () -> handler(switch (endpoint) { | |
| case Foo: {foo: "bar"}; | |
| case Bar(v): {answer: v}; | |
| }) | |
| }; | |
| } | |
| } | |
| enum Endpoint<TResponse> { | |
| Foo:Endpoint<FooResponse>; | |
| Bar(v:Int):Endpoint<BarResponse>; | |
| } | |
| typedef FooResponse = {foo:String} | |
| typedef BarResponse = {answer:Int} |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://try.haxe.org/#deDe2396