readonly variables

classic Classic list List threaded Threaded
44 messages Options
123
Reply | Threaded
Open this post in threaded view
|

readonly variables

fpc-3
Hello,

is it possible to set a variable in a programm as a readonly variable?

I would set a variable at a position in the runing programm. Since this
time the variable should be readonly. The next set of the variable
should produce an error.

In bash programming you found a command "readonly" making this effect.
Is there a command for FreePascal?

Thanks,
Markus

program readonly;
...
v:=1; // At this position it should be readonly.
...
end.




_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Jürgen Hestermann
> is it possible to set a variable in a programm as a readonly variable?

Not that I am aware of. But for what reason do you want such a behaviour?

And if I think it over, this can only work at runtime (letting the program crash on the second assignment). At compile time the compiler may not exactly know which assignment is done in what order. If you set the variable in a procedure or function the compiler would have to check when this procedure is called (which can be called by another procedure and so on). The first call (and assignment) would be ok but the second should generate an error? How should the compiler know how often the routine is called? This can even be dependend on input data.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Mattias Gaertner
On Sat, 28 Nov 2009 14:58:26 +0100
Jürgen Hestermann <[hidden email]> wrote:

> > is it possible to set a variable in a programm as a readonly variable?
>
> Not that I am aware of. But for what reason do you want such a behaviour?
>
> And if I think it over, this can only work at runtime (letting the program crash on the second assignment). At compile time the compiler may not exactly know which assignment is done in what order. If you set the variable in a procedure or function the compiler would have to check when this procedure is called (which can be called by another procedure and so on). The first call (and assignment) would be ok but the second should generate an error? How should the compiler know how often the routine is called? This can even be dependend on input data.

Same under bash.

Use the following:

property MyVar: integer read FMyVar write SetMyVar;

And in SetMyVar you can set flag and raise an exception if set for the
second time.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Howard Page-Clark
On Sat, 28 Nov 2009 15:07:42 +0100
Mattias Gaertner <[hidden email]> wrote:

> On Sat, 28 Nov 2009 14:58:26 +0100
> Jürgen Hestermann <[hidden email]> wrote:
>
> > > is it possible to set a variable in a programm as a readonly variable?

> Use the following:
>
> property MyVar: integer read FMyVar write SetMyVar;
>
> And in SetMyVar you can set flag and raise an exception if set for the
> second time.

If the value is known in advance and if no runtime alteration of the
variable is allowed you would of course declare the value as const
rather than var. But this may not fit your case?

Howard
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Aleksa Todorovic-3
In reply to this post by fpc-3
You can use read function:

var
  DirectAccessToValue: T;

function Value: T; inline;
begin
  Result := DirectAccessToValue;
end;

begin
  ...
  DirectAccessToValue := ...;
  ...
  DoSomething(Value);
end.


On Sat, Nov 28, 2009 at 13:55, Markus Glugla <[hidden email]> wrote:

> Hello,
>
> is it possible to set a variable in a programm as a readonly variable?
>
> I would set a variable at a position in the runing programm. Since this
> time the variable should be readonly. The next set of the variable
> should produce an error.
>
> In bash programming you found a command "readonly" making this effect.
> Is there a command for FreePascal?
>
> Thanks,
> Markus
>
> program readonly;
> ...
> v:=1; // At this position it should be readonly.
> ...
> end.
>
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



--
Aleksa Todorovic - Lead Programmer
Eipix Entertainment
http://www.eipix.com/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Xgelb
In reply to this post by Jürgen Hestermann
Hi Jürgen,

> Not that I am aware of. But for what reason do you want such a behaviour?
I wrote data analysis programs which are very difficult. I change some
lines to optimize a program. Somethimes it is necessary that I will save
a variable after an input because the second setup of the variable is an
error the first is ok.

If I use the bash I can save the variables after an initialisation with
the "readonly" command. At this time it is not possible to reset the
variable. This is very helpfull, because you can avoid an anlysis error
by forcing a program runtime error.


Thanks,
Markus


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Anthony Walter-3
In reply to this post by fpc-3
procedure InitMyVariable(Value: T);
function MyVariable: T;

implementation

var
  PrivateMyVariable: T;
  PrivateMyVariableSet: Boolean;

