Recent

Author Topic: Is it possible to define constant records in FPC?  (Read 25589 times)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is it possible to define constant records in FPC?
« Reply #15 on: November 24, 2010, 05:36:10 pm »
Quote
I would like to have compile directive (something like: {$+SOMETHING} and {$-SOMETHING}) to allow/prevent modify a typed constant. I don't know if that's possible, nor if that's a good idea ...
If I'm not mistaken, {$J-} would disallow it.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Is it possible to define constant records in FPC?
« Reply #16 on: November 24, 2010, 07:23:52 pm »
Quote
I would like to have compile directive (something like: {$+SOMETHING} and {$-SOMETHING}) to allow/prevent modify a typed constant. I don't know if that's possible, nor if that's a good idea ...
If I'm not mistaken, {$J-} would disallow it.

   It does that, but it only works inside the interface section and not inside implementation section. What I "wish" is to block those assignment in the whole implementation section, except in some specific methods where I would allow it through the compile directive.

   I think I should put it in the wish list...

captian jaster

  • Guest
Re: Is it possible to define constant records in FPC?
« Reply #17 on: November 24, 2010, 08:04:53 pm »
   I like the Laksen's approach over Captain Jaster's because you can define many constants of the same record type without repeating its declaration and also you can pass the whole structure as a method's parameter.
His is better...
Quote
I would like to have compile directive (something like: {$+SOMETHING} and {$-SOMETHING}) to allow/prevent modify a typed constant. I don't know if that's possible, nor if that's a good idea ...
If I'm not mistaken, {$J-} would disallow it.
If I'm correct you automatically can't modify constants..
I haven't read to far to "ResourceStrings" but I understand they are stored into resources of the program..(I have no idea what the means  :-[ ) But afaik you can't modify them at run time... What about something like resourcestring for every type?

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Is it possible to define constant records in FPC?
« Reply #18 on: November 26, 2010, 03:37:21 pm »

I haven't read to far to "ResourceStrings" but I understand they are stored into resources of the program..(I have no idea what the means  :-[ ) But afaik you can't modify them at run time... What about something like resourcestring for every type?

I haven't red much eider :-[. ResourceStrings and Localization is something I must learn...

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Is it possible to define constant records in FPC?
« Reply #19 on: December 08, 2010, 09:51:24 pm »
Note that the Pascal terms 'var' and 'const' are interchangeable in this code:
Code: [Select]
program projectVarConst;

{$mode objfpc}{$H+}
{apptype console}

uses Classes, SysUtils;

const // you can use 'var' here - code compiles and produces identical executable
  TRec : Record
          S:String;
          Int:Integer;
         end             =    (S:'';Int:10);

 begin
   writeln(Format('TRec.Int value is %d',[TRec.Int]));
   writeln;
   writeln('Changing TRec''s int value ...');
   TRec.Int := 20;
   writeln;
   writeln(Format('TRec.Int value is %d',[TRec.Int]));
   readln;
 end.     

 

TinyPortal © 2005-2018