From d0bb616c73ba7f032e18bc3b04803668db3ee4bd Mon Sep 17 00:00:00 2001 From: Cai Bingjun <1945458160@qq.com> Date: Sun, 4 Sep 2022 20:39:02 +0800 Subject: [PATCH] translate save --- doc/zh_CN/syntax/26_pattern_matching.md | 78 +++++++++---------- doc/zh_CN/syntax/27_comprehension.md | 40 +++++----- doc/zh_CN/syntax/28_spread_syntax.md | 10 +-- doc/zh_CN/syntax/29_decorator.md | 56 ++++++------- doc/zh_CN/syntax/30_error_handling.md | 57 +++++++------- doc/zh_CN/syntax/31_pipeline.md | 12 +-- .../syntax/32_integration_with_Python.md | 31 ++++---- doc/zh_CN/syntax/33_package_system.md | 30 +++---- doc/zh_CN/syntax/34_generator.md | 10 +-- doc/zh_CN/syntax/container_ownership.md | 12 +-- 10 files changed, 168 insertions(+), 168 deletions(-) diff --git a/doc/zh_CN/syntax/26_pattern_matching.md b/doc/zh_CN/syntax/26_pattern_matching.md index ac48c1e4..1b5b9bc9 100644 --- a/doc/zh_CN/syntax/26_pattern_matching.md +++ b/doc/zh_CN/syntax/26_pattern_matching.md @@ -1,51 +1,51 @@ -# pattern matching, refutable +# 模式匹配,可反驳 -## Patterns available in Erg +## Erg 中可用的模式 -### variable pattern +### 变量模式 ```python -# basic assignments +# 基本任务 i = 1 -# with type +# 有类型 i: Int = 1 -# with anonymous type +# 匿名类型 i: {1, 2, 3} = 2 -# functions +# 功能 fn x = x + 1 -# equals +# 等于 fn x: Add(Int) = x + 1 -# (anonymous) function +# (匿名)函数 fn = x -> x + 1 fn: Int -> Int = x -> x + 1 -# higher-order type +# 高阶类型 a: [Int; 4] = [0, 1, 2, 3] # or a: Array Int, 4 = [0, 1, 2, 3] ``` -### Literal patterns +### 文字字面量 ```python -# Raise a TypeError if `i` cannot be determined to be 1 at compile time. -# omit `_: {1} = i` +# 如果在编译时无法确定 `i` 为 1,则引发 TypeError。 +# 省略 `_: {1} = i` 1 = i -# simple pattern matching +# 简单的模式匹配 match x: 1 -> "1" 2 -> "2" _ -> "other" -# fibonacci function +# 斐波那契函数 fib0 = 0 fib1 = 1 fibn: Nat = fibn-1 + fibn-2 ``` -### constant pattern +### 常量模式 ```python cond=False @@ -62,18 +62,18 @@ name = match num: _ -> "unnamed" ``` -### Sieve pattern +### 筛子图案 ```python -# these two are the same +# 这两个是一样的 Array(T, N: {N | N >= 3}) Array(T, N | N >= 3) f M, N | M >= 0, N >= 1 = ... -f(1, 0) # TypeError: N (2nd parameter) must be 1 or more +f(1, 0) # 类型错误:N(第二个参数)必须为 1 或更多 ``` -### discard (wildcard) pattern +### 丢弃(通配符)模式 ```python _ = 1 @@ -82,9 +82,9 @@ zero_ = 0 right(_, r) = r ``` -### Variable length patterns +### 可变长度模式 -It is used in combination with the tuple/array/record pattern described later. +它与稍后描述的元组/数组/记录模式结合使用。 ```python [i,...j] = [1, 2, 3, 4] @@ -93,18 +93,18 @@ first|T|(fst: T, ...rest: T) = fst assert first(1, 2, 3) == 1 ``` -### Tuple pattern +### 元组模式 ```python (i, j) = (1, 2) ((k, l), _) = ((1, 2), (3, 4)) -# If not nested, () can be omitted (1, 2 are treated as (1, 2)) +# 如果不嵌套,() 可以省略(1, 2 被视为(1, 2)) m, n = 1, 2 f(x, y) = ... ``` -### array pattern +### 数组模式 ```python [i, j] = [1, 2] @@ -114,11 +114,11 @@ length[] = 0 length[_, ...rest] = 1 + lengthrest ``` -#### record pattern +#### record 模式 ```python record = {i = 1; j = 2; k = 3} -{j; ...} = record # i, k will be freed +{j; ...} = record # i, k 将被释放 {sin; cos; tan; ...} = import "math" {*} = import "math" # import all @@ -131,7 +131,7 @@ age = match person: f {x: Int; y: Int} = ... ``` -### Data class pattern +### 数据类模式 ```python Point = Inherit {x = Int; y = Int} @@ -152,9 +152,9 @@ List T. _ -> ... ``` -### enumeration pattern +### 枚举模式 -*Actually, it's just an enumeration type +* 其实只是枚举类型 ```python match x: @@ -162,9 +162,9 @@ match x: _ -> "other" ``` -### range pattern +### Range 模式 -*Actually, it is just an interval type. +* 实际上,它只是一个区间类型。 ```python # 0 < i < 1 @@ -176,17 +176,17 @@ match i i: 1..5 -> ... ``` -### Things that aren't patterns, things that can't be patterned +### 不是模式的东西,不能被模式化的东西 -A pattern is something that can be uniquely specified. In this respect pattern matching differs from ordinary conditional branching. +模式是可以唯一指定的东西。 在这方面,模式匹配不同于普通的条件分支。 -Condition specifications are not unique. For example, to check if the number `n` is even, the orthodox is `n % 2 == 0`, but you can also write `(n / 2).round() == n / 2`. -A non-unique form is not trivial whether it works correctly or is equivalent to another condition. +条件规格不是唯一的。 例如,要检查数字 `n` 是否为偶数,正统是 `n % 2 == 0`,但也可以写成 `(n / 2).round() == n / 2`。 +非唯一形式无论是正常工作还是等效于另一个条件都不是微不足道的。 -#### set +#### Set -There is no set pattern. Because the set has no way to uniquely retrieve the elements. -You can retrieve them by iterator, but the order is not guaranteed. +没有固定的模式。 因为集合没有办法唯一地检索元素。 +您可以通过迭代器检索它们,但不能保证顺序。

上一页 | 下一页 diff --git a/doc/zh_CN/syntax/27_comprehension.md b/doc/zh_CN/syntax/27_comprehension.md index d734d27b..359be021 100644 --- a/doc/zh_CN/syntax/27_comprehension.md +++ b/doc/zh_CN/syntax/27_comprehension.md @@ -1,34 +1,34 @@ # Comprehension -Array with `[expr | (name <- iterable)+ (predicate)*]`, -set with `{expr | (name <- iterable)+ (predicate)*}`, -You can create a Dict with `{key: value | (name <- iterable)+ (predicate)*}`. +Array 和 `[expr | (name <- iterable)+ (predicate)*]`, +set 和 `{expr | (name <- iterable)+ (predicate)*}`, +你可以创建一个字典 `{key: value | (name <- iterable)+ (predicate)*}`. -The first part of the clauses separated by `|` is called the layout clause (location clause), the second part is called the bind clause (binding clause), and the third part is called the guard clause (conditional clause). -A guard clause can be omitted, but a bind clause cannot be omitted, and a guard clause cannot precede a bind clause. +由`|`分隔的子句的第一部分称为布局子句(位置子句),第二部分称为绑定子句(绑定子句),第三部分称为保护子句(条件子句)。 +保护子句可以省略,但绑定子句不能省略,保护子句不能在绑定子句之前。 -Comprehension example +理解示例 ```python -# the layout clause is i -# bind clause is i <- [0, 1, 2] +# 布局子句是 i +# 绑定子句是 i <- [0, 1, 2] assert [i | i <- [0, 1, 2]] == [0, 1, 2] -# layout clause is i / 2 -# bind clause is i <- 0..2 +# 布局子句是 i / 2 +# 绑定子句是 i <- 0..2 assert [i/2 | i <- 0..2] == [0.0, 0.5, 1.0] -# layout clause is (i, j) -# bind clause i <- 0..2, j <- 0..2 -# guard clause is (i + j) % 2 == 0 +# 布局子句是 (i, j) +# 绑定子句 i <- 0..2, j <- 0..2 +# 保护子句是 (i + j) % 2 == 0 assert [(i, j) | i <- 0..2; j <- 0..2; (i + j) % 2 == 0] == [(0, 0), (0, 2), (1, 1), (2, 0), (2, 2)] assert {i % 2 | i <- 0..9} == {0, 1} assert {k: v | k <- ["a", "b"]; v <- [1, 2]} == {"a": 1, "b": 2} ``` -Erg comprehensions are inspired by Haskell, but with some differences. -For Haskell list comprehensions, the order of variables makes a difference in the result, but in Erg it doesn't matter. +Erg推导式受到 Haskell 的启发,但有一些不同。 +对于 Haskell 列表推导,变量的顺序会对结果产生影响,但在 Erg 中这并不重要。 ``` haskell -- Haskell @@ -41,21 +41,21 @@ For Haskell list comprehensions, the order of variables makes a difference in th assert [(i, j) | i <- 1..<3; j <- 3..<5] == [(i, j) | j <- 3..<5; i <- 1.. <3] ``` -This specification is the same as that of Python. +该规范与 Python 的规范相同。 ```python # Python assert [(i, j) for i in range(1, 3) for j in range(3, 5)] == [(i, j) for j in range(3, 5) for i in range(1, 3)] ``` -## Sieve type +## 筛子类型 -Similar to comprehensions are sieve types. A sieve type is a type (enumerated type) created in the form `{Name: Type | Predicate}`. -In the case of the sieve type, only one Name can be specified and the layout cannot be specified (however, multiple values ​​can be handled if it is a tuple type), and the Predicate can be calculated at compile time, that is, only a constant expression can be specified. +与推导类似的是筛类型。 筛子类型是以`{Name: Type | Predicate}`创建的(枚举类型) +sieve类型的情况下,只能指定一个Name,不能指定布局(但是如果是tuple类型可以处理多个值),Predicate可以在编译时计算,即 ,只能指定一个常量表达式。 ```python Nat = {I: Int | I >= 0} -# If the predicate expression is only and, it can be replaced with ; +# 如果谓词表达式只有and,可以替换为: # Nat2D = {(I, J): (Int, Int) | I >= 0; J >= 0} Nat2D = {(I, J): (Int, Int) | I >= 0 and J >= 0} ``` diff --git a/doc/zh_CN/syntax/28_spread_syntax.md b/doc/zh_CN/syntax/28_spread_syntax.md index a736a5a5..81b98343 100644 --- a/doc/zh_CN/syntax/28_spread_syntax.md +++ b/doc/zh_CN/syntax/28_spread_syntax.md @@ -1,6 +1,6 @@ -# Spread assignment +# 传播赋值 -In a decomposing assignment, putting `...` in front of a variable expands all remaining elements into that variable. This is called expansion assignment. +在分解赋值中,将 `...` 放在变量前面会将所有剩余元素展开到该变量中。 这称为扩展赋值。 ```python [x,...y] = [1, 2, 3] @@ -11,10 +11,10 @@ assert x == 1 assert y == (2, 3) ``` -## Extract assignment +## 提取赋值 -If nothing is written after `...`, the remaining elements are ignored and assigned. This type of expansion assignment is specifically called extractive assignment. -Extraction assignment is a convenient syntax for localizing specific attributes within a module or record. +如果在 `...` 之后没有写入任何内容,则忽略并分配剩余的元素。 这种类型的扩展赋值具体称为抽取赋值。 +提取分配是一种方便的语法,用于本地化模块或记录中的特定属性。 ```python {sin; cos; tan; ..} = import "math" diff --git a/doc/zh_CN/syntax/29_decorator.md b/doc/zh_CN/syntax/29_decorator.md index 5417f3f3..66c9a07e 100644 --- a/doc/zh_CN/syntax/29_decorator.md +++ b/doc/zh_CN/syntax/29_decorator.md @@ -1,24 +1,24 @@ -# decorator (modifier) +# 装饰器(修饰符) -Decorators are used to add or demonstrate a particular state or behavior to a type or function. -The syntax of the decorator is as follows. +装饰器用于向类型或函数添加或演示特定状态或行为。 +装饰器的语法如下。 ```python @deco X=... ``` -You can have multiple decorators as long as they don't conflict. +你可以有多个装饰器,只要它们不冲突。 -A decorator is not a special object, it's just a one-argument function. The decorator is equivalent to the following pseudocode. +装饰器不是一个特殊的对象,它只是一个单参数函数。 装饰器等价于下面的伪代码。 ```python X=... X = deco(X) ``` -Erg doesn't allow reassignment of variables, so code like the one above won't work. -For simple variables it's the same as `X = deco(...)`, but for instant blocks and subroutines you can't do that, so you need a decorator. +Erg 不允许重新分配变量,因此上面的代码不起作用。 +对于简单的变量,它与`X = deco(...)` 相同,但对于即时块和子例程,你不能这样做,所以你需要一个装饰器。 ```python @deco @@ -26,29 +26,29 @@ f x = y = ... x + y -# You can also prevent the code from becoming horizontal +# 还可以防止代码变成水平的 @LongNameDeco1 @LongNameDeco2 C = Class... ``` -Below are some frequently used built-in decorators. +下面是一些常用的内置装饰器。 -## Inheritable +## 可继承 -Indicates that the defining type is an inheritable class. If you specify `"public"` for the argument `scope`, it will be possible to inherit even the class of the external module. By default it is `"private"` and cannot be inherited externally. +指示定义类型是可继承的类。 如果为参数 `scope` 指定 `"public"`,甚至可以继承外部模块的类。 默认情况下它是`"private"`,不能被外部继承。 -##Final +## 最后 -Make the method non-overridable. Adding it to a class makes it a non-inheritable class, but since it's the default it doesn't make sense. +使该方法不可覆盖。 将它添加到类中使其成为不可继承的类,但由于它是默认值,因此没有意义。 -## Override +## 覆盖 -Used when overriding attributes. By default, Erg will throw an error if you try to define the same attribute as the base class. +覆盖属性时使用。 默认情况下,如果您尝试定义与基类相同的属性,Erg 将抛出错误。 -## Impl +## 实现 -Indicates that the argument trait is implemented. +表示参数 trait 已实现。 ```python Add = Trait { @@ -66,10 +66,10 @@ C. `_-_` self, other = C.new {i = self::i - other::} ``` -## Attach +## 附 -Specifies the attachment patch that comes with the trait by default. -This allows you to reproduce the same behavior as Rust traits. +指定默认情况下随 trait 附带的附件补丁。 +这允许您重现与 Rust 特征相同的行为。 ```python # foo.er @@ -86,17 +86,17 @@ AddForOdd = Patch(Odd, Impl := ClosedAdd) AddForOdd.AddO = Even ``` -This will automatically apply the attachment patch when importing traits from other modules. +当从其他模块导入特征时,这将自动应用附件补丁。 -```python -# Originally, IntIsBinAdd and OddIsBinAdd should be imported at the same time, but if it's an attachment patch, you can omit it +```Python +# 本来应该同时导入IntIsBinAdd和OddIsBinAdd,但是如果是附件补丁可以省略 {BinAdd; ...} = import "foo" assert Int. AddO == Int assert Odd.AddO == Even ``` -Internally it's just attached using the trait's `.attach` method. Conflicts can be removed with the trait's `.detach` method. +在内部,它只是使用 trait 的 .attach 方法附加的。 可以使用 trait 的 `.detach` 方法消除冲突。 ```python @Attach X @@ -107,13 +107,13 @@ assert X not in U. attaches assert Y in U. attaches ``` -##Deprecated +## 已弃用 -Indicates that the variable specification is obsolete and deprecated. +指示变量规范已过时且不推荐使用。 -## Test +## 测试 -Indicates that this is a test subroutine. Test subroutines are run with the `erg test` command. +表示这是一个测试子例程。 测试子程序使用 `erg test` 命令运行。

上一页 | 下一页 diff --git a/doc/zh_CN/syntax/30_error_handling.md b/doc/zh_CN/syntax/30_error_handling.md index 2e403ea7..2280a5ae 100644 --- a/doc/zh_CN/syntax/30_error_handling.md +++ b/doc/zh_CN/syntax/30_error_handling.md @@ -1,22 +1,22 @@ -# error handling system +# 错误处理系统 -Mainly use Result type. -In Erg, an error occurs if you throw away an Error type object (not supported at the top level). +主要使用Result类型。 +在 Erg 中,如果您丢弃 Error 类型的对象(顶层不支持),则会发生错误。 -## Exceptions, interop with Python +## 异常,与 Python 互操作 -Erg does not have an exception mechanism (Exception). When importing a Python function +Erg 没有异常机制(Exception)。 导入 Python 函数时 -* Set return value to `T or Error` type -* `T or Panic` type (may cause runtime error) +* 将返回值设置为 `T 或 Error` 类型 +* `T or Panic` 类型(可能导致运行时错误) -There are two options, `pyimport` defaults to the latter. If you want to import as the former, use -Specify `Error` in `pyimport` `exception_type` (`exception_type: {Error, Panic}`). +有两个选项,`pyimport` 默认为后者。 如果要作为前者导入,请使用 +在 `pyimport` `exception_type` 中指定 `Error` (`exception_type: {Error, Panic}`)。 -## Exceptions and Result types +## 异常和结果类型 -The `Result` type represents values ​​that may be errors. Error handling with `Result` is superior to the exception mechanism in several ways. -First of all, it's obvious from the type definition that the subroutine might throw an error, and it's also obvious when you actually use it. +`Result` 类型表示可能是错误的值。 `Result` 的错误处理在几个方面优于异常机制。 +首先,从类型定义中可以看出子程序可能会报错,实际使用时也很明显。 ```python # Python @@ -28,7 +28,7 @@ except e: print(e) ``` -In the above example, it is not possible to tell from this code alone which function raised the exception. Even going back to the function definition, it's hard to tell if the function throws an exception. +在上面的示例中,仅凭此代码无法判断哪个函数引发了异常。 即使回到函数定义,也很难判断函数是否抛出异常。 ```python # Erg @@ -41,15 +41,14 @@ try!: print! e ``` -On the other hand, in this example we can see that `foo!` and `qux!` can raise an error. -Precisely `y` could also be of type `Result`, but you'll have to deal with it eventually to use the value inside. +另一方面,在这个例子中,我们可以看到 `foo!` 和 `qux!` 会引发错误。 +确切地说,`y` 也可能是 `Result` 类型,但您最终必须处理它才能使用里面的值。 -The benefits of using the `Result` type don't stop there. The `Result` type is also thread-safe. This means that error information can be (easily) passed between parallel executions. +使用 `Result` 类型的好处不止于此。 `Result` 类型也是线程安全的。 这意味着错误信息可以(轻松)在并行执行之间传递。 -## Context - -Since the `Error`/`Result` type alone does not cause side effects, unlike exceptions, it cannot have information such as the sending location (Context), but if you use the `.context` method, you can put information in the `Error` object. can be added. The `.context` method is a type of method that consumes the `Error` object itself and creates a new `Error` object. They are chainable and can hold multiple contexts. +## 语境 +由于 `Error`/`Result` 类型本身不会产生副作用,不像异常,它不能有发送位置(Context)等信息,但是如果使用 `.context` 方法,可以将信息放在 `错误`对象。 可以添加。 `.context` 方法是一种使用 `Error` 对象本身并创建新的 `Error` 对象的方法。 它们是可链接的,并且可以包含多个上下文。 ```python f() = todo() \ @@ -62,14 +61,14 @@ f() # hint: and more hints ... ``` -Note that `Error` attributes such as `.msg` and `.kind` are not secondary, so they are not context and cannot be overridden as they were originally created. +请注意,诸如 `.msg` 和 `.kind` 之类的 `Error` 属性不是次要的,因此它们不是上下文,并且不能像最初创建时那样被覆盖。 -## Stack trace +## 堆栈跟踪 -The `Result` type is often used in other languages ​​because of its convenience, but it has the disadvantage of making it difficult to understand the source of an error compared to the exception mechanism. -Therefore, in Erg, the `Error` object has an attribute called `.stack`, and reproduces a pseudo-exception mechanism-like stack trace. -`.stack` is an array of caller objects. Each time an Error object is `returned` (including by `?`) it pushes its calling subroutine onto the `.stack`. -And if it is `?`ed or `.unwrap`ed in a context where `return` is not possible, it will panic with a traceback. +`Result` 类型由于其方便性在其他语言中经常使用,但与异常机制相比,它的缺点是难以理解错误的来源。 +因此,在 Erg 中,`Error` 对象具有名为 `.stack` 的属性,并再现了类似伪异常机制的堆栈跟踪。 +`.stack` 是调用者对象的数组。 每次 Error 对象被`return`(包括通过`?`)时,它都会将它的调用子例程推送到`.stack`。 +如果它是 `?`ed 或 `.unwrap`ed 在一个不可能 `return` 的上下文中,它会因为回溯而恐慌。 ```python f x = @@ -93,12 +92,12 @@ i = g(1)? # Error: ... ``` -## Panic +## 恐慌 -Erg also has a mechanism for dealing with unrecoverable errors called __panicing__. -An unrecoverable error is an error caused by an external factor such as a software/hardware malfunction, an error so fatal that it makes no sense to continue executing the code, or an error unexpected by the programmer. Etc. If this happens, the program will be terminated immediately, because the programmer's efforts cannot restore normal operation. This is called "panicing". +Erg 还有一种处理不可恢复错误的机制,称为 __panicing__。 +不可恢复的错误是由外部因素引起的错误,例如软件/硬件故障、严重到无法继续执行代码的错误或程序员未预料到的错误。 等如果发生这种情况,程序将立即终止,因为程序员的努力无法恢复正常运行。 这被称为“恐慌”。 -Panic is done with the `panic` function. +恐慌是通过 `panic` 功能完成的。 ```python panic "something went wrong!" diff --git a/doc/zh_CN/syntax/31_pipeline.md b/doc/zh_CN/syntax/31_pipeline.md index 4978f417..acaeef49 100644 --- a/doc/zh_CN/syntax/31_pipeline.md +++ b/doc/zh_CN/syntax/31_pipeline.md @@ -1,15 +1,15 @@ -# pipeline operator +# 管道运算符 -Pipeline operators are used like this: +管道运算符的使用方式如下: ```python assert f(g(x)) == (x |> g |> f) assert f(g(x, y)) == ((x, y) |> g |> f) ``` -In other words, the order `Callable(object)` can be changed to `object |> Callable`. -The pipeline operator can also be used on 方法. For 方法, `object.method(args)` changes to `object |>.method(args)`. -It looks like just more `|>`, but since the bond strength is low, you may be able to reduce the amount of `()`. +换句话说,`Callable(object)` 的顺序可以更改为 `object |> Callable`。 +管道运算符也可用于方法。 对于方法,`object.method(args)` 更改为 `object |>.method(args)`。 +它看起来只是更多的`|>`,但由于粘合强度较低,您可以减少`()`的数量。 ```python rand = -1.0..1.0 |>.sample!() @@ -18,7 +18,7 @@ log rand # 0.2597... 1+1*2 |>.times do log("a", end := "") # aaa evens = 1..100 |>.iter |>.filter i -> i % 2 == 0 |>.collect Array -# When implemented without the pipeline operator, +# 在没有管道操作符的情况下实现, _evens = (1..100).iter().filter(i -> i % 2 == 0).collect(Array) # or __evens = 1..100 \ diff --git a/doc/zh_CN/syntax/32_integration_with_Python.md b/doc/zh_CN/syntax/32_integration_with_Python.md index 021ce6dc..d18ac194 100644 --- a/doc/zh_CN/syntax/32_integration_with_Python.md +++ b/doc/zh_CN/syntax/32_integration_with_Python.md @@ -1,9 +1,9 @@ -# Integration with Python +# 与 Python 集成 -## Export to Python +## 导出到 Python -When the Erg script is compiled, a .pyc file is generated, which can simply be imported as a Python module. -However, variables set to private on the Erg side cannot be accessed from Python. +编译 Erg 脚本时,会生成一个 .pyc 文件,可以简单地将其作为 Python 模块导入。 +但是,无法从 Python 访问在 Erg 端设置为私有的变量。 ```python # foo.er @@ -19,26 +19,26 @@ erg --compile foo.er import foo print(foo.public) -print(foo.private) # AttributeError: +print(foo.private) # 属性错误: ``` -## Import from Python +## 从 Python 导入 -All objects imported from Python are by default of type `Object`. Since no comparisons can be made at this point, it is necessary to refine the type. +默认情况下,从 Python 导入的所有对象都是“Object”类型。 由于此时无法进行比较,因此有必要细化类型。 -## Type Specification in the Standard Library +## 标准库中的类型规范 -All APIs in the Python standard library are type specified by the Erg development team. +Python 标准库中的所有 API 都是由 Erg 开发团队指定的类型。 ```python time = pyimport "time" time.sleep! 1 ``` -## Type Specification for User Scripts +## 用户脚本的类型规范 -Create a `foo.d.er` file that types the Python `foo` module. -Type hints on the Python side are ignored since they are not 100% guaranteed. +创建一个类型为 Python `foo` 模块的 `foo.d.er` 文件。 +Python 端的类型提示被忽略,因为它们不是 100% 保证的。 ```python # foo.py @@ -63,11 +63,12 @@ foo = pyimport "foo" assert foo.bar(1) in Int ``` -This ensures type safety by performing type checking at runtime. The ``declare`` function works roughly as follows. +这通过在运行时执行类型检查来确保类型安全。 ``declare`` 函数大致如下工作 + ```python declare|S: Subroutine| sub!: S, T = - # Actually, => can be cast to a function without block side effects + # 实际上,=> 可以强制转换为没有块副作用的函数 x => assert x in T.Input y = sub!(x) @@ -75,7 +76,7 @@ declare|S: Subroutine| sub!: S, T = y ``` -Since this is a runtime overhead, a project is planned to statically type analyze Python scripts with Erg's type system. +由于这是运行时开销,因此计划使用 Erg 的类型系统对 Python 脚本进行静态类型分析

