#Function that takes a parameter of a type with an error: !foo

1 messages · Page 1 of 1 (latest)

midnight carbon
#

I have some code that handled errors from a function call inside a loop

const stuff = call_something() catch |err| switch (err) {
  // handle errors
  continue;
}
// handle non-error cases

I then wanted to add another code path outside the loop that would need to handle the same error and non error conditions so I refactored to

const stuff_or_error call_something;
handle_stuff_or_error(stuff_or_error);

But I can't figure out the function signature. Something like:

fn handle_stuff_or_error(anyerror!thetype) void {

I tried a few things. My actual code is more complicated of course. In the case of anyerror here is the compiler error output my actual code produces. I don't know how to use this info to find what to replade anyerror with:

src/main.zig:85:128: error: expected type 'anyerror!fs.Dir.Entry', found '@typeInfo(@typeInfo(@TypeOf(fs.Dir.Walker.next)).Fn.return_type.?).ErrorUnion.error_set!?fs.Dir.Walker.Entry'
flint raptor
#

Dir.Entry and Dir.Walker.Entry are different things

midnight carbon
# flint raptor Dir.Entry and Dir.Walker.Entry are different things

hmm. I still get this:

src/main.zig:85:128: error: expected type 'anyerror!fs.Dir.Walker.Entry', found '@typeInfo(@typeInfo(@TypeOf(fs.Dir.Walker.next)).Fn.return_type.?).ErrorUnion.error_set!?fs.Dir.Walker.Entry'
        const cont = try handle_walk_result(al, &stats, dir, walky.name_buffer.items[0 .. walky.name_buffer.items.len - 1 :0], walk_result);
flint raptor
#

oh and the second one is optional :p

#

to be clear i dont know if changing these will make it work, idk how these coercions work with error unions ive never tried this

#

but it wouldnt work regardless if the types arent compatible in the first place

midnight carbon
#

yes i didn't spot the obvious ones you pointed out. my brain must be atrophying from all the type coercion i no longer have to think about, except when i do have to (-:

flint raptor
#

you could also just use anytype if that makes it easier

midnight carbon
#

really? i don't really understand anytype. i only use it with tuples so far

#

well that compile error went away but i have another one before i know if the problem is solved