[fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Graeme Geldenhuys
Hi,

Recently I learned that you can have writeable typed constants. That
sounds rather like an oxymoron to my. Writeable constants make no sense,
is that then simply a variable? What is the use of a writeable typed
constant?

Below is two examples of how they can be used, and support for them are
toggled with the compiler directive $J.

In both these cases I would think a variable would make a lot more
sense. For the Singleton implementation, you simply need to specify a
unit wide AppSingleton variable in the implementation section (so that
it's not completely global or visible to other units.

Can anybody explain the point of writeable typed constants?

const
   foo: Integer = 12;
begin
   foo := 14;
end.

===

// Singleton implementation using typed constants
function gAppManager: TApplicationManager;
const
  {$IFDEF DELPHI}{$J+}{$ENDIF}
  AppSingleton: TApplicationManager = nil;
  {$IFDEF DELPHI}{$J-}{$ENDIF}
begin
  if not Assigned(AppSingleton) then
AppSingleton := TApplicationManager.Create;
  Result := AppSingleton;
end;


Regards,
  - Graeme -

___
fpGUI - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Vincent Snijders

Graeme Geldenhuys schreef:

Hi,

Recently I learned that you can have writeable typed constants. That
sounds rather like an oxymoron to my. Writeable constants make no sense,
is that then simply a variable? What is the use of a writeable typed
constant?



Backwards compatibility with turbo pascal, which lacked initialized 
variables and writable constants were the alternative.


Vincent
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Graeme Geldenhuys
Vincent Snijders wrote:
 
 Backwards compatibility with turbo pascal, which lacked initialized
 variables and writable constants were the alternative.

Thanks Vincent. The Kylix 3 help mentioned early versions of Delphi and
Borland Pascal, which made me think that it must have been some
limitation in the older compilers. Thanks for confirming this.


Regards,
  - Graeme -

___
fpGUI - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread fpclist
On Thursday 18 June 2009 08:51:13 Vincent Snijders wrote:
 Graeme Geldenhuys schreef:
  Hi,
 
  Recently I learned that you can have writeable typed constants. That
  sounds rather like an oxymoron to my. Writeable constants make no sense,
  is that then simply a variable? What is the use of a writeable typed
  constant?

 Backwards compatibility with turbo pascal, which lacked initialized
 variables and writable constants were the alternative.

Is this likely to be dropped in future versions of FPC?
I have taken advantage of this in functions where a static variable would be 
used in other languages like C.

In the following example, the typed constant Count is initialised only once 
when Test() is first called and thereafter retains its value between function 
calls. 

procedure Test();
const
  Count : Cardinal = 1;
begin
   Writeln('The value of Count is now ', Count);
   Inc(Count);
end;

var
  i : LongInt;

begin
   for i := 1 to 10 do
  Test();
end.

will yield;

The value of Count is now 1
.
.
The value of Count is now 10

Although a writable constant makes no sense, it come in handy and is a cool 
feature to have. Perhaps a construct derived from this feature can be added 
to FPC?

Regards,
Nino

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Graeme Geldenhuys
fpcl...@silvermono.co.za wrote:
 
 Is this likely to be dropped in future versions of FPC?

No idea. But I have noticed that FPC (mode objfpc) defaults to allow
writeable typed constants, whereas Delphi and Kylix doesn't. In the
latter two compilers you have to enable is support with $J+ compiler
directive.

I don't know (have not tested) what FPC does with mode delphi though.
But seeing that this feature is just for backward compatibility, maybe
it should be disallowed by default in mode objfpc as well?

Regards,
  - Graeme -

___
fpGUI - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Graeme Geldenhuys
 
 I don't know (have not tested) what FPC does with mode delphi though.

OK, just tested FPC modes objfpc and delphi. In both cases writeable
typed constants are allow by default. This is not compatible with
current Delphi compilers.

I still think this should be disable by default in both compiler modes -
after all, it's just a backward compatibility feature to Turbo Pascal days.


Regards,
  - Graeme -

___
fpGUI - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread fpclist
On Thursday 18 June 2009 10:48:57 Graeme Geldenhuys wrote:

 I don't know (have not tested) what FPC does with mode delphi though.
 But seeing that this feature is just for backward compatibility, maybe
 it should be disallowed by default in mode objfpc as well?

It also works in Delphi mode.

Nino

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Vincent Snijders

Graeme Geldenhuys schreef:

I don't know (have not tested) what FPC does with mode delphi though.


OK, just tested FPC modes objfpc and delphi. In both cases writeable
typed constants are allow by default. This is not compatible with
current Delphi compilers.

I still think this should be disable by default in both compiler modes -
after all, it's just a backward compatibility feature to Turbo Pascal days.


I disagree. It would be backwards incompatible with existing objfpc 
programs. That decision should have been made 10 (?) years ago, when 
mode objfpc was introduced.


Vincent
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Graeme Geldenhuys
Vincent Snijders wrote:
 
 I disagree. It would be backwards incompatible with existing objfpc
 programs. That decision should have been made 10 (?) years ago, when
 mode objfpc was introduced.

Not everything was done correctly 10 years ago (or first time round).
There are many bug reports still being submitted to fix delphi
compatibility issues in FPC. They are still being fixed today, so why
must writeable typed constants be any different to those bug reports?

FPC often implements something that breaks existing code. If the code is
being maintained, the developers can simply fix there code. I had to do
that for fpGUI Toolkit, so why can't others. Lazarus code also has to be
fixed constantly due to FPC changes in behaviour.


Regards,
  - Graeme -

___
fpGUI - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Florian Klaempfl
Graeme Geldenhuys schrieb:
 Vincent Snijders wrote:
 I disagree. It would be backwards incompatible with existing objfpc
 programs. That decision should have been made 10 (?) years ago, when
 mode objfpc was introduced.
 
 Not everything was done correctly 10 years ago (or first time round).

Nevertheless, writable typed constants are simply an important part of
Borland style Object Pascal.

 There are many bug reports still being submitted to fix delphi
 compatibility issues in FPC. They are still being fixed today, so why
 must writeable typed constants be any different to those bug reports?
 
 FPC often implements something that breaks existing code. 

Examples :)? Then I'll tell you what's the difference to writable typed
constants.


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread fpclist
It's intersting to note that as far back as Delphi 5,(oldest I have on my 
system), attempting to write to a typed constant causes a left side cannot 
be assigned to compiler error.

