Live data from Hacker News

Algebraic Data Types for C99

github.com

21–30 of 233 posts

Re: Algebraic Data Types for C99

#21

One of the crimes of modern imperative programming languages is not having ADTs (except maybe Rust) built-in. It is such a basic mental model of how humans think and solve problems. But instead we got inheritance and enums which are practically very primitive.

Pascal has had variant records since the 1970s.

But Pascal's variant records (1970-11) had very ugly design errors in comparison with the unions of Algol 68 (1968-12), which made them either useless or annoying for most applicatons.

Niklaus Wirth is well known as a critic of Algol 68 (before the design of Algol 68 was finalized), but in the case of his variant records he has completely failed to create something competitive.

Re: Algebraic Data Types for C99

#22
post #19

One of the crimes of modern imperative programming languages is not having ADTs (except maybe Rust) built-in. It is such a basic mental model of how humans think and solve problems. But instead we got inheritance and enums which are practically very primitive.

ADT feels like an unfortunately acronym-collision with "Abstract data types."

Yep, people that eventually buy a Modula-2 ADT book, when hunting old stuff, are in for a surprise. :)

Re: Algebraic Data Types for C99

#23

Earlier quoted context omitted.

Pascal has had variant records since the 1970s.

But Pascal's variant records (1970-11) had very ugly design errors in comparison with the unions of Algol 68 (1968-12), which made them either useless or annoying for most applicatons. Niklaus Wirth is well known as a critic of Algol 68 (before the design of Algol 68 was finalized), but in the case of his variant records he has completely failed to create something competitive.

Given where Algol 68 ended up, I would say Wirth was quite right.

Re: Algebraic Data Types for C99

#24
post #4

This is the work of a wizard. I've known C for almost 20 years, and never would I have thought the macro system was powerful enough to allow such black magic. This is awesome!

Yeah xmacros (the style of macro use) are pretty fancy. "Classically" they are used for creating and accessing type safe generics or for reducing boilerplate for hardware register and interrupt definitions.

They are kind of cursed but at their core they are actually incredibly simple and a reliable tool for reducing cognitive complexity and boilerplate in C based projects.

Re: Algebraic Data Types for C99

#25
post #23

Earlier quoted context omitted.

But Pascal's variant records (1970-11) had very ugly design errors in comparison with the unions of Algol 68 (1968-12), which made them either useless or annoying for most applicatons. Niklaus Wirth is well known as a critic of Algol 68 (before the design of Algol 68 was finalized), but in the case of his variant records he has completely failed to create something competitive.

Given where Algol 68 ended up, I would say Wirth was quite right.

Algol 68 was a failure mainly due to its inappropriate documentation, not due to the quality of the language.

It included many innovations that appeared again in other programming languages only decades later.

Niklaus Wirth was a good teacher and writer and the success of his languages is due mostly to his books and due to his languages being used for teaching in many universities, not due to their technical qualities.

Re: Algebraic Data Types for C99

#26
Could you not get most of the benefits of ADTs using structs + unions + enums? I've used the pattern where I had a union of several types and an enum to differentiate which one to pick. Something like std::variant seems to work a bit like a sum type.

The only issue is you can't do a clean switch statement that matches on the specific value of a field, but nested switch statements aren't that messy.

Re: Algebraic Data Types for C99

#27
post #23

Earlier quoted context omitted.

Given where Algol 68 ended up, I would say Wirth was quite right.

Algol 68 was a failure mainly due to its inappropriate documentation, not due to the quality of the language. It included many innovations that appeared again in other programming languages only decades later. Niklaus Wirth was a good teacher and writer and the success of his languages is due mostly to his books and due to his languages being used for teaching in many universities, not due to their technical qualitie…

> not due to their technical qualities

AFAIK Pascal is C and Algol 68 is C++

people used Pascal because the compiler was blazing fast, it was easier to implement and learn and the features it lacked against Algol did not really matter most of the time (at the time)

More features doesn't automatically means "better"

Also Pascal had quite strong technical qualities, not very common among other contemporary languages

edit: can I ask the reason for the downvote? I would really like to hear an opinion on what Pascal did wrong, having used it extensively in the late 80s until the end of the 90s and why my comment was awarded with a negative score.

Re: Algebraic Data Types for C99

#28
post #22
post #19

Earlier quoted context omitted.

ADT feels like an unfortunately acronym-collision with "Abstract data types."

Yep, people that eventually buy a Modula-2 ADT book, when hunting old stuff, are in for a surprise. :)

It's a term that is commonly used in computer science education to refer to any data type independent of its concrete implementation (so, basically, its interface.) I don't think it's just restricted to Modula-2?

Re: Algebraic Data Types for C99

#29
post #23

Earlier quoted context omitted.

Given where Algol 68 ended up, I would say Wirth was quite right.

Algol 68 was a failure mainly due to its inappropriate documentation, not due to the quality of the language. It included many innovations that appeared again in other programming languages only decades later. Niklaus Wirth was a good teacher and writer and the success of his languages is due mostly to his books and due to his languages being used for teaching in many universities, not due to their technical qualitie…

I beg to differ, after Modula-2 and Object Pascal (Apple designed it with Wirth's feedback).

Most of his languages ended up losing, because they weren't being shipped with a free beer OS, coupled with a free beer compiler.

Re: Algebraic Data Types for C99

#30
post #20
post #9

Earlier quoted context omitted.

Yeah, when I first learned Haskell a million years ago, and Erlang slightly less than a million years ago, the pattern matching was so plainly obviously the "correct" way to do things; it just felt like it was exactly how I thought about problems, and all the constructs with if/switch/enums had been an attempt to force my brain thinking into something that executes. It honestly does annoy me that a lot of mainstream…

> when Java 8 added a lot of (well-needed) new syntax, it felt like that was an ideal opportunity to add ADTs and pattern matching Well at least Java does now (as of Java 21) have pattern matching (including nested record destructuring) and sealed classes, which let you have decent sum types. The one issue is that everything is nullable, but that's a wider Java issue.

Yeah, but the annoying part of Java is that people stick with old versions for a long time. Java 21 looks pretty great but most companies are still using Java 17, or even Java 11 still.
Post reply on HN