From 423649a8a702dae09ef7fda98f17f73d1103c351 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Sun, 3 Mar 2024 19:50:45 +0900 Subject: [PATCH] doc: update constructor syntax description --- README.md | 2 +- README_JA.md | 2 +- README_zh-CN.md | 2 +- README_zh-TW.md | 2 +- doc/EN/syntax/08_procedure.md | 26 ++++++++------ doc/EN/syntax/type/04_class.md | 7 ++-- doc/EN/syntax/type/advanced/newtype.md | 2 +- doc/EN/syntax/type/advanced/overloading.md | 2 +- doc/JA/syntax/08_procedure.md | 2 +- doc/JA/syntax/type/04_class.md | 9 +++-- doc/JA/syntax/type/advanced/newtype.md | 2 +- doc/JA/syntax/type/advanced/overloading.md | 2 +- doc/zh_CN/syntax/08_procedure.md | 32 ++++++++++------- doc/zh_CN/syntax/type/04_class.md | 5 ++- doc/zh_CN/syntax/type/advanced/newtype.md | 2 +- doc/zh_CN/syntax/type/advanced/overloading.md | 2 +- doc/zh_TW/syntax/08_procedure.md | 34 +++++++++++-------- doc/zh_TW/syntax/type/04_class.md | 5 ++- doc/zh_TW/syntax/type/advanced/newtype.md | 2 +- doc/zh_TW/syntax/type/advanced/overloading.md | 2 +- 20 files changed, 79 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 01b1045bd..49367b2e6 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ # Code that uses a lot of side effects is redundant, so you will naturally write pure code Counter! = Inherit Int! Counter!. - new i: Int = Self!::__new__ !i + new i: Int = Counter! !i inc! ref! self = self.update! old -> old + 1 diff --git a/README_JA.md b/README_JA.md index 96758d3eb..88b5f88a5 100644 --- a/README_JA.md +++ b/README_JA.md @@ -109,7 +109,7 @@ # 副作用を多用するコードは自然と冗長になるため、自然と純粋なコードを書くように動機づけられます Counter! = Inherit Int! Counter!. - new i: Int = Self!::__new__ !i + new i: Int = Counter! !i inc! ref! self = self.update! old -> old + 1 diff --git a/README_zh-CN.md b/README_zh-CN.md index 2a7d37975..1964bcffc 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -109,7 +109,7 @@ # 使用大量副作用的代码是多余的,所以你自然会写纯代码 Counter! = Inherit Int! Counter!. - new i: Int = Self!::__new__ !i + new i: Int = Counter! !i inc! ref! self = self.update! old -> old + 1 diff --git a/README_zh-TW.md b/README_zh-TW.md index 0e127d86d..fec7a3077 100644 --- a/README_zh-TW.md +++ b/README_zh-TW.md @@ -109,7 +109,7 @@ # 使用大量副作用的代碼是多余的,所以你自然會寫純代碼 Counter! = Inherit Int! Counter!. - new i: Int = Self!::__new__ !i + new i: Int = Counter! !i inc! ref! self = self.update! old -> old + 1 diff --git a/doc/EN/syntax/08_procedure.md b/doc/EN/syntax/08_procedure.md index 9bf025fa5..fd5490afd 100644 --- a/doc/EN/syntax/08_procedure.md +++ b/doc/EN/syntax/08_procedure.md @@ -32,34 +32,40 @@ func(x: Int) = y => print! x, y # NG ``` ## Binding + Procedures can manipulate mutable variables that are out of scope. + ```python x = ! 0 proc! () = - x.inc! () + x.inc! () proc! () assert x == 1 ``` + In this case, 'proc!' has the following type. + ```python proc!: {| x: Int! |} () => () ``` + `{| x: Int! |} The ' part is called the bind column and represents the variable and its type that the procedure operates on. Binding columns are derived automatically, so you don't need to write them explicitly. Note that normal procedures can only manipulate predetermined external variables. This means that variables passed in arguments cannot be rewritten. If you want to do something like that, you need to use procedural methods. Procedural methods can rewrite 'self'. + ```python C! N = Class {arr = [Int; N]!} C!. - new() = Self! (0)::__new__ {arr = ![]} -C! (N). - # push!: {|self: C!( N) ~> C! (N+1)|} (self: RefMut(C!( N)), x: Int) => NoneType - push! ref! self, x = self.arr.push! (x) - # pop!: {|self: C!( N) ~> C! (N-1)|} (self: RefMut(C!( N))) => Int - pop! ref! self = self.arr.pop! () -c = C!. new() -c.push! (1) -assert c.pop! () == 1 + new() = Self!(0) {arr = ![]} +C!(N). + # push!: {|self: C!(N) ~> C!(N+1)|} (self: RefMut(C!(N)), x: Int) => NoneType + push! ref! self, x = self.arr.push! (x) + # pop!: {|self: C!(N) ~> C!(N-1)|} (self: RefMut(C!(N))) => Int + pop! ref! self = self.arr.pop! () +c = C!.new() +c.push!(1) +assert c.pop!() == 1 ```

