Skip to content

feat/research: optional static typing #32

@LytixDev

Description

@LytixDev

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions