holgerl / hilvl

The hilvl programming language
MIT License
7 stars 0 forks source link

Type system #14

Open holgerl opened 6 years ago

holgerl commented 6 years ago

A type system would enable better error messages and a range of other benefits.

It could be done within the current syntax:

@ var foo type number = 42
@ var bar type number = (2.5 + 40.3)

@ var value type string = "lorem ipsum" // perhaps call the type "text"?

@ var isDone type boolean = true // perhaps call the type "truth"?

// type of list is defined as the type of the last evaluated value, so that it might be a list of code to be evaluated
@ var myList type number = 
    1
    2
    3

@ var baz type MyService = (@.MyService) // Not currently possible in hilvl to store services in variables?

// Comparing types:
@ typeof baz == MyService

// Types of arguments and return values:

@ var MyService := // The type when not specifying any type is the value itself, so the type is MyService
    @ var myAction type string : // function from number to string
        @ var myVariable type number = (42 + (@ . argument type number)) // Argument usage is statically checked for type
        @.myVariable as string // The return value is statically checked for type

@ var myValue type string = (MyService myAction 1)

// Types when doing higher order programming:

@ var MyService := 
    @ var myAction type number :
        @ set foo = 42 // Variable in inner scope
        @ var myFunction : (@.argument type number) // argument is a list of code with last value number
        @ myFunction // Invoking the argument as an action

@ var bar = 
    MyService myAction 
        @ set foo = 50
        @.foo + 2

// Alternative syntax?
@ type number name foo = 42
holgerl commented 2 years ago

Alternatively, values can be used directly to express types:

(Here I am using name instead of var)

@ name myFunction type (0 , 0) :
    @ name foo type number = 3
    @ . myValue + (@ . argument)

@ name MyService := 
    @ name myAction type (0 , "") :
        @ var foo = 40
        @ . foo + (@ . argument) as string

@ name myStr type "" = "Hello"
@ name isItSo type true = false
@ name myStuff type (@ . MyService) = (@ . MyService)

Since type true is a little awkward. There could be some built in variables like this:

@ name Boolean = true
@ name Text = ""
@ name Number = 0

Then they are used like this:

@ name myStr type Text = "Hello"
@ name isItSo type Boolean = false
@ name foo type Number = 123

The number service already has an action as that takes string as as argument. This is really a kind of type, so it should be changed t use values as types:

42 as Number