#Using structs / methods as handler in main

1 messages · Page 1 of 1 (latest)

odd vector
#

I'm coming from golang and am attempting to have a postgresql pool on a struct that has methods. I'm hoping to pass those into the route handler but am getting errors.
https://gist.github.com/Smithx10/ed648316e042c84963f5f97c5e4beb2f

I saw that
try router.serve_route("/", Route.init().get(struct {}, f.getUsers));
Route.init().get takes a pointer that can be used, but for now I'm learning.

Gist

Struct learning. GitHub Gist: instantly share code, notes, and snippets.

tender burrow
#

That method syntax is only allowed when the first argument of the function is the type it's in or anytype. In this case the first argument of foo.getUsers is *Context, which is not foo, so you can't use the method syntax. You can access it with foo.getUsers instead

odd vector
#

@tender burrow where is the best place to learn more about being able to do this?

brazen ridge
#

this is the lang reference to structs but it doesn't explain much detail

#

Zig methods are basically just syntax sugar for calling a function within the struct namespace with an instance of the struct

#
const std = @import("std");

pub fn main() !void {
    std.debug.print("Calling MyStruct.a_function: {}\n", .{MyStruct.a_function(0)});

    var instance_of_my_struct = MyStruct{ .field = 3 };
    std.debug.print(
        "Calling MyStruct.a_method with method syntax: {}\n",
        .{instance_of_my_struct.a_method()},
    );
    std.debug.print(
        "Calling MyStruct.a_method with namespace syntax: {}\n",
        .{MyStruct.a_method(instance_of_my_struct)},
    );
}

const MyStruct = struct {
    field: u8 = 0,

    // This is a function in the namespace of MyStruct
    // Inside of the struct definition this function can be called directly.
    // Otherwise it can be accessed with 'dot' syntax through the MyStruct namespace.
    fn a_function(foo: u8) u8 {
        return foo;
    }

    // This is a method of the struct because the first argument in the function definition
    // is an instance of MyStruct. This can be called on an instance of MyStruct with 'dot' syntax
    // which is just syntax sugar for calling it through the namespace and passing the instance.
    fn a_method(self: MyStruct) u8 {
        return self.field;
    }
};
#

Output:

❯ zig run hello.zig
Calling MyStruct.a_function: 0
Calling MyStruct.a_method with method syntax: 3
Calling MyStruct.a_method with namespace syntax: 3
brazen ridge
#

looking closer at your example I'm seeing a few things, one question is why is foo.pool a pointer instead of a value? A pointer refers to a value stored somewhere else so a struct with the only field being a pointer to some other data is kinda meaningless. The purpose of a struct is to co-locate data with functionality in an isolated namespace

#

Also you have some syntax wrong

#

I am not familiar with these libraries so I don't know their API or the ways to interact with them, but if the purpose of the getUsers function is to take a pg.Pool and a *Context and update the context with the result of a query, then the layout of this struct doesn't make sense to me. Why is this not just a separate function? What is your goal with creating a struct?