News for hackers. Stuff that matters.
Re:Zig-style generics are not well-suited for most languages by flohofwoe on Monday October 10, 2022 @06:55AM (#33149901)
> this function should take a Writer

I think some of this can be achieved in the function body by checking the incoming generic types via Zig's comptime type inspection features (see: https://ziglang.org/documentation/master/#typeInfo). Kinda like 'comptime type-asserts'. Together with @compileError this would at least provide more useful error messages if a generic type doesn't have the expected features.

PS: I think it's important to not look at Zig's generic feature in isolation, instead it goes hand in hand with comptime and reflection.

Re:Zig-style generics are not well-suited for most languages by Ar-Curunir on Monday October 10, 2022 @12:59PM (#33153782)
How do you query if the type implements a particular method?
Re:Zig-style generics are not well-suited for most languages by flohofwoe on Monday October 10, 2022 @03:55PM (#33155752)
There's a std.meta.trait.hasFn() comptime helper function in the stdlib:

https://www.godbolt.org/z/xe98Md74r

E.g. if you change the method 'bla' to something else:

https://www.godbolt.org/z/77118zG1K

...there's probably more advanced helper functions in the same std.meta.trait module, I haven't explored that yet.

Also, disclaimer: I haven't dabbled much yet with Zig generics, don't know if this is the proper style (in this case it's probably better to get rid of T entirely and just use anytype for the obj argument).