Generics
Generic functions#
Type parameters go in angle brackets after the name.
faber convert --to en — English reader surfacefn identitas<T>(T datum) → T {
return datum
}
main {
print identitas("Marcus")
print identitas(42)
}faber convert --to la — canonical Faberfunctio identitas<T>(T datum) → T {
redde datum
}
incipit {
nota identitas("Marcus")
nota identitas(42)
}faber convert --to th-TH — Thaiฟังก์ชัน identitas<T>(T datum) → T {
คืน datum
}
เริ่ม {
บันทึก identitas("Marcus")
บันทึก identitas(42)
}faber convert --to zh-Hans — Simplified Chinese函数 identitas<T>(T datum) → T {
返回 datum
}
入口 {
显示 identitas("Marcus")
显示 identitas(42)
}faber convert --to zh-Hant — Traditional Chinese函式 identitas<T>(T datum) → T {
傳回 datum
}
入口 {
註記 identitas("Marcus")
註記 identitas(42)
}faber convert --to vi — Vietnamesehàm identitas<T>(T datum) → T {
trả datum
}
bắt_đầu {
ghi_chú identitas("Marcus")
ghi_chú identitas(42)
}faber convert --to ar — Arabicدالة identitas<T>(T datum) → T {
أعد datum
}
بداية {
اعرض identitas("Marcus")
اعرض identitas(42)
}faber convert --to hi — Hindiफलन identitas<T>(T datum) → T {
लौटाओ datum
}
आरंभ {
दिखाओ identitas("Marcus")
दिखाओ identitas(42)
}The same function serves both calls. T is fixed per call site, not erased to
a dynamic value.
Generic with a union return#
Type parameters compose with unions, which is how "maybe nothing" is expressed generically.
faber convert --to en — English reader surfacefn primum<T>(list<T> res) → T ∪ none {
if res.length() ≡ 0 then return null
return res[0]
}
main {
print primum([1, 2, 3])
}faber convert --to la — canonical Faberfunctio primum<T>(lista<T> res) → T ∪ nihil {
si res.longitudo() ≡ 0 ergo redde nulla
redde res[0]
}
incipit {
nota primum([1, 2, 3])
}faber convert --to th-TH — Thaiฟังก์ชัน primum<T>(รายการ<T> res) → T ∪ ไม่มี {
ถ้า res.ความยาว() ≡ 0 ดังนั้น คืน ขาดหาย
คืน res[0]
}
เริ่ม {
บันทึก primum([1, 2, 3])
}faber convert --to zh-Hans — Simplified Chinese函数 primum<T>(列表<T> res) → T ∪ 无 {
如果 res.长度() ≡ 0 则 返回 缺失
返回 res[0]
}
入口 {
显示 primum([1, 2, 3])
}faber convert --to zh-Hant — Traditional Chinese函式 primum<T>(列表<T> res) → T ∪ 無 {
若 res.長度() ≡ 0 則 傳回 缺失
傳回 res[0]
}
入口 {
註記 primum([1, 2, 3])
}faber convert --to vi — Vietnamesehàm primum<T>(danh_sách<T> res) → T ∪ hư_không {
nếu res.độ_dài() ≡ 0 do_đó trả vắng
trả res[0]
}
bắt_đầu {
ghi_chú primum([1, 2, 3])
}faber convert --to ar — Arabicدالة primum<T>(قائمة<T> res) → T ∪ لاشيء {
إذا res.طول() ≡ 0 إذن أعد غائب
أعد res[0]
}
بداية {
اعرض primum([1, 2, 3])
}faber convert --to hi — Hindiफलन primum<T>(सूची<T> res) → T ∪ कुछनहीं {
यदि res.लंबाई() ≡ 0 अतः लौटाओ अभाव
लौटाओ res[0]
}
आरंभ {
दिखाओ primum([1, 2, 3])
}Generic containers#
The built-in collections are generic already.
faber convert --to en — English reader surfacemain {
let list<string> nomina ← ["Marcus", "Julia"]
let map<string, int> aetates ← { "Marcus": 30 }
print nomina, aetates
}faber convert --to la — canonical Faberincipit {
sit lista<textus> nomina ← ["Marcus", "Julia"]
sit tabula<textus, numerus> aetates ← { "Marcus": 30 }
nota nomina, aetates
}faber convert --to th-TH — Thaiเริ่ม {
กำหนด รายการ<ข้อความ> nomina ← ["Marcus", "Julia"]
กำหนด ตาราง<ข้อความ, จํานวน> aetates ← { "Marcus": 30 }
บันทึก nomina, aetates
}faber convert --to zh-Hans — Simplified Chinese入口 {
设 列表<文本> nomina ← ["Marcus", "Julia"]
设 映射<文本, 整数> aetates ← { "Marcus": 30 }
显示 nomina, aetates
}faber convert --to zh-Hant — Traditional Chinese入口 {
設為 列表<文字> nomina ← ["Marcus", "Julia"]
設為 表格<文字, 整數> aetates ← { "Marcus": 30 }
註記 nomina, aetates
}faber convert --to vi — Vietnamesebắt_đầu {
đặt danh_sách<văn_bản> nomina ← ["Marcus", "Julia"]
đặt bảng<văn_bản, số> aetates ← { "Marcus": 30 }
ghi_chú nomina, aetates
}faber convert --to ar — Arabicبداية {
ليكن قائمة<نص> nomina ← ["Marcus", "Julia"]
ليكن جدول<نص, عدد> aetates ← { "Marcus": 30 }
اعرض nomina, aetates
}faber convert --to hi — Hindiआरंभ {
बैठा सूची<पाठ> nomina ← ["Marcus", "Julia"]
बैठा तालिका<पाठ, संख्या> aetates ← { "Marcus": 30 }
दिखाओ nomina, aetates
}Classes#
class (en class) declares a class with type-first fields. Each field names its
mode: let is fixed after construction and mut is writable. A field with
neither word is PARSE010 field_modifier_missing, and a ref field is
PARSE010 ref_field_refused.
class Persona {
let string nomen
let int aetas
}
fn description(Persona p) → string {
return p.nomen
}
main {
let _ p ← Persona { nomen = "Marcus", aetas = 30 }
print description(p)
}Functions on a class#
A class can carry functions alongside its fields.
class Punctum {
let int x
let int y
fn sum() → int {
return self.x + self.y
}
}
main {
let _ p ← Punctum { x = 1, y = 2 }
print p.summa()
}self — "I" — is the receiver, what other languages spell this or self.
Related: Types and widths · Functions and flow