views:

253

answers:

5

I want a better C. Let me explain:

I do a lot of programming in C, which is required for applications that have real-time needs such as audio programming, robotics, device drivers, etc.

While I love C, one thing that gets on my nerves after having spent a lot of time with Haskell is the lack of a proper type system. That is, as soon as you want to write a more general-purpose function, say something that manipulates a generic pointer, (like say a generic linked list) you have to cast things to void* or whatever, and you loose all type information. It's an all-or-nothing system, which doesn't let you write generic functions without losing all the advantages of type checking.

C++ doesn't solve this. And I don't want to use C++ anyways. I find OO classes and templates to be a headache.

Haskell and its type classes do solve this. You can have semantically useful types, and use type constraints to write functions that operate on classes of types, that don't depend on void.

But the domain I'm working in, I can't use Haskell, because it's not real-time capable--mostly due to garbage collection. GC is needed because it's very difficult to do functional programming, which is allocation-heavy, without automatic memory management. However, there is nothing specifically in the idea of type classes that goes against C's semantics. I want C, but with Haskell's dependable type system, to help me write well-typed systems. However, I really want C: I want to be in control of memory management, I want to know how the data structures are layed out, I want to use (well-typed) pointer arithmetic, I want mutability.

Is there any language like this? If so, why is it not more popular for low-level programming?

Aside: I know there are some small language experiments in this direction, but I'm interested in things that would be really usable in real-world projects. I'm interesting in growing-to-well-developed languages, but not so much "toy" languages.

I should add, I heard of Cyclone, which is interesting, but I couldn't get it to compile for me (Ubuntu) and I haven't heard of any projects actually using it.. any other suggestions in this vein are welcome.

Thanks!

+4  A: 

I'm not sure what state Cyclone is in, but that provided more safety for standard C. D can be also considered a "better C" to some extent, but its status is not very clear with its split-brain in standard library.

My language of choice as a "better C" is OOC. It's still young, but it's quite interesting. It gives you the OO without C++'s killer complexity. It gives you easy access to C interfaces (you can "cover" C structs and use them normally when calling external libraries / control the memory layout this way). It uses GC by default, but you can turn it off if you really don't want it (but that means you cannot use the standard library collections anymore without leaking).

The other comment mentioned Ada which I forgot about, but that reminded me: there's Oberon, which is supposed to be a safe(-er) language, but that also contains garbage collection mechanisms.

viraptor
OOC is interesting, I'll have a closer look at it, thanks.
Steve
A: 
Jon Purdy
+2  A: 

I don't know much about Haskell, but if you want a strong type system, take a look at Ada. It is heavily used in embedded systems for aerospace applications. The SIGADA moto is "In strong typing we trust." It won't be of much use, however, if you have to do Windows/Linux type device drivers.

A few reasons it is not so popular:

  • verbose syntax -- designed to be read, not written
  • compilers were historically expensive
  • the relationship to DOD and design committees, which programmers seem to knock

I think the truth is that most programmers don't like strong type systems.

Dr. Watson
A: 

D might offer what you want. It has a very rich type system, but you can still control memory layout if you need to. It has unrestricted pointers like C. It’s garbage collected, but you aren’t forced to use the garbage collector and you can write your own memory management code if you really want.

However, I’m not sure to what extent you can mix the type richness with the low-level approach you want to use.

Let us know if you find something that suits your needs.

Daniel Cassidy
+3  A: 

You might also want to look at BitC. It’s a serious language and not a toy, but it isn’t ready yet and probably won’t be ready in time to be of any use to you.

Nonetheless, a specific design goal of BitC is to support low-level development in conjunction with a Haskell-style type system. It was originally designed to support development of the Coyotos microkernel. I think that Coyotos was killed off, but BitC is still apparently being developed.

Daniel Cassidy
My understanding is that BitC was abandoned when the author was hired by Microsoft? Perhaps I'm wrong. It's definitely along the right lines, although I haven't read up on its type system.
Steve
Jonathan S. Shapiro has since left Microsoft and announced his intention to resume work on BitC. See: http://www.coyotos.org/pipermail/bitc-dev/2010-March/001809.html
Daniel Cassidy
Cool, thanks, that's great.
Steve