Skip to content

Commit

Permalink
doc: update constructor syntax description
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Mar 3, 2024
1 parent 0fb996f commit 423649a
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README_JA.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README_zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 16 additions & 10 deletions doc/EN/syntax/08_procedure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<p align='center'>
Expand Down
7 changes: 3 additions & 4 deletions doc/EN/syntax/type/04_class.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ 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 # <Person object>
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 `<Class name>::__new__ {<attribute name> = <value>; ...}` 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 `<Class name> {<attribute name> = <value>; ...}`.
`{.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.

Expand Down
2 changes: 1 addition & 1 deletion doc/EN/syntax/type/advanced/newtype.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion doc/EN/syntax/type/advanced/overloading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down
2 changes: 1 addition & 1 deletion doc/JA/syntax/08_procedure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions doc/JA/syntax/type/04_class.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ 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 # <Person object>
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`メソッドを定義しています。

Expand All @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion doc/JA/syntax/type/advanced/newtype.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion doc/JA/syntax/type/advanced/overloading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down
32 changes: 19 additions & 13 deletions doc/zh_CN/syntax/08_procedure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<p align='center'>
Expand Down
5 changes: 2 additions & 3 deletions doc/zh_CN/syntax/type/04_class.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ 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 # <Person object>
print! classof(john) # Person
```

赋予"Class"的类型(通常是记录类型)称为需求类型(在本例中为"{.name = Str; .age = Nat}")
可以使用 `<Class name>::__new__ {<attribute name> = <value>; 创建实例 ...}` 可以创建
可以使用 `<Class name> {<attribute name> = <value>; 创建实例 ...}` 可以创建
`{.name = "约翰·史密斯"; .age = 25}` 只是一条记录,但它通过传递 `Person.new` 转换为 `Person` 实例
创建此类实例的子例程称为构造函数
在上面的类中,`.new` 方法被定义为可以省略字段名等
Expand Down
2 changes: 1 addition & 1 deletion doc/zh_CN/syntax/type/advanced/newtype.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion doc/zh_CN/syntax/type/advanced/overloading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down
34 changes: 20 additions & 14 deletions doc/zh_TW/syntax/08_procedure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<p align='center'>
Expand Down
5 changes: 2 additions & 3 deletions doc/zh_TW/syntax/type/04_class.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ 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 # <Person object>
print! classof(john) # Person
```

賦予"Class"的類型(通常是記錄類型)稱為需求類型(在本例中為"{.name = Str; .age = Nat}")
可以使用 `<Class name>::__new__ {<attribute name> = <value>; 創建實例 ...}` 可以創建
可以使用 `<Class name> {<attribute name> = <value>; 創建實例 ...}` 可以創建
`{.name = "約翰·史密斯"; .age = 25}` 只是一條記錄,但它通過傳遞 `Person.new` 轉換為 `Person` 實例
創建此類實例的子例程稱為構造函數
在上面的類中,`.new` 方法被定義為可以省略字段名等
Expand Down
2 changes: 1 addition & 1 deletion doc/zh_TW/syntax/type/advanced/newtype.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion doc/zh_TW/syntax/type/advanced/overloading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down

0 comments on commit 423649a

Please sign in to comment.