Live data from Hacker News

Better C – A subset of D Programming Language

dlang.org

321–330 of 360 posts

Re: Better C – A subset of D Programming Language

#321
post #190

Earlier quoted context omitted.

> 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? Skim…

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 vari…

> 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

#322

Walter here - AMA!

Would you be open to being interviewed for the Computer History Museum? I suggested this in the CHM feedback form, but I'm not sure if it disappeared into the void or if the form actually even works. I know that sometimes even their search chokes. See their Oral Histories collection: https://www.computerhistory.org/collections/oralhistories/

Sure, I think that would be fun.

We've got a paper on the history of D accepted into this year's History Of Programming Languages (HOPL), which makes us very proud. Andrei, Mike and myself spent a lot of time combing through old emails and n.g. postings to develop an accurate timeline of when and how things came about and from whom.

I was really looking forward to the HOPL conference in London in June to present the paper, but CV scuttled that.

Re: Better C – A subset of D Programming Language

#323
post #290

The chosen example (printf) doesn't make D look very good compared to C. If I modify the example to include a simple type error: printf("Hello %s", 123); Then the D version will still happily compile without warnings and will segfault. Compiling with GCC -Wall -Werror, the type error is easily caught by the compiler.

Use dmd master and you will have printf/scanf format argument validation [0]. [0] https://github.com/dlang/dmd/pull/10852

Yeah, I am glad we fixed that one.

Re: Better C – A subset of D Programming Language

#325
post #93
post #83

Earlier quoted context omitted.

I'm actually curious. until now I came across new language that sells themselves as c replacement but with a lot and lot of new features, usually "here is a new language it does a lot more than C but still compatible with it". examples in top of my head: - adding object oriented feature like built-in constructor/destructor while only default values are needed as week as a defer statement. - weird template, while I ca…

There's no such thing as a hygienic macro system if you mean textual macros. Also array type is the single biggest thing required to fix C. How many bugs in big C projects are caused by bad indexing and overruns. On top of, "oh no free features".

Here's my proposal for fixing that in C:

https://www.digitalmars.com/articles/C-biggest-mistake.html

Re: Better C – A subset of D Programming Language

#326
post #281
post #260

Earlier quoted context omitted.

Do you not like those because they're unfamiliar? What about the syntax indicates over-engineering? Here's what I see in your example: - @ indicates built-in function, which avoids namespace collisions. - Optional types via the ?, which solves the null pointer problem. - Real iterators, which are less error-prone and nicer to read than traditional for loops in a lot of common applications. - The || syntax is better t…

Thanks for spending your time explaining this. I think a lof people will understand those lines better. I actually already knew everything. I watched closely the development of Zig, and just give up when I realized all those decisions where made carelessly in my view or I just simply disagree with the direction of the syntax. Since you took your time I will take mine to address what I don't like: - There is no way to…

> It's like everything has been carefully design to be even less readable than C code.

D is designed to be very readable to the C programmer. Some changes, like replacing:

    (int)(expression)
with:

    cast(int)(expression)
is designed to make the code more readable, and greppable. Cast is a fundamentally dangerous operation, so being able to grep-and-check for such is worthwhile.

The D compiler actually recognizes the C form and suggests using the D form to fix it.

Converting C to DasBetterC is largely just a) eliminating use of the preprocessor and then b) making the syntax changes suggested by the compiler.

Re: Better C – A subset of D Programming Language

#327
This is a nice tutorial introduction on using D as a better replacement for C [1].

Some excerpts "At one time, C was my go-to language for most programming. One day I realised that most of my C programs kept reimplementing things from C++: dynamic arrays, better strings, polymorphic classes, etc".

[1]https://theartofmachinery.com/2019/04/05/d_as_c_replacement....

Re: Better C – A subset of D Programming Language

#328

Earlier quoted context omitted.

> What am I looking at there? Are those all traits on Vec that I then have to parse mentally so I can understand what I can do with it? Are all those pages basically to say "Vec works like an array of T"? No. The type which tells you that Vec works like a slice of T is https://doc.rust-lang.org/std/vec/struct.Vec.html#impl-Deref The others are separate abstract operations which are available (implemented) on vecs e.g…

No this one: "Returns: A range with each fun applied to all the elements. If there is more than one fun, the element type will be Tuple containing one element for each fun."

To get that degree of flexibility in Rust you’d have to turn to macros, then the return type will depend on the generated code. That could be pretty much anything, so you’d need to read the docs anyway. Perhaps the languages are not that different after all.

D’s syntax for the template body looks much more similar to normal D code, in Rust the pattern macro syntax is like a language to itself and procedural macros need a fair bit of boilerplate, including explicit “quote” blocks.

Re: Better C – A subset of D Programming Language

#329

Earlier quoted context omitted.

I'm glad that Rust has no `auto`. I find this: fn map (it: I) -> impl Iterator where I: Iterator , U: From { it.map(|t| From::from(t)) } infinitely more readable than fn map (it: I) -> auto where I: Iterator , U: From { it.map(|t| From::from(t)) } The type signature of the first one clearly tells me that the return type is an `Iterator `, even though the actual type cannot be named because of the anonymous closure. T…

This wouldn't work for D. D doesn't constrain return types to something less than what they are. A Range is not just an Iterator, it has optional pieces that depend completely on the given type. For example, the return of map could provide indexing, or it could provide forward and backward iteration, or it might have methods that are completely unrelated to the type. There is no good reasonable and non-confusing way…

> For example, the return of map could provide indexing,

You can provide more interfaces in Rust:

    fn map(...) -> impl Iterator + Index
but you can't provide "conditional" interfaces (for most interfaces at least), e.g., this won't work:

    fn map(...) -> impl Iterator + ?Index
where `?Index` reads as "maybe implements Index".

To allow that you would essentially need to say that "if the input implements `Index`, the output implements `Index`":

    fn map(it: I) -> O
        where I: Iterator, U: From,
              O: impl Iterator,
              I:?Index -> O:?Index
    {
        it.map(|t| From::from(t))
    }
The type system implementation already supports these types of constraints, but there isn't a language extension that exposes that. I don't see any fundamental reasons that make this impossible, but there are many trade-offs involved.

Notice that, for example, the output Range does not implement the same interfaces as the input range, e.g., the input Range implements an `Index` interface over a range of `T`s, but the output Range implements an `Index` interface over a range of `U`s. In D this is super implicit in the implementation details (body) of an equivalent `map` function, but in Rust it needs to be part of the type signature to avoid changes to a function body to silently cause API breaking changes. In D, you could change the body of map to map only from Range(T) -> Range(T), without changing its interface, and that would break all code using it to map a Range(T)->Range(U).

Re: Better C – A subset of D Programming Language

#330

Earlier quoted context omitted.

This wouldn't work for D. D doesn't constrain return types to something less than what they are. A Range is not just an Iterator, it has optional pieces that depend completely on the given type. For example, the return of map could provide indexing, or it could provide forward and backward iteration, or it might have methods that are completely unrelated to the type. There is no good reasonable and non-confusing way…

In rust this would be expressed as multiple impl blocks with different generic parameters which show up as such in the documentation. https://doc.rust-lang.org/std/vec/struct.Vec.html#implementa...

I don't think that's what the OP is talking about.

They want to have a generic function that returns opaque types implementing different interfaces depending on the inputs. I've replied to that above.

Post reply on HN