from Hacker News

How I Built Zig-SQLite

by Sphax on 5/27/22, 9:16 AM with 97 comments

  • by SkeuomorphicBee on 5/27/22, 12:57 PM

    In case anyone else is unfamiliar with Zig syntax and wondering: in Zig the .{ "somevalue"} represents an anonymous list literal [1], and .{.somename = "somevalue"} is an anonymous struct literal [2].

    (A bit off topic rant, but Zig documentation is quite bad, it took me a lot more effort that it should to discover the facts above.)

    [1] https://ziglang.org/documentation/master/#Anonymous-List-Lit...

    [2] https://ziglang.org/documentation/master/#Anonymous-Struct-L...

  • by codethief on 5/27/22, 10:43 AM

    This is really cool, thanks for providing such an in-depth view into Zig's comptime features and how you used them!
  • by curist on 5/27/22, 12:49 PM

    By using comptime, the statement couldn't be runtime composed, right? That's currently the major holdback for me to spend more time on zig: if using comptime become more common in zig community, the libs could be less flexible to use. It feels sort of like function coloring to me, that the whole call chain also need to pass down the value as comptime variable. I've only spend 2 days with zig, so I would love to learn if I'm wrong on this subject.
  • by longrod on 5/27/22, 9:58 AM

    This looks really neat and handy especially the type annotations part. I always got frustrated when accidentally putting the wrong type into a column.

    How's the Language Server support for this? Last time I tried ZLS, it was quite well rounded so I am curious to know if this type annotations would work with it or not. Would be really cool if they do.

    I have never been able to fully adopt zig due to how frustrating working with strings is but I absolutely loved the comptime functionality.

  • by nickysielicki on 5/27/22, 2:01 PM

    constexpr/consteval/comptime/etc is a game changer for the systems programmer and it feels so close but yet so far. Can someone speak to what language has the best support for this? Some things that I feel are missing in C++:

    * arbitrary file I/O. I can take my compile time data and write a python script to put it in a std::array, but I shouldn’t have to.

    * non-fixed-sized containers, ie vector

    * a generic memoization utility in the standard library for caching. If I want my function to handle any input at runtime, but I want to pre-populate a cache at compile time for values that I know will be called, it’s doable but not as easy as it should be. (In general, given the amount of algorithms that rely on memoization, I’m somewhat surprised that Python is the only language I know that makes memoization as easy as a decorator).

  • by jhgb on 5/27/22, 2:35 PM

    Here's an absolutely crazy idea that I had the other day... Since Zig has comptime, couldn't a Zig reimplementation of SQLite use comptime for compile-time query compilation? Including generating native code for fixed queries? So far I haven't been able to come up with any counterargument for why this wouldn't work.
  • by voigt on 5/27/22, 2:21 PM

    I see Zig, I vote up ;) Exciting new language with compelling features!

    Great work of the author explaining comptime!

  • by noodledoodletwo on 5/27/22, 12:04 PM

    Cool article.

    I can't figure out why i am supposed to care about zig beyond it's fun? I get that it's interesting and more safe than C (honestly though what the hell isn 't).

    Say you write rust pretty regularly for new product development, what does zig offer to make my life better, my products more stable, etc?