diff --git a/doc/EN/syntax/type/04_class.md b/doc/EN/syntax/type/04_class.md index a2bebd302..2462b1f41 100644 --- a/doc/EN/syntax/type/04_class.md +++ b/doc/EN/syntax/type/04_class.md @@ -5,9 +5,8 @@ Here is an example of a simple class. ```python Person = Class {.name = Str; .age = Nat} -# If `.new` is not defined, then Erg will create `Person.new = Person::__new__` Person. - new name, age = Self::__new__ {.name = name; .age = age} + new name, age = Self {.name = name; .age = age} john = Person.new "John Smith", 25 print! john # @@ -15,8 +14,8 @@ print! classof(john) # Person ``` The type given to `Class` (normally a record type) is called the requirement type (in this case `{.name = Str; .age = Nat}`). -Instances can be created with `::__new__ { = ; ...}` can be created with. -`{.name = "John Smith"; .age = 25}` is just a record, but it is converted to a `Person` instance by passing `Person.new`. +Instances can be created with ` { = ; ...}`. +`{.name = "John Smith"; .age = 25}` is just a record, but it is converted to a `Person` instance by passing it to the constructor / `Person.new`. The subroutine that creates such an instance is called a constructor. In the class above, the `.new` method is defined so that field names, etc. can be omitted. diff --git a/doc/EN/syntax/type/advanced/newtype.md b/doc/EN/syntax/type/advanced/newtype.md index 96d653990..d5e585d6d 100644 --- a/doc/EN/syntax/type/advanced/newtype.md +++ b/doc/EN/syntax/type/advanced/newtype.md @@ -19,7 +19,7 @@ UserId = Class {id = Nat} UserId. new id: Nat = assert id.dights().len() == 8, else: "UserId must be a positive number with length 8" - UserId::__new__ {id;} + UserId {id;} i = UserId.new(10000000) print! i # <__main__.UserId object> diff --git a/doc/EN/syntax/type/advanced/overloading.md b/doc/EN/syntax/type/advanced/overloading.md index b16bd8b22..5b161c09c 100644 --- a/doc/EN/syntax/type/advanced/overloading.md +++ b/doc/EN/syntax/type/advanced/overloading.md @@ -35,7 +35,7 @@ Also, overloading of types with different numbers of arguments can be reproduced ```python C = Class {.x = Int; .y = Int} C. - new(x, y := 0) = Self::__new__ {.x; .y} + new(x, y := 0) = Self {.x; .y} assert C.new(0, 0) == C.new(0) ``` diff --git a/doc/JA/syntax/08_procedure.md b/doc/JA/syntax/08_procedure.md index 9af0aeba7..7c3ef5be8 100644 --- a/doc/JA/syntax/08_procedure.md +++ b/doc/JA/syntax/08_procedure.md @@ -61,7 +61,7 @@ proc!: {|x: Int!|}() => () ```python C! N = Class {arr = [Int; N]!} C!. - new() = Self!(0)::__new__ {arr = ![]} + new() = Self!(0) {arr = ![]} C!(N). # push!: {|self: C!(N) ~> C!(N+1)|}(self: RefMut(C!(N)), x: Int) => NoneType push! ref! self, x = self.arr.push!(x) diff --git a/doc/JA/syntax/type/04_class.md b/doc/JA/syntax/type/04_class.md index 372168aec..09cf269a0 100644 --- a/doc/JA/syntax/type/04_class.md +++ b/doc/JA/syntax/type/04_class.md @@ -7,9 +7,8 @@ Ergにおけるクラスは、大まかには自身の要素(インスタンス) ```python Person = Class {.name = Str; .age = Nat} -# .newが定義されなかった場合、自動で`Person.new = Person::__new__`となる Person. - new name, age = Self::__new__ {.name = name; .age = age} + new name, age = Self {.name = name; .age = age} john = Person.new "John Smith", 25 print! john # @@ -17,8 +16,8 @@ print! classof(john) # Person ``` `Class`に与えられる型を要件型(この場合は`{.name = Str; .age = Nat}`)といいます。 -インスタンスは`クラス名::__new__ {属性名 = 値; ...}`で生成できます。 -`{.name = "John Smith"; .age = 25}`は単なるレコードですが、`Person.new`を通すことで`Person`インスタンスに変換されるわけです。 +インスタンスは`クラス名 {属性名 = 値; ...}`で生成できます。 +`{.name = "John Smith"; .age = 25}`は単なるレコードですが、コンストラクタまたは`Person.new`を通すことで`Person`インスタンスに変換されるわけです。 このようなインスタンスを生成するサブルーチンはコンストラクタと呼ばれます。 上のクラスでは、フィールド名等を省略できるように`.new`メソッドを定義しています。 @@ -31,7 +30,7 @@ Person.new name, age = ... # SyntaxError: cannot define attributes directly on a ## 省略記法 レコードでない型`T`に対し`C = Class T`とすると、これは`C = Class {base = T}`と同じ意味になります。これはいわゆるnew typeパターンの定義を簡略化するためのものです。 -また、コンストラクタ`__new__`/`new`もレコードで包まず`T`型オブジェクトを直接渡せます。 +また、コンストラクタ/`new`もレコードで包まず`T`型オブジェクトを直接渡せます。 ```python Id = Class {base = Int} diff --git a/doc/JA/syntax/type/advanced/newtype.md b/doc/JA/syntax/type/advanced/newtype.md index 76496710c..24fb00e98 100644 --- a/doc/JA/syntax/type/advanced/newtype.md +++ b/doc/JA/syntax/type/advanced/newtype.md @@ -21,7 +21,7 @@ UserId = Class {id = Nat} UserId. new id: Nat = assert id.dights().len() == 8, else: "UserId must be a positive number with length 8" - UserId::__new__ {id;} + UserId {id;} i = UserId.new(10000000) print! i # <__main__.UserId object> diff --git a/doc/JA/syntax/type/advanced/overloading.md b/doc/JA/syntax/type/advanced/overloading.md index 8075e2b24..3d8d22bb1 100644 --- a/doc/JA/syntax/type/advanced/overloading.md +++ b/doc/JA/syntax/type/advanced/overloading.md @@ -37,7 +37,7 @@ assert add1(1.0) == 2.0 ```python C = Class {.x = Int; .y = Int} C. - new(x, y := 0) = Self::__new__ {.x; .y} + new(x, y := 0) = Self {.x; .y} assert C.new(0, 0) == C.new(0) ``` diff --git a/doc/zh_CN/syntax/08_procedure.md b/doc/zh_CN/syntax/08_procedure.md index 99a299dde..429acc38f 100644 --- a/doc/zh_CN/syntax/08_procedure.md +++ b/doc/zh_CN/syntax/08_procedure.md @@ -34,34 +34,40 @@ func(x: Int) = y => print! x, y # NG ``` ## 绑定 + 过程可以操作范围外的变量。 + ```python -x = ! 0 -proc! () = - x.inc! () -proc! () +x = !0 +proc!() = + x.inc!() +proc!() assert x == 1 ``` + 此时,“proc!” 具有以下类型: + ```python proc!: {| x: Int! |} () => () ``` -`{| x: Int! |} '部分称为绑定列,表示过程操作的变量及其类型。 + +`{| x: Int! |}`部分称为绑定列,表示过程操作的变量及其类型。 绑定列是自动派生的,因此无需显式编写。 请注意,常规过程只能操作预先确定的外部变量。 这意味着不能重写传递给参数的变量。 如果要执行此操作,则必须使用过程方法。 过程方法可以重写“自”。 + ```python C! N = Class {arr = [Int; N]!} C!. - new() = Self! (0)::__new__ {arr = ![]} + new() = Self!(0) {arr = ![]} C! (N). - # push!: {|self: C!( N) ~> C! (N+1)|} (self: RefMut(C!( N)), x: Int) => NoneType - push! ref! self, x = self.arr.push! (x) - # pop!: {|self: C!( N) ~> C! (N-1)|} (self: RefMut(C!( N))) => Int - pop! ref! self = self.arr.pop! () -c = C!. new() -c.push! (1) -assert c.pop! () == 1 + # push!: {|self: C!(N) ~> C!(N+1)|} (self: RefMut(C!(N)), x: Int) => NoneType + push! ref! self, x = self.arr.push! (x) + # pop!: {|self: C!(N) ~> C!(N-1)|} (self: RefMut(C!(N))) => Int + pop! ref! self = self.arr.pop! () +c = C!.new() +c.push!(1) +assert c.pop!() == 1 ```

