Jump to content

Declaring Record Type Constants


Recommended Posts

Why won't this work?
 

type
  TMySqlCreateTable = record
    TableName: string;
    CreateFunc: function: JPromise;
  end;

const
  _TableList: array of TMySqlCreateTable = [
    (TableName: 'Server'; CreateFunc: @CreateServer)
  ];

This would be valid in Delphi, and the SMS parser even recognizes the validity of "(TableName: 'Server'; CreateFunc: @CreateServer)", but gives the error "constant expression expected". But it *IS* a constant expression.

Thanks.

-David

Link to post
Share on other sites
solution I:
===========
var
  // _TableList: array[0..0] of variant = [
  _TableList:= [ 
  CLASS
   TableName:= 'Server'; external 'TableName';
   CreateFunc:= @CreateServer; external 'CreateFunc'
  END
  ];
// This works

==========================================================
var
  _TableList2: array[0..0] of TMySqlCreateTable  = [
  ( TableName: 'Server'; CreateFunc: @CreateServer )
  ];
// Syntax Error: Constant expression expected [line: 85, column: 52, file: unit2]
==========================================================

_TableList2[0].TableName := 'Server';
_TableList2[0].CreateFunc := @CreateServer;
// This works

 

 

Link to post
Share on other sites

Here's my solution:

type
  TMySqlCreateTable = class
    TableName: string;
    CreateFunc: function: JPromise;
    constructor Create(const aTableName: string; aCreateFunc: function: JPromise);
    begin
      TableName := aTableName;
      CreateFunc := aCreateFunc;
    end;
  end;

var
  _TableList: array of TMySqlCreateTable;

procedure CreateTableList;
begin
  _TableList.Clear;
  _TableList.Add(TMySqlCreateTable.Create('Server', @CreateServerTable));
  _TableList.Add(TMySqlCreateTable.Create('GameData', @CreateGameDataTable));
  _TableList.Add(TMySqlCreateTable.Create('Team', @CreateTeamTable));
  _TableList.Add(TMySqlCreateTable.Create('Player', @CreatePlayerTable));
  _TableList.Add(TMySqlCreateTable.Create('PlayerBody', @CreatePlayerBodyTable));
  _TableList.Add(TMySqlCreateTable.Create('PlayerLocker', @CreatePlayerLockerTable));
  _TableList.Add(TMySqlCreateTable.Create('PlayerAliases', @CreatePlayerAliasesTable));
  _TableList.Add(TMySqlCreateTable.Create('PlayerIgnores', @CreatePlayerIgnoresTable));
  _TableList.Add(TMySqlCreateTable.Create('PlayerButtons', @CreatePlayerButtonsTable));
  _TableList.Add(TMySqlCreateTable.Create('PlayerNotes', @CreatePlayerNotesTable));
  _TableList.Add(TMySqlCreateTable.Create('PlayerUIOptions', @CreatePlayerUIOptionsTable));
  _TableList.Add(TMySqlCreateTable.Create('Purchases', @CreatePurchasesTable));
  _TableList.Add(TMySqlCreateTable.Create('Rankings', @CreateRankingsTable));
end;

Turn it into a class, declare an array variable,  write a function to fill it up.

I would rather just declare it as a constant.

-David

Link to post
Share on other sites

Another solution using record type;

type
  TModelClassRegistration = record
    TableName: string; external "TableName";
    CreateFunc: function : JPromise; external "CreateFunc";
  end;

var
  RegisteredModelClasses: array of TModelClassRegistration;


initialization
  var rec :TModelClassRegistration;
  rec.TableName := 'devil_report';
  rec.CreateFunc := @CreateServer;
  RegisteredModelClasses.Add(rec);
  rec.TableName:= 'flowers_report';
  rec.CreateFunc := @CreateServer;
  RegisteredModelClasses.Add(rec);
  rec.TableName := 'cards_report';
  rec.CreateFunc := @CreateServer;
  RegisteredModelClasses.Add(rec);

(...)
for var i:= RegisteredModelClasses.Low to RegisteredModelClasses.High do
  console.log(RegisteredModelClasses[i])

 

Link to post
Share on other sites

Solution as JObject base class

Type
  TT = Class(JObject)
    TableName: string;
    CreateFunc: TFn;
end;

var
  Regist: array of TT;

initialization

var rr : TT;
rr := new TT;
rr.TableName  := 'devil_report';
rr.CreateFunc := @CreateServer;
Regist.Add(rr);
rr.TableName  := 'flowers_report';
rr.CreateFunc := @CreateServer;
Regist.Add(rr);

(...) // Display the object
for var i:= Regist.Low to Regist.High do
  console.log(Regist[i]);

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...