#array literal requires address-of operator (&) confusion

1 messages · Page 1 of 1 (latest)

alpine hearth
#

Given this declaration:

pub fn drawSplineCatmullRom(points: []Vector2, pointCount: i32, thick: f32, color: Color) void {

I am testing this code:

const rl = @import("raylib");
const p0 = rl.Vector2{ .x = 0.0, .y = 0.0 };
const p1 = rl.Vector2{ .x = 160.0, .y = 220.0 };
const p2 = rl.Vector2{ .x = 340.0, .y = 380.0 };
const p3 = rl.Vector2{ .x = 480.0, .y = 90.0 };
const p4 = rl.Vector2{ .x = 700.0, .y = 60.0 };
const points = [_]rl.Vector2{p0, p1, p2, p3, p4};
rl.drawSplineCatmullRom(points, 5, 10, rl.Color.black);

and getting this error:

src/toolbox.zig:39:29: error: array literal requires address-of operator (&) to coerce to slice type '[]raylib.Vector2'
    rl.drawSplineCatmullRom(points, 5, 10, rl.Color.black);

Then, modifying the call to this:

rl.drawSplineCatmullRom(&points, 5, 10, rl.Color.black);

I get that:

src/toolbox.zig:39:29: error: expected type '[]raylib.Vector2', found '*const [5]raylib.Vector2'
    rl.drawSplineCatmullRom(&points, 5, 10, rl.Color.black);
                            ^~~~~~~
src/toolbox.zig:39:29: note: cast discards const qualifier
lib/raylib.zig:2357:37: note: parameter type declared here
pub fn drawSplineCatmullRom(points: []Vector2, pointCount: i32, thick: f32, color: Color) void {
                                    ^~~~~~~~~

I am reading the documentation on arrays and slices but these error messages are confusing. What is my call missing ?

quaint estuary
#

Took a while for me as well, but note that the important difference relies on the *const vs *

Just declare points as var instead of const. The error message could improve imo

alpine juniper
#

Wonder if the coercion rule is documented: *[N]T (mutable pointer to array) coerces to []T (mutable slice) and similar for const array pointer/slice

alpine hearth
#

Changed to this:

const p0 = rl.Vector2{ .x = 0.0, .y = 0.0 };
const p1 = rl.Vector2{ .x = 160.0, .y = 220.0 };
const p2 = rl.Vector2{ .x = 340.0, .y = 380.0 };
const p3 = rl.Vector2{ .x = 480.0, .y = 90.0 };
const p4 = rl.Vector2{ .x = 700.0, .y = 60.0 };
var points = [_]rl.Vector2{p0, p1, p2, p3, p4};
rl.drawSplineCatmullRom(points, 5, 10, rl.Color.black);

and am now getting that:

src/toolbox.zig:31:9: error: local variable is never mutated
    var points = [_]rl.Vector2{p0, p1, p2, p3, p4};
        ^~~~~~
src/toolbox2.zig:31:9: note: consider using 'const'

The head spins. 🙂