<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>zig — Sean Barnett</title>
    <link>https://seanbarnett.id.au/tag:zig</link>
    <description>Coffee, basketball, programming</description>
    <pubDate>Mon, 23 Nov 2026 04:22:38 +0000</pubDate>
    <image>
      <url>https://i.snap.as/FOPXss01.png</url>
      <title>zig — Sean Barnett</title>
      <link>https://seanbarnett.id.au/tag:zig</link>
    </image>
    <item>
      <title>Zig Idioms for Handling Error Unions and Optionals</title>
      <link>https://seanbarnett.id.au/zig-idioms-for-handling-error-unions-and-optionals?pk_campaign=rss-feed</link>
      <description>I have written this post as a quick reference to remind me how to handle error unions, optionals, and the combination of an error union with an optional. Well, more accurately ...</description>
      <content:encoded><![CDATA[<p>I have written this post as a quick reference to remind me how to handle error unions, optionals, and the combination of an error union with an optional. Well, more accurately ...I started this post for that reason, but in truth it has led me to better understand these language features.</p>

<h2 id="optional">Optional</h2>

<p>We will start the journey with optionals, which can be either a value or null.</p>

<p>When using an optional in an expression, we can use <em>orelse</em> to simultaneously unwrap a non-null value and to provide a default value.</p>

<p>And when using vertical bars to capture a non-null value within an if or while statement, we can use else to handle the null value.</p>

<pre><code>test &#34;test optional&#34; {
    // orelse
    try std.testing.expectEqual(true, generateOptional(.VALUE) orelse false);
    try std.testing.expectEqual(false, generateOptional(.NULL) orelse false);
    try std.testing.expectEqual(false, generateOptional(.ERROR) orelse false);
    // if else
    try std.testing.expectEqual(true, 
        if (generateOptional(.VALUE)) |the_value| the_value else false);
    try std.testing.expectEqual(false, 
        if (generateOptional(.NULL)) |the_value| the_value else false);
    try std.testing.expectEqual(false, 
        if (generateOptional(.ERROR)) |the_value| the_value else false);
    // while
    try std.testing.expectEqual(true, 
        blk: while (generateOptional(.VALUE)) |the_value| break :blk the_value 
            else break :blk false);
    try std.testing.expectEqual(false, 
        blk: while (generateOptional(.NULL)) |the_value| break :blk the_value 
            else break :blk false);
    try std.testing.expectEqual(false, 
        blk: while (generateOptional(.ERROR)) |the_value| break :blk the_value 
            else break :blk false);
}
</code></pre>

<h2 id="error-union">Error Union</h2>

<p>The error union has catch as the counterpoint to optional&#39;s orelse within expressions, and catch can be used with or without vertical bars capturing the actual error.</p>

<p>But life gets confusing when we switch to if and while statements as the same else clause (as used by optionals) takes the place of catch. This is covered by <a href="https://ziglang.org/documentation/0.17.0/#while-with-Error-Unions">the language documentation</a> though, which makes it clear that else with a vertical bar capture requires the condition to have an error union type.</p>

<pre><code>test &#34;test error union&#34; {
    // simple catch
    try std.testing.expectEqual(true, generateErrorUnion(.VALUE) catch false);
    try std.testing.expectEqual(false, generateErrorUnion(.NULL) catch false);
    try std.testing.expectEqual(false, generateErrorUnion(.ERROR) catch false);
    // catch with capture
    try std.testing.expectEqual(true, generateErrorUnion(.VALUE) catch |err| switch (err) {
        TestError.ExpectedError =&gt; false,
        else =&gt; false,
    });
    try std.testing.expectEqual(false, generateErrorUnion(.NULL) catch |err| switch (err) {
        TestError.UnexpectedError =&gt; false,
        else =&gt; true,
    });
    try std.testing.expectEqual(false, generateErrorUnion(.ERROR) catch |err| switch (err) {
        TestError.ExpectedError =&gt; false,
        else =&gt; true,
    });
    // if with capture, else with capture
    try std.testing.expectEqual(true, if (generateErrorUnion(.VALUE)) |the_value| the_value else |err| switch (err) {
        TestError.ExpectedError =&gt; false,
        else =&gt; false,
    });
    try std.testing.expectEqual(false, if (generateErrorUnion(.NULL)) |the_value| the_value else |err| switch (err) {
        TestError.UnexpectedError =&gt; false,
        else =&gt; true,
    });
    try std.testing.expectEqual(false, if (generateErrorUnion(.ERROR)) |the_value| the_value else |err| switch (err) {
        TestError.ExpectedError =&gt; false,
        else =&gt; true,
    });
    // while
    try std.testing.expectEqual(true, blk: while (generateErrorUnion(.VALUE)) |the_value| break :blk the_value else |err| break :blk switch (err) {
        TestError.ExpectedError =&gt; false,
        else =&gt; false,
    });
    try std.testing.expectEqual(false, blk: while (generateErrorUnion(.NULL)) |the_value| break :blk the_value else |err| break :blk switch (err) {
        TestError.UnexpectedError =&gt; false,
        else =&gt; false,
    });
    try std.testing.expectEqual(false, blk: while (generateErrorUnion(.ERROR)) |the_value| break :blk the_value else |err| break :blk switch (err) {
        TestError.ExpectedError =&gt; false,
        else =&gt; false,
    });
}
</code></pre>

<h2 id="combined-error-union-with-optional">Combined Error Union with Optional</h2>

<p>The combination of error union and optional I struggle with handling elegantly, well particularly when not using try (e.g., when I want to map from one error code to another, or when I want to react to explicitly handle an error).</p>

