Skip to content
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

Object-based scripting #106

Merged
merged 25 commits into from
Jan 10, 2020
Merged

Object-based scripting #106

merged 25 commits into from
Jan 10, 2020

Conversation

dhleong
Copy link
Owner

@dhleong dhleong commented Jan 10, 2020

This PR introduces a large refactor to how scripting functions are declared and discovered, using a reflection + annotation on actual object methods approach rather than manually registering untyped lambdas.

While we still lean on the Any type quite a bit here, having some type-safety of argument types significantly simplifies overload handling—while it is not 100% perfect, in most cases we can simply use normal method overloads to handle different argument types. prompt(), for example, goes from unpacking an array in a when() based on args and nesting an if to disambiguate on type to two typed overloads (one calling through to the other, even) with a single "if" check testing for the presence of the optional group parameter (a normal, idiomatic thing to do!).

We don't, unfortunately, support Kotlin's default parameter values just yet. It doesn't come up terribly often, but it might be nice, for example, with the prompt string to input() or the remap boolean of normal(). Such cases can generally be handled by overloads, however, and since we already include kotlin-reflect (for some reason) it is something we could possibly support in the future.

This change does sacrifice some performance—by invoking the methods with reflection we are always re-packaging the arguments array into another array. This article suggests we could improve performance using LambdaMetafactory but most of the scripting functions are unlikely to be in hot paths. Things that could be are the Buffer methods, but those are unaffected by this change, so trying to speed this up is probably a premature optimization.

Future work might be able to take advantage of the changes here to generate offline scripting documentation.

This makes them more discoverable in the IDE, and also simplifies
discovery in case we want to generate some static docs automatically.
Plus, this greatly simplifies implementation and type safety in the
common case.
Includes a new test to verify percentage
@dhleong dhleong force-pushed the dhleong/object-based-scripting branch from a9a9094 to 60e6796 Compare January 10, 2020 15:33
@dhleong dhleong merged commit 5fe32f8 into master Jan 10, 2020
@dhleong dhleong deleted the dhleong/object-based-scripting branch January 10, 2020 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant