amber-lang / amber-lang/amber

[Feature] Status type

未关闭
#193 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
enhancement syntax
主要语言
Rust
星标
5.2k
派生
145
平均合并
5 天 3 小时
30 天内合并 PR
7

描述

One of the core feature is error handling (`failed` clause). It would be nice to have named status codes. I think of it as a C# enum. Basically they are just integer constants (`Num` without fractional part).

Example:

```
// 7za https://man.archlinux.org/man/7za.1.en#DIAGNOSTICS
status L7zaStatus { // Pascal case as other Amber types
Success = 0,
Warning = 1,
Fatal = 2,
BadCommandLineParameters = 7,
NotEnoughMemory = 8,
UserCancel = 255,
}

main {
let archive = "path/to/archive.7z"
$7za x {archive}$? failed {
if {
status == L7zaStatus.UserCancel {
echo "user canceled "
}
// ... other exit codes...
else {
echo "unknown error {status}"
}
}
}
}
```

They can be defined in library (`pub` + `import`):
```
// lib.ab
pub status Foo {
...
}

// app.ab
import { Foo } from "lib.ab"
```

EDIT:

Function could somehow specify its status type:
```
fun foo_bar(a: Num, b: Num) -> Num fail L7zaStatus {
// ...
fail L7zaStatus.Fatal
}

foo_bar(1, 2) failed {
if status == L7zaStatus.Fatal { // <-- code completion in IDE provides list of L7zaStatus items
// ...
}
if status == 42 {} // <-- it is ok to use Num
}
```

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。