News for hackers. Stuff that matters.
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).