0

I use this function in Delphi 10.3.3:

function StrTrimCharsLeft(const S: string; const Chars: array of Char): string;
var
  I, L: SizeInt;
begin
  I := 1;
  L := Length(S);
  while (I <= L) and ArrayContainsChar(Chars, S[I]) do
    Inc(I);
  Result := Copy(S, I, L - I + 1);
end;

When I use the function in this way I get an error:

[dcc32 Error]: E2250 There is no overloaded version of 'StrTrimCharsLeft' that can be called with these arguments

const
    BomChars = ['ï', '»', '¿'];
...
s := JclStrings.StrTrimCharsLeft(s, BomChars);

But when I use it in this way, all works well without an error:

s := JclStrings.StrTrimCharsLeft(s, ['ï', '»', '¿']);

So how can I define and use an array of Char as a constant?

1 Answer 1

3

When you write

const
  BomChars = ['ï', '»', '¿'];

you have declared a set named BomChars -- not an array!

If you instead declare BomChars as a static array,

const
  BomChars: array[0..2] of Char = ('ï', '»', '¿');

it will work. (See Declared Constants § Array Constants.)

(StrTrimCharsLeft(s, ['ï', '»', '¿']); works because here the brackets are part of the open array syntax. Specifically, the brackets are part of an "open array constructor".)

Sign up to request clarification or add additional context in comments.

5 Comments

So it is explicitly NOT possible to declare it as an OPEN ARRAY constant? How does this work in other languages?
@user1580348: There is no such thing as an "open array constant"; the concept doesn't even make sense. An open array parameter is a special kind of function/procedure parameter, so they only exist in the context of function/procedure parameters. An open array parameter can accept static arrays, dynamic arrays, and this kind of construct. But in this case, a static array of three characters is exactly what you need, isn't it?
I mean, outside function parameters, Delphi only has two kinds of arrays: static arrays and dynamic arrays. Basically, open array parameters allows you to define a single function that accepts both static and dynamic arrays as input. In particular, it will accept your static three-element array of characters.
In Delphi, square brackets are used for sets. Thus, your constant became a set. But the symbol is also used for the open array constructor, so the same symbol has two functions, which can be confusing. And since Delphi XE7, square brackets serve a third function (making it even more confusing): they can be used to create dynamic arrays on the fly: var a: array of Integer; begin a := [2, 3, 1];. But when it comes to declaring static array constants, you have always used the syntax with ordinary parentheses.
(Sorry, "open array parameters allows" should of course have been "open array parameters allow". But it is now too late to edit the comment.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.