0

I think I know what refertially transparent and pure mean. However here's a question about the two properties and how they differ.

As regards how referential transparency and/or purity are enforced in a language, I don't know much. (Not even this helped me understand.) I mean, I might know (kind of) how Haskell can deal with IO though being purely functional (see this), and I understand that I can't write impure functions because the type system just doesn't let me (or, better, it does let me in a controlled way, as I have to write unsafe explicitly).

But in C++, like in many other languages, functions are normally non pure nor referentially transparent.

So on the one hand I have Haskell which is constructed as a pure language, where every function is pure. On the other hand I have C++ which has no way to enforce purity (or does it?¹).²

But would it be possible, in the future, for the C++ language to provide a pure/whatever attribute that one could attach to a function so that the compiler would have to verify that the function is indeed pure (or compile-time fail otherwise)?


(¹) This question popped up in my mind when I first knew of [[gnu:pure]] and [[gnu:const]]. My understanding is that those (non-portable) attributes are for giving more guarantees to the compiler, so that it can optimize more stuff, not for telling it to check if the function is truly pure. After all this example seems to compile and run just fine.

(²) But I also remember another, very old language, which is not pure like Haskell, but gives you a PURE attribute to tell that a functions must be pure, and the compiler checked it: Fortran.

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 1
    Soo have you heard of `constexpr`? As for your question `Will it be possible..` "Yes". That's not a programming question. Everything is possible. – KamilCuk Jan 28 '22 at 17:09
  • _"Will it be possible ..."_ - You are asking an Opinion of people not qualified to answer unless they have some inside/future knowledge of the C++ Standard track. – Richard Critten Jan 28 '22 at 17:12
  • There may be a good question somewhere here, but "Is it possible to foresee?" is not a good Stack Overflow question, in my opinion. – Drew Dormann Jan 28 '22 at 17:13
  • Maybe I have to look up what exactly `constexpr` means. For me at the moment it means that _it can be done at compile time_, but from there to _pure_ the jump is a bit big for me atm. Maybe I'll reformulate the question. Will delete for now. – Enlico Jan 28 '22 at 17:13
  • @KamilCuk, thinking a bit more about it, [I can't make any function `constexpr` just because it's pure](https://godbolt.org/z/e5rexYj9P). – Enlico Feb 02 '22 at 16:19

0 Answers0