News for hackers. Stuff that matters.
Re:Zig's Comptime Is Bonkers Good by SkiFire13 on Tuesday January 07, 2025 @03:12AM (#42620409)
> For example, I believe C++ template programming also breaks "parametricity" by supporting template specialisation.

C++ breaks parametricity even with normal templates, since you can e.g. call a method that exists/is valid only on some instantiations of the template.

The issue is that the compiler can't help you check whether your template type checks or not, you will only figure out when you instantiate it with a concrete type. Things get worse when you call a templated function from within another templated function, since the error can then be arbitrarily levels deep.

> My point is that in makes sense for C++ to allow this, I think.

Whether it makes sense or not it's a big pain point and some are trying to move away from it (see e.g. Carbon's approach to generics)

Re:Zig's Comptime Is Bonkers Good by marhee on Tuesday January 07, 2025 @04:33AM (#42620843)
> C++ breaks parametricity even with normal templates

I might be wrong here, but as I understand it "parametricity" means loosely that all instantiations use the same function body. To quote wikipedia:

"parametricity is an abstract uniformity property enjoyed by parametrically polymorphic functions, which captures the intuition that all instances of a polymorphic function act the same way"

In this view, C++ does not break parametricity with "normal" (i.e. non-specialised) templates. Of course, C++ does not type check a template body against its parameters (unless concepts/trairs are used), leading to the problems you describe, but it's a different thing as far as I understand.

Re:Zig's Comptime Is Bonkers Good by jcelerier on Tuesday January 07, 2025 @08:32AM (#42622115)
Hm, wouldn't any use of if constexpr break that definition?

e.g.

     template<typename T>
     void f() { 
       if constexpr (is_int<T>) { return 0; } 
       else ...
Re:Zig's Comptime Is Bonkers Good by deredede on Tuesday January 07, 2025 @08:41AM (#42622178)
Parametricity is about behavior, not code. A function parametric in a variable should bevave identically for all values of the variable. If one instance of a C++ template fails to compile and another instance of the same template does compile it is a stretch to say they behave identically, even though they use the same code.
Re:Zig's Comptime Is Bonkers Good by lmm on Tuesday January 07, 2025 @08:45AM (#42622213)
To be parametric it needs to be the same body semantically, not just textually. Particularly in C++ with its heavy operator overloading and limited safety, you can very easily write a template whose body will do the right thing for some types and be undefined behaviour for others (e.g. if your template has comparisons in it and then you instantiate it with a pointer or something).