Quantcast

Class const of array of record.

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Class const of array of record.

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.

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

Re: Class const of array of record.

Jonas Maebe-2

On 24 Feb 2012, at 14:28, Everton Vieira wrote:

type
 TTest = class
   const Nomes: array[0..2] of TRec = (mTRec(1, 'everton'), mTRec(2,
'murilo'), mTRec(3, 'vieira'));
 end;

See http://www.freepascal.org/docs-html/ref/refse22.html (the syntax is the same for typed constants and initialized variables, as mentioned at http://www.freepascal.org/docs-html/ref/refse10.html )


Jonas

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

Re: Class const of array of record.

Everton Vieira
In reply to this post by Everton Vieira
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 <[hidden email]>
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.

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

Re: Class const of array of record.

Constantine Yannakopoulos
>> 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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Class const of array of record.

Everton Vieira
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 <[hidden email]>
>> 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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
Everton Vieira.

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

Re: Class const of array of record.

Constantine Yannakopoulos
On Fri, Feb 24, 2012 at 6:29 PM, Everton Vieira <[hidden email]> wrote:
> Doesn't work this code doesn't compile:

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

See also: http://wiki.freepascal.org/FPC_New_Features_2.6.0#Advanced_record_syntax
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Class const of array of record.

Jonas Maebe-2

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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Class const of array of record.

Everton Vieira
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 <[hidden email]>

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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
Everton Vieira.

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

Re: Class const of array of record.

Everton Vieira
static; should be a keyword, doesn't?

2012/2/24 Everton Vieira <[hidden email]>
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 <[hidden email]>

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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
Everton Vieira.



--
Everton Vieira.

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

Re: Class const of array of record.

Jonas Maebe-2

On 24 Feb 2012, at 18:07, Everton Vieira wrote:

> static; should be a keyword, doesn't?

No, it's not a keyword.


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

Re: Class const of array of record.

Everton Vieira
But is used like one. Why is that so?

2012/2/24 Jonas Maebe <[hidden email]>

On 24 Feb 2012, at 18:07, Everton Vieira wrote:

> static; should be a keyword, doesn't?

No, it's not a keyword.


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



--
Everton Vieira.

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

Re: Class const of array of record.

Jonas Maebe-2

On 28 Feb 2012, at 13:25, Everton Vieira wrote:

> 2012/2/24 Jonas Maebe <[hidden email]>
>
>> 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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Class const of array of record.

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.

2012/2/28 Jonas Maebe <[hidden email]>

On 28 Feb 2012, at 13:25, Everton Vieira wrote:

2012/2/24 Jonas Maebe <[hidden email]>

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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
Everton Vieira.

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

Re: Class const of array of record.

Sven Barth-2
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Class const of array of record.

Everton Vieira
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 <[hidden email]>
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  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal



--
Everton Vieira.

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