# Array of const

**URL:** https://talk.remobjects.com/t/array-of-const/1627
**Category:** Pascal Script
**Created:** [February 1, 2013, 6:53pm UTC](https://talk.remobjects.com/t/array-of-const/1627 "2013-02-01T18:53:07Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![nomorelogic](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/n/edb3f5/32.png) [@nomorelogic](https://talk.remobjects.com/u/nomorelogic)
#### Post date: [February 1, 2013, 6:53pm UTC](https://talk.remobjects.com/t/array-of-const/1627/1 "2013-02-01T18:53:07Z")

</div>

hi guys  
first of all congratulation for your work, very very well done!

I’m a fpc programmer and I’m experimenting with your interpreter, so, sorry if I ask trivial things.

With free pascal I wrote a function like:  
procedure Out\_WriteFmt(Data: string; const Args : array of const);  
begin  
ScriptOutput.Text:= ScriptOutput.Text + Format(Data, Args);  
end;

The purpose is to provide the potential of the Format function to my script.  
I can use it properly (both compilation and runtime), but at the end of runtime I get an Access Violation.  
I guess it depends on the parameter: const Args: array of const.

I don’t think that it is not supported (function produces output that I expect) but I cannot understand the access violation.

Thanks in advance  
nomorelogic

---

<div class="post-metadata">

### Author: ![vovanl](https://talk.remobjects.com/user_avatar/talk.remobjects.com/vovanl/32/51_2.png) [@vovanl](https://talk.remobjects.com/u/vovanl)
#### Post date: [February 5, 2013, 2:10pm UTC](https://talk.remobjects.com/t/array-of-const/1627/2 "2013-02-05T14:10:35Z")

</div>

Hello,  
Can you send us your small testcase? We will retest it.

---

<div class="post-metadata">

### Author: ![nomorelogic](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/n/edb3f5/32.png) [@nomorelogic](https://talk.remobjects.com/u/nomorelogic)
#### Post date: [February 5, 2013, 7:20pm UTC](https://talk.remobjects.com/t/array-of-const/1627/3 "2013-02-05T19:20:54Z")

</div>

sure  
in attached file both fpc binary and pascal script  
to use run from shell as:  
./lpitr -s script03.pas

As you can see in script03.pas I wrote 2 procedures: Out\_Write and Out\_WriteFmt.

The first one runs correctly, the last give a error.  
Note also that “Now” (from dateutils) returns an invalid date.

Thanks  
nomorelogic

PS:  
linux 64 bit  
lazarus 1.06  
fpc 2.6.0

---

<div class="post-metadata">

### Author: ![vovanl](https://talk.remobjects.com/user_avatar/talk.remobjects.com/vovanl/32/51_2.png) [@vovanl](https://talk.remobjects.com/u/vovanl)
#### Post date: [February 10, 2013, 6:49pm UTC](https://talk.remobjects.com/t/array-of-const/1627/4 "2013-02-10T18:49:42Z")

</div>

Hello,  
Thanks, issues were logged as #32 and #33.  
We will send you a workaround after fixing.

---

<div class="post-metadata">

### Author: ![nomorelogic](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/n/edb3f5/32.png) [@nomorelogic](https://talk.remobjects.com/u/nomorelogic)
#### Post date: [February 11, 2013, 2:18pm UTC](https://talk.remobjects.com/t/array-of-const/1627/5 "2013-02-11T14:18:03Z")

</div>

thanks

---

<div class="post-metadata">

### Author: ![spriya](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@spriya](https://talk.remobjects.com/u/spriya)
#### Post date: [December 20, 2022, 2:32pm UTC](https://talk.remobjects.com/t/array-of-const/1627/6 "2022-12-20T14:32:34Z")

</div>

Hi,

facing the same problem with array of const when using in Format(Data, Args);  
Am not getting the access violation, but getting weird characters in place of original arguments.

waiting for solution.

Thanks in advance

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [January 4, 2023, 12:54pm UTC](https://talk.remobjects.com/t/array-of-const/1627/7 "2023-01-04T12:54:58Z")

</div>

Hi,

looks like delphi RTTI can’t work with `array of const` parameters

try this testcase:

```auto
program Project13;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, System.Rtti, system.TypInfo;

begin
  var res := Invoke(@Format,
                ['this is string - %s, %d',
                 TValue.FromArray(TypeInfo(TArray<TVarRec>),
                                  ArrayOfConstToTValueArray(['string',123]))],
                ccReg,
                TypeInfo(string),
                true);
  writeln(res.AsString);
end.

```

---

<div class="post-metadata">

### Author: ![holly.lee](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@holly.lee](https://talk.remobjects.com/u/holly.lee)
#### Post date: [May 10, 2024, 4:28pm UTC](https://talk.remobjects.com/t/array-of-const/1627/8 "2024-05-10T16:28:01Z")

</div>

After some hours digging into fewer documentation and writing some test codes. I found the correct way to do the Rtti invoke call with array of const parameters

```auto
procedure F3(a: array of const);
var
  i: Integer;
begin
  Writeln('F3: Length of a is ' + IntToStr(Length(a)));
  for i := Low(a) to High(a) do begin
    if a[i].VType = vtUnicodeString then
      Writeln(IntToStr(i) + ': UnicodeString ' + UnicodeString(a[i].VUnicodeString))
    else if a[i].VType = vtInteger then
      Writeln(IntToStr(i) + ': Integer ' + IntToStr(a[i].VInteger));
    // and other types...
  end;
end;

procedure CallF3ThroughRtti(a: array of const);
var
  Args: TArray<TValue>;
begin
  SetLength(Args, 2);
  Args[0] := TValue.From(@a[0]);
  Args[1] := TValue.From(High(a));
  Invoke(@F3, Args, ccReg, nil, True);
end;

```

Hope this can help others entering into this topic later.

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [May 13, 2024, 8:42am UTC](https://talk.remobjects.com/t/array-of-const/1627/9 "2024-05-13T08:42:38Z")

</div>

Hi,

yes, this works:

```auto
program Project26;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, System.Rtti, system.TypInfo;

var
  res: TValue;
  args: TArray<TValue>;
  a: TArray<TVarRec>;
begin
  a := TValueArrayToArrayOfConst(['string', 123]);
  res := Invoke(@Format,
                [ 'this is string - %s, %d',
                  @a[0],
                  High(a)],
                ccReg,
                TypeInfo(string),
                true);
  writeln(res.AsString);

end.

```

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [May 14, 2024, 7:44am UTC](https://talk.remobjects.com/t/array-of-const/1627/10 "2024-05-14T07:44:20Z")

</div>

Hi,

I’ve fixed [Array of const broken? · Issue #267 · remobjects/pascalscript · GitHub](https://github.com/remobjects/pascalscript/issues/267)
