Author Topic: to const or not to const  (Read 21012 times)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: to const or not to const
« Reply #45 on: June 18, 2014, 11:54:12 pm »
From the doc:
Quote
Note that specifying const is a contract between the programmer and the compiler. It is the programmer who tells the compiler that the contents of the const parameter will not be changed when the routine is executed, it is not the compiler who tells the programmer that the parameter will not be changed.

Why would I need to specify a const then? just add a comment on top of the procedure please do not change the param1 value and you are done.
If what the documentation says is true which its not and can be simple proved with the following example.
Code: [Select]
procedure Test(Const a:string);
begin
  a:='This should not be allowed';
end;
can this be done? if not then why if the compiler is not enforcing the const then who is?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12641
  • Debugger - SynEdit - and more
    • wiki
Re: to const or not to const
« Reply #46 on: June 19, 2014, 12:52:30 am »
That is documented do:
Code: [Select]
and also to perform certain checks on the code inside the routine: namely, it can forbid assignments to the parameter.
That is not in conflict with the rest of the doc.

Here is another bit from that doc:
Code: [Select]
Specifying a parameter as Constant is giving the compiler a hint that the contents of the parameter will not be changed by the called routine
There is  a difference  between "assignments to the parameter" (changing it directly)
and
"contents of the parameter will not be changed by the called routine" which is any change, including change by pointer hacks, change by calling other functions that change it, .....

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: to const or not to const
« Reply #47 on: June 19, 2014, 01:03:16 am »
yeah I've read the docs and I do disagree on all counts. defining a parameter as const the compiler protects the parameter from assinging to it an other value not from the hacks that I might come up with to trick the compiler and change that value. But then again we are out of context for this thread. I'm not going to start a rewrite the documentation thread I'm stating the blindly obvious here.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

stocki

  • Full Member
  • ***
  • Posts: 144
Re: to const or not to const
« Reply #48 on: June 21, 2014, 04:44:47 pm »
const or no const on integer, tobject, etc has no effect on the produced code. Exception: string. There will be heavy ref count pre and postfix stuff without const (seen in Delphi xe6, FPC debugger is junk).

« Last Edit: June 21, 2014, 04:49:07 pm by stocki »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: to const or not to const
« Reply #49 on: June 21, 2014, 05:26:34 pm »
const or no const on integer, tobject, etc has no effect on the produced code. Exception: string.

open arrays,dynamic arrays and managed types in general.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Dibo

  • Hero Member
  • *****
  • Posts: 1061
Re: to const or not to const
« Reply #50 on: June 21, 2014, 09:05:15 pm »
Didn't read all discussion but isn't const (like var) faster if you pass big string? Let say that you need to pass some big string (HTML) into parser function/method and you call this function many times in loop. Without const a copy of string will be passed on each loop (which could be expensive), with const a reference will be passed (direct pointer in memory) or am I wrong?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: to const or not to const
« Reply #51 on: June 21, 2014, 09:28:25 pm »
there are 3 possibilities 1) const 2) var 3) by val (VB terminology) in const and var a reference is passed on by val it should be a copy bu since there is the copy on write mechanism for string a reference could possible do the same thing I did not check it so I"m not 100% sure about the 3rd option it is one that I never use my self.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

BeniBela

  • Hero Member
  • *****
  • Posts: 959
    • homepage
Re: to const or not to const
« Reply #52 on: June 22, 2014, 01:48:03 am »
The most confusing/annoying part of this is, when the compiler starts aliasing variables that do not look like they are the same variable

Code: [Select]
function trimLeft(const s: string): string;
var skip: Integer;
  i: Integer;
begin
  skip := 1;
  while s[skip] = ' ' do inc(skip);
  SetLength(result, length(s) - skip);
  if result = '' then exit;
  for i := 1 to length(result) do
    result[i] := s[i+skip-1];
end;

A perfectly reasonable way to write a trim function.

Search the non space part in the old variable (s) and copy it to a new variable (result)

And then it blows up, because result and s might be the same variable...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: to const or not to const
« Reply #53 on: June 22, 2014, 04:37:13 am »
Didn't read all discussion but isn't const (like var) faster if you pass big string? Let say that you need to pass some big string (HTML) into parser function/method and you call this function many times in loop. Without const a copy of string will be passed on each loop (which could be expensive), with const a reference will be passed (direct pointer in memory) or am I wrong?
Read the first remark: http://www.freepascal.org/docs-html/ref/refsu63.html

 

TinyPortal © 2005-2018