Sign in Join

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

Mailing lists / fpc-devel / 2019-03

Re: Some new

5 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

J. Gareth Moreton

Ah, okay.  In my case, it doesn't work if the set you specify is a constant or variable defined elsewhere.

e.g.

 const Delimiters: array[0..5] of Char = [#9, #10, #12, #13, #26, #32];

if C in Delimiters then...
If you do that, you get a compiler error about 'in' not being overloaded or some such.

Gareth aka. Kit

On Tue 05/03/19 06:36 , Michael Van Canneyt sent:

J. Gareth Moreton wrote 23 lines
Hi everyone, I've developed a couple of "in" operator overrides so you can use " in " (useful in parsing tools, say, to see if a character belongs to a set of delimiters) and " in ". e.g. operator in(_Char: Char; _Set: array of Char): Boolean; var   C: Char; begin   Result := False;   for C in _Set do     if C = _Char then     begin       Result := True;       Exit;     end; end; Would these be welcome in one of the core units? And if so, which unit would you recommend?

At the risk of sounding stupid, but why would you want to add this if the compiler supports this out of the box ?

 var
 C : char;
 begin
 if ParamCount>0 then
 c:=paramstr(1)[1]
 else
 c:=#0;
 if c in ['a','b','c'] then
 writeln('ok')
 else
 writeln('not ok')
 end.

Michael.

Reply by email

J. Gareth Moreton

Heh, okay, I stand corrected.  Thanks Michael.  Seems I need to read up on all the functions in the standard units.  Sorry for wasting everyone's time.

I do wonder though if it's worth adding " in " to SysUtils, since it's a very convenient and intuitive notation... not just for Chars but for all basic types, actually.

Gareth aka. Kit

On Tue 05/03/19 09:05 , "Michael Van Canneyt" sent:

J. Gareth Moreton wrote 11 lines
Ah, okay.  In my case, it doesn't work if the set you specify is a constant or variable defined elsewhere. e.g. const Delimiters: array[0..5] of Char = [#9, #10, #12, #13, #26, #32]; if C in Delimiters then... If you do that, you get a compiler error about 'in' not being overloaded or some such.

That's because you're defining an array, not a set.

To define it as a set do:

 Const
 Delimiters = [#9, #10, #12, #13, #26, #32];

Your c in delimiters will work because this is a set.

The FPC source tree is full of such constructs.

The sysutils unit has CharInSet() BTW, if you want to use an array.

Michael.

Reply by email

Michael Van Canneyt
to J. Gareth Moreton: Ah, okay.  In my case, it doesn't work if the set you specify is a constant or variable…
J. Gareth Moreton wrote 10 lines
Ah, okay.  In my case, it doesn't work if the set you specify is a constant or variable defined elsewhere. e.g. const Delimiters: array[0..5] of Char = [#9, #10, #12, #13, #26, #32]; if C in Delimiters then... If you do that, you get a compiler error about 'in' not being overloaded or some such.

That's because you're defining an array, not a set.

To define it as a set do:

Const
   Delimiters  = [#9, #10, #12, #13, #26, #32];

Your c in delimiters will work because this is a set.

The FPC source tree is full of such constructs.

The sysutils unit has CharInSet() BTW, if you want to use an array.

Michael.

Reply by email

Michael Van Canneyt
to J. Gareth Moreton: Heh, okay, I stand corrected.  Thanks Michael.  Seems I need to read up on all the…
J. Gareth Moreton wrote
Heh, okay, I stand corrected.  Thanks Michael.  Seems I need to read up on all the functions in the standard units.  Sorry for wasting everyone's time.

No waste of time.

I do wonder though if it's worth adding " in " to SysUtils, since it's a very convenient and intuitive notation... not just for Chars but for all basic types, actually.

If you want that, use a separate unit 'InUtils' or so.

Rationale: SysUtils is broadly used.

I don't want that my current "x in y" suddenly gets changed from a compiler internal to something else because you decided to add some in operator to sysutils.

It should be a conscious choice by the developer to use such constructs.

Michael.

Reply by email

Michael Van Canneyt
to Michael Van Canneyt
Michael Van Canneyt wrote 24 lines
On Tue, 5 Mar 2019, J. Gareth Moreton wrote: > Heh, okay, I stand corrected.  Thanks Michael.  Seems I need to read up > on all the functions in the standard units.  Sorry for wasting everyone's > time. No waste of time. > > I do wonder though if it's worth adding " in " to SysUtils, since it's a > very convenient and intuitive notation... not just for Chars but for all > basic types, actually. If you want that, use a separate unit 'InUtils' or so. Rationale: SysUtils is broadly used. I don't want that my current "x in y" suddenly gets changed from a compiler internal to something else because you decided to add some in operator to sysutils. It should be a conscious choice by the developer to use such constructs.

For all clarity: I fully support your idea to make these overloads, I'm just guarding users from inadvertent changes to their existing code.

Michael.

Reply by email