Skip to content

Commit 843a345

Browse files
committed
improve document
1 parent a90c515 commit 843a345

File tree

4 files changed

+401
-98
lines changed

4 files changed

+401
-98
lines changed

src/.vuepress/hulo.tmLanguage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
"patterns": [
216216
{
217217
"name": "keyword.control.hulo",
218-
"match": "\\b(mod|part|use|class|enum|trait|cmd|fn|let|var|const|import|is|as|extends|from|type|typeof|operator|impl|for|extend|try|catch|finally|defer|throw|throws|if|else|match|do|loop|in|range|return|break|continue|comptime|unsafe|extern|declare|async|await)\\b"
218+
"match": "\\b(mod|part|use|class|enum|trait|cmd|fn|let|var|const|import|is|as|from|type|typeof|operator|impl|for|extends|extension|try|catch|finally|defer|throw|throws|if|else|match|do|loop|in|range|return|break|continue|comptime|unsafe|extern|declare|async|await)\\b"
219219
},
220220
{
221221
"name": "keyword.operator.new",

src/grammar/stmt.md

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -489,30 +489,65 @@ type EditableConfig = Mutable<ImmutableConfig>
489489
### Exclude
490490

491491
## use
492-
```hulo
493-
cmd myecho {
494-
@flag
495-
eval: bool
496492

497-
@flag
498-
format: bool
493+
在 Hulo 语言中,`use` 是用于命令组合的核心语法结构,与 `type` 形成对称设计。
499494

500-
@param
501-
message: str
495+
在开始前假设我们有这样一个命令特征:
496+
```hulo
497+
@command
498+
trait Placeholder {
499+
readonly a: num
500+
readonly b: str
502501
503-
myecho() {}
502+
Placeholder()
504503
505-
myecho(eval) {}
504+
Placeholder(a)
506505
507-
myecho(eval, format) {}
506+
Placeholder(a, b)
508507
}
508+
```
509509

510-
impl myecho for echo {
511-
...
512-
}
510+
以及以下实现该特征的命令:
511+
```hulo
512+
impl Placeholder for Foo, Bar, Baz;
513+
```
514+
515+
### 基础用法
516+
517+
在默认情况下 Hulo 会选择首次匹配的实现类作为默认命令。
518+
```hulo
519+
Placeholder -a 10 -b "abc" // Foo -a 10 -b "abc"
520+
```
513521

514-
// myecho 类型等于echo类型排除某个构造函数
515-
use myecho = Exculde<echo, myecho() | myecho(eval)> & Pick<myecho, myecho() | myecho(eval)>
522+
指定命令
523+
```hulo
524+
use Placeholder = Bar;
525+
Placeholder -a 10 -b "abc" // Bar -a 10 -b "abc"
526+
```
527+
528+
### 命令组合
529+
```hulo
530+
use Placeholder = Exclude<Bar, Placeholder()> & Baz
531+
532+
Placeholder // Baz
533+
Placeholder -a 10 -b "abc" // Bar -a 10 -b "abc"
516534
```
517535

518-
### extends
536+
### 模式匹配
537+
```hulo
538+
use Placeholder = Bar(_) & Baz
539+
```
540+
541+
匹配规则:
542+
没有扩展包裹代表全部匹配
543+
参数匹配需要以 ( ) 包裹匹配规则
544+
545+
(_) 代表匹配待参数的所有可能
546+
(_, _) 代表匹配带两个参数的所有可能
547+
(a, b) 代表匹配入参名字为 a 和 b 的所有可能
548+
(_, b) 代表匹配首参数任意,第二个参数为 b
549+
(_, b | c) 代表匹配首参数任意,第二个参数为 b 或 c 组合
550+
(_, ~b) 代表匹配首参数任意,第二个参数除去 b
551+
(_, ~b & c) 代表首参数任意,第二个参数除去 b 但是要包含 c
552+
553+
## extension

src/grammar/trait.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tag:
88
license: MIT
99
---
1010

11-
>**trait** is a structure that defines class's members and method signatures, used to specify a contract for certain behaviors or functionalities. It is similar to the traditional interface in object-oriented programming, but usually does not contain method implementations. Any type that implements a trait must provide concrete method implementations. trait is mainly used to define a set of methods that need to be shared across multiple classes or structures, enabling code reuse and structured design.
11+
> `trait` is a structure that **defines class's members and method signatures**, used to **specify a contract for certain behaviors or functionalities**. It is similar to the traditional interface in object-oriented programming, but usually does not contain method implementations.
1212
1313
## 特征中的方法定义
1414

0 commit comments

Comments
 (0)