recursive types
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
A union or class may name itself in its own fields; every backend gives it a finite carrier.
Syntax: union <name> { <Variant> { <name> <field> } } · class <name> { <name> ∪ none <field> }
Category#
type
Related#
Examples#
radix/corpus/recursio/expressio-arbor.fab (canonical · feature)#
A union or class 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 union — `Expr` variants hold `Expr` operands, so a value
# is a whole expression tree. `match` walks it recursively.
# • Recursive class — `Nodus` holds a nullable `Nodus` link, so a value is a
# linked list ending in `none`.
# • 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 class (`Nodus next`) — no value can ever
# be built, because every node needs another node. End the chain with
# `∪ none`, or recurse through a union with a leaf variant.
#
# See also: union, class, match, variant, none
# =============================================================================
#
# EXPECTED OUTPUT:
# 14
# (2 + (3 * 4))
# -9
# 6
# 3
# 2
# 15
union Expr {
Numerus {
int value
},
Nega {
Expr interior
},
Adde {
Expr sinister
Expr dexter
},
Multiplica {
Expr sinister
Expr dexter
}
}
fn evalua(Expr e) → int {
match e {
case Numerus const value {
return value
}
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 value {
return "§"(value)
}
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 value
var Nodus ∪ none sequens
}
fn sum(Nodus ∪ none n) → int {
if n is none {
return 0
}
return n.valor + sum(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 { value = 3 }
const Expr productum ← variant Multiplica { sinister = tres, dexter = variant Numerus { value = 4 } }
const Expr arbor ← variant Adde { sinister = variant Numerus { value = 2 }, dexter = productum }
print evalua(arbor)
print depinge(arbor)
print evalua(variant Nega { interior = variant Adde { sinister = tres, dexter = variant Numerus { value = 6 } } })
var Nodus caput ← Nodus { value = 1, sequens = Nodus { value = 2, sequens = Nodus { value = 3, sequens = null } } }
print sum(caput)
print longitudo(caput)
print caput?.sequens?.valor
caput.sequens ← Nodus { value = 14, sequens = null }
print sum(caput)
}Expected output:
14
(2 + (3 * 4))
-9
6
3
2
15