Recent

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

garlar27

  • Hero Member
  • *****
  • Posts: 652
Is it possible to define constant records in FPC?
« on: November 19, 2010, 10:39:20 am »
Is it possible to define constant records in FPC?

If yes: How do you do it?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is it possible to define constant records in FPC?
« Reply #1 on: November 19, 2010, 12:57:03 pm »
No, Pascal doesn't have the concept of "constant variables".

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: Is it possible to define constant records in FPC?
« Reply #2 on: November 19, 2010, 01:14:52 pm »
Quote
Is it possible to define constant records in FPC?
Did you mean;
type
pdatas=^datas;
datas=record
id:integer;
name:string;
end;
var mydatas:pdatas;

like that? :D
Sorry If I got it wrong

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Is it possible to define constant records in FPC?
« Reply #3 on: November 19, 2010, 02:04:06 pm »
You can
Code: [Select]
type
 TTest = record
  A: string;
  B: longint;
  C: single;
 end;

const
 TestRec: TTest = (A: 'Something'; B: 123; C: 12.3);

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: Is it possible to define constant records in FPC?
« Reply #4 on: November 19, 2010, 02:42:58 pm »
 :D

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is it possible to define constant records in FPC?
« Reply #5 on: November 19, 2010, 03:04:13 pm »
Quote
You can
No, that doesn't make the record constant. Borland invented it due to the lack of variable initialization in standard Pascal.

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Is it possible to define constant records in FPC?
« Reply #6 on: November 19, 2010, 04:08:26 pm »
Sure, it's a typed constant, meaning you can change it

I don't think there's a way to change that

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: Is it possible to define constant records in FPC?
« Reply #7 on: November 19, 2010, 04:15:52 pm »
Quote
type
 TTest = record
  A: string;
  B: longint;
  C: single;
 end;
var xttest:ttest;
...
xttest.a:='somestring';
xttest.b:=12345;
xttest.c:=1;

Did that changed? :D

It will not changed only if you declare to your write procedure as nil
like;
....no declaration here....//so use the constant
memstream.writebuffer(xttest, sizeof(xttest));

Correct me if I'm wrong...


Thanks :D

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is it possible to define constant records in FPC?
« Reply #8 on: November 20, 2010, 03:20:44 am »
Quote
Sure, it's a typed constant, meaning you can change it

I don't think there's a way to change that
Try changing the record attribute, the compiler will allow it. That's "not" constant.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Is it possible to define constant records in FPC?
« Reply #9 on: November 23, 2010, 11:03:55 am »
You can
Code: [Select]
type
 TTest = record
  A: string;
  B: longint;
  C: single;
 end;

const
 TestRec: TTest = (A: 'Something'; B: 123; C: 12.3);

Yeah!! It works!! I didn't figured it out how to do it (at least I couldn't find it in the PDFs), and my guesses didn't compile.

Quote
Sure, it's a typed constant, meaning you can change it

I don't think there's a way to change that
Try changing the record attribute, the compiler will allow it. That's "not" constant.

I'm working on a project with lots of constants and I would like to have them grouped (i.e. to have the error message and its error number in the same constant record).

I know it's not a TRUE constant the compiler won't warn you if you change it by code nor at run time, but by changing its capitalization you may know that the var "TEST_REC" is something "special".

But honestly it would be nice to have TRUE constant records.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is it possible to define constant records in FPC?
« Reply #10 on: November 23, 2010, 04:04:01 pm »
Quote
I know it's not a TRUE constant the compiler won't warn you if you change it by code nor at run time, but by changing its capitalization you may know that the var "TEST_REC" is something "special".
Convention over semantics, that's fine as long as you're consistent.
Quote
But honestly it would be nice to have TRUE constant records.
I'd rather name it "read-only variables" since it's more general and is better logically than "constant variables" (a constant shouldn't have variadic values, that's silly). Anyway, I seldom need it and if I do, I'll make it a read-only property of a class.

captian jaster

  • Guest
Re: Is it possible to define constant records in FPC?
« Reply #11 on: November 23, 2010, 08:28:02 pm »
Sure, it's a typed constant, meaning you can change it

I don't think there's a way to change that
This
Though this compiled with no problem:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils;
  7.  
  8. Const
  9.  Trec:Record
  10.  S:String;
  11.  Int:Integer;
  12.  end = (S:'';Int:10);
  13.  
  14. begin
  15. end.
  16.  
Quote
I know it's not a TRUE constant the compiler won't warn you if you change it by code nor at run time, but by changing its capitalization you may know that the var "TEST_REC" is something "special".
Convention over semantics, that's fine as long as you're consistent.
Quote
But honestly it would be nice to have TRUE constant records.
I'd rather name it "read-only variables" since it's more general and is better logically than "constant variables" (a constant shouldn't have variadic values, that's silly). Anyway, I seldom need it and if I do, I'll make it a read-only property of a class.
Buh Buh..
I thought that's what constants were...

cdbc

  • Hero Member
  • *****
  • Posts: 2575
    • http://www.cdbc.dk
Re: Is it possible to define constant records in FPC?
« Reply #12 on: November 23, 2010, 09:31:30 pm »
IMHO...
 I would do it the 'Leledumbo' way :-)

Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

captian jaster

  • Guest
Re: Is it possible to define constant records in FPC?
« Reply #13 on: November 24, 2010, 12:27:54 am »
IMHO...
 I would do it the 'Leledumbo' way :-)

Regards Benny
Read Only Properties?
Still a variable depending on what your doing...

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Is it possible to define constant records in FPC?
« Reply #14 on: November 24, 2010, 01:21:32 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.

   The fact that you can alter that constant value is something that is useful in some cases, but, it's a problem if someone change its value accidentally (since compiler won't warn you).

   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 ...

 

TinyPortal © 2005-2018