Problems with 'not' statement when used with 'in[' Statement

This does not compile

Function IsValid(Name: ShortString): Boolean;
Begin
  Result := Not Name[Length(Name)] in['/', '\', '~'];

This compiles why?

Function IsValid(Name: ShortString): Boolean;
Begin
  Result := Name[Length(Name)] in['/', '\', '~'];

Because you can’t do “Not Name[Length(Name)]”. Use brackets to determine what the “Not” applies to.

Like:

Not (Name[Length[Name] in [‘/’, ‘\’, ‘~’]);

or better yet:

Not CharInSet(Name[Length(Name)], [‘/’, ‘\’, ‘~’]);

Regards,

ok dumb mistake
sorry

Why can’t the complier compile it without brackets is not the equation obvious?

It’s because in the operator precedence rules the NOT operator is evaluated before the IN operator so the compiler treats your first version as though you had written it like this.

Result := (not Name[Length(Name)]) in ['/', '\', '~'];

What I’m saying is

Name[Length(Name)])

in an If statement the code above has selected a character but has not done anything with the character in the if state. At this point a comparison symbol or addition or subtraction is yet to follow. So an ‘in’ can easily operate without being in a set of brackets if you want the complier to act that way.

I understand, I was answering your question as to why the compiler won’t compile it without brackets around the IN part of the expression.

What I wrote is how the compiler is seeing your initial code. It sees it as if you had put brackets around the first part of the expression which is why you get the compile error.

yes we did not make the complier and you have answered well thanks