The linux kernel community is discussing convert the linux kernel from c to modern c++

I think this will happen naturally. As someone who’s a C developer, I can tell you that nobody that I know really ‘wants’ C; it’s just the de facto. C has many qualities, but I think it’s reasonable to say that a language designed 50 years ago is not shaped to define the next 50 years of computing. From the perspective of a C developer, I don’t really see why you wouldn’t pick Zig once it’s mature; it’s such an obvious choice, in my opinion.

I know that a ton of people like to believe that ‘C will never die,’ and they will provide the same argument that ‘Many have tried, none have succeeded.’ This is true, but also, to my knowledge, I don’t think there ever was any language whose entire philosophy, tooling, and capacity were made to replace C. None have succeeded because nobody really tried; they might have advertised themselves as such, but that’s not the same.

Zig already provides a good experience for the developer than C, despite being still a very young language. The toolchain is better, the defaults are better, and basically all of the very error-prone patterns in C are safer to express in Zig already. Take the ‘defer’ or ‘errdefer,’ for example; that one feature right there—how many ‘goto’ statements can it shave off in the Linux kernel while also improving the correctness and readability of the code base? For fun, I downloaded from GitHub the Linux kernel to count the result:

┌─(~/workspace/temporary/linux)──────────────────────────────────────────────────────────────────────────────────(plgol@pollivies-MBP:s000)─┐
└─(10:50:42 on master ✹)──> sc goto | wc -l                                                                             130 ↵ ──(Sat,Jan20)─┘
  199715

That one feature alone can probably save 150k lines of code for free. This is just one silly example, but with comptime now, how many ‘void*’ can we avoid?

┌─(~/workspace/temporary/linux)──────────────────────────────────────────────────────────────────────────────────(plgol@pollivies-MBP:s000)─┐
└─(10:52:44 on master ✹)──> rg 'void\s*\*' | wc -l                                                                            ──(Sat,Jan20)─┘
  102424

The same thing now with ‘optional’ in Zig—how much more safety can we gain from replacing all checks against null in C?

┌─(~/workspace/temporary/linux)──────────────────────────────────────────────────────────────────────────────────(plgol@pollivies-MBP:s000)─┐
└─(10:57:23 on master ✹)──> rg '\s*\(!\s*\w+\s*\)' | wc -l                                                                    ──(Sat,Jan20)─┘

  128116
┌─(~/workspace/temporary/linux)──────────────────────────────────────────────────────────────────────────────────(plgol@pollivies-MBP:s000)─┐
└─(11:04:35 on master ✹)──> rg '\s*\(\s*\w+\s*!=\s*(NULL|0)\s*\)' | wc -l                                                     ──(Sat,Jan20)─┘

  9677

My point is that once Zig becomes mature and more people become aware of its existence and features, it will most certainly make its way into the Linux kernel. At the very least, as a toolchain, but more realistically, as an easy path to maintain/extend/rewrite modules of the kernel.

12 Likes