Recent

Author Topic: Const record parameters by default  (Read 24091 times)

Ryan J

  • Full Member
  • ***
  • Posts: 112
Re: Const record parameters by default
« Reply #60 on: April 05, 2024, 04:23:38 pm »
Btw, const parameters aren’t even necessarily good. From a low-level perspective, it’s harder to pass a record to a procedure by value, but easier to access from inside the procedure: ]

Another question, isn't it also slower to read from a pointer then if it was copied to the stack? If loading the value from the pointer is slower than reading from the stack then a parameter which was read often would benefit from copied anyways. No idea how this really works but it seems like the code generator should be doing this for us when it knows it's smart to do so.

alpine

  • Hero Member
  • *****
  • Posts: 1067
Re: Const record parameters by default
« Reply #61 on: April 05, 2024, 05:39:32 pm »
C++ has a const for methods? As in it knows if the method writes to any fields and requires it to be const?

In Swift if you have a method on a struct and you write to one of its fields you are required to mark the method as "mutating" so the compiler knows calling it may mutate data and thus is not const-compatible. It's kind of annoying and I don't think I'd want to be forced into this for Pascal but it would be useful to opt in to. This would make const vastly more useful than it is now.
https://en.cppreference.com/w/cpp/language/member_functions#Member_functions_with_cv-qualifiers

AFAIK right from the start. C++98 introduces mutable so it must be before that, circa 1990.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Ryan J

  • Full Member
  • ***
  • Posts: 112
Re: Const record parameters by default
« Reply #62 on: April 07, 2024, 03:06:01 pm »
C++ has a const for methods? As in it knows if the method writes to any fields and requires it to be const?

In Swift if you have a method on a struct and you write to one of its fields you are required to mark the method as "mutating" so the compiler knows calling it may mutate data and thus is not const-compatible. It's kind of annoying and I don't think I'd want to be forced into this for Pascal but it would be useful to opt in to. This would make const vastly more useful than it is now.
https://en.cppreference.com/w/cpp/language/member_functions#Member_functions_with_cv-qualifiers

AFAIK right from the start. C++98 introduces mutable so it must be before that, circa 1990.

ohhhh that's what that's for. I always wondered what the "const function" thing in C++ was but I never got deep enough into the language to use it myself.

I think this is a smart feature, however, once it's there I think people are going to be compelled to use it everywhere and burden themselves. Swift has this problem too, ok so the compiler forces you to write "correct" code but you may be wasting time if this problem doesn't really exist for you. What FPC does with minimal protection is not great either though.

 

TinyPortal © 2005-2018