I guess as long as FPC compiler offers TP7 compatibility, this feature must be 
retained. Also, bear in mind that some developers might want to mix old style 
constructs with new ones, (TP7 object and Delphi classes), so restricting 
available feature to certain modes might not be the way to go.

Regards,
Nino

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Marco van de Voort
In our previous episode, Florian Klaempfl said:
  
  Not everything was done correctly 10 years ago (or first time round).
 
 Nevertheless, writable typed constants are simply an important part of
 Borland style Object Pascal.

Is it, since D4 also supports var? I agree it is a sterile difference, but it
provides an alternative to the use of CONST.
 
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Graeme Geldenhuys
Florian Klaempfl wrote:
 
 Nevertheless, writable typed constants are simply an important part of
 Borland style Object Pascal.

I'm not arguing that. Then maybe writeable typed constants should be
disabled for compiler mode delphi at least. It's a Delphi compatibility
issue after all.

Just this morning I got confirmation that code I wanted to implement in
tiOPF did not compile under D7 and D2009. I had to introduce IFDEF
Delphi and $J+ around the writeable constant. tiOPF and FPC used mode
delphi throughout the framework.


 FPC often implements something that breaks existing code. 
 
 Examples :)?


Please note that I am not saying I disagree with the changes. I know
they were introduced for the better. What I am saying, is that changes
do get introduced into FPC (for whatever reason) and does sometimes
break existing code.

Example #1:
  You cannot pass a property as var parameter anymore. Introduced in
  v2.2.0

Example #2:
  Boolean type change in Xlib units. TBool(false) or TBool(true) type
  usage.

Example #3:
  ComponentState change. I wasn't allowed to use the following code
  anymore:  Include(ComponentState, csDesigning);


I think there was a few more that affected fpGUI Toolkit project, but I
think you get the idea. As long as code is being maintained, such
changes are not the end of the world. :-)  But my point is, they do happen!


 Then I'll tell you what's the difference to writable typed
 constants.

OK, now tell me... :-)


Regards,
  - Graeme -

___
fpGUI - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Martin Friebe

Graeme Geldenhuys wrote:
FPC often implements something that breaks existing code. 
  

Examples :)?




