erg/doc/zh_CN/API/funcs.md
GreasySlug c053fb55d0 doc(cn): update hash and contents
basically, copies from en doc
2024-05-20 00:26:01 +09:00

3.2 KiB
Raw Blame History

功能

badge

基本功能

if|T, U|(cond: Bool, then: T, else: U) -> T or U

map|T, U|(i: Iterable T, f: T -> U) -> Map U

请注意,参数的顺序与 Python 相反

log(x: Object, type: LogType = Info) -> None

在调试显示中记录"x"。执行完成后汇总并显示日志 支持表情符号的终端根据"类型"添加前缀

  • type == Info: 💬
  • type == Ok:
  • type == Warn: ⚠️
  • type == Hint: 💡

panic(msg: Str) -> Panic

显示msg并停止 支持表情符号的终端有一个🚨前缀

discard|T|(x: *T) -> NoneType

扔掉x。不使用返回值时使用。与 Del 不同,它不会使变量 x 不可访问

p! x =
    # q!应该返回一些不是None或()的值
    # 如果不需要,请使用`discard`
    discard q!(x)
    f x

discard True
assert True # OK

import(path: Path) -> Module or CompilerPanic

导入一个模块。如果找不到模块,则引发编译错误

eval(code: Str) -> Object

code作为代码进行评估并返回

classof(object: Object) -> Class

返回object的类 但是,由于无法比较类,如果要判断实例,请使用object in Class而不是classof(object) == Class 编译时确定的结构类型是通过Typeof获得的

Iterator, List生成系统

repeat|T|(x: T) -> RepeatIterator T

rep = repeat 1 # Repeater(1)
for! rep, i =>
    print! i
# 1 1 1 1 1 ...

dup|T, N|(x: T, N: Nat) -> [T; N]

[a, b, c] = dup new(), 3
print! a # <Object object>
print! a == b # False

cycle|T|(it: Iterable T) -> CycleIterator T

cycle([0, 1]).take 4 # [0, 1, 0, 1]
cycle("hello").take 3 # "hellohellohello"

定数式関数

Class

生成新类。 与Inherit不同,通过Class与基类型(第一个参数Base)无关,并且方法丢失。

C = Class {i = Int}
NewInt = Class Int
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

继承类可以直接使用父类(Super)的方法。可以在第二参数Layout中指定新的布局。 此时,必须Super.Base:> Layout

@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!}

Trait

创造一个新的trait。目前只能指定记录类型

Typeof

返回参数类型。如果要获取运行时类,请使用classof 如果您将其用于类型规范,则会出现警告

x: Typeof i = ...
# TypeWarning: Typeof(i) == Int, please replace it

Deprecated

作为解码器使用。警告不推荐使用的类型或函数