diff --git a/doc/zh_CN/syntax/type/04_class.md b/doc/zh_CN/syntax/type/04_class.md index 528ec40fc..804939c1c 100644 --- a/doc/zh_CN/syntax/type/04_class.md +++ b/doc/zh_CN/syntax/type/04_class.md @@ -7,9 +7,8 @@ Erg 中的类大致是一种可以创建自己的元素(实例)的类型 ```python Person = Class {.name = Str; .age = Nat} -# 如果 `.new` 没有定义,那么 Erg 将创建 `Person.new = Person::__new__` Person. - new name, age = Self::__new__ {.name = name; .age = age} + new name, age = Self {.name = name; .age = age} john = Person.new "John Smith", 25 print! john # @@ -17,7 +16,7 @@ print! classof(john) # Person ``` 赋予"Class"的类型(通常是记录类型)称为需求类型(在本例中为"{.name = Str; .age = Nat}") -可以使用 `::__new__ { = ; 创建实例 ...}` 可以创建 +可以使用 ` { = ; 创建实例 ...}` 可以创建 `{.name = "约翰·史密斯"; .age = 25}` 只是一条记录,但它通过传递 `Person.new` 转换为 `Person` 实例 创建此类实例的子例程称为构造函数 在上面的类中,`.new` 方法被定义为可以省略字段名等 diff --git a/doc/zh_CN/syntax/type/advanced/newtype.md b/doc/zh_CN/syntax/type/advanced/newtype.md index b3585804b..951c17ee8 100644 --- a/doc/zh_CN/syntax/type/advanced/newtype.md +++ b/doc/zh_CN/syntax/type/advanced/newtype.md @@ -21,7 +21,7 @@ UserId = Class {id = Nat} UserId. new id: Nat = assert id.dights().len() == 8, else: "UserId 必须是长度为 8 的正数" - UserId::__new__ {id;} + UserId {id;} i = UserId.new(10000000) print! i # <__main__.UserId object> diff --git a/doc/zh_CN/syntax/type/advanced/overloading.md b/doc/zh_CN/syntax/type/advanced/overloading.md index 2a6535e5b..d846bc635 100644 --- a/doc/zh_CN/syntax/type/advanced/overloading.md +++ b/doc/zh_CN/syntax/type/advanced/overloading.md @@ -37,7 +37,7 @@ assert add1(1.0) == 2.0 ```python C = Class {.x = Int; .y = Int} C. - new(x, y := 0) = Self::__new__ {.x; .y} + new(x, y := 0) = Self {.x; .y} assert C.new(0, 0) == C.new(0) ``` diff --git a/doc/zh_TW/syntax/08_procedure.md b/doc/zh_TW/syntax/08_procedure.md index 28f7acd6d..925b9f119 100644 --- a/doc/zh_TW/syntax/08_procedure.md +++ b/doc/zh_TW/syntax/08_procedure.md @@ -34,34 +34,40 @@ func(x: Int) = y => print! x, y # NG ``` ## 綁定 + 過程可以操作范圍外的變量。 + ```python -x = ! 0 -proc! () = - x.inc! () -proc! () +x = !0 +proc!() = + x.inc!() +proc!() assert x == 1 ``` + 此時,“proc!” 具有以下類型: + ```python proc!: {| x: Int! |} () => () ``` -`{| x: Int! |} '部分稱為綁定列,表示過程操作的變量及其類型。 + +`{| x: Int! |}`部分稱為綁定列,表示過程操作的變量及其類型。 綁定列是自動派生的,因此無需顯式編寫。 請注意,常規過程只能操作預先確定的外部變量。 這意味著不能重寫傳遞給參數的變量。 如果要執行此操作,則必須使用過程方法。 過程方法可以重寫“自”。 + ```python C! N = Class {arr = [Int; N]!} C!. - new() = Self! (0)::__new__ {arr = ![]} -C! (N). - # push!: {|self: C!( N) ~> C! (N+1)|} (self: RefMut(C!( N)), x: Int) => NoneType - push! ref! self, x = self.arr.push! (x) - # pop!: {|self: C!( N) ~> C! (N-1)|} (self: RefMut(C!( N))) => Int - pop! ref! self = self.arr.pop! () -c = C!. new() -c.push! (1) -assert c.pop! () == 1 + new() = Self!(0) {arr = ![]} +C!(N). + # push!: {|self: C!(N) ~> C!(N+1)|} (self: RefMut(C!(N)), x: Int) => NoneType + push! ref! self, x = self.arr.push!(x) + # pop!: {|self: C!(N) ~> C!(N-1)|} (self: RefMut(C!(N))) => Int + pop! ref! self = self.arr.pop!() +c = C!.new() +c.push!(1) +assert c.pop!() == 1 ```

