News for hackers. Stuff that matters.
Re:Better C – A subset of D Programming Language by sgt on Tuesday April 28, 2020 @06:43AM (#23005638)
D is pretty exciting. Seems like a perfect choice for those of us looking for an alternative systems programming language and are not completely convinced we'll be happy in Rust.
Re:Better C – A subset of D Programming Language by skohan on Tuesday April 28, 2020 @07:12AM (#23005814)
Yeah I think "better C" is a space which has a really good reason to exist and I'm always interested to see new entrants.

IMO describing Rust as a "C replacement" is slightly off the mark because Rust's value proposition is very different from C. Rust is about giving you the best possible performance in a safe-by-default language. C is about giving you maximal control over memory, with a very thin layer of abstraction over the hardware.

C has traditionally been used in a lot of places where Rust's USPs add a lot of value -- for instance in kernel development -- simply because there wasn't a safe alternative. However I think there are other cases where the strengths of C still add value; for instance in game development you're largely trying to do high-throughput processing over large swaths of structured data, and Rust concepts which help safety like RAII just get in your way. Yes there are ways to get around this in Rust, but Rust is not really optimized for structured, manual memory management.

As a result I think there is plenty of room for something like C which just has better ergonomics and some more modern features.

Re:Better C – A subset of D Programming Language by fluffything on Tuesday April 28, 2020 @11:17AM (#23007798)
Many programmers think of the programming world as: "assembly < C < everything else", and that is a misconception that was actually quite easy to "learn" if one wasn't ready to invest a lot of time in looking for more options.

When I see someone saying that they prefer D/Nim/Zig value proposition as a "better C" than Rust, what I see is that people are just starting to realize that there are many more options than C for doing programming tasks that require a more precise interaction with the hardware.

It's just not "assembly", but there are multiple different assemblers with different trade-offs. It's also not "assembler < C" anymore, since in some aspects newer languages do offer more low level control about certain things than C.

It's also not just C/C++/Zig/Rust/Nim/D competing at the lowest-level of the space. Ada, Forth, Scheme, and many other languages are also widely used in this space. They just aren't the "next new thing".

Re:Better C – A subset of D Programming Language by pjmlp on Tuesday April 28, 2020 @11:55AM (#23008236)
I think that turn over happened when a new generation started to learn computing after UNIX took over the server room and teachers that didn't care to teach anything else.

So only the old generation remembers how things used to be, and those that are curious about the computing history and evolution of programming languages.

Re:Better C – A subset of D Programming Language by WalterBright on Tuesday April 28, 2020 @07:17AM (#23005847)
D also now supports Ownership/Borrowing (experimental) on a per-function basis, meaning it can be added incrementally to an existing program.
Re:Better C – A subset of D Programming Language by dgellow on Tuesday April 28, 2020 @07:43AM (#23006025)
For reference, the blog article on borrowing/ownership from 2019: https://dlang.org/blog/2019/07/15/ownership-and-borrowing-in...
Re:Better C – A subset of D Programming Language by exiled13 on Tuesday April 28, 2020 @01:27PM (#23009408)
D's borrow checker fails in comparison to Rust's. It serves very little purpose and provides almost no guarantees. These issues have been brought up multiple times but they are brushed aside with ignorant responses like "I've been told my entire career what I'm doing will fail but I continue to do it anyways".
Re:Better C – A subset of D Programming Language by WalterBright on Tuesday April 28, 2020 @03:57PM (#23011162)
Yes, you do create accounts to follow D around and complain about it.
Re:Better C – A subset of D Programming Language by throwaway17_17 on Tuesday April 28, 2020 @05:23PM (#23012257)
What is the difference between the two?
Re:Better C – A subset of D Programming Language by exiled13 on Tuesday April 28, 2020 @06:30PM (#23012920)
Rust was designed from the ground up and every feature is implemented and designed to work with its borrow checker. So it is able to provide a definitive guarantee that memory isn't free'd or used after it is free'd. The only exception is in "unsafe" code, where you can break the borrow checker. But this limits where you have to worry about potential bugs.

In D, the ""borrow checker"" is being tacked on as an after thought, in an attempt to copy Rust. This means that it doesn't play nice with existing features and makes it difficult if not impossible to guarantee that memory isn't leaked or used after it was free'd. For example with exceptions. The checker doesn't check for exceptions and if memory is free'd correctly if an exception is thrown. This isn't a problem in Rust because it doesn't have exceptions so it doesn't have to worry about checking them so it can maintain its strong guarantee.

Re:Better C – A subset of D Programming Language by cesarb on Tuesday April 28, 2020 @08:05PM (#23013696)
> This isn't a problem in Rust because it doesn't have exceptions

Rust does have something similar to exceptions when compiled with "panic=unwind" (the default). It uses the same mechanism as C++ exceptions to unwind the stack (while calling all the necessary destructors), can be caught (std::panic::catch_unwind) and rethrown (std::panic::resume_unwind), and has some of the same concerns as C++ about "exception safety" (mostly within unsafe code - the programmer has to take care to leave the objects in a safe state when it can unwind).

Re:Better C – A subset of D Programming Language by exiled13 on Tuesday April 28, 2020 @10:31PM (#23014626)
I didn't know that used something akin to exceptions. Still panics aren't as common as exceptions (in D), and still only have to worry about unsafe code (for the most part).
Re:Better C – A subset of D Programming Language by WalterBright on Wednesday April 29, 2020 @12:35AM (#23015405)
D's version of Ownership/Borrowing adheres to the notion:

"only one mutable reference to a mutable object OR many const references to an object"

and that's where the similarity to Rust begins and ends. The realization of that principle is quite different.

Re:Better C – A subset of D Programming Language by exiled13 on Wednesday April 29, 2020 @06:42PM (#23025420)
Rust guarantees memory safety, or are you saying D isn't similar in that regard as well? If that's what you are saying, then I agree completely. D's ownership/borrowing does not provide memory safety, and that makes it pretty much useless.

There's really no point discussing this with you any further. You don't contribute anything to the discussion. That's just PR babble, and provides zero information and doesn't even deny the fact that exceptions break memory safety with D's borrow checker (you know it that's why you don't deny it like a "good" PR person).

Re:Better C – A subset of D Programming Language by kllrnohj on Tuesday April 28, 2020 @12:13PM (#23008463)
> However I think there are other cases where the strengths of C still add value; for instance in game development you're largely trying to do high-throughput processing over large swaths of structured data

That world is dominated by C++ for the most part, though. So D's -betterC would be fighting against an incumbent that is itself also already a "better C".

What value does D's -betterC bring to the table here? Skimming through it, it doesn't look like there's any compelling reason to switch to -betterC from an existing C++ code & knowledge base.

Re:Better C – A subset of D Programming Language by skohan on Tuesday April 28, 2020 @12:21PM (#23008543)
I think a lot of people would argue that many C++ features, like OO, are not strictly improvements. Also C++ is a massive language, and part of the beauty of C is it's simplicity.

I think what a lot of people would want is something which maintains that simplicity, but mainly delivers on basic QOL lessons we've learned in the past 40 years, like that it's nice not to have to pass around array lengths as separate variables.

Re:Better C – A subset of D Programming Language by kllrnohj on Tuesday April 28, 2020 @12:29PM (#23008638)
> I think a lot of people would argue that many C++ features, like OO, are not strictly improvements.

Sure, but D has those OO features, too, so that's an argument against both, not one.

> like that it's nice not to have to pass around array lengths as separate variables.

-betterC doesn't have dynamic arrays, though. That's one of the "unavailable features" currently.

But that's also sommething C++ solved as well - std::vector does exist, after all, as does move semantics to avoid copying it unnecessarily.

C++ is far from perfect, of course, but D's -betterC doesn't really fix any of C++'s issues and it doesn't retain C's simplicity. It's in an awkward middle ground between the two - is that really a viable place to be? For users that already know & have C++ codebases, what's the sales pitch here? For existing C users, what does -betterC do that hasn't already been done and already failed to attract the C holdouts?

Re:Better C – A subset of D Programming Language by pjmlp on Tuesday April 28, 2020 @01:45PM (#23009662)
One thing that D does better and that on C++ depends pretty much on the team and which compiler toolchain you use, bounds checking for strings and arrays.

You can get them in C++, and VC++ does it by default on debug code, but not all compilers do by default and it isn't required by the standard unless you explicitly make use of the at() variants.

On the other hand, every attempt to bring bounds checking to C has failed, lets see how far Microsoft goes with Checked C.

Re:Better C – A subset of D Programming Language by WalterBright on Wednesday April 29, 2020 @12:40AM (#23015437)
> part of the beauty of C is it's simplicity.

True, but the trouble with simplicity is a number of things become excessively tedious and error-prone to code - such as ensuring no buffer overflows.

C makes up for its lack of expressivity by adding a text preprocessor. The preprocessor is a tacit admission that the core language simply isn't powerful enough. When people find themselves doing metaprogramming with the C preprocessor, it's really time to graduate to a more powerful language. DasBetterC doesn't need a preprocessor, and its metaprogramming facilities far outstrip the C preprocessor.

Re:Better C – A subset of D Programming Language by maxsam on Wednesday April 29, 2020 @06:10AM (#23017437)
> its metaprogramming facilities far outstrip the C preprocessor.

...and are severely crippled by applying betterC constraints to CTFE:

  // CTFE-only
  string genint(string name) {
      return "int " ~ name ~ ";";
  }

  void main() {
      mixin(genint("x"));
  }
Error: array concatenation of expression "int " ~ name ~ ";" requires the GC which is not available with -betterC
Re:Better C – A subset of D Programming Language by Shorel on Tuesday April 28, 2020 @02:21PM (#23010097)
You don't have to use all of C++ everywhere when programming C++.

In particular, I really love that I seldom use operator new. Objects are instantiated in the stack and object creation is ridiculously fast.

Re:Better C – A subset of D Programming Language by msla on Tuesday April 28, 2020 @02:28PM (#23010185)
> C is about giving you maximal control over memory, with a very thin layer of abstraction over the hardware.

Which suggests an avenue I haven't seen anyone take yet: Pick a subset of features common to sane hardware and write a language which gives access to those features in a way which is reasonably portable and as explicit as C.

Pick a spot midway between a macro assembler and a language with a complicated optimizer and give as much access to the hardware as possible while not wedding yourself to a single ISA. Make cross-compilation a first-class feature using a module system, and error out if the programmer tries to use SIMD intrinsics when compiling for an MSP430 or something.

Re:Better C – A subset of D Programming Language by neutronicus on Tuesday April 28, 2020 @07:40AM (#23006007)
Rust is essentially C++ with auto-generated Move Constructors, and compiler errors on use-after-move, at the cost of preventing library developers from providing function overloads that eat temporaries as an optimization.

So, yeah, really not a C replacement at all imo

Re:Better C – A subset of D Programming Language by oconnor663 on Tuesday April 28, 2020 @09:37AM (#23006805)
I'm not super familiar with r-value references, but doesn't passing-by-value in Rust do essentially the same thing as that optimization does in C++?
Re:Better C – A subset of D Programming Language by neutronicus on Tuesday April 28, 2020 @10:12AM (#23007140)
Yes.

But this is all a bit uglier at the call site, since either the library provides value-semantically-similar things with different names that either eat their arguments or copy-from-reference them, provides only the argument-eating version and relies on the caller to `clone` at their discretion, or provides only the referencing version and fails to elide copies.

In C++ you can provide a referencing version, and an argument-eating version with the same name that is called automatically when the user gives it a temporary or specifically requests it via `std::move`. Automatically eating temporaries is very nice in the case where the caller would like to compose a bunch of "create-new-from-a-set-of-references" operations in a single expression to create one new thing from an initial set of references.

The canonical example is eliding copies in stuff like

    B*(A*x + b) + c
with overloaded * and + for vectors, since you really don't want to use something with a name other than + to request copy-elision. If you are one of those people who is grumpy about operator-overloading, you can imagine doing this with other "copy-some-refs-create-something" type functions, ... but actually you are probably grumpy about overloading those too.
Re:Better C – A subset of D Programming Language by oconnor663 on Tuesday April 28, 2020 @11:50AM (#23008189)
There is a Rust idiom that works a little bit like that overloading case:

    fn eats_a_string(s: impl Into<String>) {
        dbg!(s.into());
    }

    fn main() {
        eats_a_string("foo");
        eats_a_string("foo".to_string());
        eats_a_string(&"foo".to_string());
    }
In that case, eats_a_string() will allocate internally in the first and third cases, but it won't allocate in the second case, because the caller gives up ownership of its own allocated String. I wouldn't say this is a super common idiom, and "provide only the argument-eating version and rely on the caller to `clone` at their discretion" is often preferred to be simpler and more explicit. But you see it occasionally in the standard library, for example here: https://doc.rust-lang.org/std/ffi/struct.CString.html#method...
Re:Better C – A subset of D Programming Language by FreeFull on Tuesday April 28, 2020 @02:42PM (#23010338)
This sort of pattern tends to be more useful in case it's something like `CustomString` rather than just a regular `String`, and you still want your function/method to work with regular string literals. Also, other similar usecases, where there's a custom type that can be made from a standard type.
Re:Better C – A subset of D Programming Language by Shorel on Tuesday April 28, 2020 @02:24PM (#23010129)
Can you expand on this point of view?

I would enjoy reading a couple paragraphs, or a blog entry about this.

Re:Better C – A subset of D Programming Language by pjmlp on Tuesday April 28, 2020 @07:35AM (#23005977)
The only place where I think C on kernel will always have a place is on UNIX clones, they are just too symbiotic.

So as long as UNIX clones exist C will be around. OSes with POSIX APIs is a different matter, because there the kernel can be something totally different, e.g. mainframes.

So for that, we still need something like Checked C.

Or as Oracle, ARM, Google are doing, hardware memory tagging.

Re:Better C – A subset of D Programming Language by pjmlp on Tuesday April 28, 2020 @07:32AM (#23005945)
D is a much better option for those that believe that it isn't a crime to have a GC on a systems programming language.

It basically follows on the school of thought at Xerox PARC, ETHZ, and Microsoft Research.

Now what it lacks is more more manpower to improve its runtime capabilities and having a big name actually pushing it forward.

However this doesn't need to be a zero sum game, any language that helps to fix C is welcomed to the party, including attempts like Checked C and Frama-C.

Re:Better C – A subset of D Programming Language by schveiguy on Tuesday April 28, 2020 @01:37PM (#23009551)
the betterC form of D does not use the GC (and the compiler will let you know).
Re:Better C – A subset of D Programming Language by pjmlp on Tuesday April 28, 2020 @01:49PM (#23009730)
I used to be a regular on D forums. :)

Yes, betterC does not use the GC, but I was speaking about the whole language, and many anti-GC folks eventually discover that they can stick to regular D and still deliver what they were trying to do.

You can imagine an OS written in D, where BetterC mode gets used in the layers that for whatever reason cannot afford a GC, while all the remaning layers can happily take advantage of it.

Re:Better C – A subset of D Programming Language by schveiguy on Tuesday April 28, 2020 @02:38PM (#23010290)
Agreed. I've never felt the need to use betterC. The biggest use case I think is writing modules/libraries for other languages to use.
Re:Better C – A subset of D Programming Language by pron on Tuesday April 28, 2020 @06:44AM (#23005642)
Mine is Zig.
Re:Better C – A subset of D Programming Language by logicchains on Tuesday April 28, 2020 @07:04AM (#23005772)
Are you aware of anyone with large projects in Zig? I'm curious how fast it compiles, given in my view faster compilation is one of the biggest advantages of D over C++ and Rust, due to it having a lightweight compiler, DMD. Zig seems to still use LLVM, like Rust, which is relatively slow.
Re:Better C – A subset of D Programming Language by drfuchs on Tuesday April 28, 2020 @07:25AM (#23005908)
In his live-stream three days ago, Andrew Kelley showed the beginnings of Zig directly generating debug-mode executables, very quickly. And that’s before incremental compilation and linking. This guy is amazing.

https://www.twitch.tv/videos/602715503 [edit: at 10:20]

Re:Better C – A subset of D Programming Language by logicchains on Tuesday April 28, 2020 @07:44AM (#23006031)
Ah, that's awesome! Really cool to see a new language taking compile-time so seriously.
Re:Better C – A subset of D Programming Language by setpatchaddress on Tuesday April 28, 2020 @12:58PM (#23008985)
Fast compile time isn't actually that hard for a C-like language. Good code optimization and fast compile time is hard.
Re:Better C – A subset of D Programming Language by alexhutcheson on Wednesday April 29, 2020 @05:54PM (#23024875)
Fast compile time on large projects is really hard for C and C-compatible languages due to the preprocessor and #include. Not an issue if you're comparing compilation for a single file, but it turns into a substantial fraction if your compile time when that file transitively #includes 10s or 100s of headers.
Re:Better C – A subset of D Programming Language by dgellow on Tuesday April 28, 2020 @06:56AM (#23005720)
Zig is still very experimental though, the language isn’t stable yet
Re:Better C – A subset of D Programming Language by pron on Tuesday April 28, 2020 @07:01AM (#23005754)
True, but I'm not in a hurry. We've waited a long time for a C/C++ alternative, we can wait a bit more. Some find it in D, some find it in Rust, some (like me) are waiting for Zig, and some for something else.
Re:Better C – A subset of D Programming Language by AnIdiotOnTheNet on Tuesday April 28, 2020 @09:26AM (#23006721)
Definitely. However I will say that Zig does so many things right already that I struggle to code in more mature languages without thinking "I wish I was writing Zig right now" all the time.
Re:Better C – A subset of D Programming Language by smallstepforman on Tuesday April 28, 2020 @09:10AM (#23006579)
Andrew needs to radically change his stance on Tabs for the language to be taken seriously. White space must be ignored, and Andrew should overlook his personal bias and cater to the ‘preserve white space’ crowd. Because formatting and readability matter - a lot.
Re:Better C – A subset of D Programming Language by schaefer on Tuesday April 28, 2020 @09:30AM (#23006751)
You refer to "Andrew's stance on tabs" without being specific or providing a reference, so I wend digging for more details in the language FAQ.

From the reference I found[1], Andrew's stance is: The current zig tooling forces spaces instead of tabs because the tooling for the language is not yet complete. The self hosted compiler (which the community will start using just as soon as it's ready) already does accept both tabs and spaces.

So, I'm not able to understand your complaint at all. Perhaps you could you elaborate, provide a reference, or soften your stance?

[1]: https://github.com/ziglang/zig/wiki/FAQ#why-does-zig-force-m...

Re:Better C – A subset of D Programming Language by saagarjha on Tuesday April 28, 2020 @04:24PM (#23011479)
I am very curious why the stage1 compiler cannot just run zig fmt prior to parsing the code as a stopgap until the self-hosted compiler is complete…
Re:Better C – A subset of D Programming Language by rakoo on Tuesday April 28, 2020 @09:47AM (#23006896)
I'm not looking for an alternative to C but I used this quarantine to start nim, and it feels quite right until now
Re:Better C – A subset of D Programming Language by dom96 on Tuesday April 28, 2020 @10:38AM (#23007404)
Nim actually happens to be a very good better C. The fact that it compiles to C is a key feature of that.