#`use of undeclared identifier 'updater'` error

1 messages · Page 1 of 1 (latest)

frosty loom
#

Trying to call a function from outside of a struct that contains a public function doesn't seem to work. I'm sure there's a way, I just don't understand how.

src\main.zig:29:27: error: use of undeclared identifier 'updater'
    var update_instance = updater{};
    var update_instance = updater{};
    var serve_instance = server{};

    const server = struct {
        pub fn serve() !void {
            if (unserved > 0) {
                unserved -= 1;
                served += 1;
                std.debug.print("Serving guest. Served: {d}, Unserved: {d}\n", .{ served, unserved });

                if (std.rand.random().int(u32) < 5) {
                    new_guest = std.time.milli(std.rand.random().intRange(1000, 10000));
                }
            }
            .serve;
        }
    };

    const updater = struct {
        pub fn update() !void {
            if (new_guest <= 0) {
                new_guest = std.time.milli(std.rand.random().intRange(1000, 10000));
                std.debug.print("New guest arrived!\n", .{});
            }
            .update;
        }
    };

Maybe I'm doing it wrong, anyone got a solution and explanation? And preferably a docs link where I can read up a bit more about how it works in-depth? This is week 3 of trying to learn Zig (I allocate 1 1/2 hours a week to learning).

viral bay
#

Looks like you just have the .update in the wrong place

#

Should go outside the struct

#

That and you need the vars to be after the consts and there shouldnt be brackets

dull juniper
#

are updater and server supposed to be functions or structs?

viral bay
#

you're probably also going to have problems with the variables from outside used inside those functions

#

likely what you want to do here is pull those functions out into a struct and put any state they're using into that struct as well

#
const State = struct {
  unserved: usize,
  served: usize,
  new_guest: usize,
  pub fn serve(self: *State) ...
  pub fn update(self: *State) ...
};
var state = State {.unserved = 0, .served = 0, .new_guest = 0};
...
state.serve();
...
state.update();
frosty loom
#

yes thank you both

#

i resolved issues !