procedure InitMyVariable(Value: T);
begin
  if not PrivateMyVariableSet then
    PrivateMyVariable := Value;
  PrivateMyVariableSet := True;
end;

function MyVariable: T;
begin
  Result := PrivateMyVariable;
end;
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Howard Page-Clark
On Sat, 28 Nov 2009 12:10:48 -0500
Anthony Walter <[hidden email]> wrote:

> procedure InitMyVariable(Value: T);
> function MyVariable: T;
>
> implementation
>
> var
>   PrivateMyVariable: T;
>   PrivateMyVariableSet: Boolean;
>
> procedure InitMyVariable(Value: T);
> begin
>   if not PrivateMyVariableSet then
>     PrivateMyVariable := Value;
>   PrivateMyVariableSet := True;
> end;
>
> function MyVariable: T;
> begin
>   Result := PrivateMyVariable;
> end;

PrivateMyVariableSet is not intialised, so will have an undefined
(random) value when InitMyVariable is first called.
Mattias' code given earlier avoids this problem.

Howard
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Anthony Walter-3
> PrivateMyVariableSet is not intialised, so will have an undefined
> (random) value when InitMyVariable is first called.
> Mattias' code given earlier avoids this problem.

Bzzzt. Wrong.

Global variables (even in the implementation section) are always
initialized to 0.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Michael Van Canneyt


On Sat, 28 Nov 2009, Anthony Walter wrote:

>> PrivateMyVariableSet is not intialised, so will have an undefined
>> (random) value when InitMyVariable is first called.
>> Mattias' code given earlier avoids this problem.
>
> Bzzzt. Wrong.
>
> Global variables (even in the implementation section) are always
> initialized to 0.

This is not guaranteed in any way. It happens to be so most of the time,
but your code should never assume this is so, except for global Ansistring
variables.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Anthony Walter-3
> This is not guaranteed in any way. It happens to be so most of the time,
> but your code should never assume this is so, except for global Ansistring
> variables.

If all globals weren't initialized to 0 a lot of code from lots of
people would potentially be in trouble. I've been using Pascal/Delphi
for a very long time. Trust me I know my stuff.

>From the language specification:

Section: Data Types, Variables, and Constants
Sub-section: Declaring Variables
Page: 102

"If you don't explicitly initialize a global variable, the compiler
initializes it to 0."
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Michael Van Canneyt


On Sat, 28 Nov 2009, Anthony Walter wrote:

>> This is not guaranteed in any way. It happens to be so most of the time,
>> but your code should never assume this is so, except for global Ansistring
>> variables.
>
> If all globals weren't initialized to 0 a lot of code from lots of
> people would potentially be in trouble. I've been using Pascal/Delphi
> for a very long time. Trust me I know my stuff.

I believe that. I know it is so in practice, but nowhere it says in the
Pascal language specification that this is guaranteed by the compiler.

>
>> From the language specification:
>
> Section: Data Types, Variables, and Constants
> Sub-section: Declaring Variables
> Page: 102
>
> "If you don't explicitly initialize a global variable, the compiler
> initializes it to 0."

That should be removed, actually.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Anthony Walter-3
> That should be removed, actually.

I'll take that as an admission that you were wrong. It's in the
specification, lot's of code uses the feature, and it works the way I
described.

Changing the specification to match your argument is stupid.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Michael Van Canneyt


On Sat, 28 Nov 2009, Anthony Walter wrote:

>> That should be removed, actually.
>
> I'll take that as an admission that you were wrong. It's in the
> specification, lot's of code uses the feature, and it works the way I
> described.

I'm not admitting anything here, I am attempting to enlighten you :-)

>
> Changing the specification to match your argument is stupid.

I'm not changing any specifications. They are what they are,
I neither make nor control them. I did create the docs, and at
that time I believed that the zero-out behaviour of the
compiler/linker for certain sections in the executable
could be taken for granted.