Please note that I am not saying I disagree with the changes. I know
they were introduced for the better. What I am saying, is that changes
do get introduced into FPC (for whatever reason) and does sometimes
break existing code.

Example #1:
  You cannot pass a property as var parameter anymore. Introduced in
  v2.2.0

Example #2:
  Boolean type change in Xlib units. TBool(false) or TBool(true) type
  usage.

Example #3:
  ComponentState change. I wasn't allowed to use the following code
  anymore:  Include(ComponentState, csDesigning);


I think there was a few more that affected fpGUI Toolkit project, but I
think you get the idea. As long as code is being maintained, such
changes are not the end of the world. :-)  But my point is, they do happen!

  


Afaik 1  3 are the same?

And if I understand the changes where mad, because this feature was 
broken before:
That is the syntax you mentioned lead to an unpredictable result, and 
the circumstances under which it did what you wanted could change 
outside your code and without the intend to make them change.


Or in other words: This only worked if a property directly read a 
variable, but not if it was a function result = meaning you had 2 
different types of properties. But only one was documented.


Best Regards
Martin


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Graeme Geldenhuys
Martin Friebe wrote:
 
 Afaik 1  3 are the same?

True.


 And if I understand the changes where mad, because this feature was
 broken before:

To quote myself, even though I know one shouldn't. ;-)

 I know they were introduced for the better.

I was not arguing the point that those changes should not have been
made. I was simply stating that other projects code was broken due to
behaviour changes made in FPC. This was my counter argument to Vincent's
reply that it would break existing code.

If external projects are still being maintained, they should keep up
with the latest FPC. I do it with fpGUI, Martin does it with
MSEide+MSEgui and Lazarus does it, etc...

So I don't see a problem if FPC made $J- the default behaviour in FPC
mode delphi. At least FPC is then Delphi compatible - which is one of
FPC's goals I believe. And as Nino pointed out, even the old Delphi 5
doesn't allow writeable typed constants by default.

What should be the default behaviour in mode objfpc? Well that I am not
sure about.


Regards,
  - Graeme -

___
fpGUI - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] object.ClassInfo.Create

2009-06-18 Thread Marc Weustink

Martin wrote:

Just got a question, to ensure I understand thinks correctly.

Let's start with stuff I definitely know (or think so).

Destructors are virtual/overriden, because they are called on the 
instance, and the instance may be assigned to a variable foo: Tobject, 
which would call TObject.Destroy instead of TMyClass.Destroy.


Constructors don't (usually) need this, because the are (usually) called 
on a class (that is the classname usually appears hardcoded in the source)


on TComponent constructors are virtual, because when loading from 
resource, those objects are instantiated separately (with NewInstance() 
), and the constructor is called on an instance. So nothing new, the 
usual way of virtual methods


It's not only for streaming, there are more common cases where you might 
need virtual constructors:


var
  GraphicClass: TGraphicClass;
  Graphic: TGraphic;
begin
  case Something of
1: GraphicClass := TBitmap;
2: GraphicClass := TPNGImage;
...
  end;
  Graphic := GraphicClass.Create;
  ...
end;




Now if I have a variable/value with classinfo?
AnyObject.ClassInfo = has the class info for it's class

BUT
TMyClass.Classinfo, could either be a TmyClass or a TMySubClass
(and per definition ClassInfo is TObject, and could be anything 
inherited from Tbject)


Correct.


SO if I wrote
 FMyClass := AMyClass.Classinfo.Create;

What will actually happen?


It creates an instance of your class, but calls TObject.Create

If I understand this right, an instance of TMyClass (or TMySubClass , if 
AMyClass was a TMySubClass) is created; BUT the constructor 
TObject.Create is called (with then TMyClass instance) ?

And it would skip any constructor that was on TMyclass?


Correct.


Then I could of course write
 TMyClassType = class of TMyClass;
 FMyClass := TMyClassType (AMyClass.Classinfo).Create;
and it would call TMyClass.Create. And if TMyClass wanted to have it's 
own constructor, the TMyClass.Create must be virtual?


No, you don't need a virtual constructor here yet, since the 
TMyClass.Create will be called. However if you have TMySubClass too with 
an own create, you need one.



How close am I do the truth?


Close :)

