Conversions
↦ converts a value to another type at runtime. It parses, coerces, and can
fail — which is exactly what distinguishes it from casting.
Converting#
faber format --locale en — English reader surfacemain {
const int n ← "42" ↦ int
const string s ← 42 ↦ string
const f64 f ← "3.14159" ↦ f64
print n, s, f
}faber format --locale la — canonical Faberincipit {
fixum numerus n ← "42" ↦ numerus
fixum textus s ← 42 ↦ textus
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 — Vietnamesebắ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 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.
faber format --locale en — English reader surfacemain {
const int good ← "42" ↦ int ⇥ 0
const int bad ← "not a number" ↦ int ⇥ 0
print good, bad
}faber format --locale la — canonical Faberincipit {
fixum numerus good ← "42" ↦ numerus ⇥ 0
fixum numerus bad ← "not a number" ↦ numerus ⇥ 0
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 — Vietnamesebắ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.
faber format --locale en — English reader surfacemain {
const string roundtrip ← ("42" ↦ int) ↦ string
print roundtrip
}faber format --locale la — canonical Faberincipit {
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 — Vietnamesebắ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.
faber format --locale en — English reader surfacemain {
const int<i32> narrow ← "255" ↦ int<i32>
const f32 single ← "1.5" ↦ f32
print narrow, single
}faber format --locale la — canonical Faberincipit {
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 — Vietnamesebắ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 | |
|---|---|---|
| When | Runtime | Compile time |
| Can fail | Yes — that is why ⇥ exists | No |
| 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