It was Jonas Maebe (Jonas, correct me if I'm wrong) who pointed
out (already some time ago) that this behaviour is purely
coincidental (but admittedly convenient), and should not be taken
for granted. At that time I should have removed the statement
you refer to from the docs, but I did not (I probably forgot
it was there in the first place).

So: no "admissions", just a lesson in history of FPC and its docs.
I'll now confer with the rest of the Core team about our 'official'
attitude in this.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Anthony Walter-3
Okay, I am going to call bullshit on you. This is the second time (in
a few weeks) where you've replied to something I've written with wrong
information.

This first time concerning  the topic "const records passed
incorrectly" you said, "It is nowhere written in the Delphi specs that
const parameters are passed by reference. It is often so, but is by no
means guaranteed"

That time I corrected you by pointing out the line in the help file
contradicting your assertion. Martin Schreiber also chimed in,
pointing out you were plainly wrong. In case you forgot, here is a
refresher: http://tinyurl.com/yz8pqfv

This second time regarding the current discussion you said: "This is
not guaranteed in any way." and "nowhere it says in the Pascal
language specification that this is guaranteed by the compiler"

And I responded with the section, subsection, page number, and direct
quote contradicting your assertion.

I don't care if you claim to have written documentation, you clearly
either don't have a grasp of the English language, good memory,
research skills, or some combination those deficiencies. When we
(English speakers) say "nowhere", it means "a state of nonexistence"
or "not anywhere". When I, and others, then find the exact lines of
said documentation proving your assertions that nothing of the sort
was in the documentation, then you are wrong, and now doubly so.

You have lost credibility IMO, and I foresee a trend where you are
digging yourself further into a hole.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Andrew Hall
In reply to this post by Michael Van Canneyt
Michael - I see you are one of the authors of fpc (thank you), so I assume your statement is true by virtue of "inside knowledge".  But this is a concern.  As I'm sure you will know, in Delphi globals are always initialised to zero - and in my experience (almost 15 years with a 30-strong Delphi developer team) it is universally relied upon.  Is this a case where FPC does not conform to Delphi behaviour?  If not, why not?  It would seem simple enough to ensure the global space in the heap is zeroed at allocation - or even that the compiler initialised each individually by "assuming" " = 0", " = nil", etc at the end of a global declaration if nothing else has been specified.

Regards,

Andrew Hall.

On 28 Nov 09, at 11:58 , Michael Van Canneyt wrote:

>> Global variables (even in the implementation section) are always
>> initialized to 0.
>
> This is not guaranteed in any way. It happens to be so most of the time,
> but your code should never assume this is so, except for global Ansistring variables.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Ivo Steinmann
In reply to this post by Anthony Walter-3
Anthony Walter schrieb:
> I don't care if you claim to have written documentation, you clearly
> either don't have a grasp of the English language, good memory,
> research skills, or some combination those deficiencies.
Michael and so Jonas are Authors and Developers of FPC. Most FPC Authors
are from Europe and english is not their first language...

-Ivo Steinmann (Swiss, just to mention that english isn't my first
language either)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Bart-48
Can we please not start bashing people here?

(Cuuntry of origin: Netherlands, so not a native English speaker as well)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Florian Klämpfl
In reply to this post by Anthony Walter-3
Anthony Walter schrieb:
> Okay, I am going to call bullshit on you.

What about just unsubscribing from the list? Nobody needs you here. MvC
did an incredible job on FPC docs, you just waste our time.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: readonly variables

Mehmet Erol Sanliturk-3
Florian Klaempfl wrote:
> Anthony Walter schrieb:
>> Okay, I am going to call bullshit on you.
>
> What about just unsubscribing from the list? Nobody needs you here. MvC
> did an incredible job on FPC docs, you just waste our time.
>


Actually it is a very ridiculous behavior to talk about developers of
the Free Pascal and Lazarus by using such words . To hear such an ugly
sentence is a very disgusting experience . Ideas may be different from
the Free Pascal or Lazarus developers , but expressing such ideas do not
give any one to right to use any meaningless and useless expressions .

No one should forget that Free Pascal and Lazarus developers are
consuming a portion of their lives for benefits of us , and they only
deserve a great thank you to themselves .

I am writing programs since 1970 and I know many programming languages .
Pascal is still the one of the most elegant languages . The tasks of
developers of Free Pascal and Lazarus are not easy ones . Instead of
blaming them , it is necessary to help them by writing test programs ,
feature implementation samples tested very well and accompanied with
test programs and documentation , documentation of parts which can be
used for teaching of these parts , bug reports containing sufficiently
expressive how to fixes may be developed and others .


Thank you very much .


Mehmet Erol Sanliturk





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
123