erg/doc/EN/syntax/26_object_system.md
Cai BingJun e959b3e54b trifle
2023-01-07 19:20:12 +08:00

82 lines
No EOL
3.2 KiB
Markdown

# Object
All data that can be assigned to a variable. The attributes of the `Object` class are as follows.
* `.__repr__`: Returns a (non-rich) string representation of the object
* `.__sizeof__`: Returns the size of the object (including heap allocation)
* `.__dir__`: Returns a list of object attributes
* `.__hash__`: returns the hash value of the object
* `.__getattribute__`: Get and return an attribute of an object
* `.clone`: Creates and returns a clone of an object (with an independent entity in memory)
* `.copy`: Returns a copy of the object (pointing to the same thing in memory)
## Record
An object generated by a record literal (`{attr = value; ...}`).
This object has basic methods such as `.clone` and `.__sizeof__`.
```python
obj = {.x = 1}
assert obj.x == 1
obj2 = {...x; .y = 2}
assert obj2.x == 1 and obj2.y == 2
```
## Attribute
An object associated with an object. In particular, a subroutine attribute that takes self (`self`) as its implicit first argument is called a method.
```python
# note that there is no `.` in private_attr
record = {.public_attr = j; private_attr = 2; .method = self -> self.i + 1}
record. public_attr == 2
record.private_attr # AttributeError: private_attr is private
assert record.method() == 3
```
## Element
An object belonging to a particular type (e.g. `1` is an element of type `Int`). All objects are at least elements of type `{=}`.
Elements of classes are sometimes called instances.
## Subroutine
Indicates an object that is an instance of a function or procedure (including methods). The class representing a subroutine is `Subroutine`.
An object that implements `.__call__` is more commonly called a `Callable`.
## Callable
An object that implements `.__call__`. It is also the superclass of `Subroutine`.
## Type
An object that defines requirement attributes and commonizes objects.
There are two main types: Polymorphic Type and Monomorphic Type. Typical monomorphic types are `Int`, `Str`, etc., and polymorphic types are `Option Int`, `[Int; 3]`, etc.
Furthermore, a type that defines a method that changes the state of an object is called a Mutable type, and it is necessary to add `!` to the variable attribute (e.g. dynamic array: `[T; !_]`) .
## Class
A type that has `.__new__`, `.__init__` methods, etc. Implement class-based object orientation.
## Function
A subroutine that has read permission for external variables (excluding static variables) but does not have read/write permission for external variables. In other words, it has no external side effects.
Erg functions are defined differently than Python's because they do not allow side effects.
## Procedure
It has read and `self` permissions for external variables, read/write permissions for static variables, and is allowed to use all subroutines. It can have external side effects.
## Method
A subroutine that implicitly takes `self` as the first argument. It is a different type than a simple function/procedure.
## Entity
Objects that are not subroutines and types.
Monomorphic entities (`1`, `"a"`, etc.) are also called value objects, polymorphic entities (`[1, 2, 3], {"a": 1}`) are also called container objects .
<p align='center'>
<a href='./25_module.md'>Previous</a> | <a href='./27_pattern_matching.md'>Next</a>
</p>