<p>The golden rule is that the error union envelops the optional, and so must be unwrapped first. Consequently, the else prong of an if or while statement (where the condition is an error union wrapping an optional) must be for the error case and so should have vertical bars capturing the error. And the unwrapped non-error value is still an optional.</p>

<pre><code>test &#34;error union optional&#34; {
    // using try
    try std.testing.expectEqual(true, 
        try generateErrorUnionOptional(.VALUE) orelse false);
    // else must have capture
    try std.testing.expectEqual(true, 
        if (generateErrorUnionOptional(.VALUE)) |the_value| the_value 
            orelse false 
        else |the_err| switch (the_err) {
            else =&gt; false,
        });
}
</code></pre>

<p><em>Note: I kinda would have liked to see an if () |value| ... else |err| ... else ... construct to handle the value, the error and the null (respectively), but there&#39;s probably legitimate reasons for its absence.</em></p>

<p><a href="https://seanbarnett.id.au/tag:Zig" class="hashtag"><span>#</span><span class="p-category">Zig</span></a></p>
]]></content:encoded>
      <guid>https://seanbarnett.id.au/zig-idioms-for-handling-error-unions-and-optionals</guid>
      <pubDate>Wed, 14 Oct 2026 03:41:07 +0000</pubDate>
    </item>
    <item>
      <title>Roll Your Own Geometry</title>
      <link>https://seanbarnett.id.au/geometry?pk_campaign=rss-feed</link>
      <description>This post forms part of the ongoing #TagJob project.&#xA;&#xA;For reasons discussed in this post, I have tentatively decided to roll my own minimal geospatial types and calculations.</description>
      <content:encoded><![CDATA[<p><em>This post forms part of the ongoing <a href="https://seanbarnett.id.au/tag:TagJob" class="hashtag"><span>#</span><span class="p-category">TagJob</span></a> project.</em></p>

<p>For reasons discussed in this post, I have tentatively decided to roll my own minimal geospatial types and calculations.</p>

<p>There are many excellent Geospatial libraries available, even in the brave new frontier of an embryonic language such as Zig (thanks to the excellent bridging to C). Principal among these is <a href="https://libgeos.org">Geos</a>. However, I decided to move forward with my own geospatial types and calculations because:</p>
<ul><li>I want both visibility of and direct control over performance-related aspects such as memory management (more on this below)</li>
<li>I want the ability to tinker with algorithms that may favourably trade performance for functionality that is irrelevant to my use case</li>
<li>there&#39;s fun to be had</li></ul>

<p>My initial thinking was to define Zig structs for each basic geometry, but I have now moved to using simple aliases of native types and arrays:</p>

<pre><code>pub fn WithDimension(comptime dimension: comptime_int) type {
    return struct {

        pub const Vector = @Vector(dimension, f64);
        pub const Coordinate = [dimension]f64;
        pub const Coordinates = []Coordinate;
        pub const Point = Coordinate;
        pub const Line = [2]Coordinate;
        pub const LineString = Coordinates;
        pub const LinearRing = Coordinates;
        pub const Polygon = []LinearRing;

        pub const AnyGeometry = union(enum) {
            point: Point,
            line: Line,
            lineString: LineString,
            linearRing: LinearRing,
            polygon: Polygon,
        };

        pub const Envelope = struct {
            min: Vector, // stored as vector as heavily used with SIMD
            max: Vector, // stored as vector as heavily used with SIMD
        };

    };
}
</code></pre>

<p>This design decision does mean that I cannot add either additional state or instance functions to my types. But I do avoid any overhead of allocating an instance of the wrapper type, and of needlessly creating instances of the wrapper type simply to call a function that is interested in the internal state only.</p>

<p>In place of instance methods, I&#39;ll be using a separate calculator that accepts each geometry type. Again, I&#39;ll claim a win here because I can use different calculation functions (e.g. Euclidean versus Geodesic distance) without coupling either to the type.</p>

<p>The one exception is the Envelope type, for which I have wrapped min and max points, and a small number of instance functions:</p>
<ul><li>valid</li>
<li>intersects</li>
<li>covers</li>
<li>intersectionOf</li>
<li>unionOf</li>
<li>bufferOf</li></ul>

<p>Within Envelope I am storing min and max as Vectors as most functions use SIMD, my first foray into this world:</p>

<pre><code>/// Determine if this envelope and another envelope intersect.
pub fn intersects(self: @This(), other: @This()) bool {
    const result_min_max = self.min &lt;= other.max;
    const result_max_min = other.min &lt;= self.max;
    const rval = @reduce(.And, result_min_max &amp; result_max_min);
    return rval;
}
</code></pre>

<p>The code lives <a href="https://bitbucket.org/tagsoftware/tagjobspatial">here</a>.</p>

<p>Tags: <a href="https://seanbarnett.id.au/tag:TagJob" class="hashtag"><span>#</span><span class="p-category">TagJob</span></a> <a href="https://seanbarnett.id.au/tag:Geospatial" class="hashtag"><span>#</span><span class="p-category">Geospatial</span></a> <a href="https://seanbarnett.id.au/tag:Zig" class="hashtag"><span>#</span><span class="p-category">Zig</span></a> <a href="https://seanbarnett.id.au/tag:SIMD" class="hashtag"><span>#</span><span class="p-category">SIMD</span></a></p>
]]></content:encoded>
      <guid>https://seanbarnett.id.au/geometry</guid>
      <pubDate>Mon, 08 Jun 2026 06:07:47 +0000</pubDate>
    </item>
  </channel>
</rss>