#optional value ignored by orelse

1 messages · Page 1 of 1 (latest)

maiden nimbus
#

i have a struct A that contains a field f of type ?*const fn (...) that is set to null by default. Then, i have a constructor that creates a struct B which has a field that is of type A and i take an instamce of A as parameter of the constructor, when creating the field A i manually set each field, so .f = arg.f but i use an orelse for null values .f = arg.f orelse @compileError("Function is undefined") but this does sometimes trigger even tho i set f in the argument A, i've tried setting it to function_name and even &function_name but nothing works. I have no idea why when i provide an f in the argument A its ignored and gives my comp error. Thanks, Osaki.

woven lark
#

my guess is arg.f isn't comptime known

maiden nimbus
#

the actual code is a bit to long to send but i think it should be?

woven lark
#

what you're doing will only compile if arg.f is known at comptime to not be null, because otherwise it'll have to analyze the orelse clause and that will generate an error

maiden nimbus
#
/// Settings to initialize an Endpoint
pub const Settings = struct {
    /// path / slug of the endpoint
    path: []const u8,
    /// callback to GET request handler
    get: ?RequestFn = null,
    /// callback to POST request handler
    post: ?RequestFn = null,
    /// callback to PUT request handler
    put: ?RequestFn = null,
    /// callback to DELETE request handler
    delete: ?RequestFn = null,
    /// callback to PATCH request handler
    patch: ?RequestFn = null,
    /// callback to OPTIONS request handler
    options: ?RequestFn = null,
    /// Only applicable to Authenticating Endpoint: handler for unauthorized requests
    unauthorized: ?RequestFn = null,
    // callback to any unset request type
    unset: ?RequestFn = null,
};
pub fn init(s: Settings) Endpoint {
    return .{
        .settings = .{
            .path = s.path,
            .get = s.get orelse s.unset orelse @compileError("Endpoint handler `.get` is unset, and no `.unset` handler is provided."),
            .post = s.post orelse s.unset orelse @compileError("Endpoint handler `.post` is unset, and no `.unset` handler is provided."),
            .put = s.put orelse s.unset orelse @compileError("Endpoint handler `.put` is unset, and no `.unset` handler is provided."),
            .delete = s.delete orelse s.unset orelse @compileError("Endpoint handler `.delete` is unset, and no `.unset` handler is provided."),
            .patch = s.patch orelse s.unset orelse @compileError("Endpoint handler `.patch` is unset, and no `.unset` handler is provided."),
            .options = s.options orelse s.unset orelse @compileError("Endpoint handler `.options` is unset, and no `.unset` handler is provided."),
            .unauthorized = s.unauthorized orelse s.unset orelse @compileError("Endpoint handler `.unauthorized` is unset, and no `.unset` handler is provided."),
            .unset = s.unset,
        },
    };
}
#
pub fn init() Self {
        return .{
            .ep = zap.Endpoint.init(.{
                .path = "/doesn't+matter",
                .get = &get,
                .unset = &zap.Endpoint.dummy_handler,
            }),
        };
    }
woven lark
#

i would try adding comptime to the call to the outer init() or adding comptime to the parameter of the inner init()

maiden nimbus
#

by inner you mean Endpoint?

woven lark
#

yeah

maiden nimbus
#

hmm, now i get a diffrent error

expected optional type, found '*const fn (*endpoint, request) void'
            .get = s.get orelse s.unset orelse @compileError("Endpoint handler `.get` is unset, and no `.unset` handler is provided."),
                   ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if its comptime can i provide an actual value or do i have to provide an optional?

maiden nimbus
#

i did a bit more testing and found out that for some reason it doesn't work with ?*const fn(void) void but it works with ?fn (void) void

#

any idea why?

woven lark
#

fn() is a comptime-only type, *const fn() can be runtime

#

so using fn() is probably forcing the entire operation to be comptime

#

you might want that here since it seems like it has to be comptime anyway