Urgent.News

What's breaking now, across thousands of outlets.

Tech

We have Traits at Home

Recently, I have dedicated more time to writing code in Zig, which has been beneficial for my mental well-being. While much attention has been given to Zig Interfaces and Comptime, there is an opportunity to explore alternative abstractions in Zig. Interfaces provide runtime polymorphism in Zig, but they come with a performance overhead due to the additional indirection.

Instead, Zig encourages comptime duck typing, where a function argument is either anytype or parameterized by a type argument T, allowing the function implementation to rely on compile-time reflection or compile-time assumptions about the methods of the types involved. This approach is safer than runtime duck typing, as any interface violations are caught at compile time.

However, this requires careful documentation and adherence to the contract between the caller and callee. Zig aims for simplicity, but for those familiar with traits or typeclasses, Zig may seem lacking in abstractions. In this article, I propose a new convention for defining compile-time type contracts as Traits in Zig, inspired by functional programming concepts, particularly from OCaml.

Traits are essentially compile-time structs that contain related functions over a given type. They serve as bags of functions and types that dictate how certain subtasks should be executed without the receiving function needing to define them explicitly. Traits behave similarly to vtables but are evaluated at comptime. To illustrate the usefulness of traits, let's consider arithmetic operations on user-defined types like Rational and Complex.

In a typical scenario, we might define operations such as addition or multiplication for these types. However, without a standardized abstraction, implementing these operations can become cumbersome. One might resort to wrapping primitive types in user-defined types with their own methods, which can lead to an object-oriented approach that is less efficient and slower due to the need for dynamic dispatch.

With Traits, we can define a compile-time contract for arithmetic operations on any type, enabling the compiler to optimize and inline these functions more effectively. This approach allows for a more streamlined and efficient way to handle arithmetic operations on user-defined types, potentially improving performance and simplifying the codebase.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at axelmagn.com →

More in Tech

Microsoft Says October Updates Didn't Actually Fix VPN Issues

joshuark shares a report from BleepingComputer: Microsoft says it mistakenly flagged a bug causing Always On VPN connection issues as fixed in the October 2026 Windows 11 cumulative updates.

  • October 2026 Windows 11 updates failed to fix Always On VPN issues.
  • Bug caused VPN connections to get stuck or fail to connect.
  • Microsoft apologized for confusion and promised fix in weeks.

More from Thursday 22 October →