Renderingen-US

Conversions

↦ converts a value to another type at runtime. It parses, coerces, and can fail — which is exactly what distinguishes it from casting.

Converting#

reader locale
faber convert --to en — English reader surface
main {
    const int n ← "42" ↦ int
    const string s ← 42 ↦ string
    const f64 f ← "3.14159" ↦ f64
    print n, s, f
}
faber convert --to la — canonical Faber
incipit {
    fixum numerus n ← "42" ↦ numerus
    fixum textus s ← 42 ↦ textus
    fixum f64 f ← "3.14159" ↦ f64
    nota n, s, f
}
faber convert --to th-TH — Thai
เริ่ม {
    คงที่ จำนวน n ← "42" ↦ จำนวน
    คงที่ ข้อความ s ← 42 ↦ ข้อความ
    คงที่ f64 f ← "3.14159" ↦ f64
    บันทึก n, s, f
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    常量 整数 n ← "42" ↦ 整数
    常量 文本 s ← 42 ↦ 文本
    常量 f64 f ← "3.14159" ↦ f64
    显示 n, s, f
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    定值 整數 n ← "42" ↦ 整數
    定值 文字 s ← 42 ↦ 文字
    定值 f64 f ← "3.14159" ↦ f64
    註記 n, s, f
}
faber convert --to vi — Vietnamese
bắt_đầu {
    hằng số n ← "42" ↦ số
    hằng văn_bản s ← 42 ↦ văn_bản
    hằng f64 f ← "3.14159" ↦ f64
    ghi_chú n, s, f
}
faber convert --to ar — Arabic
بداية {
    ثابت عدد n ← "42" ↦ عدد
    ثابت نص s ← 42 ↦ نص
    ثابت f64 f ← "3.14159" ↦ f64
    اعرض n, s, f
}
faber convert --to hi — Hindi
आरंभ {
    स्थिर संख्या n ← "42" ↦ संख्या
    स्थिर पाठ s ← 42 ↦ पाठ
    स्थिर f64 f ← "3.14159" ↦ f64
    दिखाओ n, s, f
}

Recovery on failure#

⊥ after a conversion supplies the value to use when it fails. Without it, a failed conversion travels down the error channel.

reader locale
faber convert --to en — English reader surface
main {
    const int good ← "42" ↦ int ⊥ 0
    const int bad ← "not a number" ↦ int ⊥ 0
    print good, bad
}
faber convert --to la — canonical Faber
incipit {
    fixum numerus good ← "42" ↦ numerus ⊥ 0
    fixum numerus bad ← "not a number" ↦ numerus ⊥ 0
    nota good, bad
}
faber convert --to th-TH — Thai
เริ่ม {
    คงที่ จํานวน good ← "42" ↦ จํานวน ⊥ 0
    คงที่ จํานวน bad ← "not a number" ↦ จํานวน ⊥ 0
    บันทึก good, bad
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    常量 整数 good ← "42" ↦ 整数 ⊥ 0
    常量 整数 bad ← "not a number" ↦ 整数 ⊥ 0
    显示 good, bad
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    定值 整數 good ← "42" ↦ 整數 ⊥ 0
    定值 整數 bad ← "not a number" ↦ 整數 ⊥ 0
    註記 good, bad
}
faber convert --to vi — Vietnamese
bắt_đầu {
    hằng số good ← "42" ↦ số ⊥ 0
    hằng số bad ← "not a number" ↦ số ⊥ 0
    ghi_chú good, bad
}
faber convert --to ar — Arabic
بداية {
    ثابت عدد good ← "42" ↦ عدد ⊥ 0
    ثابت عدد bad ← "not a number" ↦ عدد ⊥ 0
    اعرض good, bad
}
faber convert --to hi — Hindi
आरंभ {
    स्थिर संख्या good ← "42" ↦ संख्या ⊥ 0
    स्थिर संख्या bad ← "not a number" ↦ संख्या ⊥ 0
    दिखाओ good, bad
}

⊥ is not the error channel. ⇥ only ever names the error type a function may fail with; ⊥ only ever holds a value. Read ⊥ 0 as on failure, 0.

Chaining#

Conversions apply left to right.

reader locale
faber convert --to en — English reader surface
main {
    const string roundtrip ← ("42" ↦ int) ↦ string
    print roundtrip
}
faber convert --to la — canonical Faber
incipit {
    fixum textus roundtrip ← ("42" ↦ numerus) ↦ textus
    nota roundtrip
}
faber convert --to th-TH — Thai
เริ่ม {
    คงที่ ข้อความ roundtrip ← ("42" ↦ จำนวน) ↦ ข้อความ
    บันทึก roundtrip
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    常量 文本 roundtrip ← ("42" ↦ 整数) ↦ 文本
    显示 roundtrip
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    定值 文字 roundtrip ← ("42" ↦ 整數) ↦ 文字
    註記 roundtrip
}
faber convert --to vi — Vietnamese
bắt_đầu {
    hằng văn_bản roundtrip ← ("42" ↦ số) ↦ văn_bản
    ghi_chú roundtrip
}
faber convert --to ar — Arabic
بداية {
    ثابت نص roundtrip ← ("42" ↦ عدد) ↦ نص
    اعرض roundtrip
}
faber convert --to hi — Hindi
आरंभ {
    स्थिर पाठ roundtrip ← ("42" ↦ संख्या) ↦ पाठ
    दिखाओ roundtrip
}

Converting to a width#

The target can name a precise width, not just a family.

reader locale
faber convert --to en — English reader surface
main {
    const i32 narrow ← "255" ↦ i32
    const f32 single ← "1.5" ↦ f32
    print narrow, single
}
faber convert --to la — canonical Faber
incipit {
    fixum i32 narrow ← "255" ↦ i32
    fixum f32 single ← "1.5" ↦ f32
    nota narrow, single
}
faber convert --to th-TH — Thai
เริ่ม {
    คงที่ i32 narrow ← "255" ↦ i32
    คงที่ f32 single ← "1.5" ↦ f32
    บันทึก narrow, single
}
faber convert --to zh-Hans — Simplified Chinese
入口 {
    常量 i32 narrow ← "255" ↦ i32
    常量 f32 single ← "1.5" ↦ f32
    显示 narrow, single
}
faber convert --to zh-Hant — Traditional Chinese
入口 {
    定值 i32 narrow ← "255" ↦ i32
    定值 f32 single ← "1.5" ↦ f32
    註記 narrow, single
}
faber convert --to vi — Vietnamese
bắt_đầu {
    hằng i32 narrow ← "255" ↦ i32
    hằng f32 single ← "1.5" ↦ f32
    ghi_chú narrow, single
}
faber convert --to ar — Arabic
بداية {
    ثابت i32 narrow ← "255" ↦ i32
    ثابت f32 single ← "1.5" ↦ f32
    اعرض narrow, single
}
faber convert --to hi — Hindi
आरंभ {
    स्थिर i32 narrow ← "255" ↦ i32
    स्थिर f32 single ← "1.5" ↦ f32
    दिखाओ narrow, single
}

Conversion is not casting#

Conversion ↦Casting
WhenRuntimeCompile time
Can failYes — that is why ⊥ and the error channel existNo
Means"Parse or coerce this into that""I already know this is that"

A conversion does work: it reads "42" and produces a number, and it has to cope with "banana". A cast asserts something to the compiler and produces no runtime behaviour. If a value might not be what you claim, you want ↦ and a recovery, not a cast.

Related: Types and widths · Errors and catching