rune-rs / rune

An embeddable dynamic programming language for Rust.
https://rune-rs.github.io
Apache License 2.0
1.7k stars 85 forks source link

Feature request: optional type annotations (to benefit LSP mostly) #731

Open VorpalBlade opened 1 month ago

VorpalBlade commented 1 month ago

While Rune is dynamically typed it would sometimes be useful to have type annotations or hints, especially for the LSP. This can be especially useful when integrating rune as a plugin system in a larger program, with custom Rust native external types.

A concrete example is that I have custom script entry points (instead of main) that look like:

pub fn generate_config(commands, settings)
{
   // Here the script should do things with methods on commands and settings to generate the system config in /etc
}

Unfortunately even though the custom LSP knows about the types of commands and settings, it doesn't know that those parameters are of those types. My proposal is to add optional and informational type hints (similar to what python has):

pub fn generate_config(commands: ::mynamespace::Commands, settings: ::mynamespace::Settings)
{
   // Here the script should do things with methods on commands and settings to generate the system config in /etc
}

There are of course a bunch of design questions around this, but I believe all of these can be handled as "future extensions" in order to get an MVP done:

VorpalBlade commented 1 month ago

Oops, I see now that this is a dupe of #673

Feel free to close this, though this issue goes into (a bit) more detail and motivation.

udoprog commented 1 month ago

Thanks for the input!

Sum types like int | float is something I want to avoid. Once type annotations is present I want to enforce strict typing. Not necessarily allow all Rune programs to be legally typed (like TypeScript does for JavaScript). The latter leads to a much more complicated type system.

So with typed Rune if you want to pass in an int or float, I think the encouraged pattern should be to use an enum.

Feel free to close this, though this issue goes into (a bit) more detail and motivation.

We'll probably end up merging all of the issues into one for easier discovery. For now, I'll leave this open.