Hacker News new | past | comments | ask | show | jobs | submit login

How do you query if the type implements a particular method?



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).




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: