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

This is the same glyph as the error channel in a function signature, doing the same job in miniature: this may fail, and here is the other path.

Chaining#

Conversions apply left to right.

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

Converting to a width#

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

reader locale
faber format --locale en — English reader surface
main {
    const int<i32> narrow  "255"int<i32>
    const f32 single  "1.5" ↦ f32
    print narrow, single
}
faber format --locale la — canonical Faber
incipit {
    fixum numerus<i32> narrow  "255"numerus<i32>
    fixum f32 single  "1.5" ↦ f32
    nota narrow, single
}
faber format --locale th-TH — Thai
เริ่ม {
    คงที่ จำนวน<i32> narrow  "255"จำนวน<i32>
    คงที่ f32 single  "1.5" ↦ f32
    บันทึก narrow, single
}
faber format --locale zh-Hans — Simplified Chinese
入口 {
    常量 整数<i32> narrow  "255"整数<i32>
    常量 f32 single  "1.5" ↦ f32
    显示 narrow, single
}
faber format --locale zh-Hant — Traditional Chinese
入口 {
    定值 整數<i32> narrow  "255"整數<i32>
    定值 f32 single  "1.5" ↦ f32
    註記 narrow, single
}
faber format --locale vi — Vietnamese
bắt_đầu {
    hằng số<i32> narrow  "255"số<i32>
    hằng f32 single  "1.5" ↦ f32
    ghi_chú narrow, single
}
faber format --locale ar — Arabic
بداية {
    ثابت عدد<i32> narrow  "255"عدد<i32>
    ثابت f32 single  "1.5" ↦ f32
    اعرض narrow, single
}
faber format --locale 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 existsNo
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