上一页 | 下一页 diff --git a/doc/zh_CN/syntax/33_package_system.md b/doc/zh_CN/syntax/33_package_system.md index 9d885e0e..c3453333 100644 --- a/doc/zh_CN/syntax/33_package_system.md +++ b/doc/zh_CN/syntax/33_package_system.md @@ -1,15 +1,15 @@ -# Package System +# 打包系统 -Erg packages can be roughly classified into the app package, which is the application, and the lib package, which is the library. -The entry point of the app package is `src/app.er`. The `main` function defined in `app.er` is executed. -The entry point for the lib package is `src/lib.er`. Importing a package is equivalent to importing `lib.er`. +Erg包大致可以分为app包,即应用程序,以及lib包,即库。 +应用包的入口点是`src/app.er`。 `app.er` 中定义的`main` 函数被执行。 +lib 包的入口点是`src/lib.er`。导入包相当于导入 `lib.er`。 -A package has a sub-structure called a module, which in Erg is an Erg file or directory composed of Erg files. External Erg files/directories are manipulatable objects as module objects. +一个包有一个称为模块的子结构,在 Erg 中是一个 Erg 文件或由 Erg 文件组成的目录。外部 Erg 文件/目录是作为模块对象的可操作对象。 -In order for a directory to be recognized as a module, it is necessary to place a `(directory name).er` file in the directory. -This is similar to Python's `__init__.py`, but unlike `__init__.py`, it is placed outside the directory. +为了将目录识别为模块,有必要在目录中放置一个“(目录名称).er”文件。 +这类似于 Python 的 `__init__.py`,但与 `__init__.py` 不同的是,它放在目录之外。 -As an example, consider the following directory structure. +例如,考虑以下目录结构。 ```console └─┬ ./src @@ -21,9 +21,9 @@ As an example, consider the following directory structure. └─ qux.er ``` -You can import `foo` and `bar` modules in `app.er`. The `bar` directory can be recognized as a module because of the `bar.er` file. -A `foo` module is a module consisting of files, and a `bar` module is a module consisting of directories. The `bar` module also contains `baz` and `qux` modules. -This module is simply an attribute of the `bar` module, and can be accessed from `app.er` as follows. +您可以在 `app.er` 中导入 `foo` 和 `bar` 模块。由于 `bar.er` 文件,`bar` 目录可以被识别为一个模块。 +`foo` 模块是由文件组成的模块,`bar` 模块是由目录组成的模块。 `bar` 模块还包含 `baz` 和 `qux` 模块。 +该模块只是 `bar` 模块的一个属性,可以从 `app.er` 访问,如下所示。 ```python # app.er @@ -36,9 +36,9 @@ main args = ... ``` -Note the `/` delimiter for accessing submodules. This is because there can be file names such as `bar.baz.er`. -Such filenames are discouraged, since the `.er` prefix is meaningful in Erg. -For example, a module for testing. A file ending with `.test.er` is a (white box) test module, which executes a subroutine decorated with `@Test` when the test is run. +请注意用于访问子模块的 `/` 分隔符。 这是因为可以有诸如 `bar.baz.er` 之类的文件名。 +不鼓励使用此类文件名,因为 `.er` 前缀在 Erg 中是有意义的。 +例如,用于测试的模块。 以 `.test.er` 结尾的文件是一个(白盒)测试模块,它在运行测试时执行一个用 `@Test` 修饰的子例程。 ```console └─┬ ./src @@ -55,7 +55,7 @@ main args = ... ``` -Also, files ending in ``.private.er`` are private modules and can only be accessed by modules in the same directory. +此外,以 .private.er 结尾的文件是私有模块,只能由同一目录中的模块访问。 ```console └─┬ diff --git a/doc/zh_CN/syntax/34_generator.md b/doc/zh_CN/syntax/34_generator.md index 8e927025..5c807345 100644 --- a/doc/zh_CN/syntax/34_generator.md +++ b/doc/zh_CN/syntax/34_generator.md @@ -1,6 +1,6 @@ -# Generator +# 生成器 -Generators are special procedures that use the `yield!` procedure in a block. +生成器是在块中使用 `yield!` 过程的特殊过程。 ```python g!() = @@ -9,8 +9,8 @@ g!() = yield! 3 ``` -`yield!` is a procedure defined in a block of subroutines that calls `self!.yield!`. Like `return`, it returns the value passed to it as a return value, but it has the feature of saving the current execution state of the block and executing it from the beginning when it is called again. -A generator is both a procedure and an iterator; a Python generator is a function that creates an iterator, while Erg iterates directly. Procedures themselves are generally not mutable objects (no `!`), but a generator is a mutable object because its own contents can change with each execution. +`yield!` 是在调用`self!.yield!` 的子程序块中定义的过程。 和`return`一样,它把传递给它的值作为返回值返回,但它具有保存block当前执行状态,再次调用时从头开始执行的特性。 +生成器既是过程又是迭代器; Python 生成器是一个创建迭代器的函数,而 Erg 直接迭代。 过程本身通常不是可变对象(没有`!`),但生成器是可变对象,因为它自己的内容可以随着每次执行而改变。 ```python # Generator! @@ -20,7 +20,7 @@ assert g!() == 2 assert g!() == 3 ``` -A Python-style generator can be defined as follows. +Python 风格的生成器可以定义如下。 ```python make_g() = () => diff --git a/doc/zh_CN/syntax/container_ownership.md b/doc/zh_CN/syntax/container_ownership.md index 08f90efc..4195d404 100644 --- a/doc/zh_CN/syntax/container_ownership.md +++ b/doc/zh_CN/syntax/container_ownership.md @@ -1,6 +1,6 @@ -# Subscript (index access) +# 下标(索引访问) -`[]` is different from normal 方法. +`[]` 不同于普通的方法。 ```python a = [!1, !2] @@ -8,10 +8,10 @@ a[0].inc!() assert a == [2, 2] ``` -Recall that the return value of a subroutine cannot be a reference. -The type of `a[0]` here should clearly be `Ref!(Int!)` (the type of `a[0]` depends on the context). -So `[]` is actually part of a special syntax, just like `.`. Unlike Python, it cannot be overloaded. -It is also not possible to reproduce the behavior of `[]` in a method. +回想一下,子例程的返回值不能是引用。 +这里的 `a[0]` 的类型显然应该是 `Ref!(Int!)`(`a[0]` 的类型取决于上下文)。 +所以 `[]` 实际上是特殊语法的一部分,就像 `.` 一样。 与 Python 不同,它不能被重载。 +也无法在方法中重现 `[]` 的行为。 ```python C = Class {i = Int!}