Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-02

Class const of array of record.

15 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Everton Vieira

I was trying to make this construction:

type
  TRec = record
    Numero: Integer;
    Nome: String;
  end;
function mTRec(Numero: Integer; Nome: String): TRec;
type
  TTest = class
    const Nomes: array[0..2] of TRec = (mTRec(1, 'everton'), mTRec(2,
'murilo'), mTRec(3, 'vieira'));
  end;
implementation
{$R *.lfm}
function mTRec(Numero: Integer; Nome: String): TRec;
begin
  Result.Numero := Numero;
  Result.Nome := Nome;
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  i1: Integer;
begin
  for i1 := 0 to Length(TTest.Nome) -1 do
    ShowMessage(TTest.Nomes[i1].Nome);
end;

Is there any how to do it?

Everton Vieira.
Everton Vieira
to Everton Vieira: I was trying to make this construction: type TRec = record Numero: Integer; Nome: String;…

Is contructions like this that aren't available:

type
  TRec = record
    Numero: Integer;
    Nome: String;
  end;
implementation
{$R *.lfm}
procedure ShowTRec(rec: TRec);
begin
  ShowMessage(rec.Nome);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowTRec((Numero: 1; Nome: 'Pascal'));
end;

In this constructions is that today is nice to use a function to create the TRec.

2012/2/24 Everton Vieira

Show quoted text 41 lines
I was trying to make this construction: type TRec = record Numero: Integer; Nome: String; end; function mTRec(Numero: Integer; Nome: String): TRec; type TTest = class const Nomes: array[0..2] of TRec = (mTRec(1, 'everton'), mTRec(2, 'murilo'), mTRec(3, 'vieira')); end; implementation {$R *.lfm} function mTRec(Numero: Integer; Nome: String): TRec; begin Result.Numero := Numero; Result.Nome := Nome; end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject); var i1: Integer; begin for i1 := 0 to Length(TTest.Nome) -1 do ShowMessage(TTest.Nomes[i1].Nome); end; Is there any how to do it? -- Everton Vieira.
Everton Vieira.
kyan
to Everton Vieira
> Is there any how to do it?

You can use advanced record syntax and do something like this:

type
  TRec = record
    Numero: Integer;
    Nome: String;
    class function Create(ANumero: Integer; const ANome: string):
TRec; static; inline;
  end;

...

class function TRec.Create(ANumero: Integer; const ANome: string): TRec;
begin
  with Result do
  begin
    Numero := ANumero;
    Nome := ANome;
  end;
end;

...

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowTRec(TRec.Create(1, 'Pascal'));
end;

PS: In Delphi advanced records can have constructors but not yet in FPC. A static class function can easily replace them.

Everton Vieira
to kyan

Doesn't work this code doesn't compile:

type
  { TRec }
  TRec = record
    Nome: String;
    class function Create(aNome: String): TRec; static; inline;
  end;
procedure ShowTRec(rec: TRec);
implementation
{$R *.lfm}
procedure ShowTRec(rec: TRec);
begin
  ShowMessage(rec.Nome);
end;

{ TRec }

class function TRec.Create(aNome: String): TRec;
begin
  Result.Nome := aNome;
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowTRec(TRec.Create('Pascal'));
end;

2012/2/24 kyan

Show quoted text 36 lines
>> Is there any how to do it? You can use advanced record syntax and do something like this: type TRec = record Numero: Integer; Nome: String; class function Create(ANumero: Integer; const ANome: string): TRec; static; inline; end; ... class function TRec.Create(ANumero: Integer; const ANome: string): TRec; begin with Result do begin Numero := ANumero; Nome := ANome; end; end; ... procedure TForm1.Button1Click(Sender: TObject); begin ShowTRec(TRec.Create(1, 'Pascal')); end; PS: In Delphi advanced records can have constructors but not yet in FPC. A static class function can easily replace them. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Everton Vieira.
Jonas Maebe
to kyan
kyan wrote
Sorry, I forgot to mention that advanced record syntax works in Delphi mode only. To compile it you must change {$mode objfpc} to {$mode Delphi}.

Or add {$modeswitch advancedrecords} after {$mode objfpc}

Jonas

Everton Vieira
to Jonas Maebe

Is true, with {$modeswitch advancedrecords} worked. But i dont see much sense in put a diretive for enable a new feature.

2012/2/24 Jonas Maebe

