4 个版本
新版本 0.2.2 | 2024 年 8 月 22 日 |
---|---|
0.2.1 | 2024 年 8 月 22 日 |
0.1.1 | 2024 年 8 月 21 日 |
0.1.0 | 2024 年 8 月 20 日 |
#107 in 编程语言
225 每月下载量
215KB
5K SLoC
Stella 是 Lua 的类型检查器,它为您的代码添加了类似 TypeScript 的类型安全性。它有助于在早期捕获错误,确保代码运行顺畅,并且无需对现有 Lua 代码进行任何修改即可与之协同工作。
安装
安装依赖项
在 Linux 或 Mac 上
# Install Rust if you haven't already.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装 Stella
# Install Stella
cargo install stellla_checker
# Check if Stella is installed correctly
stella --version
斐波那契
function fibonacci(sequence_position: number): number
if sequence_position <= 1 then
return sequence_position
end
return fibonacci(sequence_position - 1) + fibonacci(sequence_position - 2)
end
local fibonacci_result = fibonacci(10)
print(fibonacci_result)
stella check fibonacci.lua
或运行它
stella run fibonacci.lua
# output:
# done. 0 errors, 0 warnings
# emitting lua code...
# 55
二分查找
type Array<T> = {T}
function binary_search(sorted_array: Array<number>, target_value: number): option<number>
local low_index = 1
local high_index = #sorted_array
while low_index <= high_index do
local mid_index = math.floor((low_index + high_index) / 2)
if sorted_array[mid_index] == target_value then
return mid_index
elseif sorted_array[mid_index] < target_value then
low_index = mid_index + 1
else
high_index = mid_index - 1
end
end
return nil
end
local target_index = binary_search({1, 3, 5, 7, 9}, 5)
print(target_index)
检查它
stella check binary_search.lua
运行它
stella run binary_search.lua
复杂示例
type Fn<T, R> = function(param: T): R
type Array<T> = {T}
type ProcessListType<T> = function(list: Array<T>, apply_fn: Fn<T, T>): Array<T>
local process_list: ProcessListType<number> = function(list, apply_fn)
local result = {}
for i = 1, #list do
table.insert(result, apply_fn(list[i]))
end
return result
end
local function increment(n: number): number
return n + 1
end
local function double(n: number): number
return n * 2
end
local numbers = {1, 2, 3, 4}
-- Apply the 'increment' function to each number in the list
local incremented_numbers = process_list(numbers, increment) -- ok :)
-- Apply the 'double' function to each number in the list
local doubled_numbers = process_list(numbers, double) -- ok :)
--- error
local numbers_error = {1, 2, 3, 4, "hello"}
-- ERROR >>> expected `table<number>`, found `table<string, number>`
-- in `numbers_error`
local incremented_numbers = process_list(numbers_error, increment)
stella check process_list.lua
# let me know if you have any questions or suggestions :) I hope you have a amazing day!
依赖项
~5.5–7.5MB
~134K SLoC