mirror of
https://github.com/luau-lang/luau.git
synced 2025-04-10 22:00:54 +01:00
Add syntax highlighting
This commit is contained in:
parent
8206758bdc
commit
11f460d271
1 changed files with 9 additions and 9 deletions
|
@ -20,19 +20,19 @@ is fine, but there is no way for a user to write the type of `id`.
|
||||||
|
|
||||||
Allow functions to take type parameters as well as function parameters, similar to Java/Typescript/...
|
Allow functions to take type parameters as well as function parameters, similar to Java/Typescript/...
|
||||||
|
|
||||||
```
|
```lua
|
||||||
function id<a>(x : a) : a return x end
|
function id<a>(x : a) : a return x end
|
||||||
```
|
```
|
||||||
|
|
||||||
Functions may also take generic type pack arguments for varargs, for instance:
|
Functions may also take generic type pack arguments for varargs, for instance:
|
||||||
|
|
||||||
```
|
```lua
|
||||||
function compose<a...>(... : a...) -> (a...) return ... end
|
function compose<a...>(... : a...) -> (a...) return ... end
|
||||||
```
|
```
|
||||||
|
|
||||||
This change is *not* only syntax, as explicit type parameters need to be part of the semantics of types. For example, we can define a generic identity function
|
This change is *not* only syntax, as explicit type parameters need to be part of the semantics of types. For example, we can define a generic identity function
|
||||||
|
|
||||||
```
|
```lua
|
||||||
local function id(x) return x end
|
local function id(x) return x end
|
||||||
local x: string = id("hi")
|
local x: string = id("hi")
|
||||||
local y: number = id(37)
|
local y: number = id(37)
|
||||||
|
@ -41,7 +41,7 @@ type Id = typeof(id)
|
||||||
|
|
||||||
and two functions
|
and two functions
|
||||||
|
|
||||||
```
|
```lua
|
||||||
function f()
|
function f()
|
||||||
return id
|
return id
|
||||||
end
|
end
|
||||||
|
@ -57,14 +57,14 @@ end
|
||||||
|
|
||||||
The types of these functions are
|
The types of these functions are
|
||||||
|
|
||||||
```
|
```lua
|
||||||
f : () -> <a>(a) -> a
|
f : () -> <a>(a) -> a
|
||||||
g : <a>() -> (a) -> a
|
g : <a>() -> (a) -> a
|
||||||
```
|
```
|
||||||
|
|
||||||
so this is okay:
|
so this is okay:
|
||||||
|
|
||||||
```
|
```lua
|
||||||
local i: Id = f()
|
local i: Id = f()
|
||||||
local x: string = i("hi")
|
local x: string = i("hi")
|
||||||
local y: number = i(37)
|
local y: number = i(37)
|
||||||
|
@ -72,7 +72,7 @@ so this is okay:
|
||||||
|
|
||||||
but this is not:
|
but this is not:
|
||||||
|
|
||||||
```
|
```lua
|
||||||
-- This assignment shouldn't typecheck!
|
-- This assignment shouldn't typecheck!
|
||||||
local i: Id = g()
|
local i: Id = g()
|
||||||
local x: string = i("hi")
|
local x: string = i("hi")
|
||||||
|
@ -92,7 +92,7 @@ Types become more complex, so harder for programmers to reason about, and adding
|
||||||
|
|
||||||
Not having higher-kinded types stops some examples which are parameterized on container types, for example:
|
Not having higher-kinded types stops some examples which are parameterized on container types, for example:
|
||||||
|
|
||||||
```
|
```lua
|
||||||
function g<c>(f : <a>(a) -> c<a>) : <b>(b) -> c<c<b>>
|
function g<c>(f : <a>(a) -> c<a>) : <b>(b) -> c<c<b>>
|
||||||
return function(x) return f(f(x)) end
|
return function(x) return f(f(x)) end
|
||||||
end
|
end
|
||||||
|
@ -100,7 +100,7 @@ Not having higher-kinded types stops some examples which are parameterized on co
|
||||||
|
|
||||||
Not having bounded types stops some examples like giving a type to the function that sums an non-empty array:
|
Not having bounded types stops some examples like giving a type to the function that sums an non-empty array:
|
||||||
|
|
||||||
```
|
```lua
|
||||||
function sum(xs)
|
function sum(xs)
|
||||||
local result = x[0]
|
local result = x[0]
|
||||||
for i=1,#xs
|
for i=1,#xs
|
||||||
|
|
Loading…
Add table
Reference in a new issue