recursive types
Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans pack; supporting prose may still be English.
A 判别 or 类 may name itself in its own fields; every backend gives it a finite carrier.
Syntax: 判别 <name> { <Variant> { <name> <field> } } · 类 <name> { <name> ∪ 空类型 <field> }
Category#
type
Related#
Examples#
radix/corpus/recursio/expressio-arbor.fab (canonical · feature)#
A 判别 or 类 may name itself in its own fields; every backend gives it a finite carrier.
# =============================================================================
# recursive types — a type may refer to itself in its own fields.
# =============================================================================
#
# What this teaches:
# • Recursive 判别 — `Expr` variants hold `Expr` operands, so a value
# is a whole expression tree. `匹配` walks it recursively.
# • Recursive 类 — `Nodus` holds a nullable `Nodus` link, so a value is a
# linked list ending in `空类型`.
# • No keyword, no box type — Faber values alias by reference, so the
# indirection is implied. Backends that store fields inline (Rust) insert
# the pointer themselves on the fields that close the cycle.
#
# Common mistakes:
# • A non-nullable self field on a 类 (`Nodus next`) — no value can ever
# be built, because every node needs another node. End the chain with
# `∪ 空类型`, or recurse through a 判别 with a leaf variant.
#
# See also: 判别, 类, 匹配, 构造, 空类型
# =============================================================================
#
# EXPECTED OUTPUT:
# 14
# (2 + (3 * 4))
# -9
# 6
# 3
# 2
# 15
union Expr {
Numerus {
int 动态值
},
Nega {
Expr interior
},
Adde {
Expr sinister
Expr dexter
},
Multiplica {
Expr sinister
Expr dexter
}
}
fn evalua(Expr e) → int {
match e {
case Numerus const 动态值 {
return 动态值
}
case Nega const interior {
return 0 - evalua(interior)
}
case Adde const sinister, dexter {
return evalua(sinister) + evalua(dexter)
}
case Multiplica const sinister, dexter {
return evalua(sinister) * evalua(dexter)
}
}
}
fn depinge(Expr e) → string {
match e {
case Numerus const 动态值 {
return "§"(动态值)
}
case Nega const interior {
return "-§"(depinge(interior))
}
case Adde const sinister, dexter {
return "(§ + §)"(depinge(sinister), depinge(dexter))
}
case Multiplica const sinister, dexter {
return "(§ * §)"(depinge(sinister), depinge(dexter))
}
}
}
class Nodus {
var int 动态值
var Nodus ∪ none sequens
}
fn 求和(Nodus ∪ none n) → int {
if n is none {
return 0
}
return n.valor + 求和(n.sequens)
}
fn longitudo(Nodus ∪ none n) → int {
if n is none {
return 0
}
return 1 + longitudo(n.sequens)
}
main {
const Expr tres ← variant Numerus { 动态值 = 3 }
const Expr productum ← variant Multiplica { sinister = tres, dexter = variant Numerus { 动态值 = 4 } }
const Expr arbor ← variant Adde { sinister = variant Numerus { 动态值 = 2 }, dexter = productum }
print evalua(arbor)
print depinge(arbor)
print evalua(variant Nega { interior = variant Adde { sinister = tres, dexter = variant Numerus { 动态值 = 6 } } })
var Nodus caput ← Nodus { 动态值 = 1, sequens = Nodus { 动态值 = 2, sequens = Nodus { 动态值 = 3, sequens = null } } }
print 求和(caput)
print longitudo(caput)
print caput?.sequens?.valor
caput.sequens ← Nodus { 动态值 = 14, sequens = null }
print 求和(caput)
}Expected output:
14
(2 + (3 * 4))
-9
6
3
2
15