Initialising struct vars and type conversion [n]f32 to []f32

Two questions in one, since same code base, pseudo code at least.

q1. When I pass the locally constructed radi into my struct, some of the values get changed. see below.

q2. Is there a way to access struct fields when adding struct vars? eg. ‘derived1’, maybe some kind of struct initialiser that has access to its own struct consts?

and for bonus points, how do I refer to these types in Zig lingo. eg. when doing .radi = radi (not .radi = &radi) I was getting:

error: expected type '[]f32', found '[9]f32'

Is it complaining about assigning an array to a slice? or a multi-pointer type to an unknown sized array pointer? and &radi would be taking the address of a multi pointer item, or the address of an array. I had to add: ‘comptime’ to ‘lineCount’ param to get this far, so I am thinking maybe this is actually a const array…

Sorry for more newbie questions, but loving the language so far!

pub fn new(ctx: js.Context2D, comptime lineCount: u8, lineWidth: u8) RendererInstance {
    
    var radi = [_]f32{0} ** lineCount;     //or maybe this:  var radi: [lineCount]f32 = undefined;
    for (radi) |_, i| {
        radi[i] = (@intToFloat(f32, i * lineWidth) + @intToFloat(f32, lineWidth / 2));
        js.logNum("radi", radi[i]);
    }
     const result = RendererInstance{
        .lineCount = lineCount,
        .lineWidth = lineWidth,
        .radi = &radi,
    };
    result.setupSomeThingsThatNeedCalculation(lineCount * lineWidth); 
    return result;
    ...


pub const RendererInstance = struct {
    lineCount: u8,
    lineWidth: u8,
    radi: []f32,

    var derived1:f32 = 0.0;     //otherwise would use  lineCount * lineWidth or expected maybe @This().lineCount to be accessible here.

    fn setupSomeThingsThatNeedCalculation(self: RendererInstance, v1:f32) {
        derived1 = v1
    }
    ...

    fn render(self: RendererInstance) void {
        for (self.radi) |r, i| {
            js.logNum("radi[i]", self.radi[i]);
        }
        ...


radi  =5 
radi  =15 
radi  =25 
radi  =35 
radi  =45 
radi  =55 
radi  =65 
radi  =75 
radi  =85

radi[i]  =731.3411254882812 
radi[i]  =15 
radi[i]  =25 
radi[i]  =2.802596928649634e-45 
radi[i]  =45 
radi[i]  =4.440892098500626e-16 
radi[i]  =2.524354896707238e-29 
radi[i]  =5.739718509874451e-42 
radi[i]  =85

I have managed to create a standalone example (not wasm) for (q1), i.e. why is my useGrid() call seeing different values. (zig 0.8.1).

const std = @import("std");

const Point = struct {
    x: f32 = 0.0,
    y: f32 = 0.0,
};
const pointCount = 8;
const stdout = std.io.getStdOut().writer();

pub fn main() anyerror!void {
    createGrid().useGrid();
}

fn createGrid() Grid {
    var points = [_]Point{Point{}} ** pointCount;
    stdout.print("\ncreateGrid::type of grid: {anytype}\n", .{@TypeOf(points)}) catch unreachable;
    for (points) |p, i| {
        points[i].x = @intToFloat(f32, i);
        points[i].y = @intToFloat(f32, i) * 2.0;
        stdout.print("{}={d},{d}\n", .{ i, points[i].x, points[i].y }) catch unreachable;
    }

    return Grid{
        .points = points[0..],
    };
}

pub const Grid = struct {
    points: []Point,

    pub fn useGrid(self: Grid) void {
        stdout.print("\nuseGrid::type of grid: {anytype}\n", .{@TypeOf(self.points)}) catch unreachable;
        for (self.points) |p, i| {
            stdout.print("{}={d},{d}\n", .{ i, p.x, p.y }) catch unreachable;
        }
    }
};

OK, so I eliminated the struct confusion, and now I see that I was stupidly creating a slice onto a stack allocated array, which goes out of scope, oopsi! Well I was looking for a language with no GC, so I only have myself to blame :slight_smile: time to learn about allocators :slight_smile: