Skip to content

Support for Creating Classes, Variables, and Functions at Runtime #4341

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

Open
maynul4 opened this issue Apr 25, 2025 · 1 comment
Open

Support for Creating Classes, Variables, and Functions at Runtime #4341

maynul4 opened this issue Apr 25, 2025 · 1 comment
Labels
request Requests to resolve a particular developer problem

Comments

@maynul4
Copy link

maynul4 commented Apr 25, 2025

Currently, Dart is a statically typed language, and while it provides robust typing mechanisms, it does not support the ability to create classes, variables, and functions dynamically at runtime. This feature would enable more flexible and dynamic development, especially for tools that need to create models and data structures on the fly, such as API response handling, code generation tools, and more.

For instance, I am developing a tool where users make an API call, and the response is automatically converted into a Dart model. This process requires creating classes, variables, and functions dynamically based on the API response. However, Dart's current capabilities do not support this at runtime, which makes it difficult to automate this process in an elegant manner. A feature that allows the creation of classes, variables, and functions dynamically at runtime would significantly improve the flexibility of such tools and applications.

Benefits:

Dynamic Model Creation: It would enable automatic generation of models and data structures from dynamic sources (like API responses).

Flexible Tools: Developers would be able to create code-generation tools that can handle dynamic data with more ease.

Enhanced Development Speed: This feature would speed up the development of tools, especially for rapid prototyping, testing, and working with dynamic data.

Improved Developer Experience: Developers can handle unknown or changing data structures more efficiently without needing to manually update models for each new API response.

Potential Implementation: It could be implemented as a language feature similar to how languages like JavaScript support dynamic class creation, or through reflection mechanisms that are currently limited in Dart. A proposal to extend the reflection API or introduce a new runtime API for class creation and dynamic function binding could be explored.

Conclusion: Adding the ability to dynamically create classes, variables, and functions at runtime would significantly enhance Dart's capabilities, making it more flexible for developers working with dynamic data and code generation tools. This feature would be particularly useful in scenarios where API responses or other external data sources are unknown ahead of time.

Thank you for considering this feature request.

@srawlins srawlins transferred this issue from dart-lang/sdk Apr 28, 2025
@lrhn
Copy link
Member

lrhn commented Apr 29, 2025

This is all very abstract. Can you describe a concrete use-case, and how it would look in the source code when you use it?

The drawback of dynamic features is performance.

Dart is primarily ahead-of-time compiled, which allows the compiler to:

  • perform type-checking at compiler time
  • optimize calls to statically known functions.
  • including inlining them.
  • optimize away type checks that are statically known to always be satisfied.
  • omit code that isn't used (tree shaking)
  • lots of other optimizations that I don't even know about.

If you can add classes at runtime, then every member access on such an object is a dynamic access. It has to be, because the type did not exist when the program was type-checked.
The program cannot refer to the type by name, if the type did not exist when the program source was compiled.

Alternatively, the new types are allowed to implement existing interfaces. That breaks static dispatch optimizations, which means that all other code becomes slower. (The defensive programming way to avoid that is to mark every class as final, to prevent creating new subtypes.)
Most likely a new class cannot implement any existing interfaces. (Not even Comparable!)

If the new classes are not just bare data classes, but can have methods with arbitrary parameters and newly created behavior, then it breaks tree shaking. The new code might want to call a method that doesn't exist. So it cannot. Any new methods have to delegate to existing functions.

With those restrictions, it doesn't feel like there is anything left that cannot be handled by an object typed as dynamic and dispatching member access using noSuchMethod.

@lrhn lrhn added the request Requests to resolve a particular developer problem label May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

2 participants