From 3e3ec44cdfefd974aaf9dfc38ef6141322775ca1 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Wed, 25 Oct 2023 21:48:34 +0900 Subject: [PATCH] doc: update 14_record.md --- doc/EN/syntax/14_record.md | 19 ++++++++++++++++--- doc/JA/syntax/11_dict.md | 6 +++--- doc/JA/syntax/14_record.md | 19 ++++++++++++++++--- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/doc/EN/syntax/14_record.md b/doc/EN/syntax/14_record.md index f88b04792..3e0a9d164 100644 --- a/doc/EN/syntax/14_record.md +++ b/doc/EN/syntax/14_record.md @@ -18,7 +18,7 @@ The difference from JavaScript object literals is that they are not accessible a This is because access to the value is determined at compile-time, and because dictionaries and records are different things. In other words, `{"name": "John"}` is a Dict and `{name = "John"}` is a record. So how should we use dictionaries and records? In general, we recommend using records. Records have the advantages of being checked at compile-time for the existence of elements and of being able to specify __visibility_. -Specifying visibility is equivalent to specifying public/private in Java and other languages. For details, see [visibility](./20_visibility.md) for details. +Specifying visibility is equivalent to specifying public/private in Java and other languages. For details, see [visibility](./21_visibility.md) for details. ```python,compile_fail a = {x = 1; .y = x + 1} @@ -27,7 +27,7 @@ a.x # AttributeError: x is private # Hint: declare as `.x`. ``` -The above example may seem strange to someone familiar with JavaScript, but simply declaring `x` makes it inaccessible from the outside. `. `. `. +The above example may seem strange to someone familiar with JavaScript. Simply declaring `x` makes it inaccessible from the outside, and adding `.` makes it accessible from the outside. You can also explicitly specify the type of an attribute. @@ -135,7 +135,7 @@ empty_record: Structural {=} An empty record is different from an empty Dict `{:}` or empty set `{}`. In particular, note that it is the opposite of `{}` in meaning (in Python, `{}` is an empty dictionary, while in Erg it is `!{:}` in Erg). As an enumerated type, `{}` is an empty type that contains nothing in its elements. The `Never` type is a classification of this type. Conversely, the record class `{=}` has no required instance attribute, so all objects are elements of it. An `Object` is an alias of this. -An `Object` (a patch of `Object`) is an element of `. __sizeof__` and other very basic provided methods. +An `Object` (a patch of `Object`) is an element of `.__sizeof__` and other very basic provided methods. ```python AnyPatch = Patch Structural {=} @@ -194,6 +194,19 @@ print! john + 1 # TypeError: + is not implemented for Person, Int ``` +# Conversion with Dict + +Dict, whose key is a string, and record can be mutually converted. +To convert a dict to a record, use the `as_record` method, and to convert a record to a dict, use the `as_dict` method. +However, `as_record` cannot be used if the key string cannot be determined at compile time. Also, the visibility of record attributes converted with `as_record` will be public. + +```python +rec = {.name = "John Smith"; .age = 20} +dic = {"name": "John Smith", "age": 20} +assert rec.as_dict() == dic +assert rec == dic.as_record() +``` +

Previous | Next

diff --git a/doc/JA/syntax/11_dict.md b/doc/JA/syntax/11_dict.md index 6726f9654..6ede7750d 100644 --- a/doc/JA/syntax/11_dict.md +++ b/doc/JA/syntax/11_dict.md @@ -2,14 +2,14 @@ [![badge](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fgezf7g7pd5.execute-api.ap-northeast-1.amazonaws.com%2Fdefault%2Fsource_up_to_date%3Fowner%3Derg-lang%26repos%3Derg%26ref%3Dmain%26path%3Ddoc/EN/syntax/11_dict.md%26commit_hash%3De598201a939e24a41d3c26a828fdee01ad18eaf8)](https://gezf7g7pd5.execute-api.ap-northeast-1.amazonaws.com/default/source_up_to_date?owner=erg-lang&repos=erg&ref=main&path=doc/EN/syntax/11_dict.md&commit_hash=e598201a939e24a41d3c26a828fdee01ad18eaf8) -Dictはキーと値のペアを持つコレクションです。 +Dict(辞書)はキーと値のペアを持つコレクションです。 ```python ids = {"Alice": 145, "Bob": 214, "Charlie": 301} assert ids["Alice"] == 145 ``` -キーはHashableであるならば文字列でなくても構いません。 +キーはHashable(Hashトレイトを実装した型)であるならば文字列でなくても構いません。 ```python # rangeオブジェクトをキーにするのは非推奨(スライスと混同される) @@ -20,7 +20,7 @@ assert l[[]] == "empty" l = {0.0: "a", 1.0: "b"} # TypeError: Float is not Hashable ``` -Dictに順番は関係ありません。また、重複する要素を持つことも出来ません。この点でDictは[Set](./14_set.md)と似ています。 +Dictに順番は関係ありません。また、重複する要素を持つことも出来ません。この点でDictは[Set](./15_set.md)と似ています。 Dictは値付きのSetと言うこともできるでしょう。 ```python,compile_fail diff --git a/doc/JA/syntax/14_record.md b/doc/JA/syntax/14_record.md index d15a0f5de..53bd4e283 100644 --- a/doc/JA/syntax/14_record.md +++ b/doc/JA/syntax/14_record.md @@ -20,7 +20,7 @@ JavaScriptのオブジェクトリテラルとの相違点は、文字列でア これは、値へのアクセスをコンパイル時に決定するためと、辞書とレコードが別物であるためといった理由があります。つまり、`{"name": "John"}`はDict,`{name = "John"}`はレコードです。 では、辞書とレコードはどう使い分ければいいのでしょうか。 一般的にはレコードの使用を推奨します。レコードには、コンパイル時に要素が存在するかチェックされる、 __可視性(visibility)__ を指定できるなどのメリットがあります。 -可視性の指定は、Java言語などでみられるpublic/privateの指定に相当します。詳しくは[可視性](./19_visibility.md)を参照してください。 +可視性の指定は、Java言語などでみられるpublic/privateの指定に相当します。詳しくは[可視性](./21_visibility.md)を参照してください。 ```python,compile_fail a = {x = 1; .y = x + 1} @@ -54,8 +54,8 @@ o.inc!() assert o.i == 1 ``` -レコードに関して特筆すべき文法があります。レコードの属性値が全てクラス(構造型ではダメです)のとき、そのレコード自体が、自身の属性を要求属性とする型としてふるまいます。 -このような型をレコード型と呼びます。詳しくは[レコード]の項を参照してください。 +レコードに関して特筆すべき文法があります。レコードの属性値が全て型のとき、そのレコード自体が、自身の属性を要求属性とする型としてふるまいます。 +このような型をレコード型と呼びます。詳しくは[レコード型](type/20_compound.md#レコード型)の項を参照してください。 ```python # レコード @@ -196,6 +196,19 @@ print! john + 1 # TypeError: + is not implemented for Person, Int ``` +# Dictとの相互変換 + +キーが文字列のDictとレコードは相互に変換できます。 +Dictをレコードに変換したい場合は`as_record`メソッドを、レコードをDictに変換したい場合は`as_dict`メソッドを使います。 +ただし`as_record`はキー文字列がコンパイル時に決定できない場合は使えません。また、`as_record`で変換したレコード属性の可視性はpublicになります。 + +```python +rec = {.name = "John Smith"; .age = 20} +dic = {"name": "John Smith", "age": 20} +assert rec.as_dict() == dic +assert rec == dic.as_record() +``` +

Previous | Next