-
Notifications
You must be signed in to change notification settings - Fork 4
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
Smart generators #1
Comments
Is https://github.com/FranzSkuffka/elm-coder-generator relevant here? |
Yeah there have been similar tools over the years. I think this idea has two novel aspects to it:
|
Agreed, just collecting additional points of data. Looking at the template: Would you be able to mentor this? |
Probably not as I have no relevant experience and I am unfortunately over committed as is. |
Okay, we might still have to consult you at some point |
i think https://github.com/kirchner/elm-type-directed-autocomplete could also be relevant here. /cc @kirchner |
So here is an idea for a tooling project, I hope it is appropriate:
Problem
It a common design pattern in Elm that we make a type and then need to add "capabilities" to that type. Usually these are forms of integration with various packages. Some common examples include:
Most programming languages support this by either meta programming (or macros, deriving, template Haskell, etc) or introspection and dynamic dispatch (interfaces/protocols with default implementations).
Elm doesn't support those forms of abstraction instead preferring manual implementation and duplication of these tasks. This has the benefit of allowing more control and customisability of the process, but forces programmers to type out more "boilerplate" code.
A solution
Code generation. However, I think the code generation has to be rather smart for this to work nicely. First of all, it should be type driven. I envisage the user writing the type in a module and then getting a completion suggestion in their editor:
⚡ Generate JSON encoder...
⚡ Generate Fuzzer...
etc.
The generator needs to be able to traverse the type and for each subtype* encountered:
Color
, look for a value with typeJson.Decode.Decoder Color
:Debug.todo
listing the candidates and instructing the user to pick.Debug.todo
with a message instructing the user to implement that.If anything goes wrong, generate a
Debug.todo
with a message explaining the problem.However, since the set of packages that could benefit from this is unbounded, I suggest that each package can contain a script in some well known location that details how this process works for it. Then the extension would search through the packages that are dependencies of the current project and load these as completion items.
Technical challenges
Since this whole process is based around types, there needs to be solid type resolution facilities. For example if the package considers
Color
a primitive type, than it needs to be ensured that we are taking about the sameColor
(i.e. for example that this is the color from avh4/elm-color rather than from some other package).Second, loading scripts from packages into the editor environment has some security, stability and performance implications. Having correct handling around these is a bit tricky.
* By subtype I mean any type encountered in the type declaration:
So for
Int
andQumox
are subtypes.The text was updated successfully, but these errors were encountered: