mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-28 12:14:43 +00:00
127 lines
3 KiB
Markdown
127 lines
3 KiB
Markdown
# functions
|
|
|
|
## basic functions
|
|
|
|
### if|T, U|(cond: Bool, then: T, else: U) -> T or U
|
|
|
|
### map|T, U|(i: Iterable T, f: T -> U) -> Map U
|
|
|
|
Note that the order of arguments is reversed from Python.
|
|
|
|
### log(x: Object, type: LogType = Info) -> None
|
|
|
|
Log `x` in debug display. Logs are summarized and displayed after the execution is finished.
|
|
Emoji-capable terminals are prefixed according to `type`.
|
|
|
|
* type == Info: 💬
|
|
* type == Ok: ✅
|
|
* type == Warn: ⚠️
|
|
* type == Hint: 💡
|
|
|
|
### panic(msg: Str) -> Panic
|
|
|
|
Display msg and stop.
|
|
Emoji-capable terminals have a 🚨 prefix.
|
|
|
|
### discard|T|(*x: T) -> NoneType
|
|
|
|
Throw away `x`. Used when the return value is not used. Unlike `Del`, it does not make the variable `x` inaccessible.
|
|
|
|
```python
|
|
p! x =
|
|
# Let q! return some None or non-() value
|
|
# use `discard` if you don't need it
|
|
discard q!(x)
|
|
f x
|
|
|
|
discard True
|
|
assert True # OK
|
|
```
|
|
|
|
### import(path: Path) -> Module or CompilerPanic
|
|
|
|
Import a module. Raises a compilation error if the module is not found.
|
|
|
|
### eval(code: Str) -> Object
|
|
|
|
Evaluate code as code and return.
|
|
|
|
### classof(object: Object) -> Class
|
|
|
|
Returns the class of `object`.
|
|
However, since classes cannot be compared, use `object in Class` instead of `classof(object) == Class` if you want to judge instances.
|
|
The structure type determined at compile time is obtained with `Typeof`.
|
|
|
|
## Iterator, List generation system
|
|
|
|
### repeat|T|(x: T) -> RepeatIterator T
|
|
|
|
```python
|
|
rep = repeat 1 # Repeater(1)
|
|
for! rep, i =>
|
|
print!i
|
|
# 1 1 1 1 1 ...
|
|
```
|
|
|
|
### dup|T, N|(x: T, N: Nat) -> [T; N]
|
|
|
|
```python
|
|
[a, b, c] = dup new(), 3
|
|
print! a # <Object object>
|
|
print! a == b # False
|
|
```
|
|
|
|
### cycle|T|(it: Iterable T) -> CycleIterator T
|
|
|
|
```python
|
|
cycle([0, 1]).take 4 # [0, 1, 0, 1]
|
|
cycle("hello").take 3 # "hellohellohello"
|
|
```
|
|
|
|
## constant expression functions
|
|
|
|
### Class
|
|
|
|
Create a new class. Unlike 'Inherit', passing 'Class' is independent of the base type (the first argument 'Base') and the method is lost.
|
|
You won't be able to compare, but you can do things like pattern matching.
|
|
|
|
```python
|
|
C = Class {i = Int}
|
|
NewInt = ClassInt
|
|
Months = Class 1..12
|
|
jan = Months.new(1)
|
|
jan + Months.new(2) # TypeError: `+` is not implemented for 'Months'
|
|
match jan:
|
|
1 -> log "January"
|
|
_ -> log "Other"
|
|
```
|
|
|
|
### Inherit
|
|
|
|
Inherit classes. You can use the methods of the parent class ('Super') as is. The second parameter 'Layout' can specify a new layout.
|
|
It must be 'Super.Base:> Layout'.
|
|
|
|
```python
|
|
@ Inheritable
|
|
C = Class {i = Int}
|
|
D = Inherit C, {i = Int; j = Int} # C.Layout == {i = Int} :> {i = Int; j = Int}
|
|
E! = Inherit C, {i = Int!} # {i = Int} :> {i = Int!}
|
|
```
|
|
|
|
### Traits
|
|
|
|
Create a new trait. Currently, only record types can be specified.
|
|
|
|
### Type of
|
|
|
|
Returns the argument type. Use `classof` if you want to get the runtime class.
|
|
If you use it for type specification, Warning will appear.
|
|
|
|
```python,compile_warn
|
|
x: Type of i = ...
|
|
# TypeWarning: Typeof(i) == Int, please replace it
|
|
```
|
|
|
|
### Deprecated
|
|
|
|
Use as a decorator. Warn about deprecated types and functions.
|