|
|
|
|
|
by nicoburns
2294 days ago
|
|
Rust is excellent for this. You can define: struct PositiveInteger(u32);
Then give it a constructor (which in rust is just a regular static function) that checks the non-zero variant. You can define method on this type, and make it implement traits (which are kind of like interfaces).The best bit: there is zero runtime cost to this, the memory-representation of this type is identical to that of the underlying u32. Rust-style enums which can contain data, and also have method implemented on them are even better. Doesn't mean classes (structs in Rust) aren't useful, but once you use a language that allows you to define other kinds of custom types, they seem very restrictive when they're the only available tool. |
|