diff --git a/doc/zh_TW/syntax/type/04_class.md b/doc/zh_TW/syntax/type/04_class.md index 958f00ffe..c09ee90a4 100644 --- a/doc/zh_TW/syntax/type/04_class.md +++ b/doc/zh_TW/syntax/type/04_class.md @@ -7,9 +7,8 @@ Erg 中的類大致是一種可以創建自己的元素(實例)的類型 ```python Person = Class {.name = Str; .age = Nat} -# 如果 `.new` 沒有定義,那么 Erg 將創建 `Person.new = Person::__new__` Person. - new name, age = Self::__new__ {.name = name; .age = age} + new name, age = Self {.name = name; .age = age} john = Person.new "John Smith", 25 print! john # @@ -17,7 +16,7 @@ print! classof(john) # Person ``` 賦予"Class"的類型(通常是記錄類型)稱為需求類型(在本例中為"{.name = Str; .age = Nat}") -可以使用 `::__new__ { = ; 創建實例 ...}` 可以創建 +可以使用 ` { = ; 創建實例 ...}` 可以創建 `{.name = "約翰·史密斯"; .age = 25}` 只是一條記錄,但它通過傳遞 `Person.new` 轉換為 `Person` 實例 創建此類實例的子例程稱為構造函數 在上面的類中,`.new` 方法被定義為可以省略字段名等 diff --git a/doc/zh_TW/syntax/type/advanced/newtype.md b/doc/zh_TW/syntax/type/advanced/newtype.md index 876af5d4c..40840df4c 100644 --- a/doc/zh_TW/syntax/type/advanced/newtype.md +++ b/doc/zh_TW/syntax/type/advanced/newtype.md @@ -21,7 +21,7 @@ UserId = Class {id = Nat} UserId. new id: Nat = assert id.dights().len() == 8, else: "UserId 必須是長度為 8 的正數" - UserId::__new__ {id;} + UserId {id;} i = UserId.new(10000000) print! i # <__main__.UserId object> diff --git a/doc/zh_TW/syntax/type/advanced/overloading.md b/doc/zh_TW/syntax/type/advanced/overloading.md index 62112f59a..195be55f7 100644 --- a/doc/zh_TW/syntax/type/advanced/overloading.md +++ b/doc/zh_TW/syntax/type/advanced/overloading.md @@ -37,7 +37,7 @@ assert add1(1.0) == 2.0 ```python C = Class {.x = Int; .y = Int} C. - new(x, y := 0) = Self::__new__ {.x; .y} + new(x, y := 0) = Self {.x; .y} assert C.new(0, 0) == C.new(0) ```