Live data from Hacker News

Generics in C without void* or macros – enabled by psychec

github.com

41–50 of 74 posts

Re: Generics in C without void* or macros – enabled by psychec

#41

I don't really understand what the point of all this is, and why anyone would consider using this in a real-world application they're developing. There's already a C-like language which lets you write type-safe generic programs: it's called C++! And if for some reason you really want or need to restrict yourself to using C language features (almost) exclusively, you can disable RTTI, exceptions, and use a technique t…

One can also use D (now part of GCC). D with its better dialect allows one to not use D run time libraries, or even C++ runtime libraries. A developer can just choose what C libraries they want to link with.

In this mode, no GC is provided, so a developer will be using manual allocation.

With upcoming D's dip1000 feature, it will be able to do rust-like borrows checker at compile time (or at least this is my understanding). And that capability will be available in betterC mode as well (not just for full blown D).

The code in betterC can use generics and other advanced features

https://dlang.org/spec/betterc.html

---

Nearly the full language remains available. Highlights include:

- Unrestricted use of compile-time features

- Full metaprogramming facilities

- Nested functions, nested structs, delegates and lambdas

- Member functions, constructors, destructors, operating overloading, etc.

- The full module system

- Array slicing, and array bounds checking

- RAII (yes, it can work without exceptions) scope(exit)

- Memory safety protections

- Interfacing with C++

- COM classes and C++ classes

- assert failures are directed to the C runtime library

- switch with strings

- final switch

- unittest

dip1000:

https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1000...

Re: Generics in C without void* or macros – enabled by psychec

#42

Interesting link, but if anybody is interested in making real practical use of generics in a C-style language, then it's best to take a look at Zig.

Zig is not able to output c code that could be consumed by an alternative [optimized|verified/proofed] C compiler like Intel along with other questionable decisions such as "Zig does not support RAII or operator overloading because both make it very difficult to tell where function calls happen just by looking at a function body. Zig tends to avoid syntactic sugar except where it would have a significant effect on th…

Just a note for drive by viewers, there's a link here to 0.1.1 release notes but a lot has changed since 0.1.1:

* [0.2.0 release notes](https://ziglang.org/download/0.2.0/release-notes.html)

* [0.3.0 release notes](https://ziglang.org/download/0.3.0/release-notes.html)

* [0.4.0 release notes](https://ziglang.org/download/0.4.0/release-notes.html)

0.5.0 is scheduled to be released Sept 30.

Re: Generics in C without void* or macros – enabled by psychec

#43

I don't really understand what the point of all this is, and why anyone would consider using this in a real-world application they're developing. There's already a C-like language which lets you write type-safe generic programs: it's called C++! And if for some reason you really want or need to restrict yourself to using C language features (almost) exclusively, you can disable RTTI, exceptions, and use a technique t…

I also think it is rather strange for many projects to continue with C, esp for large projects, where c++ offers much conveniences to the programmer. IIRC gcc only recently started allowing c++ in it's source, and I wonder if emacs allows it still in it's source.

SQLite kicks ass. Don't hate.

Re: Generics in C without void* or macros – enabled by psychec

#44
post #2

You should never typedef a pointer to an object. That’s common knowledge amongst experienced C developers and an immediate red flag, even for myself normally welcoming of anything built with Haskell.

I agree that it's something you shouldn't do, but that's just an opinion. Many C programmers are fine with it.

Re: Generics in C without void* or macros – enabled by psychec

#46

I don't really understand what the point of all this is, and why anyone would consider using this in a real-world application they're developing. There's already a C-like language which lets you write type-safe generic programs: it's called C++! And if for some reason you really want or need to restrict yourself to using C language features (almost) exclusively, you can disable RTTI, exceptions, and use a technique t…

I also think it is rather strange for many projects to continue with C, esp for large projects, where c++ offers much conveniences to the programmer. IIRC gcc only recently started allowing c++ in it's source, and I wonder if emacs allows it still in it's source.

Exceptions are a major point of contention. Most of the conveniences C++ offers to the programmer are not possible without exceptions, and non-local transfer of control is not just a huge departure from C, it's one that is hard to gradually transition to.

Re: Generics in C without void* or macros – enabled by psychec

#47
post #19

Earlier quoted context omitted.

The problem with C++ is that in addition to generic functionality, it also brings a ton of other baggage that even if you do not use, you still pay for (e.g. personally i really dislike how slow C++ compilers are). Though TBH i wouldn't use OP's preprocessor either as i think that void* and macros are perfectly fine.

One of the reasons C++ is so complicated is that you don't pay for the features that you don't use. If you wanted generics in C, you can easily use C++ without any of those other features (baggage, if you will) and not pay for it. Heck, you could define a few macros and make it impossible to use any baggage features you don't like. I've used C++ in a pretty tight embedded environment and it was great.

> I've used C++ in a pretty tight embedded environment and it was great.

I'd love to hear some details. I'm always looking for C++ stories that didn't end in disaster and what the people did that worked.

Re: Generics in C without void* or macros – enabled by psychec

#49
post #46

Earlier quoted context omitted.

I also think it is rather strange for many projects to continue with C, esp for large projects, where c++ offers much conveniences to the programmer. IIRC gcc only recently started allowing c++ in it's source, and I wonder if emacs allows it still in it's source.

Exceptions are a major point of contention. Most of the conveniences C++ offers to the programmer are not possible without exceptions, and non-local transfer of control is not just a huge departure from C, it's one that is hard to gradually transition to.

> Most of the conveniences C++ offers to the programmer are not possible without exceptions

Like what?

Re: Generics in C without void* or macros – enabled by psychec

#50
post #46

Earlier quoted context omitted.

Exceptions are a major point of contention. Most of the conveniences C++ offers to the programmer are not possible without exceptions, and non-local transfer of control is not just a huge departure from C, it's one that is hard to gradually transition to.

> Most of the conveniences C++ offers to the programmer are not possible without exceptions Like what?

Pretty much anything involving constructors that fail.
Post reply on HN