-
Notifications
You must be signed in to change notification settings - Fork 213
Easy typesafe JSON parsing with invoke
function and variadic generics
#4267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
invoke
function and variadic genericsinvoke
function and variadic generics
Have you seen Function.apply, which is partially what you are asking for here it seems? The argument names are symbols though instead of strings which is awkward to deal with, but it makes sense when you consider that the argument names can get mangled, I think the symbols can guarantee the same mangling (although don't quote me on that). In general though I think we would prefer people do not use Function.apply and its performance is quite poor. |
I talked a bit offline about this and a good point was made that this likely isn't possible because the tree-shaker can rip out methods that aren't used. You could fix that by having a annotation to avoid a class and all of it's methods from being tree shaken. That's not ideal though. We do have |
This comment has been minimized.
This comment has been minimized.
We could solve that with a magic type called DoNotTreeShake that blocks it from getting removed and using generic type constraints. dynamic parse<NestedTypes... extends DoNotTreeShake>(String json) { ... } |
It's not reflection reflection, but it's darn close. You are dynamically looking up a static function using a value for the name (not a name that occurred in the source). Then you invoke it with an argument list that didn't exist in the source either. If it's generic, I don't know how you would provide the type arguments. The name resolution at runtime, using a value as name, is refection. As mentioned, this has many of the same issues that makes AoT-compiling |
I have been working on an alternative take on this problem (including a prototype). I have now ready to share: see #4271 |
Description
Users want type-safe json deserialization. Today they have to use
build_runner
which complicates and slows down the build process or usemirrors
which isn't supported everywhere. IfObject
had a method that allowed people to generateInvocation
objects and send them to be dispatched one could write json deserializers that don't require code generation or runtime reflection.We already have
noSuchMethod
so we are halfway to having full dynamic invocation.Simple example
Nested types
The invocation above works fine for flat types. It gets more complicated for nested types though since
parse
only has access to 1 type parameter. In order to support that we need better support for meta-programming inside of generics. It could look something like this:This could be simplified for users if you allowed reflection into variadic generic parameters at compile time:
Summary
Features requested:
invoke
function for dynamically calling methods on objects or classesgetType
function that transforms variadic generic arguments to one typegetType
themselvesThe text was updated successfully, but these errors were encountered: