|
| 1 | +--- |
| 2 | +slug: /type-system |
| 3 | +--- |
| 4 | + |
| 5 | +# Type System |
| 6 | + |
| 7 | +## Builtin Types |
| 8 | + |
| 9 | +Rue has a structural type system, which is similar to [duck typing](https://en.wikipedia.org/wiki/Duck_typing). One key difference is that types with different semantics must be cast, even if they have the same structure. |
| 10 | + |
| 11 | +### Any |
| 12 | + |
| 13 | +Makes no assumption about the structure of the value. Anything can be assigned to `Any`. |
| 14 | + |
| 15 | +```rue |
| 16 | +let value: Any = (14, 32); |
| 17 | +``` |
| 18 | + |
| 19 | +### List |
| 20 | + |
| 21 | +Represents a recursive structure of nested pairs. |
| 22 | + |
| 23 | +```rue |
| 24 | +let value: List<Int> = [1, 2, 3]; |
| 25 | +``` |
| 26 | + |
| 27 | +### Bytes |
| 28 | + |
| 29 | +Any atomic CLVM value can be represented as `Bytes`. However, only strings and hex literals are treated as `Bytes` by default. |
| 30 | + |
| 31 | +```rue |
| 32 | +let hex: Bytes = 0xFACE; |
| 33 | +let string: Bytes = "Hello"; |
| 34 | +``` |
| 35 | + |
| 36 | +When you add byte values together, they will be concatenated. |
| 37 | + |
| 38 | +### Bytes32 |
| 39 | + |
| 40 | +When an atomic value is exactly 32 bytes in length, it can be represented as `Bytes32`. This enhances type safety of things such as sha256 hashes. |
| 41 | + |
| 42 | +```rue |
| 43 | +let hash: Bytes32 = 0x38b1cec180a0bc0f5ec91097cec51971df126e3b18af54ddba4a3e4a36f9c285; |
| 44 | +``` |
| 45 | + |
| 46 | +### PublicKey |
| 47 | + |
| 48 | +When an atomic value is exactly 48 bytes in length, it can be represented as `PublicKey`. |
| 49 | + |
| 50 | +More specifically, `PublicKey` is a [BLS12-381](https://en.wikipedia.org/wiki/BLS_digital_signature#BLS12-381) G1Element. |
| 51 | + |
| 52 | +```rue |
| 53 | +let pk: PublicKey = 0xb3596acaa39f19956f77b84cef87a684ea0fec711e6ec9e55df3cffd4a6e05d3e2da842433dccb6042ee35c14d892206; |
| 54 | +``` |
| 55 | + |
| 56 | +When you add public keys together, it will call the CLVM `g1_add` operator (formerly known as `point_add`). |
| 57 | + |
| 58 | +### Int |
| 59 | + |
| 60 | +Any atomic CLVM value can be represents as `Int`. |
| 61 | + |
| 62 | +```rue |
| 63 | +let num: Int = 42; |
| 64 | +``` |
| 65 | + |
| 66 | +You can perform standard arithmetic on integers, as well as bitwise math: |
| 67 | + |
| 68 | +```rue |
| 69 | +let a: Int = ((42 * 34) / 8 + 9 - -10) % 16; |
| 70 | +let b: Int = ((100 >> 3) << 2) & 420 | ~6; |
| 71 | +``` |
| 72 | + |
| 73 | +### Bool |
| 74 | + |
| 75 | +A boolean is either `true` or `false`. |
| 76 | + |
| 77 | +```rue |
| 78 | +let flag: Bool = true; |
| 79 | +``` |
| 80 | + |
| 81 | +You can use logical operators on booleans: |
| 82 | + |
| 83 | +```rue |
| 84 | +let value: Bool = (true && false) || true; |
| 85 | +``` |
| 86 | + |
| 87 | +### nil |
| 88 | + |
| 89 | +The simplest type of them all, `nil` only has one value: |
| 90 | + |
| 91 | +```rue |
| 92 | +let value: nil = nil; |
| 93 | +``` |
| 94 | + |
| 95 | +## Inference |
| 96 | + |
| 97 | +In many cases, the type of variables can be inferred based on their value. |
| 98 | + |
| 99 | +For example: |
| 100 | + |
| 101 | +```rue |
| 102 | +let value = 42; |
| 103 | +``` |
| 104 | + |
| 105 | +Here, `value` is known to be `Int` even though it wasn't specified. It's generally cleaner to omit the type in places where it's obvious. |
| 106 | + |
| 107 | +## Casting |
| 108 | + |
| 109 | +### Structural Cast |
| 110 | + |
| 111 | +You can cast the type of a value with the `as` keyword. This can only be done as long as both types are structurally related. |
| 112 | + |
| 113 | +For example, casting an `Int` to `Bytes` is fine since both of them are atomic values: |
| 114 | + |
| 115 | +```rue |
| 116 | +let value = 42; |
| 117 | +let bytes = 42 as Bytes; |
| 118 | +``` |
| 119 | + |
| 120 | +You can cast more complicated types such as pairs: |
| 121 | + |
| 122 | +```rue |
| 123 | +let pair = ("Hello, ", "world!"); |
| 124 | +let casted = pair as (Int, Int); |
| 125 | +``` |
| 126 | + |
| 127 | +However, this is **not** allowed, since the structure would differ (and this could cause runtime errors): |
| 128 | + |
| 129 | +```rue |
| 130 | +let pair = (42, 34); |
| 131 | +let num = pair as Int; // Type error |
| 132 | +``` |
| 133 | + |
| 134 | +### Reinterpret Cast |
| 135 | + |
| 136 | +Sometimes, you need to change the type of a variable without the type system getting in the way. |
| 137 | + |
| 138 | +:::warning |
| 139 | +This is unsafe and could result in hard to debug issues at runtime if you are not careful. It should be scrutinized when auditing Rue code. |
| 140 | +::: |
| 141 | + |
| 142 | +There is a built in function called `cast` which will do this: |
| 143 | + |
| 144 | +```rue |
| 145 | +let pair = (42, 34); |
| 146 | +let num = cast::<Int>(pair); // Ok |
| 147 | +``` |
0 commit comments