mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-30 04:44:44 +00:00
Merge branch 'main' into fix-#247
This commit is contained in:
commit
a373185dec
10 changed files with 62 additions and 25 deletions
|
@ -1,4 +1,8 @@
|
|||
class Array(list):
|
||||
def dedup(self):
|
||||
return Array(list(set(self)))
|
||||
def dedup_by(self, f):
|
||||
return Array(list(set(map(f, self))))
|
||||
def push(self, value):
|
||||
self.append(value)
|
||||
return self
|
||||
|
|
|
@ -8,3 +8,32 @@ class Str(str):
|
|||
return Str(s)
|
||||
else:
|
||||
return Error("Str can't be other than str")
|
||||
def get(self, i: int):
|
||||
if len(self) > i:
|
||||
return Str(self[i])
|
||||
else:
|
||||
return None
|
||||
|
||||
class StrMut(Str):
|
||||
def try_new(s: str):
|
||||
if isinstance(s, str):
|
||||
return StrMut(s)
|
||||
else:
|
||||
return Error("Str! can't be other than str")
|
||||
def clear(self):
|
||||
self = ""
|
||||
def pop(self):
|
||||
if len(self) > 0:
|
||||
last = self[-1]
|
||||
self = self[:-1]
|
||||
return last
|
||||
else:
|
||||
return Error("Can't pop from empty `Str!`")
|
||||
def push(self, c: str):
|
||||
self += c
|
||||
def remove(self, idx: int):
|
||||
char = self[idx]
|
||||
self = self[:idx] + self[idx+1:]
|
||||
return char
|
||||
def insert(self, idx: int, c: str):
|
||||
self = self[:idx] + c + self[idx:]
|
||||
|
|
|
@ -47,7 +47,7 @@ assert points.iter().map(x -> x.norm()).collect(Array) == [5, 25].
|
|||
|
||||
## Trait inclusion
|
||||
|
||||
The expansion operator `...` allows you to define a trait that contains a certain trait as a supertype. This is called the __subsumption__ of a trait.
|
||||
`Subsume` allows you to define a trait that contains a certain trait as a supertype. This is called the __subsumption__ of a trait.
|
||||
In the example below, `BinAddSub` subsumes `BinAdd` and `BinSub`.
|
||||
This corresponds to Inheritance in a class, but unlike Inheritance, multiple base types can be combined using `and`. Traits that are partially excluded by `not` are also allowed.
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ However, external libraries may not support multiple languages.
|
|||
|
||||
```python
|
||||
record: {.name = Str; .age = Nat; .height = CentiMeter}
|
||||
{height; rest; ...} = record
|
||||
{height; ...rest} = record
|
||||
mut_record = {.height = !height; ...rest}
|
||||
```
|
||||
|
||||
|
@ -71,7 +71,7 @@ method 1:
|
|||
|
||||
```python
|
||||
arr = [...]
|
||||
for! arr.iter().enumerate(start: 1), i =>
|
||||
for! arr.iter().enumerate(start := 1), i =>
|
||||
...
|
||||
```
|
||||
|
||||
|
@ -118,16 +118,18 @@ C.
|
|||
...
|
||||
```
|
||||
|
||||
## Want the argument names to be identified on the type system
|
||||
## When implementing a trait's methods, warnings are given for variables that were not used
|
||||
|
||||
You can receive arguments by record.
|
||||
You can use `discard`.
|
||||
|
||||
```python
|
||||
Point = {x = Int; y = Int}
|
||||
T = Trait {.f = (Self, x: Int, s: Str) -> Int}
|
||||
|
||||
norm: Point -> Int
|
||||
norm({x: Int; y: Int}): Int = x**2 + y**2
|
||||
assert norm({x = 1; y = 2}) == norm({y = 2; x = 1})
|
||||
C = Class T
|
||||
C|<: T|.
|
||||
f self, x, s =
|
||||
discard s
|
||||
...
|
||||
```
|
||||
|
||||
## Want to stop warnings
|
||||
|
|
|
@ -34,7 +34,7 @@ f: (x: Int, y: Int) -> Int
|
|||
f: (Int, Int) -> Int
|
||||
```
|
||||
|
||||
引数名を明示して宣言した場合、定義時に名前が違うと型エラーとなります。引数名の任意性を与えたい場合は2番目の方法で宣言すると良いでしょう。その場合、型検査で見られるのはメソッド名とその型のみです。
|
||||
引数名を明示して宣言した場合、定義時に名前が違うと型エラーとなります。引数名の任意性を与えたい場合は2番目の方法で宣言すると良いでしょう。その場合、型検査で見られるのはメソッド名とその型のみです。キーワード指定による呼び出しはできなくなります。
|
||||
|
||||
```python
|
||||
T = Trait {
|
||||
|
|
|
@ -83,7 +83,7 @@ f u := 6, v := 5, w := 4, x := 1, y := 2, z := 3
|
|||
|
||||
ある引数が大抵の場合決まりきっており省略できるようにしたい場合、デフォルト引数を使うと良いでしょう。
|
||||
|
||||
デフォルト引数は`:=`(or-assign operator)で指定します。`base`が指定されなかったら`math.E`を`base`に代入します。
|
||||
デフォルト引数は`:=`(default-assign operator)で指定します。`base`が指定されなかったら`math.E`を`base`に代入します。
|
||||
|
||||
```python
|
||||
math_log x: Ratio, base := math.E = ...
|
||||
|
|
|
@ -49,7 +49,7 @@ assert points.iter().map(x -> x.norm()).collect(Array) == [5, 25]
|
|||
|
||||
## トレイトの包摂
|
||||
|
||||
展開演算子`...`によって、あるトレイトを上位型として含むトレイトを定義できます。これをトレイトの __包摂(Subsumption)__ と呼びます。
|
||||
関数`Subsume`によって、あるトレイトを上位型として含むトレイトを定義できます。これをトレイトの __包摂(Subsumption)__ と呼びます。
|
||||
下の例でいうと、`BinAddSub`は`BinAdd`と`BinSub`を包摂しています。
|
||||
これはクラスにおける継承(Inheritance)に対応しますが、継承と違い複数の基底型を`and`で合成して指定できます。`not`によって一部を除外したトレイトでもOKです。
|
||||
|
||||
|
@ -147,15 +147,15 @@ SafeDiv R, O = Subsume Div, {
|
|||
```python
|
||||
Add R = Trait {
|
||||
.Output = Type
|
||||
.`_+_` = Self.(R) -> .Output
|
||||
.`_+_` = (Self, R) -> .Output
|
||||
}
|
||||
Sub R = Trait {
|
||||
.Output = Type
|
||||
.`_-_` = Self.(R) -> .Output
|
||||
.`_-_` = (Self, R) -> .Output
|
||||
}
|
||||
Mul R = Trait {
|
||||
.Output = Type
|
||||
.`*` = Self.(R) -> .Output
|
||||
.`*` = (Self, R) -> .Output
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
```python
|
||||
record: {.name = Str; .age = Nat; .height = CentiMeter}
|
||||
{height; rest; ...} = record
|
||||
{height; ...rest} = record
|
||||
mut_record = {.height = !height; ...rest}
|
||||
```
|
||||
|
||||
|
@ -73,7 +73,7 @@ method 1:
|
|||
|
||||
```python
|
||||
arr = [...]
|
||||
for! arr.iter().enumerate(start: 1), i =>
|
||||
for! arr.iter().enumerate(start := 1), i =>
|
||||
...
|
||||
```
|
||||
|
||||
|
@ -120,16 +120,18 @@ C.
|
|||
...
|
||||
```
|
||||
|
||||
## 引数名を型システム上で識別させたい
|
||||
## トレイトのメソッドを実装する際に、使わなかった変数の警告が出る
|
||||
|
||||
引数をレコードで受け取ると良いでしょう。
|
||||
`discard`を使うとよいでしょう。
|
||||
|
||||
```python
|
||||
Point = {x = Int; y = Int}
|
||||
T = Trait {.f = (Self, x: Int, s: Str) -> Int}
|
||||
|
||||
norm: Point -> Int
|
||||
norm({x: Int; y: Int}): Int = x**2 + y**2
|
||||
assert norm({x = 1; y = 2}) == norm({y = 2; x = 1})
|
||||
C = Class T
|
||||
C|<: T|.
|
||||
f self, x, s =
|
||||
discard s
|
||||
...
|
||||
```
|
||||
|
||||
## 警告を出さないようにしたい
|
||||
|
|
|
@ -49,7 +49,7 @@ assert points.iter().map(x -> x.norm()).collect(Array) == [5, 25].
|
|||
|
||||
## Trait包含
|
||||
|
||||
扩展运算符 `...` 允许您将包含某个Trait的Trait定义为超类型。这称为Trait的 __subsumption__
|
||||
`Subsume` 允许您将包含某个Trait的Trait定义为超类型。这称为Trait的 __subsumption__
|
||||
在下面的示例中,`BinAddSub` 包含 `BinAdd` 和 `BinSub`
|
||||
这对应于类中的继承,但与继承不同的是,可以使用"和"组合多个基类型。也允许被 `not` 部分排除的Trait
|
||||
|
||||
|
|
|
@ -49,7 +49,7 @@ assert points.iter().map(x -> x.norm()).collect(Array) == [5, 25].
|
|||
|
||||
## Trait包含
|
||||
|
||||
擴展運算符 `...` 允許您將包含某個Trait的Trait定義為超類型。這稱為Trait的 __subsumption__
|
||||
`Subsume` 允許您將包含某個Trait的Trait定義為超類型。這稱為Trait的 __subsumption__
|
||||
在下面的示例中,`BinAddSub` 包含 `BinAdd` 和 `BinSub`
|
||||
這對應于類中的繼承,但與繼承不同的是,可以使用"和"組合多個基類型。也允許被 `not` 部分排除的Trait
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue