LytixDev / slash

The Slash Scripting Language
GNU General Public License v3.0
7 stars 0 forks source link

feat/research: optional static typing #32

Open LytixDev opened 1 year ago

LytixDev commented 1 year ago

The idea is to have optional static typing similar to Python though somewhat stricter.

Idea

Syntax:

var a: str = "Yo"

Here the : str signals that the right hand side of the expression should evaluate to a value of type str. If the value turns out to be something other than str then an error will be raised and the program will terminate. This differs from Python. In Python this is only considered a type "hint" and the program will continue execution despite the mismatch between the hint and the actual type. To me, it seems a lot more logical to treat such a case as an error rather than ignoring it.

This is valid Python:

a: float = "Yo"

I claim this should raise an error. In Slash it will.

Similar syntax should be optional for the parameter list in function declarations (#25). Similar syntax should also be optional for the product type (#30).

Type check before or during runtime?

For the time being the type checks can be done in runtime. In the future it would be interesting to move the type checks into a seperate pass before the interpreting stage. I am unsure how feasible this is at it may be cases where evaluating the type of an expression before runtime may be difficult or time consuming. I don't know, it remains to be seen.

Static typing mode

Somewhere in the future it would also be interesting to have a flag in the interpreter that enforces static typing. In this mode, the code snippet:

var a = "hello"

would not be allowed. This may be too strict though. I like Golangs := operator that infers the type. If we also implemented this we would get the benefits of static typing as function declarations and product types would still need a type associated with the expected value, however, the tediousness of typing the types for variables goes away.

LytixDev commented 1 year ago

What do you think @callumgran ?