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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any plans for annotations on methods? #403

Closed
novabyte opened this issue Jan 18, 2017 · 7 comments
Closed

Any plans for annotations on methods? #403

novabyte opened this issue Jan 18, 2017 · 7 comments
Labels
enhancement New feature or refinement to existing one future Issues that are closed but useful for future consideration language The Wren language syntax and semantics

Comments

@novabyte
Copy link

Some languages have a way to attach and process custom syntactic metadata on methods in source code:

  • In Java it's annotations
  • In Python it's decorators
  • Dart has metadata 馃槈

I wonder if there's plans to add support for this feature to Wren. I'm currently looking around for a potential alternative to Lua as an embedded scripting language for our distributed game server. We had/have planned to use Lua and used it in a previous generation of our server technology but I found Wren and it fits the perfect sweet spot with its rationale as an alternative choice to Lua.

The only part it misses before I can investigate it on an experimental branch for the server is some way to attach metadata to methods. This would be nice for our use case because we have a sequence of hooks points in the socket pipeline where custom scripts can be used to execute on input data like a user's virtual wallet or their quest/challenge reward. Ideally I'd like to keep the registration for the hooks next to the method which they register. For example (Wren pseudo code):

class VWallet {
  @Storage
  @BeforeSave
  static velocityCheck(input) {
    if (input > 100) {
      Fiber.abort("New coin count is too high.")
    }
    return input
  }
}
@minirop
Copy link
Contributor

minirop commented Jan 18, 2017

wren is a simple single-pass compiler, it could be tricky to support them. But if proved possible, it would probably not as powerful as those languages鹿.

What do you need to do with them ?
1/ Make wren call them with the method as a parameter (i.e. Storage(BeforeSave(velocityCheck)))
2/ Have access to them at runtime (i.e. velocityCheck.getAnnotations() would return ["Storage", "BeforeSave"])

鹿: I have no idea how they work in general.

@novabyte
Copy link
Author

novabyte commented Jan 18, 2017

@minirop For my use case it'd be proposal 2. Some way for me to get a list of strings which represent the metadata "tagged" to the method or function. I don't need to be able to store information in the annotation. I.e. No @Method("GET") just a way to read function lists owned by modules and classes and retrieve their annotations.

This way I could load a directory of wren sources at server startup, inspect the functions for their annotations and wire that into our server socket hooks for the realtime pipeline.

@munificent
Copy link
Member

munificent commented Jan 20, 2017

This is a really interesting idea.

Wren's metaprogramming story is pretty barebones in general right now. One thing I worry about with metadata annotations (which I've seen be a problem in both Java and Dart) is that they tend to be used for two mostly unrelated things:

  1. Attaching declarative data that is intended to be used during static analysis. Things like @deprecated and other annotations that mostly show up in doc generators and IDEs.

  2. Attaching declarative data that is used at runtime. I think this is what you have in mind.

Annotations can support both of those, but I've seen it be a problem when the annotations from 1 are kept around at runtime where they aren't needed. They waste memory and code size. That may be less of an issue for Wren since Wren isn't an ahead-of-time compiled language anyway, but it left me feeling like annotations were a weird fit sometimes.

Maybe the right answer is two separate solutions. Something like annotations in comments (//@ ...) for the first case and "real" annotations for the second.

Python's decorators are also quite a bit more powerful than annotations in Java, because they can imperatively affect how the decorated function behaves. That could be useful for Wren, or could be a rathole of complexity. :)

@munificent munificent added enhancement New feature or refinement to existing one language The Wren language syntax and semantics labels Jan 20, 2017
@novabyte
Copy link
Author

@munificent Thanks for the reply. I completely agree annotations are overused for two distinct use cases. I like the idea that there's a way to mark an annotation as retained at runtime separately to those which are part of some kind of compiler annotation pipeline.

I'm not very familiar with Python's decorators but for my use case I don't need any way to imperatively affect decorated functions. I simply need a way at VM start to cycle through functions registered in modules and wire them into the socket pipeline.

Let me know what syntax you have in mind and I'd be happy to work on bits of an implementation. 馃槃

@novabyte
Copy link
Author

@munificent Would you be able to give me an idea on what sort of syntax you have in mind for Wren and annotations?

@marcobambini
Copy link

marcobambini commented Feb 6, 2017

I am adding annotation functionality to my Gravity language, I decided for the following syntax and I'd like to make a suggestion for Wren:

  • @@key means key is available during static analysis
  • @key means key is available during runtime

I think it could be also really useful to support the notations:

@munificent
Copy link
Member

It's hard to do good syntax design in a vacuum, so it depends a lot on:

  • What semantics the annotations have.
  • What kinds of use cases users would use them for.
  • What kinds of elements you can attach them too. Methods? Classes? Variable declarations? Local variable declarations?

Having said that @foo (Java) and [Foo] (C#) are both reasonable. The former moreso because it's less likely to collide with other parts of the grammar.

@ruby0x1 ruby0x1 added the future Issues that are closed but useful for future consideration label Sep 18, 2019
@ruby0x1 ruby0x1 closed this as completed Sep 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or refinement to existing one future Issues that are closed but useful for future consideration language The Wren language syntax and semantics
Projects
None yet
Development

No branches or pull requests

5 participants