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
Jai has no header files. Code organization is handled by `#import` and `#load` instead.
188
188
There is no need for `#include` or header guards in Jai, because multiple `#load` calls dont cause duplicate symbols inclusion.
@@ -279,23 +279,23 @@ result := action(1, 2);
279
279
280
280
## Casting
281
281
282
-
The jai syntax is similar to c in this regard, in c it would be `(T)value` while in jai its `cast(T)value`, it only adds the cast word and an extra space.
282
+
The jai syntax is similar to c in this regard, in c it would be `(T)value` while in jai its `cast(T)value`, it only adds the cast word.
283
283
Jai is strict about casts. Implicit widening is allowed, but narrowing requires an explicit cast.
284
284
285
285
Example:
286
286
```jai
287
287
a: u32 = 50000;
288
-
b: u16 = cast(u16)a;
288
+
b: u16 = cast(u16)a;
289
289
```
290
290
291
291
For truncation or unchecked casts, use the `trunc` or `no_check` flags.
292
-
Also Jai uses comma-separated “attributes” like this: `operation,modifier1,modifier2(type)value`:
292
+
Jai uses comma-separated “attributes” like this: `operation,modifier1,modifier2(type)value`:
293
293
```jai
294
-
b = cast,trunc(u16)a;
295
-
b = cast,no_check(u16)a;
294
+
b = cast,trunc(u16)a;
295
+
b = cast,no_check(u16)a;
296
296
```
297
297
298
-
Use `xx` when you want the compiler to infer the target type:
298
+
Use `xx` when you want the compiler to infer the target type to cast to:
299
299
```jai
300
300
b = xx a;
301
301
```
@@ -304,7 +304,7 @@ b = xx a;
304
304
305
305
Jai supports operator overloading for many operators: `+`, `-`, `*`, `/`, `%`, `==`, `!=`, `<<`, `>>`, `[]`, and more.
306
306
307
-
Jai syntax follows this rule: `operator + :: implementation`, where `+` can be any other operator. Unlike C, Jai needs spaces around the operator symbol.
307
+
Jai syntax follows this rule: `operator + :: implementation`, where `+` can be any other operator.
in jai a normal array is typed like ``[size]T``, and a slice like ``[]T``, and a dynamic array like ``[..]T``.
350
340
351
-
Static arrays expose `.count`:
352
-
```jai
353
-
print("count = %\n", arr.count);
354
-
```
341
+
unlike in c, arrays store their length in jai, you can get it by the `array.count` member.
355
342
356
-
Multi-dimensional arrays are nested:
357
-
```jai
358
-
matrix : [4][4]float;
359
-
```
343
+
you can get the memory adress of the first element in an array using the `array.data` member.
360
344
361
-
Dynamic arrays are similar to `std::vector` but use Jai’s allocator and context system.
345
+
Both Static Arrays and Dynamic Arrays are autocasted to Array Views if the array view is a parameter. Because strings are array views with u8, both share the same definition.
362
346
363
-
## Array Pointer Decay
347
+
Arrays in jai are simply a tiny struct that holds the size and location of where the array actually is in memory. so copying an array directly makes an array that points to the same data.
364
348
365
-
Jai static arrays do not decay to pointers the way C/C++ arrays do.
366
-
Instead, an array has a `.data` field for its backing pointer.
349
+
You can initialize arrays using the following syntax:
367
350
368
-
Example:
369
351
```jai
370
-
arr : [5]int;
371
-
ptr : *int = arr.data;
352
+
array: [4]float = float.[10.0, 20.0, 1.4, 10.0];
372
353
```
373
354
374
-
If you need a view, use a separate array view or dynamic array type.
375
-
376
-
## Generics / Templates
377
-
378
-
Jai uses compile-time polymorphism with `$T` and `Type`.
379
-
380
-
Generic function:
355
+
regular arrays:
381
356
```jai
382
-
foo :: (x: $T) {
383
-
print("%\n", x);
384
-
}
357
+
// simple array
358
+
array: [8]int; // create
359
+
value: int = array[0]; // index
385
360
```
386
361
387
-
Polymorphic struct:
362
+
dynamic arrays:
388
363
```jai
389
-
Box :: struct(T: Type) {
390
-
value: T;
391
-
};
364
+
// create dynamic array
365
+
array: [..]int;
392
366
393
-
b : Box(int);
394
-
b.value = 5;
395
-
```
367
+
// get length of dynamic array
368
+
length: int = array.count;
396
369
397
-
You can constrain polymorphic types with `/` and `interface` syntax.
370
+
// add to the dynamic array
371
+
array_add(*array, 4);
398
372
399
-
## Strings
373
+
// remove third element from the dynamic array
400
374
401
-
Jai strings are views over `u8`.
375
+
// index the dynamic array
376
+
value: int = array[0];
402
377
403
-
Examples:
404
-
```jai
405
-
s := "hello";
406
-
print("%\n", s);
378
+
// clear the dynamic array
379
+
array_reset(*array);
407
380
```
408
381
409
-
Common helpers include `join`, `split`, `equal`, `compare`, `contains`, `begins_with`, and `ends_with`.
410
-
411
-
`to_c_string` allocates a C-style string on the heap, and `c_style_strlen` computes its length.
412
-
413
-
## Immutables And Statics
414
-
415
-
Jai constants are created with `::`.
416
-
417
-
Example:
382
+
slices (array views):
418
383
```jai
419
-
PI :: 3.141592;
420
-
```
384
+
arr: []int = int.[1,2,3,4,5]; // represents a view into the data that is contained in an array or a subsection of an array
421
385
422
-
There is no `readonly` or `constexpr` keyword in the same way as C++.
386
+
```
423
387
424
-
File-local scope is expressed with `#scope_file`, while exported symbols use `#scope_export`.
388
+
multi dimensional arrays:
389
+
```jai
390
+
array: [2][2]int; // creating a 2D static array
391
+
array: [2][2][2]int; // creating a 3D static array
array: [2][2]int = .[.[1, 0], .[0, 3]]; // initializing a 2D array with inferred type
427
395
428
-
Not applicable in Jai: there are no header files, so there is no need for header guards.
396
+
value: int = array[0][0]; // indexing a 2D array
397
+
```
429
398
430
-
## Optional Header Only Libraries
399
+
## Array Pointer Decay
431
400
432
-
Not applicable in Jai either.
433
-
Jai libraries are modules and source files, not header-only libraries.
401
+
Jai static arrays do not decay to pointers the way C/C++ arrays do.
402
+
Instead, an array has a `.data` field for its backing pointer.
434
403
435
-
## Preprocessor
404
+
## Polymorphism (Generics)
436
405
437
-
Jai has compile-time directives rather than a traditional C preprocessor.
406
+
In jai generics is called polymorphism.
438
407
439
-
Common directives:
440
-
-`#import`
441
-
-`#load`
442
-
-`#if`, `#else`, `#elif`, `#endif`
443
-
-`#exists`
444
-
-`#run`
445
-
-`#type`
446
-
-`#add_context`
408
+
Jai’s generics are compile-time polymorphism using `$T` and `Type` parameters.
447
409
448
-
Example:
410
+
Generic function:
449
411
```jai
450
-
#if DEBUG {
451
-
print("debug build\n");
412
+
foo :: (x: $T) {
413
+
print("%\n", x);
452
414
}
453
-
```
454
-
455
-
`#exists` lets you ask whether a symbol is available at compile time.
456
415
457
-
## Function Pointers
458
-
459
-
Function pointers are declared much like function types.
416
+
foo(1);
417
+
foo("hello");
418
+
```
460
419
461
-
Example:
420
+
Generic function with multiple types:
462
421
```jai
463
-
type_fn : (int, int) -> int;
464
-
add :: (a: int, b: int) -> int { return a + b; }
465
-
fn : type_fn = add;
466
-
result := fn(1, 2);
422
+
foo :: (a: $A, b: $B, c: $C) {
423
+
// use a or b or c here
424
+
}
467
425
```
468
426
469
-
## Member Definition Outside The Class
470
-
471
-
Jai does not have separate class declarations and definitions.
472
-
There is no equivalent to `ClassName::MethodName()` because Jai does not use C++-style classes.
473
-
474
-
## Member Initializer List
427
+
Polymorphic structs:
428
+
```jai
429
+
Box :: struct(T: Type) {
430
+
value: T;
431
+
};
475
432
476
-
Jai has no constructor member initializer list.
477
-
Structs are initialized directly, and fields are assigned via aggregate initialization or explicit assignment.
433
+
b: Box(int);
434
+
b.value = 5;
435
+
print("type = %", a.T); // you can quiry the type of a stuct like this (prints out "type = int")
436
+
```
478
437
479
-
## L-values And R-values
438
+
Jai also supports type constraints such as `$T/SomeStruct` and `$T/interface SomeStruct`, which are similar to traits or interfaces.
480
439
481
-
Jai mostly treats named variables as addressable values and expressions as temporaries.
440
+
## Interfaces / Traits
482
441
483
-
A variable like `x` is addressable, while `x + 1` is a temporary expression.
442
+
todo
484
443
485
-
## Move Semantics
444
+
You can constrain polymorphic types with `/` and `interface` syntax.
486
445
487
-
Jai does not have C++ move constructors or rvalue references.
488
-
Ownership is explicit: either copy values, or use pointers and allocators.
446
+
## Strings
489
447
490
-
Example:
491
-
```jai
492
-
value := big_data;
493
-
copy := value; // copies the value if needed
494
-
```
448
+
todo
495
449
496
-
For large buffers, prefer pointers or custom allocator-based containers.
450
+
Jai strings are are array views (slices) over `u8`, arrays are also not null terminated.
497
451
498
-
## Notes
452
+
## Compile Time Directives (Preprocessor)
499
453
500
-
This guide matches the structure of the C++ reference guide, while explaining Jai’s equivalent concepts and the places where Jai intentionally diverges.
501
-
Multi-dimensional arrays work too:
502
-
```jai
503
-
matrix : [4][4]float;
504
-
```
454
+
todo
505
455
506
-
## Polymorphism
456
+
## Function Pointers / Function Types
507
457
508
-
Jai’s generics are compile-time polymorphism using `$T` and `Type` parameters.
458
+
Function pointers are declared much like function types.
509
459
510
-
A simple polymorphic function:
460
+
Example:
511
461
```jai
512
-
foo :: (x: $T) {
513
-
print("%\n", x);
514
-
}
462
+
add :: (a: int, b: int) -> int { return a + b; }
515
463
516
-
foo(1);
517
-
foo("hello");
518
-
```
464
+
function_type: (int,int)->int;
465
+
function: function_type = add;
519
466
520
-
A polymorphic struct:
521
-
```jai
522
-
Box :: struct(T: Type) {
523
-
value: T;
524
-
};
467
+
function: (int,int)->int = add; // also works
525
468
526
-
b : Box(int);
527
-
b.value = 5;
469
+
result := function(1, 2);
528
470
```
529
471
530
-
Jai also supports type constraints such as `$T/SomeStruct` and `$T/interface SomeStruct`, which are similar to traits or interface-based matching.
531
-
532
-
## Modules and External Libraries
533
-
534
-
Jai uses modules instead of headers. You can import bundled modules with `#import` and local code with `#load`.
472
+
## External Libraries / Interop
535
473
536
474
For external libraries, Jai can map foreign functions and dynamic libraries using `#library` and `#foreign` declarations.
0 commit comments