Live data from Hacker News

Show HN: Sum (algebraic) types for C in one 100 line header

github.com

11–20 of 46 posts

Re: Show HN: Sum (algebraic) types for C in one 100 line header

#11
post #5

Question: is C gaining in popularity? If so, why exactly? Are their good reasons to be using C in product development rather than more modern languages?

It looks like some cool new kids are rediscovering it, after more than a decade of pythonisation and javascriptisation of programming. You can have things that behave like values and actually reside on the stack, you can ask the operating system for more memory and you have static fixed-size memory which is allocated when the program is run. Good old procedures without the distractions of prototypal inheritance, vari…

There are a large number of "C but better" options, where the direction of "better" varies. Examples would be Go, Zig, Rust, each a different "direction" of better, but generally better.

Anyone thinking of trying this I would advise against going to C directly. The footguns are dangerous enough as it is and are arguably even more dangerous if you're coming from a more modern language that if you came at C directly. You don't need to go this far back in time to have these things.

But I do strongly agree that anyone who has done nothing by dynamic scripting language development should pick up a modern static language and learn how to use it. I consider having at least one of each a basic tool in the well-rounded developer's toolkit. And if you're still running on 200x-era arguments about the advantages and disadvantages of static versus dynamic languages, as many people are, you're going to be pleasantly surprised by the modern static languages.

But you won't be pleasantly surprised by C. Most of the arguments developed for static vs. dynamic in that era were really about dynamic versus C. They all still apply; C qua C has not moved much since then.

(Maybe you could justify C as "closer to the metal" but in the modern era personally I'd recommend just going straight to assembly for orientation and a tour. You don't have to dive super deep into it to get the sense.)

Re: Show HN: Sum (algebraic) types for C in one 100 line header

#13
post #3

Nice system built with only a couple of C99 macros. That small header also achieves matching for specific C types over the "sum types". In the context of C99, isn't tagged union the more usual name instead of sum type?

It's interesting but not very practical. It's an "algebra of types" with only constants, no variables, since C does not have type variables. Thus it misses much of the point of algebraic data types in a language like Haskell (deep composability and generics with very little code to write).

Take one of the most basic sum types in Haskell's base library, Maybe:

    data Maybe a = Nothing | Just a
This simple construction gives you the ability to write functions over optional values and avoid the issues with null pointers. This would be an amazing feature to have in C but it can't be achieved due to C's very limited type system.

Re: Show HN: Sum (algebraic) types for C in one 100 line header

#14
post #5

Question: is C gaining in popularity? If so, why exactly? Are their good reasons to be using C in product development rather than more modern languages?

I think it's less that it's gaining in popularity than that we're seeing some backlash from the people who aren't liking Rust as much as they wanted to. The boring truth is still that if you have a "bottom layer" task to solve that needs access to hardware details (be it instruction formats, SIMD algorithms, MMIO drivers, system-dependent stuff like container management, etc...) C remains the quickest and easiest and frankly the most fun environment to do it in.

I mean, there's a reason why Karpathy is hacking on "llm.c" as part of his "clean up the world" project and not "llm.rs".

Is that a good thing or bad thing? I won't engage. But I think that's where the psychology is. For myself, I genuinely love C in a way that it's clear I never will Rust.

Re: Show HN: Sum (algebraic) types for C in one 100 line header

#16
post #5

Question: is C gaining in popularity? If so, why exactly? Are their good reasons to be using C in product development rather than more modern languages?

From what I've seen of people progressing through their careers, being a strong C++ programmer helps a lot with learning Rust, and being a strong C programmer helps a lot with C++.

I'd be curious to see how new developers do with Rust out of the gate moving forward. It basically makes you generate a machine checkable proof that you're using advanced memory management techniques correctly, and those techniques were invented to make zero cost abstractions work in C++.

Anyway, for me, it's Rust for all new projects these days, but I still like the minimalism of C. Big companies say it takes about 6 months for strong engineers unfamiliar with all this stuff to reach productivity with Rust. I'd guess those same engineers could get to the point of writing sort-of-correct C in a week or so.

Re: Show HN: Sum (algebraic) types for C in one 100 line header

#17
post #14
post #5

Question: is C gaining in popularity? If so, why exactly? Are their good reasons to be using C in product development rather than more modern languages?

I think it's less that it's gaining in popularity than that we're seeing some backlash from the people who aren't liking Rust as much as they wanted to. The boring truth is still that if you have a "bottom layer" task to solve that needs access to hardware details (be it instruction formats, SIMD algorithms, MMIO drivers, system-dependent stuff like container management, etc...) C remains the quickest and easiest and…

> I mean, there's a reason why Karpathy is hacking on "llm.c" as part of his "clean up the world" project and not "llm.rs".

You post this as if everyone is supposed to know who Karpathy is or what any of that means...

Re: Show HN: Sum (algebraic) types for C in one 100 line header

#18
post #5

Question: is C gaining in popularity? If so, why exactly? Are their good reasons to be using C in product development rather than more modern languages?

C evolves, even if slowly.

I think some devs are fed up of Complexity and excessive abstractions.

C has very few language features, it's a very simple language -- while all modern languages have new (complex and taxing) features.

It's kinda like a Minimalist movement, can we do the same Modern Mumbo Jumbo but in a simple, minimalistic C way?

GC and RAII? Nah, just use Arenas/Pools. Or don't use heap at all.

C is an unsafe language but modern tools get better and better every year, with GCC doing really cool static analysis and finding buffer overflows at compile time.

Also, C runs anywhere and everywhere.

Re: Show HN: Sum (algebraic) types for C in one 100 line header

#19
post #5

Question: is C gaining in popularity? If so, why exactly? Are their good reasons to be using C in product development rather than more modern languages?

TFA isn't about product development.

Things that you hope will be used in widely different contexts need to be created using tools that can easily be "glued" into those widely different contexts.

Right now, C remains the best glue language we have. If you want to write things that can be used from dozens of other languages (or even just 2 other languages), you are likely to write it in C.

In 5-10 years time? We'll see (no pun intended)

Re: Show HN: Sum (algebraic) types for C in one 100 line header

#20
post #5

Question: is C gaining in popularity? If so, why exactly? Are their good reasons to be using C in product development rather than more modern languages?

Perhaps this is obvious, but embedded systems. C/C++ are still really the only languages where you can basically take for granted that you'll have functional tooling and something resembling a standard library regardless of what processor you're targeting. And many embedded projects still reach for C over C++ because a lot of the functionality the C++ adds isn't really ideal for embedded environments anyway, so for team ergonomics it ends up being better to reach for the simpler language that's easier to teach to new devs, etc.
Post reply on HN