News for hackers. Stuff that matters.
Re:Zig-style generics are not well-suited for most languages by WalterBright on Sunday October 09, 2022 @05:44PM (#33145607)
D templates can have "constraints", which are composed of conventional code that is executed at compile time to see what values and types are acceptable. It is not necessary to compile the template implementation looking for errors. The constraints also enable template overloading.

https://dlang.org/spec/template.html#template_constraints

It's simple to implement and understand, as it doesn't introduce any new syntax, and people already know how to write conventional code.

Re:Zig-style generics are not well-suited for most languages by valenterry on Sunday October 09, 2022 @11:16PM (#33147401)
But don't forget that it also doesn't compose as well as the other options.
Re:Zig-style generics are not well-suited for most languages by arunc on Sunday October 09, 2022 @11:33PM (#33147489)
I don't think it's true. Show some example?

Templates work even in betterC mode.

Re:Zig-style generics are not well-suited for most languages by valenterry on Sunday October 09, 2022 @11:50PM (#33147579)
Can you template a template?
Re:Zig-style generics are not well-suited for most languages by ben-schaaf on Sunday October 09, 2022 @11:59PM (#33147633)
I believe so; see the entire meta package in the standard library: https://dlang.org/phobos/std_meta.html
Re:Zig-style generics are not well-suited for most languages by valenterry on Monday October 10, 2022 @12:10AM (#33147691)
Usually when something is called "meta" it means that it is special and does not compose.

I had someone explaining D templates to me on an Idris meetup, but it's been a few years, so I might very well be wrong here and not remember correctly. I'm happy to be corrected.

My next question would be: can I write those meta templates myself and modify them with themselves? In the same way that I could have a type-level function that applies another type-level function twice and then apply it on itself to get a new type-level function that applies another type-level function four times.

Re:Zig-style generics are not well-suited for most languages by WalterBright on Monday October 10, 2022 @01:04AM (#33147956)
Do you mean does recursion work? Absolutely!
Re:Zig-style generics are not well-suited for most languages by valenterry on Monday October 10, 2022 @01:36AM (#33148133)
I wasn't talking about recursion but composition. Can I write template A and then apply it to itself to get template B and then apply that onto template C to get template D.
Re:Zig-style generics are not well-suited for most languages by WalterBright on Monday October 10, 2022 @02:07AM (#33148301)
Yes.
Re:Zig-style generics are not well-suited for most languages by valenterry on Monday October 10, 2022 @09:12PM (#33158302)
I can't quite wrap my head around how that would work, but I'll trust you and then take my original claim back!
Re:Zig-style generics are not well-suited for most languages by he_the_great on Saturday October 15, 2022 @12:02PM (#33215937)
auto alist = [1, 3, 2];

alist .sort .map!(x => x * x) .map!(x => x - x/2) .map!(x => x + 1);

This seems like what you're asking. Each of those is a template. And while this is all logic code, D has inferred the type aka: SortedRange!Map!Map!Map

With some details removed.

Re:Zig-style generics are not well-suited for most languages by WalterBright on Sunday October 09, 2022 @11:24PM (#33147442)
I don't see that as true.
Re:Zig-style generics are not well-suited for most languages by comex on Monday October 10, 2022 @01:40AM (#33148155)
But are templates guaranteed to compile without errors for any arguments that match the constraints? If not, then D is in the same situation as C++ with concepts: there is always the possibility of having to debug errors from the template implementation because the authors didn’t choose precise enough constraints. Rust, Go, Haskell, etc. do guarantee successful compilation, though at the cost of making their generics systems far less suitable for metaprogramming.
Re:Zig-style generics are not well-suited for most languages by pjmlp on Monday October 10, 2022 @02:01AM (#33148264)
Unfortunately C++20 only gave us concepts light, however Circle might be getting full concepts as they were originally designed, and those are like Rust, Go, Haskell, etc.
Re:Zig-style generics are not well-suited for most languages by thechao on Monday October 10, 2022 @09:09AM (#33150889)
There's a core contingent of ISO members who don't like (don't understand?) `concept_map`; without late (and layered) binding of types to concepts you're really just rolling out a template prediction scheme.
Re:Zig-style generics are not well-suited for most languages by pjmlp on Monday October 10, 2022 @09:12AM (#33150924)
I mainly use C++ on side projects and native library bindings, somehow I think ISO will need to change their ways if they want people to still care about anything past ISO C++23 for green field projects.
Re:Zig-style generics are not well-suited for most languages by WalterBright on Monday October 10, 2022 @02:07AM (#33148295)
> there is always the possibility of having to debug errors from the template implementation because the authors didn’t choose precise enough constraints

That is indeed possible, but it's hard to see why that is a problem. Any problems would show up at compile time, and fixing errors flagged by the compiler is pretty routine programming work.