Renderingen-US

Generics

Generic functions#

Type parameters go in angle brackets after the name.

reader locale
faber convert --to en — English reader surface
fn identitas<T>(T datum) → T {
    return datum
}

main {
    print identitas("Marcus")
    print identitas(42)
}
faber convert --to la — canonical Faber
functio 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 — Vietnamese
hà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.

reader locale
faber convert --to en — English reader surface
fn 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 Faber
functio 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 — Vietnamese
hàm primum<T>(danh_sách<T> res) → T ∪ rỗng_ty {
    nếu res.độ_dài() ≡ 0 do_đó trả không_gì
    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.

reader locale
faber convert --to en — English reader surface
main {
    const list<string> nomina ← ["Marcus", "Julia"]
    const map<string, int> aetates ← { "Marcus": 30 }
    print nomina, aetates
}
faber convert --to la — canonical Faber
incipit {
    fixum lista<textus> nomina ← ["Marcus", "Julia"]
    fixum 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 — Vietnamese
bắt_đầu {
    hằng danh_sách<văn_bản> nomina ← ["Marcus", "Julia"]
    hằng 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
}

Records#

class declares a record type with type-first fields.

class Persona {
    const string name
    const int aetas
}

fn description(Persona p) → string {
    return p.nomen
}

main {
    const _ p ← Persona { name = "Marcus", aetas = 30 }
    print description(p)
}

Functions on a record#

A class can carry functions alongside its fields.

class Punctum {
    const int x
    const int y

    fn sum() → int {
        return self.x + self.y
    }
}

main {
    const _ 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