rhaiscript / lsp

Language server for Rhai.
Apache License 2.0
43 stars 4 forks source link

Flow typing #104

Open schungx opened 1 year ago

schungx commented 1 year ago

Right now assignments do not do anything to the type of the variable:

let a = 42;    // a is int

a = true;    // a is still int

On a straight assignment to a lone variable, that variable should take on the type of the expression.

tamasfe commented 1 year ago

This should be done alongside proper flow type-inference.

let a = 1;

if true {
  a = true;
  // a is bool here
}

// a is still int here

In the meantime we can probably just create a union of all possible types, however I'm not sure how useful that is.

schungx commented 1 year ago

In the meantime we can probably just create a union of all possible types, however I'm not sure how useful that is.

I believe a union of possible types would be very very useful, because in a lot of cases the choices are really not too many, and control flow down the road may start restricting them.

For example:

let a = 1;   // a is int

if condition {
  a = true;  // a is bool
}

// a is int | bool

do_something(a);  // only do_something(int) or do_something(bool) match

if type_of(a) == "int" {
    // a is int here
} else {
    // a is bool here, essentially (int | bool) - int
}