dot-shorthand and more
This commit is contained in:
@@ -277,6 +277,13 @@ SumBox :: struct ($T: Type/Summable) {
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Struct constants test
|
||||
Phys :: struct {
|
||||
x, y: f32;
|
||||
GRAVITY :f32: 9.81;
|
||||
MAX_SPEED :: 100;
|
||||
}
|
||||
|
||||
// Init block test struct
|
||||
Builder :: struct {
|
||||
total: s32;
|
||||
@@ -1702,6 +1709,15 @@ END;
|
||||
b := y ?? 99;
|
||||
print("coalesce a: {}\n", a); // coalesce a: 42
|
||||
print("coalesce b: {}\n", b); // coalesce b: 99
|
||||
|
||||
// Chained ?? (right-associative): a ?? b ?? c
|
||||
z: ?s32 = null;
|
||||
c := x ?? y ?? 0;
|
||||
d := z ?? y ?? 99;
|
||||
e := z ?? z ?? 0;
|
||||
print("chained ?? c: {}\n", c); // chained ?? c: 42
|
||||
print("chained ?? d: {}\n", d); // chained ?? d: 99
|
||||
print("chained ?? e: {}\n", e); // chained ?? e: 0
|
||||
}
|
||||
|
||||
// If-binding (safe unwrap)
|
||||
@@ -2902,6 +2918,15 @@ END;
|
||||
print("AE5: {}\n", acc.total);
|
||||
}
|
||||
|
||||
// --- Struct Constants ---
|
||||
print("=== Struct Constants ===\n");
|
||||
{
|
||||
print("gravity: {}\n", Phys.GRAVITY); // gravity: 9.810000
|
||||
print("max speed: {}\n", Phys.MAX_SPEED); // max speed: 100
|
||||
p := Phys.{ x = 0.0, y = Phys.GRAVITY };
|
||||
print("p.y: {}\n", p.y); // p.y: 9.810000
|
||||
}
|
||||
|
||||
// --- Init Blocks (IB) ---
|
||||
print("=== Init Blocks ===\n");
|
||||
|
||||
@@ -2954,5 +2979,43 @@ END;
|
||||
print("IB5: {}\n", result);
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// SECTION: Struct static method shorthand (.method(args) syntax)
|
||||
// ============================================================
|
||||
print("--- struct static method shorthand ---\n");
|
||||
|
||||
// SM1: Basic shorthand — .create(args) resolves to Dims.create(args)
|
||||
{
|
||||
Dims :: struct {
|
||||
w: f32;
|
||||
h: f32;
|
||||
|
||||
create :: (w: f32, h: f32) -> Dims {
|
||||
Dims.{ w = w, h = h };
|
||||
}
|
||||
|
||||
square :: (size: f32) -> Dims {
|
||||
Dims.{ w = size, h = size };
|
||||
}
|
||||
}
|
||||
use_dims :: (d: Dims) { print("SM1: {} {}\n", d.w, d.h); }
|
||||
use_dims(.create(16.0, 8.0));
|
||||
use_dims(.square(5.0));
|
||||
}
|
||||
|
||||
// SM2: Shorthand in variable declaration with explicit type
|
||||
{
|
||||
Pair :: struct {
|
||||
a: s64;
|
||||
b: s64;
|
||||
|
||||
make :: (a: s64, b: s64) -> Pair {
|
||||
Pair.{ a = a, b = b };
|
||||
}
|
||||
}
|
||||
p : Pair = .make(10, 20);
|
||||
print("SM2: {} {}\n", p.a, p.b);
|
||||
}
|
||||
|
||||
print("=== DONE ===\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user