#How to type hint GenericReader?

1 messages · Page 1 of 1 (latest)

quick elbow
#

I need to read a complicated binary file that contains data to be parsed into multiple structs:

fn readComplicatedOne(reader: anytype) ComplicatedStruct {
    reader.read(); //...
    return .{...};
}

fn readComplicatedTwo(reader: anytype) ComplicatedStruct2 {
    reader.read(); //...
    return .{...};
}

test "read file" {
    const file = try std.fs.cwd().openFile("my_file.bin", .{ .mode = std.fs.File.OpenMode.read_only });
    const p1 = read()
    var reader = file.reader();
    readComplicatedOne(&reader);
    readComplicatedTwo(&reader);
}

this works, but while writing the "readComplicated" functions, I dont have any help from the language server. I know that reader is some kind of GenericReader(A, B), but I dont know how to write that in the readComplicated* function definitions.

Is there a way to write the read functions signatures in a way I get code completions (functions, signatures, return types) for the reader: GenericReader parameter?

bold rivet
#

firstly, you shouldn't pass a pointer to reader, just pass the reader itself

#

secondly, zls will try to pick up the type from usages in the same file, so the test should be enough

#

but it's not always perfect

#

unfortunately there's not much you can do about that

quick elbow
#

i see. i think I saw a zls config to..build on save? Maybe I can use that and it'll seamlessly work
i gotta say though, I'm not loving this anytype/unconstrained generics business. I know the compiler will eventually scream at me if I pass parameters that doesnt meet the required interface, but being able to declare it would be really awesome