Marc


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Paul Nicholls
2009/6/18 Graeme Geldenhuys grae...@opensoft.homeip.net

 Vincent Snijders wrote:
 
  Backwards compatibility with turbo pascal, which lacked initialized
  variables and writable constants were the alternative.

 Thanks Vincent. The Kylix 3 help mentioned early versions of Delphi and
 Borland Pascal, which made me think that it must have been some
 limitation in the older compilers. Thanks for confirming this.


 Regards,
  - Graeme -


I also find writable constants hand for things like this where I can define
the 'variable' + values too so I don't have to set the values at run time:

Const
{..}
cNote_Unknown = -1;
cNote_C   = 0;
cNote_Cs  = 1;  cNote_Db = 1;
cNote_D   = 2;
cNote_Ds  = 3;  cNote_Eb = 3;
cNote_E   = 4;
cNote_F   = 5;
cNote_Fs  = 6;  cNote_Gb = 6;
cNote_G   = 7;
cNote_Gs  = 8;  cNote_Ab = 8;
cNote_A   = 9;
cNote_As  = 10; cNote_Bb = 10;
cNote_B   = 11;
cNotes : Array[0..11,0..8] Of Single =
(
//Octave
//Note0 1 2
3 4 5 6
7 8
{C}  (16.3515978312874, 32.7031956625748, 65.4063913251497,
130.812782650299, 261.625565300599, 523.251130601197, 1046.50226120239,
2093.00452240479, 4186.00904480958),
{C#/Db}  (17.3239144360545, 34.647828872109 , 69.295657744218 ,
138.591315488436, 277.182630976872, 554.365261953744, 1108.73052390749,
2217.46104781498, 4434.92209562995),
{D}  (18.354047994838 , 36.7080959896759, 73.4161919793519,
146.832383958704, 293.664767917408, 587.329535834815, 1174.65907166963,
2349.31814333926, 4698.63628667852),
{D#/Eb}  (19.4454364826301, 38.8908729652601, 77.7817459305202,
155.56349186104 , 311.126983722081, 622.253967444162, 1244.50793488832,
2489.01586977665, 4978.03173955329),
{E}  (20.6017223070544, 41.2034446141088, 82.4068892282175,
164.813778456435, 329.62755691287 , 659.25511382574 , 1318.51022765148,
2637.02045530296, 5274.04091060592),
{F}  (21.8267644645628, 43.6535289291255, 87.307057858251 ,
174.614115716502, 349.228231433004, 698.456462866008, 1396.91292573202,
2793.82585146403, 5587.65170292806),
{F#/Gb}  (23.1246514194772, 46.2493028389543, 92.4986056779086,
184.997211355817, 369.994422711634, 739.988845423269, 1479.97769084654,
2959.95538169308, 5919.91076338615),
{G}  (24.4997147488593, 48.9994294977187, 97.9988589954373,
195.997717990875, 391.995435981749, 783.990871963499, 1567.981743927  ,
3135.96348785399, 6271.92697570799),
{G#/Ab}  (25.9565435987466, 51.9130871974931, 103.826174394986,
207.652348789973, 415.304697579945, 830.60939515989 , 1661.21879031978,
3322.43758063956, 6644.87516127912),
{A}  (27.5, 55  , 110 ,
220 , 440 , 880 , 1760,
3520, 7040),
{A#/Bb}  (29.1352350948806, 58.2704701897613, 116.540940379522,
233.081880759045, 466.16376151809 , 932.32752303618 , 1864.65504607236,
3729.31009214472, 7458.62018428944),
{B}  (30.8677063285078, 61.7354126570155, 123.470825314031,
246.941650628062, 493.883301256124, 987.766602512248, 1975.5332050245 ,
3951.06641004899, 7902.13282009799)
);
{..}


Notice the cNotes array - it is already filled with values for me to use at
runtime :)

cheers,
Paul
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Writeable typed constants - what's the point?

2009-06-18 Thread Jürgen Hestermann



 Paul Nicholls schrieb:
I also find writable constants hand for things like this where I can 
define the 'variable' + values too so I don't have to set the values at 
run time:


But can't you do the same with a variable declaration? If you want to 
change the value at runtime it's definitely not a constant anymore.


Beeing able to change a typed constant was added by Borland as a quick 
hack (because constants were put into the global data segement even in 
function declarations) but actually its semantic is wrong. If you can 
change it, it has to be a variable declaration and you should not be 
able to change a constant.


Jürgen Hestermann

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal