Types
Beans statically typed, আর এখানে কোনো inference নেই: প্রতিটা binding, parameter, আর field তার type বলে দেয়। এই পেজটা শেখানোর জন্য একটা overview। পুরো signature দেখতে চাইলে builtin reference দেখুন।
Primitive type
“Primitive type” সেকশনসব primitive generated code-এ unboxed, আর সবগুলোরই method আছে ((-5).abs(),
3.7.round())।
- Integer:
int(64-bit signed),i8 i16 i32 i64,uint(64-bit unsigned),u8 u16 u32 u64, আরbyte(=u8)। - Float:
float(=f64),f32 f64। decimal: exact base-10 number, টাকা-পয়সার জন্য (নিচে দেখুন)।bool,string(immutable UTF-8)।
কোনো usize/isize নেই: word type হলো int আর uint। পুরো বিস্তারিত:
Primitive types।
Number-এর নিয়ম
“Number-এর নিয়ম” সেকশনBeans নিজে থেকে কখনও number convert করে না। নিয়মগুলো ছোট আর কড়া:
- একটা literal যে জায়গায় বসছে, সেটা যে type চায় সেটাই নেয়:
let p: decimal = 19.99একটা decimal;let f: f64 = 19.99একটা float। কোনো numeric suffix নেই। - কেউ কিছু না চাইলে, integer literal হয়
intআর decimal-point literal হয়f64। - কখনও implicit numeric conversion নেই। type মেলাতে
asদিতে হয়:price * (qty as decimal)। - একটা integer literal যে জায়গায় বসছে, সেই type-এ ধরতে হবে। ওই type-এর exact range-এর বাইরের value checker reject করে।
- fixed-width integer-এর
+,-,*, unary-, আর bit operation ওই width-এ wrap করে। zero দিয়ে divide বা modulo করলে panic হয়।
আরও: Numbers and decimal আর Operators and precedence।
decimal
“decimal” সেকশনdecimal হলো exact base-10 number। 0.1 + 0.2 হয় ঠিক 0.3, কোনো rounding error
ছাড়া — এই কারণেই টাকা-পয়সার প্রতিটা value-র জন্য এই type-টা ব্যবহার করা হয়। টাকার
অঙ্কে float-এর বদলে সবসময় decimal বেছে নেওয়া হয়।
let price: decimal = 19.99let qty: int = 3let total: decimal = price * (qty as decimal) // 59.97, exactly1.0 contract-এ থাকে 38টা significant digit; এর বাইরে গেলে decimal overflow
বলে panic হয়। division-এ সর্বোচ্চ 38টা significant digit পাওয়া যায়, আর শেষটা
half-even round হয়। call site-এ নিজের পছন্দমতো mode বেছে round করা যায়:
total.round(2, RoundingMode.half_even)।
Collection
“Collection” সেকশনvar xs: List<int> = [1, 2, 3]var m: Map<string, int> = {"a": 1, "b": 2}var ordered: OrderedMap<string, int> = {}xs.push(4)let got: Option<int> = m.get("a") // no null, no panicList, Map, আর OrderedMap generic আর move-only। bracket read চেক করা হয়:
index range-এর বাইরে গেলে xs[i] panic করে, আর key না থাকলে m[key] panic করে।
যেখানে absence স্বাভাবিক, সেখানে .get(...) ব্যবহার করা হয় — এটা একটা Option ফেরত
দেয়। পুরো বিস্তারিত: Collections।
কোনো null নেই, কোনো exception নেই
“কোনো null নেই, কোনো exception নেই” সেকশনBeans-এ null নেই, exception নেই। absence-এর জন্য Option<T>; failure-এর জন্য
Result<T>। দেখুন Option and Result।
নিজের বানানো type
“নিজের বানানো type” সেকশননিজের type তৈরি করা হয় এগুলো দিয়ে:
- Classes: method,
init/deinit, inheritance, আর interface-সহ reference object। - Structs and unions: inline value type, C-layout record-ও।
- Enums: tagged variant,
match-এর জন্য বানানো। - Generics: interface bound-সহ type parameter।