68 lines
1.9 KiB
Markdown
68 lines
1.9 KiB
Markdown
# STWL: Strongly Typed Web Language
|
|
|
|
Proposal: A strongly typed, programming language that compiles to JavaScript, with the following features:
|
|
* Error unions: `Error!string`
|
|
* Optional values: `?string`
|
|
* scoped blocks
|
|
* exhaustive pattern matching
|
|
* "unreachable" keyword
|
|
* built-in reactivity system (using signals?)
|
|
|
|
Syntax example (very early proposal, might change):
|
|
|
|
```ts
|
|
//imports work just as in ES6
|
|
import { func1, func2, func3 as funFunc } from './MyCoolModule'
|
|
import Cool from './MyCoolModule'
|
|
|
|
// although I might add something like this in the future:
|
|
import './MyCoolModule' as Cool // because it just reads so much nicer
|
|
|
|
// Declarations can be constant, variable or live (a.k.a. reactive):
|
|
|
|
// Constants can never change
|
|
const c = 299792468 // neither type nor value will ever change
|
|
c++ // Error! Constant values cannot change.
|
|
|
|
const car = struct{
|
|
tires: 4,
|
|
engine: true
|
|
}
|
|
|
|
car.tires = 5 // Error! Unlike in JS, struct values cannot be changed, either
|
|
|
|
// Variables can be changed, as long as their type stays the same:
|
|
var vehicle = struct{
|
|
tires: 2
|
|
engine: true
|
|
}
|
|
vehicle.engine = false // no problem
|
|
vehicle = struct{ // also fine
|
|
tires: 2
|
|
engine: false
|
|
}
|
|
vehicle = struct{ // Error! Type cannot be changed
|
|
color: 'red'
|
|
}
|
|
|
|
// Live values can be changed and their changes are tracked
|
|
live speed = 50
|
|
track speed {
|
|
console.log(`Driving ${speed} km/h now`)
|
|
}
|
|
speed++ // logs speed automatically
|
|
|
|
speed = 'fast' // gives an error, because the type cannot change
|
|
|
|
// Computed values are basically getters (similar to Vuejs' computed)
|
|
computed apparentSpeed {
|
|
// pattern matching is another useful language feature that is explained in
|
|
// further detail later
|
|
return match speed {
|
|
10 -> 'walking speed'
|
|
_ < 50 -> 'inside town, maybe'
|
|
_ -> 'outside town, hopefully'
|
|
}
|
|
}
|
|
|
|
```
|