You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/jai-guide.md
+64-1Lines changed: 64 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -516,7 +516,70 @@ print("type = %", box.T); // you can quiry the type of a stuct like this (prints
516
516
517
517
## Interfaces / Traits / Constraints
518
518
519
-
todo `/`, `interface`, `$T/SomeStruct`, `$T/interface SomeStruct`
519
+
todo
520
+
521
+
uses things like `/`, `interface`, `$T/SomeStruct`, `$T/interface SomeStruct`
522
+
523
+
When working with polymorphic procedures and structs, there's a lightweight syntax for restricting the type of the argument. It looks like: `f :: (x: $T/SomeType) { ... }`
524
+
525
+
`$T/Object` indicates that the `$T` must be a parameterized struct of the type `Object`
526
+
527
+
```jai
528
+
HashTable :: struct (K: Type, V: Type, N: int) {
529
+
keys: [N]K;
530
+
values: [N]V;
531
+
}
532
+
533
+
function :: (table: $T/HashTable, key: T.K, value: T.V) {
534
+
// do stuff
535
+
}
536
+
537
+
// implicit polymorphism version
538
+
function :: (table: Table, key: table.K, value: table.V) {
539
+
// do stuff
540
+
}
541
+
```
542
+
543
+
`$T/interface` Object indicates that the `$T` must have the fields that `Object` has. `$T/interface` accepts only types that contain members declared in the target struct.
0 commit comments