mirror of
https://github.com/erg-lang/erg.git
synced 2025-12-23 05:36:48 +00:00
72 lines
1.7 KiB
Python
72 lines
1.7 KiB
Python
json = pyimport "json"
|
|
|
|
arr = ["a"]
|
|
assert arr in List(Str)
|
|
assert arr in List(Str, 1)
|
|
assert arr notin List(Int)
|
|
assert arr notin List(Str, 2)
|
|
|
|
j = json.loads "{ \"a\": [1] }"
|
|
assert j in {Str: Obj}
|
|
assert j["a"] in List(Int)
|
|
assert j["a"] notin List(Str)
|
|
_: List(Int) = j["a"]
|
|
|
|
k = json.loads "{ \"a\": [1] }"
|
|
assert k in {Str: Obj}
|
|
assert k["a"] notin List(Str)
|
|
|
|
dic = {"a": "b", "c": "d"}
|
|
assert dic in {Str: {"b", "d"}}
|
|
assert dic in {Str: Str}
|
|
|
|
.f dic: {Str: Str or List(Str)} =
|
|
assert dic["key"] in Str # Required to pass the check on the next line
|
|
assert dic["key"] in {"a", "b", "c"}
|
|
assert dic["key2"] in List(Str)
|
|
|
|
b as Bytes or NoneType = bytes "aaa", "utf-8"
|
|
_ = if b != None:
|
|
do b.decode("utf-8")
|
|
do ""
|
|
|
|
s1 as Obj = "abc"
|
|
assert s1 in (Int or Str)
|
|
s2 as Obj = "abc"
|
|
assert s2 in (Str or Int)
|
|
s3 as Obj = "abc"
|
|
assert s3 in (Int or NoneType or Str)
|
|
|
|
p! as Obj = print!
|
|
assert p! in (*objs: Obj) => NoneType
|
|
p!("OK")
|
|
|
|
xs: [Nat or NoneType; _] = [1, None, 2]
|
|
ys: [Nat; _] = list filter x -> x != None, xs
|
|
nats _: [Nat; _] = None
|
|
nats ys
|
|
|
|
opt_i as Int or NoneType = 1
|
|
rec = { .opt_i; }
|
|
if rec.opt_i != None, do:
|
|
assert rec.opt_i.abs() == 1
|
|
|
|
ints_or_strs(r: {.x = Int; .y = Int} or {.x = Str; .y = Str}): Nat =
|
|
if r.x in Int:
|
|
do: r.y.abs()
|
|
do: 0
|
|
assert ints_or_strs({.x = 1; .y = 2}) == 2
|
|
|
|
int_or_strs(rec: { .x = Int } or { .x = Str; .y = Str }): Str =
|
|
if rec.x in Str:
|
|
do: rec.y
|
|
do: str rec.x
|
|
assert int_or_strs({.x = 1}) == "1"
|
|
|
|
C = Class { .foo = Int; .bar = Str }
|
|
D = Class { .baz = Str }
|
|
bar x: C or D =
|
|
if hasattr(x, "foo"):
|
|
do: x.bar
|
|
do: "?"
|
|
assert bar(C.new { .foo = 1; .bar = "bar" }) == "bar"
|