Show quoted text 17 lines
On 24 Feb 2012, at 17:50, kyan wrote: Sorry, I forgot to mention that advanced record syntax works in Delphi mode only. To compile it you must change {$mode objfpc} to {$mode Delphi}. Or add {$modeswitch advancedrecords} after {$mode objfpc} Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Everton Vieira.
Everton Vieira
to Everton Vieira

static; should be a keyword, doesn't?

2012/2/24 Everton Vieira

Show quoted text 28 lines
Is true, with {$modeswitch advancedrecords} worked. But i dont see much sense in put a diretive for enable a new feature. 2012/2/24 Jonas Maebe > > On 24 Feb 2012, at 17:50, kyan wrote: > > Sorry, I forgot to mention that advanced record syntax works in Delphi > mode only. To compile it you must change {$mode objfpc} to {$mode > Delphi}. > > > Or add {$modeswitch advancedrecords} after {$mode objfpc} > > > Jonas > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > -- Everton Vieira.
Everton Vieira.
Jonas Maebe
to Everton Vieira
Everton Vieira wrote 9 lines
2012/2/24 Jonas Maebe > On 24 Feb 2012, at 18:07, Everton Vieira wrote: > >> static; should be a keyword, doesn't? > > No, it's not a keyword. But is used like one. Why is that so?

It's not used like one. Keyword means "it's a reserved word by the language, and you can never use it for anything other than how it's defined by the language". Examples of keywords are "begin" and "procedure". Some identifiers are only keywords in certain language modes (e.g. try/except/on/finally/raise are only keywords in language modes that know about exception handling).

There are other identifiers that can have a special meaning in a particular context (such as "cdecl" and "static"), but outside that context they behave like any other modifier. Or they may simply be types or routines that are part of the system unit. The reason is that code written for language dialects that do not treat those identifiers as keywords can otherwise not be compiled. To illustrate:

{$mode objfpc}
type
   longint = string;
   cdecl = longint;
   protected = longint;
   varargs = char;
   sin = byte;
var
   stdcall: cdecl;
// note that the final "cdecl;" here does have the "original" meaning
of cdecl
function static(const inline: protected): varargs; cdecl;
begin
   static:=stdcall[sin(inline[1])];
end;
begin
end.

Jonas

Everton Vieira
to Jonas Maebe

I see. I only said so because i thought would became more pretty if his was bolded like de inline next to him.

2012/2/28 Jonas Maebe

Show quoted text 60 lines
On 28 Feb 2012, at 13:25, Everton Vieira wrote: 2012/2/24 Jonas Maebe > > On 24 Feb 2012, at 18:07, Everton Vieira wrote: >> >> static; should be a keyword, doesn't? >>> >> >> No, it's not a keyword. >> > > But is used like one. Why is that so? > It's not used like one. Keyword means "it's a reserved word by the language, and you can never use it for anything other than how it's defined by the language". Examples of keywords are "begin" and "procedure". Some identifiers are only keywords in certain language modes (e.g. try/except/on/finally/raise are only keywords in language modes that know about exception handling). There are other identifiers that can have a special meaning in a particular context (such as "cdecl" and "static"), but outside that context they behave like any other modifier. Or they may simply be types or routines that are part of the system unit. The reason is that code written for language dialects that do not treat those identifiers as keywords can otherwise not be compiled. To illustrate: {$mode objfpc} type longint = string; cdecl = longint; protected = longint; varargs = char; sin = byte; var stdcall: cdecl; // note that the final "cdecl;" here does have the "original" meaning of cdecl function static(const inline: protected): varargs; cdecl; begin static:=stdcall[sin(inline[1])**]; end; begin end. Jonas ______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>
Everton Vieira.
Sven Barth
to Everton Vieira
Everton Vieira wrote
I see. I only said so because i thought would became more pretty if his was bolded like de inline next to him.

If this is your problem then you should ask this on the Lazarus list. This is a problem of the highlighter not of the compiler.

Note: There was a discussion about this recently already (maybe you even read it).

Regards,
Sven

Everton Vieira
to Sven Barth

Is true, in that case here isn't the place. But nice that why isn't a keyword is clear now.

2012/2/28 Sven Barth

Show quoted text 19 lines
Am 28.02.2012 14:13, schrieb Everton Vieira: I see. I only said so because i thought would became more pretty if his > was bolded like de inline next to him. > > If this is your problem then you should ask this on the Lazarus list. This is a problem of the highlighter not of the compiler. Note: There was a discussion about this recently already (maybe you even read it). Regards, Sven ______________________________**_________________ fpc-pascal maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-pascal<http://lists.freepascal.org/mailman/listinfo/fpc-pascal>
Everton Vieira.